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

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

Added an iis6_mode variable and new code for working around IIS 6 craziness.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 30.2 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 # Check that Java is installed and accessible
267 my $java = $gsdl_cgi->get_java_path();
268 my $java_command = "$java -version 2>&1";
269 my $java_output = `$java_command`;
270 my $java_status = $?;
271 if ($java_status < 0) {
272 # The Java command failed
273 $installation_status = "Java failed -- do you have the Java run-time installed?\n" . $gsdl_cgi->check_java_home() . "\n";
274 $installation_ok = 0;
275 }
276 else {
277 $installation_status = "Java found: $java_output";
278 }
279
280 # Show the values of some important environment variables
281 $installation_status .= "\n";
282 $installation_status .= "GSDLHOME: " . $ENV{'GSDLHOME'} . "\n";
283 $installation_status .= "GSDLOS: " . $ENV{'GSDLOS'} . "\n";
284 $installation_status .= "PATH: " . $ENV{'PATH'} . "\n";
285
286 if ($installation_ok) {
287 $gsdl_cgi->generate_ok_message($installation_status . "\nInstallation OK!");
288 }
289 else {
290 $gsdl_cgi->generate_error($installation_status);
291 }
292}
293
294
295sub delete_collection
296{
297 my ($gsdl_cgi, $username, $timestamp) = @_;
298
299 my $collection = $gsdl_cgi->clean_param("c");
300 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
301 $gsdl_cgi->generate_error("No collection specified.");
302 }
303
304 # Ensure the user is allowed to edit this collection
305 &authenticate_user($gsdl_cgi, $username, $collection);
306
307 my $gsdlhome = $ENV{'GSDLHOME'};
308 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
309 $gsdl_cgi->checked_chdir($collect_directory);
310
311 # Check that the collection exists
312 if (!-d $collection) {
313 $gsdl_cgi->generate_error("Collection $collection does not exist.");
314 }
315
316 # Make sure the collection isn't locked by someone else
317 &lock_collection($gsdl_cgi, $username, $collection);
318
319 $gsdl_cgi->checked_chdir($collect_directory);
320 $gsdl_cgi->local_rm_r("$collection");
321
322 # Check that the collection was deleted
323 if (-e $collection) {
324 $gsdl_cgi->generate_error("Could not delete collection $collection.");
325 }
326
327 $gsdl_cgi->generate_ok_message("Collection $collection deleted successfully.");
328}
329
330
331sub delete_collection_file
332{
333 my ($gsdl_cgi, $username, $timestamp) = @_;
334
335 my $collection = $gsdl_cgi->clean_param("c");
336 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
337 $gsdl_cgi->generate_error("No collection specified.");
338 }
339 my $file = $gsdl_cgi->clean_param("file");
340 if ((!defined $file) || ($file =~ m/^\s*$/)) {
341 $gsdl_cgi->generate_error("No file specified.");
342 }
343 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
344
345 # Make sure we don't try to delete anything outside the collection
346 if ($file =~ /\.\./) {
347 $gsdl_cgi->generate_error("Illegal file specified.");
348 }
349
350 # Ensure the user is allowed to edit this collection
351 &authenticate_user($gsdl_cgi, $username, $collection);
352
353 my $gsdlhome = $ENV{'GSDLHOME'};
354 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
355 $gsdl_cgi->checked_chdir($collection_directory);
356
357 # Make sure the collection isn't locked by someone else
358 &lock_collection($gsdl_cgi, $username, $collection);
359
360 # Check that the collection file exists
361 if (!-e $file) {
362 $gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
363 }
364 $gsdl_cgi->local_rm_r("$file");
365
366 # Check that the collection file was deleted
367 if (-e $file) {
368 $gsdl_cgi->generate_error("Could not delete collection file $file.");
369 }
370
371 $gsdl_cgi->generate_ok_message("Collection file $file deleted successfully.");
372}
373
374
375sub download_collection
376{
377 my ($gsdl_cgi, $username, $timestamp) = @_;
378
379 my $collection = $gsdl_cgi->clean_param("c");
380 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
381 $gsdl_cgi->generate_error("No collection specified.");
382 }
383
384 # Ensure the user is allowed to edit this collection
385 &authenticate_user($gsdl_cgi, $username, $collection);
386
387 my $gsdlhome = $ENV{'GSDLHOME'};
388 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
389 $gsdl_cgi->checked_chdir($collect_directory);
390
391 # Check that the collection exists
392 if (!-d $collection) {
393 $gsdl_cgi->generate_error("Collection $collection does not exist.");
394 }
395
396 # Make sure the collection isn't locked by someone else
397 &lock_collection($gsdl_cgi, $username, $collection);
398
399 # Zip up the collection
400 my $java = $gsdl_cgi->get_java_path();
401 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
402 my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-" . $timestamp . ".zip");
403 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
404 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args";
405
406 my $java_output = `$java_command`;
407 my $java_status = $?;
408 if ($java_status > 0) {
409 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
410 }
411
412 # Check that the zip file was created successfully
413 if (!-e $zip_file_path || -z $zip_file_path) {
414 $gsdl_cgi->generate_error("Collection zip file $zip_file_path could not be created.");
415 }
416
417 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
418 unlink("$zip_file_path") unless $debugging_enabled;
419}
420
421
422sub download_collection_archives
423{
424 my ($gsdl_cgi, $username, $timestamp) = @_;
425
426 my $collection = $gsdl_cgi->clean_param("c");
427 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
428 $gsdl_cgi->generate_error("No collection specified.");
429 }
430
431 # Ensure the user is allowed to edit this collection
432 &authenticate_user($gsdl_cgi, $username, $collection);
433
434 my $gsdlhome = $ENV{'GSDLHOME'};
435 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
436 $gsdl_cgi->checked_chdir($collect_directory);
437
438 # Check that the collection archives exist
439 if (!-d &util::filename_cat($collection, "archives")) {
440 $gsdl_cgi->generate_error("Collection archives do not exist.");
441 }
442
443 # Make sure the collection isn't locked by someone else
444 &lock_collection($gsdl_cgi, $username, $collection);
445
446 # Zip up the collection archives
447 my $java = $gsdl_cgi->get_java_path();
448 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
449 my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-archives-" . $timestamp . ".zip");
450 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
451 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args";
452
453 my $java_output = `$java_command`;
454 my $java_status = $?;
455 if ($java_status > 0) {
456 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
457 }
458
459 # Check that the zip file was created successfully
460 if (!-e $zip_file_path || -z $zip_file_path) {
461 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
462 }
463
464 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
465 unlink("$zip_file_path") unless $debugging_enabled;
466}
467
468
469# Collection locking unnecessary because this action isn't related to a particular collection
470sub download_collection_configurations
471{
472 my ($gsdl_cgi, $username, $timestamp) = @_;
473
474 # Users can be in any group to perform this action
475 my $user_groups = &authenticate_user($gsdl_cgi, $username, "");
476
477 my $gsdlhome = $ENV{'GSDLHOME'};
478 my $collect_directory = &util::filename_cat($gsdlhome, "collect");
479 $gsdl_cgi->checked_chdir($collect_directory);
480
481 # Zip up the collection configurations
482 my $java = $gsdl_cgi->get_java_path();
483 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
484 my $zip_file_path = &util::filename_cat($collect_directory, "collection-configurations-" . $timestamp . ".zip");
485 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$username\" \"$user_groups\"";
486 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args";
487
488 my $java_output = `$java_command`;
489 my $java_status = $?;
490 if ($java_status > 0) {
491 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
492 }
493
494 # Check that the zip file was created successfully
495 if (!-e $zip_file_path || -z $zip_file_path) {
496 $gsdl_cgi->generate_error("Collection configurations zip file $zip_file_path could not be created.");
497 }
498
499 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
500 unlink("$zip_file_path") unless $debugging_enabled;
501}
502
503
504sub download_collection_file
505{
506 my ($gsdl_cgi, $username, $timestamp) = @_;
507
508 my $collection = $gsdl_cgi->clean_param("c");
509 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
510 $gsdl_cgi->generate_error("No collection specified.");
511 }
512 my $file = $gsdl_cgi->clean_param("file");
513 if ((!defined $file) || ($file =~ m/^\s*$/)) {
514 $gsdl_cgi->generate_error("No file specified.");
515 }
516 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
517
518 # Make sure we don't try to download anything outside the collection
519 if ($file =~ /\.\./) {
520 $gsdl_cgi->generate_error("Illegal file specified.");
521 }
522
523 # Ensure the user is allowed to edit this collection
524 &authenticate_user($gsdl_cgi, $username, $collection);
525
526 my $gsdlhome = $ENV{'GSDLHOME'};
527 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
528 $gsdl_cgi->checked_chdir($collection_directory);
529
530 # Check that the collection file exists
531 if (!-e $file) {
532 $gsdl_cgi->generate_error("Collection file $file does not exist.");
533 }
534
535 # Make sure the collection isn't locked by someone else
536 &lock_collection($gsdl_cgi, $username, $collection);
537
538 # Zip up the collection file
539 my $java = $gsdl_cgi->get_java_path();
540 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
541 my $zip_file_path = &util::filename_cat($collection_directory, $collection . "-file-" . $timestamp . ".zip");
542 my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
543 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
544
545 my $java_output = `$java_command`;
546 my $java_status = $?;
547 if ($java_status > 0) {
548 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
549 }
550
551 # Check that the zip file was created successfully
552 if (!-e $zip_file_path || -z $zip_file_path) {
553 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
554 }
555
556 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
557 unlink("$zip_file_path") unless $debugging_enabled;
558}
559
560
561# Collection locking unnecessary because this action isn't related to a particular collection
562sub get_script_options
563{
564 my ($gsdl_cgi, $username, $timestamp) = @_;
565
566 my $script = $gsdl_cgi->clean_param("script");
567 if ((!defined $script) || ($script =~ m/^\s*$/)) {
568 $gsdl_cgi->generate_error("No script specified.");
569 }
570 $gsdl_cgi->delete("script");
571
572 # Users can be in any group to perform this action
573 &authenticate_user($gsdl_cgi, $username, "");
574
575 my $perl_args = "";
576 if ($script eq "classinfo.pl") {
577 $perl_args = $gsdl_cgi->clean_param("classifier") || "";
578 $gsdl_cgi->delete("classifier");
579 }
580 if ($script eq "pluginfo.pl") {
581 $perl_args = $gsdl_cgi->clean_param("plugin") || "";
582 $gsdl_cgi->delete("plugin");
583 }
584
585 foreach my $cgi_arg_name ($gsdl_cgi->param) {
586 my $cgi_arg_value = $gsdl_cgi->clean_param($cgi_arg_name) || "";
587 $cgi_arg_value = $gsdl_cgi->safe_val($cgi_arg_value);
588 if ($cgi_arg_value eq "") {
589 $perl_args = "-$cgi_arg_name " . $perl_args;
590 }
591 else {
592 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
593 }
594 }
595
596 my $perl_command = "perl -S $script $perl_args 2>&1";
597 my $perl_output = `$perl_command`;
598 my $perl_status = $?;
599 if ($perl_status > 0) {
600 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
601 }
602
603 print STDOUT "Content-type:text/plain\n\n";
604 print STDOUT $perl_output;
605}
606
607
608sub move_collection_file
609{
610 my ($gsdl_cgi, $username, $timestamp) = @_;
611
612 my $collection = $gsdl_cgi->clean_param("c");
613 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
614 $gsdl_cgi->generate_error("No collection specified.");
615 }
616 my $source_file = $gsdl_cgi->clean_param("source");
617 if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
618 $gsdl_cgi->generate_error("No source file specified.");
619 }
620 $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
621 my $target_file = $gsdl_cgi->clean_param("target");
622 if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
623 $gsdl_cgi->generate_error("No target file specified.");
624 }
625 $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
626
627 # Make sure we don't try to move anything outside the collection
628 if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
629 $gsdl_cgi->generate_error("Illegal file specified.");
630 }
631
632 # Ensure the user is allowed to edit this collection
633 &authenticate_user($gsdl_cgi, $username, $collection);
634
635 my $gsdlhome = $ENV{'GSDLHOME'};
636 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
637 $gsdl_cgi->checked_chdir($collection_directory);
638
639 # Check that the collection source file exists
640 if (!-e $source_file) {
641 $gsdl_cgi->generate_error("Collection file $source_file does not exist.");
642 }
643
644 # Make sure the collection isn't locked by someone else
645 &lock_collection($gsdl_cgi, $username, $collection);
646
647 &util::mv($source_file, $target_file);
648
649 # Check that the collection source file was moved
650 if (-e $source_file || !-e $target_file) {
651 $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
652 }
653
654 $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
655}
656
657
658sub new_collection_directory
659{
660 my ($gsdl_cgi, $username, $timestamp) = @_;
661
662 my $collection = $gsdl_cgi->clean_param("c");
663 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
664 $gsdl_cgi->generate_error("No collection specified.");
665 }
666 my $directory = $gsdl_cgi->clean_param("directory");
667 if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
668 $gsdl_cgi->generate_error("No directory specified.");
669 }
670 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
671
672 # Make sure we don't try to create anything outside the collection
673 if ($directory =~ /\.\./) {
674 $gsdl_cgi->generate_error("Illegal directory specified.");
675 }
676
677 # Ensure the user is allowed to edit this collection
678 &authenticate_user($gsdl_cgi, $username, $collection);
679
680 my $gsdlhome = $ENV{'GSDLHOME'};
681 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
682 $gsdl_cgi->checked_chdir($collection_directory);
683
684 # Check that the collection directory doesn't already exist
685 # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicity
686 # try to create the import directory
687# if (-d $directory) {
688# $gsdl_cgi->generate_error("Collection directory $directory already exists.");
689# }
690
691 # Make sure the collection isn't locked by someone else
692 &lock_collection($gsdl_cgi, $username, $collection);
693
694 &util::mk_dir($directory);
695
696 # Check that the collection directory was created
697 if (!-d $directory) {
698 $gsdl_cgi->generate_error("Could not create collection directory $directory.");
699 }
700
701 $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
702}
703
704
705sub run_script
706{
707 my ($gsdl_cgi, $username, $timestamp) = @_;
708
709 my $script = $gsdl_cgi->clean_param("script");
710 if ((!defined $script) || ($script =~ m/^\s*$/)) {
711 $gsdl_cgi->generate_error("No script specified.");
712 }
713 $gsdl_cgi->delete("script");
714 my $collection = $gsdl_cgi->clean_param("c");
715 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
716 $gsdl_cgi->generate_error("No collection specified.");
717 }
718 $gsdl_cgi->delete("c");
719
720 # Ensure the user is allowed to edit this collection
721 &authenticate_user($gsdl_cgi, $username, $collection);
722
723 # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
724 &lock_collection($gsdl_cgi, $username, $collection) unless ($script eq "mkcol.pl");
725
726 # Last argument is the collection name, except for explode_metadata_database.pl
727 my $perl_args = $collection;
728 if ($script eq "explode_metadata_database.pl") {
729 # Last argument is the file to be exploded
730 my $file = $gsdl_cgi->clean_param("file");
731 if ((!defined $file) || ($file =~ m/^\s*$/)) {
732 $gsdl_cgi->generate_error("No file specified.");
733 }
734 $gsdl_cgi->delete("file");
735 $perl_args = $file;
736 }
737
738 foreach my $cgi_arg_name ($gsdl_cgi->param) {
739 my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
740 if ($cgi_arg_value eq "") {
741 $perl_args = "-$cgi_arg_name " . $perl_args;
742 }
743 else {
744 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
745 }
746 }
747
748 my $perl_command = "perl -S $script $perl_args 2>&1";
749 if (!open(PIN, "$perl_command |")) {
750 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
751 }
752
753 print STDOUT "Content-type:text/plain\n\n";
754 while (defined (my $perl_output_line = <PIN>)) {
755 print STDOUT $perl_output_line;
756 }
757 close(PIN);
758
759 my $perl_status = $?;
760 if ($perl_status > 0) {
761 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
762 }
763 elsif ($mail_enabled) {
764 if ($script eq "buildcol.pl") {
765 &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
766 }
767 }
768}
769
770
771sub upload_collection_file
772{
773 my ($gsdl_cgi, $username, $timestamp) = @_;
774
775 my $collection = $gsdl_cgi->clean_param("c");
776 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
777 $gsdl_cgi->generate_error("No collection specified.");
778 }
779 my $file = $gsdl_cgi->clean_param("file");
780 if ((!defined $file) || ($file =~ m/^\s*$/)) {
781 $gsdl_cgi->generate_error("No file specified.");
782 }
783 my $directory = $gsdl_cgi->clean_param("directory") || "";
784 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
785 my $zip = $gsdl_cgi->clean_param("zip");
786
787 # Make sure we don't try to upload anything outside the collection
788 if ($file =~ /\.\./) {
789 $gsdl_cgi->generate_error("Illegal file specified.");
790 }
791 if ($directory =~ /\.\./) {
792 $gsdl_cgi->generate_error("Illegal directory specified.");
793 }
794
795 # Ensure the user is allowed to edit this collection
796 &authenticate_user($gsdl_cgi, $username, $collection);
797
798 my $gsdlhome = $ENV{'GSDLHOME'};
799 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
800 $gsdl_cgi->checked_chdir($collection_directory);
801
802 # Make sure the collection isn't locked by someone else
803 &lock_collection($gsdl_cgi, $username, $collection);
804
805 my $directory_path = &util::filename_cat($collection_directory, $directory);
806 if (!-d $directory_path) {
807 &util::mk_dir($directory_path);
808 if (!-d $directory_path) {
809 $gsdl_cgi->generate_error("Could not create directory $directory_path.");
810 }
811 }
812
813 my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
814 if (!open(FOUT, ">$file_path")) {
815 $gsdl_cgi->generate_error("Unable to write file $file_path");
816 }
817
818 # Read the uploaded data and write it out to file
819 my $buf;
820 my $num_bytes = 0;
821 binmode(FOUT);
822 while (read(STDIN, $buf, 1024) > 0) {
823 print FOUT $buf;
824 $num_bytes += length($buf);
825 }
826 close(FOUT);
827
828 # If we have downloaded a zip file, unzip it
829 if (defined $zip) {
830 my $java = $gsdl_cgi->get_java_path();
831 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
832 my $java_args = "\"$file_path\" \"$directory_path\"";
833 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
834
835 my $java_output = `$java_command`;
836 my $java_status = $?;
837
838 # Remove the zip file once we have unzipped it, since it is an intermediate file only
839 unlink("$file_path");
840
841 if ($java_status > 0) {
842 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
843 }
844 }
845
846 $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
847}
848
849
850sub put_file
851{
852 my $gsdl_cgi = shift(@_);
853 my $file_path = shift(@_);
854 my $content_type = shift(@_);
855
856 if (open(PIN, "<$file_path")) {
857 print STDOUT "Content-type:$content_type\n\n";
858
859 my $buf;
860 my $num_bytes = 0;
861 binmode(PIN);
862 while (read(PIN, $buf, 1024) > 0) {
863 print STDOUT $buf;
864 $num_bytes += length($buf);
865 }
866
867 close(PIN);
868 }
869 else {
870 $gsdl_cgi->generate_error("Unable to read file $file_path\n $!");
871 }
872}
873
874
875sub send_mail
876{
877 my $gsdl_cgi = shift(@_);
878 my $mail_subject = shift(@_);
879 my $mail_content = shift(@_);
880
881 my $sendmail_command = "perl -S sendmail.pl";
882 $sendmail_command .= " -to \"" . $mail_to_address . "\"";
883 $sendmail_command .= " -from \"" . $mail_from_address . "\"";
884 $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
885 $sendmail_command .= " -subject \"" . $mail_subject . "\"";
886
887 if (!open(POUT, "| $sendmail_command")) {
888 $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
889 }
890 print POUT $mail_content . "\n";
891 close(POUT);
892}
893
894
895&main();
Note: See TracBrowser for help on using the repository browser.