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

Last change on this file since 15928 was 15928, checked in by ak19, 16 years ago

Duplicate retrieval of arguments in sub file-exists removed

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