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

Last change on this file since 15170 was 15170, checked in by ak19, 15 years ago

2 changes: 1. added file-exists command and matching routine file_exists; 2. modified

  • 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, $username, $timestamp) = @_;
521
522 my ($gsdl_cgi) = @_;
523
524 my $collection = $gsdl_cgi->clean_param("c");
525 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
526 $gsdl_cgi->generate_error("No collection specified.");
527 }
528 my $file = $gsdl_cgi->clean_param("file");
529 if ((!defined $file) || ($file =~ m/^\s*$/)) {
530 $gsdl_cgi->generate_error("No file specified.");
531 }
532 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
533
534 # Not necessary: checking whether the user is authenticated to query existance of the file
535 #&authenticate_user($gsdl_cgi, $username, $collection);
536
537 my $gsdlhome = $ENV{'GSDLHOME'};
538 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
539 $gsdl_cgi->checked_chdir($collection_directory);
540
541 # Check that the collection file exists
542 if (-e $file) {
543 $gsdl_cgi->generate_ok_message("File $file exists.");
544 } else {
545 $gsdl_cgi->generate_ok_message("File $file does not exist.");
546 }
547}
548
549sub download_collection_file
550{
551 my ($gsdl_cgi, $username, $timestamp) = @_;
552
553 my $collection = $gsdl_cgi->clean_param("c");
554 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
555 $gsdl_cgi->generate_error("No collection specified.");
556 }
557 my $file = $gsdl_cgi->clean_param("file");
558 if ((!defined $file) || ($file =~ m/^\s*$/)) {
559 $gsdl_cgi->generate_error("No file specified.");
560 }
561 $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 download anything outside the collection
564 if ($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 file exists
576 if (!-e $file) {
577 $gsdl_cgi->generate_error("Collection file $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 # Zip up the collection file
584 my $java = $gsdl_cgi->get_java_path();
585 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
586 my $zip_file_path = &util::filename_cat($collection_directory, $collection . "-file-" . $timestamp . ".zip");
587 my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
588 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
589
590 my $java_output = `$java_command`;
591 my $java_status = $?;
592 if ($java_status > 0) {
593 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
594 }
595
596 # Check that the zip file was created successfully
597 if (!-e $zip_file_path || -z $zip_file_path) {
598 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
599 }
600
601 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
602 unlink("$zip_file_path") unless $debugging_enabled;
603}
604
605
606# Collection locking unnecessary because this action isn't related to a particular collection
607sub get_script_options
608{
609 my ($gsdl_cgi, $username, $timestamp) = @_;
610
611 my $script = $gsdl_cgi->clean_param("script");
612 if ((!defined $script) || ($script =~ m/^\s*$/)) {
613 $gsdl_cgi->generate_error("No script specified.");
614 }
615 $gsdl_cgi->delete("script");
616
617 # Users can be in any group to perform this action
618 &authenticate_user($gsdl_cgi, $username, "");
619
620 my $perl_args = "";
621 if ($script eq "classinfo.pl") {
622 $perl_args = $gsdl_cgi->clean_param("classifier") || "";
623 $gsdl_cgi->delete("classifier");
624 }
625 if ($script eq "pluginfo.pl") {
626 $perl_args = $gsdl_cgi->clean_param("plugin") || "";
627 $gsdl_cgi->delete("plugin");
628 }
629
630 foreach my $cgi_arg_name ($gsdl_cgi->param) {
631 my $cgi_arg_value = $gsdl_cgi->clean_param($cgi_arg_name) || "";
632 $cgi_arg_value = $gsdl_cgi->safe_val($cgi_arg_value);
633 if ($cgi_arg_value eq "") {
634 $perl_args = "-$cgi_arg_name " . $perl_args;
635 }
636 else {
637 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
638 }
639 }
640
641 print STDOUT "Content-type:text/plain\n\n";
642
643 my $perl_command = "perl -S $script $perl_args 2>&1";
644
645 # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
646 # directly out to the page
647 if ($iis6_mode)
648 {
649 $perl_command = "perl -S $script $perl_args";
650 }
651
652 my $perl_output = `$perl_command`;
653 my $perl_status = $?;
654 if ($perl_status > 0) {
655 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
656 }
657
658 if (defined($perl_output))
659 {
660 print STDOUT $perl_output;
661 }
662}
663
664
665sub move_collection_file
666{
667 my ($gsdl_cgi, $username, $timestamp) = @_;
668
669 my $collection = $gsdl_cgi->clean_param("c");
670 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
671 $gsdl_cgi->generate_error("No collection specified.");
672 }
673 my $source_file = $gsdl_cgi->clean_param("source");
674 if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
675 $gsdl_cgi->generate_error("No source file specified.");
676 }
677 $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
678 my $target_file = $gsdl_cgi->clean_param("target");
679 if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
680 $gsdl_cgi->generate_error("No target file specified.");
681 }
682 $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
683
684 # Make sure we don't try to move anything outside the collection
685 if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
686 $gsdl_cgi->generate_error("Illegal file specified.");
687 }
688
689 # Ensure the user is allowed to edit this collection
690 &authenticate_user($gsdl_cgi, $username, $collection);
691
692 my $gsdlhome = $ENV{'GSDLHOME'};
693 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
694 $gsdl_cgi->checked_chdir($collection_directory);
695
696 # Check that the collection source file exists
697 if (!-e $source_file) {
698 $gsdl_cgi->generate_error("Collection file $source_file does not exist.");
699 }
700
701 # Make sure the collection isn't locked by someone else
702 &lock_collection($gsdl_cgi, $username, $collection);
703
704 &util::mv($source_file, $target_file);
705
706 # Check that the collection source file was moved
707 if (-e $source_file || !-e $target_file) {
708 $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
709 }
710
711 $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
712}
713
714
715sub new_collection_directory
716{
717 my ($gsdl_cgi, $username, $timestamp) = @_;
718
719 my $collection = $gsdl_cgi->clean_param("c");
720 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
721 $gsdl_cgi->generate_error("No collection specified.");
722 }
723 my $directory = $gsdl_cgi->clean_param("directory");
724 if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
725 $gsdl_cgi->generate_error("No directory specified.");
726 }
727 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
728
729 # Make sure we don't try to create anything outside the collection
730 if ($directory =~ /\.\./) {
731 $gsdl_cgi->generate_error("Illegal directory specified.");
732 }
733
734 # Ensure the user is allowed to edit this collection
735 &authenticate_user($gsdl_cgi, $username, $collection);
736
737 my $gsdlhome = $ENV{'GSDLHOME'};
738 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
739 $gsdl_cgi->checked_chdir($collection_directory);
740
741 # Check that the collection directory doesn't already exist
742 # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicity
743 # try to create the import directory
744# if (-d $directory) {
745# $gsdl_cgi->generate_error("Collection directory $directory already exists.");
746# }
747
748 # Make sure the collection isn't locked by someone else
749 &lock_collection($gsdl_cgi, $username, $collection);
750
751 &util::mk_dir($directory);
752
753 # Check that the collection directory was created
754 if (!-d $directory) {
755 $gsdl_cgi->generate_error("Could not create collection directory $directory.");
756 }
757
758 $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
759}
760
761
762sub run_script
763{
764 my ($gsdl_cgi, $username, $timestamp) = @_;
765
766 my $script = $gsdl_cgi->clean_param("script");
767 if ((!defined $script) || ($script =~ m/^\s*$/)) {
768 $gsdl_cgi->generate_error("No script specified.");
769 }
770 $gsdl_cgi->delete("script");
771 my $collection = $gsdl_cgi->clean_param("c");
772 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
773 $gsdl_cgi->generate_error("No collection specified.");
774 }
775 $gsdl_cgi->delete("c");
776
777 # Ensure the user is allowed to edit this collection
778 &authenticate_user($gsdl_cgi, $username, $collection);
779
780 # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
781 &lock_collection($gsdl_cgi, $username, $collection) unless ($script eq "mkcol.pl");
782
783 # Last argument is the collection name, except for explode_metadata_database.pl and
784 # replace_srcdoc_with_html (where there's a "file" option followed by the filename. These two preceed the collection name)
785 my $perl_args = $collection;
786 if ($script eq "explode_metadata_database.pl" || $script eq "replace_srcdoc_with_html.pl") {
787 # Last argument is the file to be exploded
788 my $file = $gsdl_cgi->clean_param("file");
789 if ((!defined $file) || ($file =~ m/^\s*$/)) {
790 $gsdl_cgi->generate_error("No file specified.");
791 }
792 $gsdl_cgi->delete("file");
793 $file =~ s/ /\\ /g; # escape all spaces in filename with a backslash, i.e. "\ "
794 $perl_args = $file;
795 }
796
797 foreach my $cgi_arg_name ($gsdl_cgi->param) {
798 my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
799 if ($cgi_arg_value eq "") {
800 $perl_args = "-$cgi_arg_name " . $perl_args;
801 }
802 else {
803 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
804 }
805 }
806
807 print STDOUT "Content-type:text/plain\n\n";
808
809 my $perl_command = "perl -S $script $perl_args 2>&1";
810
811 # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
812 # directly out to the page
813 if ($iis6_mode)
814 {
815 $perl_command = "perl -S $script $perl_args";
816 }
817
818 if (!open(PIN, "$perl_command |")) {
819 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
820 }
821
822 while (defined (my $perl_output_line = <PIN>)) {
823 print STDOUT $perl_output_line;
824 }
825 close(PIN);
826
827 my $perl_status = $?;
828 if ($perl_status > 0) {
829 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
830 }
831 elsif ($mail_enabled) {
832 if ($script eq "buildcol.pl") {
833 &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
834 }
835 }
836}
837
838
839sub upload_collection_file
840{
841 my ($gsdl_cgi, $username, $timestamp) = @_;
842
843 my $collection = $gsdl_cgi->clean_param("c");
844 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
845 $gsdl_cgi->generate_error("No collection specified.");
846 }
847 my $file = $gsdl_cgi->clean_param("file");
848 if ((!defined $file) || ($file =~ m/^\s*$/)) {
849 $gsdl_cgi->generate_error("No file specified.");
850 }
851 my $directory = $gsdl_cgi->clean_param("directory") || "";
852 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
853 my $zip = $gsdl_cgi->clean_param("zip");
854
855 # Make sure we don't try to upload anything outside the collection
856 if ($file =~ /\.\./) {
857 $gsdl_cgi->generate_error("Illegal file specified.");
858 }
859 if ($directory =~ /\.\./) {
860 $gsdl_cgi->generate_error("Illegal directory specified.");
861 }
862
863 # Ensure the user is allowed to edit this collection
864 &authenticate_user($gsdl_cgi, $username, $collection);
865
866 my $gsdlhome = $ENV{'GSDLHOME'};
867 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
868 $gsdl_cgi->checked_chdir($collection_directory);
869
870 # Make sure the collection isn't locked by someone else
871 &lock_collection($gsdl_cgi, $username, $collection);
872
873 my $directory_path = &util::filename_cat($collection_directory, $directory);
874 if (!-d $directory_path) {
875 &util::mk_dir($directory_path);
876 if (!-d $directory_path) {
877 $gsdl_cgi->generate_error("Could not create directory $directory_path.");
878 }
879 }
880
881 my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
882 if (!open(FOUT, ">$file_path")) {
883 $gsdl_cgi->generate_error("Unable to write file $file_path");
884 }
885
886 # Read the uploaded data and write it out to file
887 # We have to pass the size of the uploaded data in the "fs" argument because IIS 6 seems to be
888 # completely incapable of working this out otherwise (causing the old code to crash)
889 my $buf;
890 my $num_bytes = 0;
891 my $num_bytes_remaining = $gsdl_cgi->clean_param("fs");
892 my $bytes_to_read = $num_bytes_remaining;
893 if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
894 binmode(FOUT);
895 while (read(STDIN, $buf, $bytes_to_read) > 0) {
896 print FOUT $buf;
897 $num_bytes += length($buf);
898 $num_bytes_remaining -= length($buf);
899 $bytes_to_read = $num_bytes_remaining;
900 if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
901 }
902 close(FOUT);
903
904 # If we have downloaded a zip file, unzip it
905 if (defined $zip) {
906 my $java = $gsdl_cgi->get_java_path();
907 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
908 my $java_args = "\"$file_path\" \"$directory_path\"";
909 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
910
911 my $java_output = `$java_command`;
912 my $java_status = $?;
913
914 # Remove the zip file once we have unzipped it, since it is an intermediate file only
915 unlink("$file_path");
916
917 if ($java_status > 0) {
918 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
919 }
920 }
921
922 $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
923}
924
925
926sub put_file
927{
928 my $gsdl_cgi = shift(@_);
929 my $file_path = shift(@_);
930 my $content_type = shift(@_);
931
932 if (open(PIN, "<$file_path")) {
933 print STDOUT "Content-type:$content_type\n\n";
934
935 my $buf;
936 my $num_bytes = 0;
937 binmode(PIN);
938 while (read(PIN, $buf, 1024) > 0) {
939 print STDOUT $buf;
940 $num_bytes += length($buf);
941 }
942
943 close(PIN);
944 }
945 else {
946 $gsdl_cgi->generate_error("Unable to read file $file_path\n $!");
947 }
948}
949
950
951sub send_mail
952{
953 my $gsdl_cgi = shift(@_);
954 my $mail_subject = shift(@_);
955 my $mail_content = shift(@_);
956
957 my $sendmail_command = "perl -S sendmail.pl";
958 $sendmail_command .= " -to \"" . $mail_to_address . "\"";
959 $sendmail_command .= " -from \"" . $mail_from_address . "\"";
960 $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
961 $sendmail_command .= " -subject \"" . $mail_subject . "\"";
962
963 if (!open(POUT, "| $sendmail_command")) {
964 $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
965 }
966 print POUT $mail_content . "\n";
967 close(POUT);
968}
969
970
971&main();
Note: See TracBrowser for help on using the repository browser.