source: trunk/gsdl/cgi-bin/gliserver.pl@ 11940

Last change on this file since 11940 was 11940, checked in by mdewsnip, 18 years ago

New authentication system: users with group "personal-collections-editor" can edit any collection starting with their username, users with group "all-collections-editor" can edit any collection, users with group "<collection>-collection-editor" can edit the <collection> collection.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 27.1 KB
Line 
1#!perl -w
2# Need to specify the full path of Perl above
3
4
5use gsdlCGI;
6use strict;
7
8
9my $authentication_enabled = 1;
10my $debugging_enabled = 0;
11
12my $mail_enabled = 0;
13my $mail_to_address = "user\@server"; # Set this appropriately
14my $mail_from_address = "user\@server"; # Set this appropriately
15my $mail_smtp_server = "smtp.server"; # Set this appropriately
16
17
18sub main
19{
20 my $gsdl_cgi = new gsdlCGI();
21
22 # Load the Greenstone modules that we need to use
23 $gsdl_cgi->setup_gsdl();
24 my $gsdlhome = $ENV{'GSDLHOME'};
25 $gsdl_cgi->checked_chdir($gsdlhome);
26 require "$gsdlhome/perllib/util.pm"; # This is OK on Windows
27 require "$gsdlhome/perllib/cpan/Crypt/UnixCrypt.pm"; # This is OK on Windows
28
29 # Encrypt the password
30 if (defined $gsdl_cgi->param("pw")) {
31 $gsdl_cgi->param('-name' => "pw", '-value' => &Crypt::UnixCrypt::crypt($gsdl_cgi->clean_param("pw"), "Tp"));
32 }
33
34 $gsdl_cgi->parse_cgi_args();
35
36 my $cmd = $gsdl_cgi->clean_param("cmd");
37 if (!defined $cmd) {
38 $gsdl_cgi->generate_error("No command specified.");
39 }
40 $gsdl_cgi->delete("cmd");
41
42 # The check-installation command has no arguments
43 if ($cmd eq "check-installation") {
44 &check_installation($gsdl_cgi);
45 return;
46 }
47
48 # All other commands require a username, for locking and authentication
49 my $username = $gsdl_cgi->clean_param("un");
50 if ((!defined $username) || ($username =~ m/^\s*$/)) {
51 $gsdl_cgi->generate_error("No username specified.");
52 }
53 $gsdl_cgi->delete("un");
54
55 if ($cmd eq "delete-collection") {
56 &delete_collection($gsdl_cgi, $username);
57 }
58 elsif ($cmd eq "download-collection") {
59 &download_collection($gsdl_cgi, $username);
60 }
61 elsif ($cmd eq "download-collection-archives") {
62 &download_collection_archives($gsdl_cgi, $username);
63 }
64 elsif ($cmd eq "download-collection-configurations") {
65 &download_collection_configurations($gsdl_cgi, $username);
66 }
67 elsif ($cmd eq "download-collection-file") {
68 &download_collection_file($gsdl_cgi, $username);
69 }
70 elsif ($cmd eq "delete-collection-file") {
71 &delete_collection_file($gsdl_cgi, $username);
72 }
73 elsif ($cmd eq "get-script-options") {
74 &get_script_options($gsdl_cgi, $username);
75 }
76 elsif ($cmd eq "move-collection-file") {
77 &move_collection_file($gsdl_cgi, $username);
78 }
79 elsif ($cmd eq "new-collection-directory") {
80 &new_collection_directory($gsdl_cgi, $username);
81 }
82 elsif ($cmd eq "run-script") {
83 &run_script($gsdl_cgi, $username);
84 }
85 elsif ($cmd eq "timeout-test") {
86 while (1) { }
87 }
88 elsif ($cmd eq "upload-collection-file") {
89 &upload_collection_file($gsdl_cgi, $username);
90 }
91 else {
92 $gsdl_cgi->generate_error("Unrecognised command: '$cmd'");
93 }
94}
95
96
97sub authenticate_user
98{
99 my $gsdl_cgi = shift(@_);
100 my $username = shift(@_);
101 my $collection = shift(@_);
102
103 # Even if we're not authenticating remove the un and pw arguments, since these can mess up other scripts
104 my $user_password = $gsdl_cgi->clean_param("pw");
105 $gsdl_cgi->delete("pw");
106
107 # Only authenticate if it is enabled
108 return if (!$authentication_enabled);
109
110 if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) {
111 $gsdl_cgi->generate_error("Authentication failed: no password specified.");
112 }
113
114 my $gsdlhome = $ENV{'GSDLHOME'};
115 my $etc_directory = &util::filename_cat($gsdlhome, "etc");
116 my $users_db_file_path = &util::filename_cat($etc_directory, "users.db");
117
118 # Use db2txt instead of GDBM_File to get the user accounts information
119 my $users_db_content = "";
120 open(USERS_DB, "db2txt \"$users_db_file_path\" |");
121 while (<USERS_DB>) {
122 $users_db_content .= $_;
123 }
124
125 # Get the user account information from the users.db database
126 my %users_db_data = ();
127 foreach my $users_db_entry (split(/-{70}/, $users_db_content)) {
128 if ($users_db_entry =~ /\n?\[(.+)\]\n/) {
129 $users_db_data{$1} = $users_db_entry;
130 }
131 }
132
133 # Check username
134 my $user_data = $users_db_data{$username};
135 if (!defined $user_data) {
136 $gsdl_cgi->generate_error("Authentication failed: no account for user '$username'.");
137 }
138
139 # Check password
140 my ($valid_user_password) = ($user_data =~ /\<password\>(.*)/);
141 if ($user_password ne $valid_user_password) {
142 $gsdl_cgi->generate_error("Authentication failed: incorrect password.");
143 }
144
145 # If we're not editing a collection then the user doesn't need to be in a particular group
146 if ($collection eq "") {
147 return; # Authentication successful
148 }
149
150 # Check group
151 my ($user_groups) = ($user_data =~ /\<groups\>(.*)/);
152 foreach my $user_group (split(/\,/, $user_groups)) {
153 # Does this user have access to all collections?
154 if ($user_group eq "all-collections-editor") {
155 return; # Authentication successful
156 }
157 # Does this user have access to personal collections, and is this one?
158 if ($user_group eq "personal-collections-editor" && $collection =~ /^$username\-/) {
159 return; # Authentication successful
160 }
161 # Does this user have access to this collection
162 if ($user_group eq "$collection-collection-editor") {
163 return; # Authentication successful
164 }
165 }
166
167 $gsdl_cgi->generate_error("Authentication failed: user is not in the required group.");
168}
169
170
171sub lock_collection
172{
173 my $gsdl_cgi = shift(@_);
174 my $username = shift(@_);
175 my $collection = shift(@_);
176
177 my $steal_lock = $gsdl_cgi->clean_param("steal_lock");
178 $gsdl_cgi->delete("steal_lock");
179
180 my $gsdlhome = $ENV{'GSDLHOME'};
181 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
182 $gsdl_cgi->checked_chdir($collection_directory);
183
184 # Check if a lock file already exists for this collection
185 my $lock_file_name = "gli.lck";
186 if (-e $lock_file_name) {
187 # A lock file already exists... check if it's ours
188 open(LOCK_FILE, "<$lock_file_name");
189 my $lock_file_owner = <LOCK_FILE>;
190 close(LOCK_FILE);
191
192 # The lock file is ours, so there is no problem
193 if ($lock_file_owner eq $username) {
194 return;
195 }
196
197 # The lock file is not ours, so throw an error unless "steal_lock" is set
198 unless (defined $steal_lock) {
199 $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner");
200 }
201 }
202
203 # Create a lock file for us and we're done
204 open(LOCK_FILE, ">$lock_file_name");
205 print LOCK_FILE "$username";
206 close(LOCK_FILE);
207}
208
209
210# ----------------------------------------------------------------------------------------------------
211# ACTIONS
212# ----------------------------------------------------------------------------------------------------
213
214
215sub check_installation
216{
217 my ($gsdl_cgi) = @_;
218
219 # Check that Java is installed and accessible
220 my $java = $gsdl_cgi->get_java_path();
221 my $java_command = "$java -version 2>&1";
222 my $java_output = `$java_command`;
223 my $java_status = $?;
224 if ($java_status < 0) {
225 $gsdl_cgi->generate_error("Java failed -- do you have the Java run-time installed?\n" . $gsdl_cgi->check_java_home());
226 }
227
228 $gsdl_cgi->generate_ok_message("Java found: $java_output\nInstallation OK!");
229}
230
231
232sub delete_collection
233{
234 my ($gsdl_cgi, $username) = @_;
235
236 my $collection = $gsdl_cgi->clean_param("c");
237 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
238 $gsdl_cgi->generate_error("No collection specified.");
239 }
240
241 # Ensure the user is allowed to edit this collection
242 &authenticate_user($gsdl_cgi, $username, $collection);
243
244 my $gsdlhome = $ENV{'GSDLHOME'};
245 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
246 $gsdl_cgi->checked_chdir($collect_directory);
247
248 # Check that the collection exists
249 if (!-d $collection) {
250 $gsdl_cgi->generate_error("Collection $collection does not exist.");
251 }
252
253 # Make sure the collection isn't locked by someone else
254 &lock_collection($gsdl_cgi, $username, $collection);
255
256 $gsdl_cgi->checked_chdir($collect_directory);
257 $gsdl_cgi->local_rm_r("$collection");
258
259 # Check that the collection was deleted
260 if (-e $collection) {
261 $gsdl_cgi->generate_error("Could not delete collection $collection.");
262 }
263
264 $gsdl_cgi->generate_ok_message("Collection $collection deleted successfully.");
265}
266
267
268sub delete_collection_file
269{
270 my ($gsdl_cgi, $username) = @_;
271
272 my $collection = $gsdl_cgi->clean_param("c");
273 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
274 $gsdl_cgi->generate_error("No collection specified.");
275 }
276 my $file = $gsdl_cgi->clean_param("file");
277 if ((!defined $file) || ($file =~ m/^\s*$/)) {
278 $gsdl_cgi->generate_error("No file specified.");
279 }
280 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
281
282 # Make sure we don't try to delete anything outside the collection
283 if ($file =~ /\.\./) {
284 $gsdl_cgi->generate_error("Illegal file specified.");
285 }
286
287 # Ensure the user is allowed to edit this collection
288 &authenticate_user($gsdl_cgi, $username, $collection);
289
290 my $gsdlhome = $ENV{'GSDLHOME'};
291 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
292 $gsdl_cgi->checked_chdir($collection_directory);
293
294 # Make sure the collection isn't locked by someone else
295 &lock_collection($gsdl_cgi, $username, $collection);
296
297 # Check that the collection file exists
298 if (!-e $file) {
299 $gsdl_cgi->generate_error("Collection file $file does not exist.");
300 }
301 $gsdl_cgi->local_rm_r("$file");
302
303 # Check that the collection file was deleted
304 if (-e $file) {
305 $gsdl_cgi->generate_error("Could not delete collection file $file.");
306 }
307
308 $gsdl_cgi->generate_ok_message("Collection file $file deleted successfully.");
309}
310
311
312sub download_collection
313{
314 my ($gsdl_cgi, $username) = @_;
315
316 my $collection = $gsdl_cgi->clean_param("c");
317 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
318 $gsdl_cgi->generate_error("No collection specified.");
319 }
320
321 # Ensure the user is allowed to edit this collection
322 &authenticate_user($gsdl_cgi, $username, $collection);
323
324 my $gsdlhome = $ENV{'GSDLHOME'};
325 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
326 $gsdl_cgi->checked_chdir($collect_directory);
327
328 # Check that the collection exists
329 if (!-d $collection) {
330 $gsdl_cgi->generate_error("Collection $collection does not exist.");
331 }
332
333 # Make sure the collection isn't locked by someone else
334 &lock_collection($gsdl_cgi, $username, $collection);
335
336 # Zip up the collection
337 my $java = $gsdl_cgi->get_java_path();
338 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
339 my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . $collection . ".zip");
340 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
341 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args";
342
343 my $java_output = `$java_command`;
344 my $java_status = $?;
345 if ($java_status > 0) {
346 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
347 }
348
349 # Check that the zip file was created successfully
350 if (!-e $zip_file_path || -z $zip_file_path) {
351 $gsdl_cgi->generate_error("Collection zip file $zip_file_path could not be created.");
352 }
353
354 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
355 unlink("$zip_file_path") unless $debugging_enabled;
356}
357
358
359sub download_collection_archives
360{
361 my ($gsdl_cgi, $username) = @_;
362
363 my $collection = $gsdl_cgi->clean_param("c");
364 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
365 $gsdl_cgi->generate_error("No collection specified.");
366 }
367
368 # Ensure the user is allowed to edit this collection
369 &authenticate_user($gsdl_cgi, $username, $collection);
370
371 my $gsdlhome = $ENV{'GSDLHOME'};
372 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
373 $gsdl_cgi->checked_chdir($collect_directory);
374
375 # Check that the collection archives exist
376 if (!-d &util::filename_cat($collection, "archives")) {
377 $gsdl_cgi->generate_error("Collection archives do not exist.");
378 }
379
380 # Make sure the collection isn't locked by someone else
381 &lock_collection($gsdl_cgi, $username, $collection);
382
383 # Zip up the collection archives
384 my $java = $gsdl_cgi->get_java_path();
385 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
386 my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . $collection . "-archives.zip");
387 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
388 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args";
389
390 my $java_output = `$java_command`;
391 my $java_status = $?;
392 if ($java_status > 0) {
393 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
394 }
395
396 # Check that the zip file was created successfully
397 if (!-e $zip_file_path || -z $zip_file_path) {
398 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
399 }
400
401 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
402 unlink("$zip_file_path") unless $debugging_enabled;
403}
404
405
406# Collection locking unnecessary because this action isn't related to a particular collection
407sub download_collection_configurations
408{
409 my ($gsdl_cgi, $username) = @_;
410
411 # Users can be in any group to perform this action
412 &authenticate_user($gsdl_cgi, $username, "");
413
414 my $gsdlhome = $ENV{'GSDLHOME'};
415 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
416 $gsdl_cgi->checked_chdir($collect_directory);
417
418 # Zip up the collection configurations
419 my $java = $gsdl_cgi->get_java_path();
420 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
421 my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . "collection-configurations.zip");
422 my $java_args = "\"$zip_file_path\" \"$collect_directory\"";
423 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args";
424
425 my $java_output = `$java_command`;
426 my $java_status = $?;
427 if ($java_status > 0) {
428 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
429 }
430
431 # Check that the zip file was created successfully
432 if (!-e $zip_file_path || -z $zip_file_path) {
433 $gsdl_cgi->generate_error("Collection configurations zip file $zip_file_path could not be created.");
434 }
435
436 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
437 unlink("$zip_file_path") unless $debugging_enabled;
438}
439
440
441sub download_collection_file
442{
443 my ($gsdl_cgi, $username) = @_;
444
445 my $collection = $gsdl_cgi->clean_param("c");
446 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
447 $gsdl_cgi->generate_error("No collection specified.");
448 }
449 my $file = $gsdl_cgi->clean_param("file");
450 if ((!defined $file) || ($file =~ m/^\s*$/)) {
451 $gsdl_cgi->generate_error("No file specified.");
452 }
453 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
454
455 # Make sure we don't try to download anything outside the collection
456 if ($file =~ /\.\./) {
457 $gsdl_cgi->generate_error("Illegal file specified.");
458 }
459
460 # Ensure the user is allowed to edit this collection
461 &authenticate_user($gsdl_cgi, $username, $collection);
462
463 my $gsdlhome = $ENV{'GSDLHOME'};
464 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
465 $gsdl_cgi->checked_chdir($collection_directory);
466
467 # Check that the collection file exists
468 if (!-e $file) {
469 $gsdl_cgi->generate_error("Collection file $file does not exist.");
470 }
471
472 # Make sure the collection isn't locked by someone else
473 &lock_collection($gsdl_cgi, $username, $collection);
474
475 # Zip up the collection file
476 my $java = $gsdl_cgi->get_java_path();
477 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
478 my $zip_file_path = &util::filename_cat($collection_directory, $username . "-" . $collection . "-file.zip");
479 my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
480 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
481
482 my $java_output = `$java_command`;
483 my $java_status = $?;
484 if ($java_status > 0) {
485 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
486 }
487
488 # Check that the zip file was created successfully
489 if (!-e $zip_file_path || -z $zip_file_path) {
490 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
491 }
492
493 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
494 unlink("$zip_file_path") unless $debugging_enabled;
495}
496
497
498# Collection locking unnecessary because this action isn't related to a particular collection
499sub get_script_options
500{
501 my ($gsdl_cgi, $username) = @_;
502
503 my $script = $gsdl_cgi->clean_param("script");
504 if ((!defined $script) || ($script =~ m/^\s*$/)) {
505 $gsdl_cgi->generate_error("No script specified.");
506 }
507 $gsdl_cgi->delete("script");
508
509 # Users can be in any group to perform this action
510 &authenticate_user($gsdl_cgi, $username, "");
511
512 my $perl_args = "";
513 if ($script eq "classinfo.pl") {
514 $perl_args = $gsdl_cgi->clean_param("classifier") || "";
515 $gsdl_cgi->delete("classifier");
516 }
517 if ($script eq "pluginfo.pl") {
518 $perl_args = $gsdl_cgi->clean_param("plugin") || "";
519 $gsdl_cgi->delete("plugin");
520 }
521
522 foreach my $cgi_arg_name ($gsdl_cgi->param) {
523 my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
524 if ($cgi_arg_value eq "") {
525 $perl_args = "-$cgi_arg_name " . $perl_args;
526 }
527 else {
528 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
529 }
530 }
531
532 my $perl_command = "perl -S $script $perl_args 2>&1";
533 my $perl_output = `$perl_command`;
534 my $perl_status = $?;
535 if ($perl_status > 0) {
536 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
537 }
538
539 print STDOUT "Content-type:text/plain\n\n";
540 print STDOUT $perl_output;
541}
542
543
544sub move_collection_file
545{
546 my ($gsdl_cgi, $username) = @_;
547
548 my $collection = $gsdl_cgi->clean_param("c");
549 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
550 $gsdl_cgi->generate_error("No collection specified.");
551 }
552 my $source_file = $gsdl_cgi->clean_param("source");
553 if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
554 $gsdl_cgi->generate_error("No source file specified.");
555 }
556 $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
557 my $target_file = $gsdl_cgi->clean_param("target");
558 if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
559 $gsdl_cgi->generate_error("No target file specified.");
560 }
561 $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
562
563 # Make sure we don't try to move anything outside the collection
564 if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
565 $gsdl_cgi->generate_error("Illegal file specified.");
566 }
567
568 # Ensure the user is allowed to edit this collection
569 &authenticate_user($gsdl_cgi, $username, $collection);
570
571 my $gsdlhome = $ENV{'GSDLHOME'};
572 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
573 $gsdl_cgi->checked_chdir($collection_directory);
574
575 # Check that the collection source file exists
576 if (!-e $source_file) {
577 $gsdl_cgi->generate_error("Collection file $source_file does not exist.");
578 }
579
580 # Make sure the collection isn't locked by someone else
581 &lock_collection($gsdl_cgi, $username, $collection);
582
583 &util::mv($source_file, $target_file);
584
585 # Check that the collection source file was moved
586 if (-e $source_file || !-e $target_file) {
587 $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
588 }
589
590 $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
591}
592
593
594sub new_collection_directory
595{
596 my ($gsdl_cgi, $username) = @_;
597
598 my $collection = $gsdl_cgi->clean_param("c");
599 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
600 $gsdl_cgi->generate_error("No collection specified.");
601 }
602 my $directory = $gsdl_cgi->clean_param("directory");
603 if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
604 $gsdl_cgi->generate_error("No directory specified.");
605 }
606 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
607
608 # Make sure we don't try to create anything outside the collection
609 if ($directory =~ /\.\./) {
610 $gsdl_cgi->generate_error("Illegal directory specified.");
611 }
612
613 # Ensure the user is allowed to edit this collection
614 &authenticate_user($gsdl_cgi, $username, $collection);
615
616 my $gsdlhome = $ENV{'GSDLHOME'};
617 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
618 $gsdl_cgi->checked_chdir($collection_directory);
619
620 # Check that the collection directory doesn't already exist
621 if (-d $directory) {
622 $gsdl_cgi->generate_error("Collection directory $directory already exists.");
623 }
624
625 # Make sure the collection isn't locked by someone else
626 &lock_collection($gsdl_cgi, $username, $collection);
627
628 &util::mk_dir($directory);
629
630 # Check that the collection directory was created
631 if (!-d $directory) {
632 $gsdl_cgi->generate_error("Could not create collection directory $directory.");
633 }
634
635 $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
636}
637
638
639sub run_script
640{
641 my ($gsdl_cgi, $username) = @_;
642
643 my $script = $gsdl_cgi->clean_param("script");
644 if ((!defined $script) || ($script =~ m/^\s*$/)) {
645 $gsdl_cgi->generate_error("No script specified.");
646 }
647 $gsdl_cgi->delete("script");
648 my $collection = $gsdl_cgi->clean_param("c");
649 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
650 $gsdl_cgi->generate_error("No collection specified.");
651 }
652 $gsdl_cgi->delete("c");
653
654 # Ensure the user is allowed to edit this collection
655 &authenticate_user($gsdl_cgi, $username, $collection);
656
657 # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
658 &lock_collection($gsdl_cgi, $username, $collection) unless ($script eq "mkcol.pl");
659
660 my $perl_args = $collection;
661 foreach my $cgi_arg_name ($gsdl_cgi->param) {
662 my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
663 if ($cgi_arg_value eq "") {
664 $perl_args = "-$cgi_arg_name " . $perl_args;
665 }
666 else {
667 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
668 }
669 }
670
671 my $perl_command = "perl -S $script $perl_args 2>&1";
672 if (!open(PIN, "$perl_command |")) {
673 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
674 }
675
676 print STDOUT "Content-type:text/plain\n\n";
677 while (defined (my $perl_output_line = <PIN>)) {
678 print STDOUT $perl_output_line;
679 }
680 close(PIN);
681
682 my $perl_status = $?;
683 if ($perl_status > 0) {
684 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
685 }
686 elsif ($mail_enabled) {
687 if ($script eq "buildcol.pl") {
688 &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
689 }
690 }
691}
692
693
694sub upload_collection_file
695{
696 my ($gsdl_cgi, $username) = @_;
697
698 my $collection = $gsdl_cgi->clean_param("c");
699 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
700 $gsdl_cgi->generate_error("No collection specified.");
701 }
702 my $file = $gsdl_cgi->clean_param("file");
703 if ((!defined $file) || ($file =~ m/^\s*$/)) {
704 $gsdl_cgi->generate_error("No file specified.");
705 }
706 my $directory = $gsdl_cgi->clean_param("directory") || "";
707 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
708 my $zip = $gsdl_cgi->clean_param("zip");
709
710 # Make sure we don't try to upload anything outside the collection
711 if ($file =~ /\.\./) {
712 $gsdl_cgi->generate_error("Illegal file specified.");
713 }
714 if ($directory =~ /\.\./) {
715 $gsdl_cgi->generate_error("Illegal directory specified.");
716 }
717
718 # Ensure the user is allowed to edit this collection
719 &authenticate_user($gsdl_cgi, $username, $collection);
720
721 my $gsdlhome = $ENV{'GSDLHOME'};
722 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
723 $gsdl_cgi->checked_chdir($collection_directory);
724
725 # Make sure the collection isn't locked by someone else
726 &lock_collection($gsdl_cgi, $username, $collection);
727
728 my $directory_path = &util::filename_cat($collection_directory, $directory);
729 &util::mk_dir($directory_path);
730 if (!-e $directory_path) {
731 $gsdl_cgi->generate_error("Could not create directory $directory_path.");
732 }
733
734 my $file_path = &util::filename_cat($directory_path, $username . "-" . $file);
735 if (!open(FOUT, ">$file_path")) {
736 $gsdl_cgi->generate_error("Unable to write file $file_path");
737 }
738
739 # Read the uploaded data and write it out to file
740 my $buf;
741 my $num_bytes = 0;
742 binmode(FOUT);
743 while (read(STDIN, $buf, 1024) > 0) {
744 print FOUT $buf;
745 $num_bytes += length($buf);
746 }
747 close(FOUT);
748
749 # If we have downloaded a zip file, unzip it
750 if (defined $zip) {
751 my $java = $gsdl_cgi->get_java_path();
752 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
753 my $java_args = "\"$file_path\" \"$directory_path\"";
754 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.util.Unzip $java_args";
755
756 my $java_output = `$java_command`;
757 my $java_status = $?;
758
759 # Remove the zip file once we have unzipped it, since it is an intermediate file only
760 unlink("$file_path");
761
762 if ($java_status > 0) {
763 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
764 }
765 }
766
767 $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
768}
769
770
771sub put_file
772{
773 my $gsdl_cgi = shift(@_);
774 my $file_path = shift(@_);
775 my $content_type = shift(@_);
776
777 if (open(PIN, "<$file_path")) {
778 print STDOUT "Content-type:$content_type\n\n";
779
780 my $buf;
781 my $num_bytes = 0;
782 binmode(PIN);
783 while (read(PIN, $buf, 1024) > 0) {
784 print STDOUT $buf;
785 $num_bytes += length($buf);
786 }
787
788 close(PIN);
789 }
790 else {
791 $gsdl_cgi->generate_error("Unable to read file $file_path\n $!");
792 }
793}
794
795
796sub send_mail
797{
798 my $gsdl_cgi = shift(@_);
799 my $mail_subject = shift(@_);
800 my $mail_content = shift(@_);
801
802 my $sendmail_command = "perl -S sendmail.pl";
803 $sendmail_command .= " -to \"" . $mail_to_address . "\"";
804 $sendmail_command .= " -from \"" . $mail_from_address . "\"";
805 $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
806 $sendmail_command .= " -subject \"" . $mail_subject . "\"";
807
808 if (!open(POUT, "| $sendmail_command")) {
809 $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
810 }
811 print POUT $mail_content . "\n";
812 close(POUT);
813}
814
815
816&main();
Note: See TracBrowser for help on using the repository browser.