source: main/tags/2.80/gsdl/cgi-bin/gliserver.pl@ 24529

Last change on this file since 24529 was 14260, checked in by mdewsnip, 17 years ago

The upload-collection-file action now gets passed a "fs" argument indicating the size of the uploaded data, otherwise it will crash on IIS 6 when trying to read the last block (!?).

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