source: main/trunk/greenstone2/cgi-bin/gliserver.pl@ 24818

Last change on this file since 24818 was 24292, checked in by ak19, 13 years ago

Dr Bainbridge fixed a possible error: a file handle to the users database was not closed before.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 46.5 KB
RevLine 
[22472]1#!/usr/bin/perl -w
[11293]2# Need to specify the full path of Perl above
[11110]3
[16467]4# This file merges Michael Dewsnip's gliserver.pl for GS2 and Quan Qiu's gliserver4gs3.pl (GS3)
[11110]5
[11148]6use strict;
[16467]7no strict 'subs';
8no strict 'refs'; # allow filehandles to be variables and viceversa
[11110]9
[23197]10BEGIN {
11
12 # Line to stop annoying child DOS CMD windows from appearing
13 Win32::SetChildShowWindow(0)
14 if defined &Win32::SetChildShowWindow;
15
16}
17
18
[14235]19# Set this to 1 to work around IIS 6 craziness
20my $iis6_mode = 0;
21
[16467]22##
[14235]23# IIS 6: for some reason, IIS runs this script with the working directory set to the Greenstone
24# directory rather than the cgi-bin directory, causing lots of stuff to fail
25if ($iis6_mode)
26{
[16467]27 # Change into cgi-bin directory - need to ensure it exists, since gliserver deals with both GS2 and GS3
28 if(-e "cgi-bin" && -d "cgi-bin") { # GS2
29 chdir("cgi-bin");
30 } else { # iis6_mode is not applicable for Greenstone 3
31 $iis6_mode = 0;
32 }
[14235]33}
34
35
[16467]36# We use require and an eval here (instead of "use package") to catch any errors loading the module (for IIS)
[14235]37eval("require \"gsdlCGI.pm\"");
38if ($@)
39{
40 print STDOUT "Content-type:text/plain\n\n";
41 print STDOUT "ERROR: $@\n";
42 exit 0;
43}
44
45
[16467]46#my $authentication_enabled = 0;
47my $debugging_enabled = 0; # if 1, debugging is enabled and unlinking intermediate files (deleting files) will not happen
[11110]48
49my $mail_enabled = 0;
50my $mail_to_address = "user\@server"; # Set this appropriately
51my $mail_from_address = "user\@server"; # Set this appropriately
52my $mail_smtp_server = "smtp.server"; # Set this appropriately
53
54sub main
[16467]55{
[11148]56 my $gsdl_cgi = new gsdlCGI();
[11110]57
[11114]58 # Load the Greenstone modules that we need to use
59 $gsdl_cgi->setup_gsdl();
60 my $gsdlhome = $ENV{'GSDLHOME'};
[16467]61
[11114]62 $gsdl_cgi->checked_chdir($gsdlhome);
63
[11110]64 # Encrypt the password
[16467]65 $gsdl_cgi->encrypt_password();
[11110]66
67 $gsdl_cgi->parse_cgi_args();
68
[13168]69 # We don't want the gsdlCGI module to return errors and warnings in XML
70 $gsdl_cgi->{'xml'} = 0;
71
72 # Retrieve the (required) command CGI argument
[11110]73 my $cmd = $gsdl_cgi->clean_param("cmd");
74 if (!defined $cmd) {
75 $gsdl_cgi->generate_error("No command specified.");
76 }
77 $gsdl_cgi->delete("cmd");
78
[16467]79 # The check-installation, greenstone-server-version and get-library-url commands have no arguments
[11293]80 if ($cmd eq "check-installation") {
81 &check_installation($gsdl_cgi);
82 return;
83 }
[16467]84 elsif ($cmd eq "greenstone-server-version") {
85 &greenstone_server_version($gsdl_cgi);
86 return;
87 }
88 elsif ($cmd eq "get-library-url-suffix") {
89 &get_library_url_suffix($gsdl_cgi);
90 return;
91 }
[11293]92
93 # All other commands require a username, for locking and authentication
[11110]94 my $username = $gsdl_cgi->clean_param("un");
95 if ((!defined $username) || ($username =~ m/^\s*$/)) {
96 $gsdl_cgi->generate_error("No username specified.");
97 }
98 $gsdl_cgi->delete("un");
99
[13809]100 # Get then remove the ts (timestamp) argument (since this can mess up other scripts)
101 my $timestamp = $gsdl_cgi->clean_param("ts");
[13811]102 if ((!defined $timestamp) || ($timestamp =~ m/^\s*$/)) {
103 $timestamp = time(); # Fall back to using the Perl time() function to generate a timestamp
104 }
[13809]105 $gsdl_cgi->delete("ts");
106
[16467]107 my $site; # undefined on declaration, see http://perldoc.perl.org/perlsyn.html
108 if($gsdl_cgi->greenstone_version() != 2) { # all GS versions after 2 may define site
109 $site = $gsdl_cgi->clean_param("site");
110 if (!defined $site) {
111 $gsdl_cgi->generate_error("No site specified.");
112 }
113 $gsdl_cgi->delete("site");
114 }
115
116
[11110]117 if ($cmd eq "delete-collection") {
[16467]118 &delete_collection($gsdl_cgi, $username, $timestamp, $site);
[11110]119 }
120 elsif ($cmd eq "download-collection") {
[16467]121 &download_collection($gsdl_cgi, $username, $timestamp, $site);
[11110]122 }
123 elsif ($cmd eq "download-collection-archives") {
[16467]124 &download_collection_archives($gsdl_cgi, $username, $timestamp, $site);
[11110]125 }
126 elsif ($cmd eq "download-collection-configurations") {
[16467]127 &download_collection_configurations($gsdl_cgi, $username, $timestamp, $site);
[11110]128 }
129 elsif ($cmd eq "download-collection-file") {
[16467]130 &download_collection_file($gsdl_cgi, $username, $timestamp, $site);
[11110]131 }
132 elsif ($cmd eq "delete-collection-file") {
[16467]133 &delete_collection_file($gsdl_cgi, $username, $timestamp, $site);
[11110]134 }
135 elsif ($cmd eq "get-script-options") {
[16467]136 &get_script_options($gsdl_cgi, $username, $timestamp, $site);
[11110]137 }
138 elsif ($cmd eq "move-collection-file") {
[16467]139 &move_collection_file($gsdl_cgi, $username, $timestamp, $site);
[11110]140 }
141 elsif ($cmd eq "new-collection-directory") {
[16467]142 &new_collection_directory($gsdl_cgi, $username, $timestamp, $site);
[11110]143 }
144 elsif ($cmd eq "run-script") {
[16467]145 &run_script($gsdl_cgi, $username, $timestamp, $site);
[11110]146 }
147 elsif ($cmd eq "timeout-test") {
148 while (1) { }
149 }
150 elsif ($cmd eq "upload-collection-file") {
[16467]151 &upload_collection_file($gsdl_cgi, $username, $timestamp, $site);
152 }
[15170]153 elsif ($cmd eq "file-exists") {
[16467]154 &file_exists($gsdl_cgi, $site);
155 }
156 # cmds not in Greenstone 2:
157 elsif ($gsdl_cgi->greenstone_version() != 2) {
158 if ($cmd eq "download-web-xml-file") {
159 &download_web_xml_file($gsdl_cgi, $username, $timestamp, $site);
160 }
161 elsif ($cmd eq "user-validation") {
162 &user_validation($gsdl_cgi, $username, $timestamp, $site);
163 }
164 elsif ($cmd eq "get-site-names") {
165 &get_site_names($gsdl_cgi, $username, $timestamp, $site);
166 }
167 }
[11110]168 else {
169 $gsdl_cgi->generate_error("Unrecognised command: '$cmd'");
170 }
[16467]171
[11110]172}
173
[16467]174
[11110]175sub authenticate_user
176{
177 my $gsdl_cgi = shift(@_);
178 my $username = shift(@_);
[11937]179 my $collection = shift(@_);
[16467]180 my $site = shift(@_);
[11110]181
[16467]182 # Even if we're not authenticating remove the un and pw arguments, since these can mess up other scripts
[11110]183 my $user_password = $gsdl_cgi->clean_param("pw");
184 $gsdl_cgi->delete("pw");
185
[16467]186 # Only authenticate if it is enabled
187 # return "all-collections-editor" if (!$authentication_enabled);
188
[11110]189 if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) {
190 $gsdl_cgi->generate_error("Authentication failed: no password specified.");
191 }
192
[16467]193 my $users_db_content;
194 if($gsdl_cgi->greenstone_version() == 2) {
195 my $etc_directory = &util::filename_cat($ENV{'GSDLHOME'}, "etc");
[19137]196 my $users_db_file_path = &util::filename_cat($etc_directory, "users.gdb");
[16467]197
198 # Use db2txt instead of GDBM_File to get the user accounts information
199 $users_db_content = "";
200 open(USERS_DB, "db2txt \"$users_db_file_path\" |");
201 while (<USERS_DB>) {
202 $users_db_content .= $_;
203 }
[24292]204 close(USERS_DB);
[16467]205 }
206 elsif($gsdl_cgi->greenstone_version() == 3) {
207 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
[11110]208
[16467]209 my $java = $gsdl_cgi->get_java_path();
210 my $java_gsdl3_classpath = &util::filename_cat($gsdl3srchome, "web", "WEB-INF", "lib", "gsdl3.jar");
211 my $java_derby_classpath = &util::filename_cat($gsdl3srchome, "web", "WEB-INF", "lib", "derby.jar");
212 my $java_classpath;
213 my $gsdlos = $ENV{'GSDLOS'};
214 if ($gsdlos !~ m/windows/){
215 $java_classpath = $java_gsdl3_classpath . ":" . $java_derby_classpath;
216 }else{
217 $java_classpath = $java_gsdl3_classpath . ";" . $java_derby_classpath;
218 }
219 my $java_args = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "etc", "usersDB");
220 $gsdl_cgi->checked_chdir($java_args);
221 my $java_command="\"$java\" -classpath \"$java_classpath\" org.greenstone.gsdl3.util.usersDB2txt \"$java_args\" 2>&1";
222 $users_db_content = `$java_command`;
[11110]223 }
[16467]224
225 # Get the user account information from the usersDB database
[11110]226 my %users_db_data = ();
227 foreach my $users_db_entry (split(/-{70}/, $users_db_content)) {
[16467]228 if ($users_db_entry =~ m/\n?\[(.+)\]\n/) {
[11110]229 $users_db_data{$1} = $users_db_entry;
230 }
231 }
232
233 # Check username
234 my $user_data = $users_db_data{$username};
235 if (!defined $user_data) {
236 $gsdl_cgi->generate_error("Authentication failed: no account for user '$username'.");
237 }
238
239 # Check password
[16467]240 my ($valid_user_password) = ($user_data =~ m/\<password\>(.*)/);
[11110]241 if ($user_password ne $valid_user_password) {
242 $gsdl_cgi->generate_error("Authentication failed: incorrect password.");
243 }
244
[11941]245 # Check group
[16467]246 my ($user_groups) = ($user_data =~ m/\<groups\>(.*)/);
247
[11938]248 if ($collection eq "") {
[11941]249 # If we're not editing a collection then the user doesn't need to be in a particular group
250 return $user_groups; # Authentication successful
[11938]251 }
[16467]252
[11110]253 foreach my $user_group (split(/\,/, $user_groups)) {
[11940]254 # Does this user have access to all collections?
255 if ($user_group eq "all-collections-editor") {
[11941]256 return $user_groups; # Authentication successful
[11110]257 }
[11940]258 # Does this user have access to personal collections, and is this one?
[16467]259 if ($user_group eq "personal-collections-editor" && $collection =~ m/^$username\-/) {
[11941]260 return $user_groups; # Authentication successful
[11940]261 }
262 # Does this user have access to this collection
263 if ($user_group eq "$collection-collection-editor") {
[11941]264 return $user_groups; # Authentication successful
[11940]265 }
[11110]266 }
267 $gsdl_cgi->generate_error("Authentication failed: user is not in the required group.");
268}
269
270
271sub lock_collection
272{
273 my $gsdl_cgi = shift(@_);
[11936]274 my $username = shift(@_);
[11110]275 my $collection = shift(@_);
[16467]276 my $site = shift(@_);
[11110]277
278 my $steal_lock = $gsdl_cgi->clean_param("steal_lock");
279 $gsdl_cgi->delete("steal_lock");
280
[16467]281 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
[11110]282 $gsdl_cgi->checked_chdir($collection_directory);
283
284 # Check if a lock file already exists for this collection
285 my $lock_file_name = "gli.lck";
286 if (-e $lock_file_name) {
287 # A lock file already exists... check if it's ours
[13395]288 my $lock_file_content = "";
[11110]289 open(LOCK_FILE, "<$lock_file_name");
[13395]290 while (<LOCK_FILE>) {
291 $lock_file_content .= $_;
292 }
[11110]293 close(LOCK_FILE);
294
[13395]295 # Pick out the owner of the lock file
[16467]296 $lock_file_content =~ m/\<User\>(.*?)\<\/User\>/;
[13395]297 my $lock_file_owner = $1;
298
[11110]299 # The lock file is ours, so there is no problem
300 if ($lock_file_owner eq $username) {
301 return;
302 }
303
304 # The lock file is not ours, so throw an error unless "steal_lock" is set
305 unless (defined $steal_lock) {
306 $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner");
307 }
308 }
309
[13395]310 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
311 my $current_time = sprintf("%02d/%02d/%d %02d:%02d:%02d", $mday, $mon + 1, $year + 1900, $hour, $min, $sec);
312
313 # Create a lock file for us (in the same format as the GLI) and we're done
[11110]314 open(LOCK_FILE, ">$lock_file_name");
[13395]315 print LOCK_FILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
316 print LOCK_FILE "<LockFile>\n";
317 print LOCK_FILE " <User>" . $username . "</User>\n";
318 print LOCK_FILE " <Machine>(Remote)</Machine>\n";
319 print LOCK_FILE " <Date>" . $current_time . "</Date>\n";
320 print LOCK_FILE "</LockFile>\n";
[11110]321 close(LOCK_FILE);
322}
323
324
325# ----------------------------------------------------------------------------------------------------
326# ACTIONS
327# ----------------------------------------------------------------------------------------------------
[16467]328# This routine, which uses the variable site, won't get called by GS2,
329sub user_validation{
330 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
331
332 # Users can be in any group to perform this action
333 my $user_err = &authenticate_user($gsdl_cgi, $username, "", $site);
334 if (defined $user_err && $user_err!~ m/ERROR/){
335 if ($user_err!~ m/ERROR/){
336 #$gsdl_cgi->generate_error("Authentication failed: $username is not valid");
337 $gsdl_cgi->generate_ok($user_err);
338 #print $user_err;
339 }else{
340 $gsdl_cgi->generate_error($user_err);
341 #print "not valid" . $user_err;
342 }
343 }else{
344 $gsdl_cgi->generate_error("Authentication failed: $username is not valid");
345 }
346}
[11110]347
[11293]348sub check_installation
349{
350 my ($gsdl_cgi) = @_;
351
[13385]352 my $installation_ok = 1;
353 my $installation_status = "";
354
[11293]355 # Check that Java is installed and accessible
356 my $java = $gsdl_cgi->get_java_path();
[16467]357 my $java_command = "\"$java\" -version 2>&1";
358
[14236]359 # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
360 # directly out to the page
[16467]361 if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) { ##
362 print STDOUT "Content-type:text/plain\n\n";
363 $java_command = "\"$java\" -version";
[14236]364 }
365
[11293]366 my $java_output = `$java_command`;
[16467]367
[11293]368 my $java_status = $?;
369 if ($java_status < 0) {
[13385]370 # The Java command failed
371 $installation_status = "Java failed -- do you have the Java run-time installed?\n" . $gsdl_cgi->check_java_home() . "\n";
372 $installation_ok = 0;
[11293]373 }
[13385]374 else {
375 $installation_status = "Java found: $java_output";
376 }
[11293]377
[13385]378 # Show the values of some important environment variables
379 $installation_status .= "\n";
[16467]380 if($gsdl_cgi->greenstone_version() != 2) {
381 $installation_status .= "GSDL3SRCHOME: " . $ENV{'GSDL3SRCHOME'} . "\n";
382 $installation_status .= "GSDL3HOME: " . $ENV{'GSDL3HOME'} . "\n";
383 }
[13385]384 $installation_status .= "GSDLHOME: " . $ENV{'GSDLHOME'} . "\n";
385 $installation_status .= "GSDLOS: " . $ENV{'GSDLOS'} . "\n";
[16467]386 $installation_status .= "JAVA_HOME: " . $ENV{'JAVA_HOME'} . "\n" if defined($ENV{'JAVA_HOME'}); # on GS2, Java's only on the PATH
[13385]387 $installation_status .= "PATH: " . $ENV{'PATH'} . "\n";
[16467]388 if(defined $ENV{'FEDORA_VERSION'}) { # not using FLI unless version set
389 $installation_status .= "FEDORA_HOME: ".$ENV{'FEDORA_HOME'} . "\n";
390 $installation_status .= "FEDORA_VERSION: ".$ENV{'FEDORA_VERSION'};
[13385]391 }
[16467]392
393 if ($installation_ok) { ## M. Dewsnip's svn log comment stated that for iis6_mode output needs to go to STDOUT
394 if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) {
395 print STDOUT $installation_status . "\nInstallation OK!";
396 } else {
397 $gsdl_cgi->generate_ok_message($installation_status . "\nInstallation OK!");
398 }
399 }
[13385]400 else {
[16467]401 if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) {
402 print STDOUT $installation_status;
403 } else {
404 $gsdl_cgi->generate_error($installation_status);
405 }
[13385]406 }
[11293]407}
408
409
[11110]410sub delete_collection
411{
[16467]412 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]413
414 my $collection = $gsdl_cgi->clean_param("c");
415 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
416 $gsdl_cgi->generate_error("No collection specified.");
417 }
[22453]418 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
[11110]419
[11937]420 # Ensure the user is allowed to edit this collection
[16467]421 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]422
[16467]423
424 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
[11110]425 $gsdl_cgi->checked_chdir($collect_directory);
426
427 # Check that the collection exists
428 if (!-d $collection) {
429 $gsdl_cgi->generate_error("Collection $collection does not exist.");
430 }
431
432 # Make sure the collection isn't locked by someone else
[16467]433 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]434
435 $gsdl_cgi->checked_chdir($collect_directory);
436 $gsdl_cgi->local_rm_r("$collection");
437
438 # Check that the collection was deleted
439 if (-e $collection) {
440 $gsdl_cgi->generate_error("Could not delete collection $collection.");
441 }
442
443 $gsdl_cgi->generate_ok_message("Collection $collection deleted successfully.");
444}
445
446
447sub delete_collection_file
448{
[16467]449 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]450
451 my $collection = $gsdl_cgi->clean_param("c");
452 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
453 $gsdl_cgi->generate_error("No collection specified.");
454 }
[22453]455 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
456
[11110]457 my $file = $gsdl_cgi->clean_param("file");
458 if ((!defined $file) || ($file =~ m/^\s*$/)) {
459 $gsdl_cgi->generate_error("No file specified.");
460 }
[18649]461 $file = $gsdl_cgi->decode($file);
[11110]462 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
463
464 # Make sure we don't try to delete anything outside the collection
[16467]465 if ($file =~ m/\.\./) {
[11110]466 $gsdl_cgi->generate_error("Illegal file specified.");
467 }
468
[11937]469 # Ensure the user is allowed to edit this collection
[16467]470 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]471
[16467]472 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
473 if (!-d $collection_directory){ ## wasn't there in gs2, ok_msg or error_msg?
474 $gsdl_cgi->generate_ok_message("Directory $collection_directory does not exist.");
475 die;
476 }
477
[11110]478 $gsdl_cgi->checked_chdir($collection_directory);
479
480 # Make sure the collection isn't locked by someone else
[16467]481 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]482
483 # Check that the collection file exists
[16467]484 if (!-e $file) { ## original didn't have 'die', but it was an ok message
[11943]485 $gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
[16467]486 die;
[11110]487 }
488 $gsdl_cgi->local_rm_r("$file");
489
490 # Check that the collection file was deleted
491 if (-e $file) {
492 $gsdl_cgi->generate_error("Could not delete collection file $file.");
493 }
494
495 $gsdl_cgi->generate_ok_message("Collection file $file deleted successfully.");
496}
497
498
499sub download_collection
500{
[16467]501 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]502
503 my $collection = $gsdl_cgi->clean_param("c");
504 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
505 $gsdl_cgi->generate_error("No collection specified.");
506 }
[22453]507 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
[19172]508
509 # language and region Environment Variable setting on the client side that was used to zip files.
[19189]510 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
[19172]511 $gsdl_cgi->delete("lr");
[11110]512
[11937]513 # Ensure the user is allowed to edit this collection
[16467]514 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]515
[16467]516 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
[11110]517 $gsdl_cgi->checked_chdir($collect_directory);
518
519 # Check that the collection exists
520 if (!-d $collection) {
[16467]521 $gsdl_cgi->generate_ok_message("Collection $collection does not exist."); ## original had an error msg (from where it would die)
522 die;
[11110]523 }
524
525 # Make sure the collection isn't locked by someone else
[16467]526 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]527
528 # Zip up the collection
529 my $java = $gsdl_cgi->get_java_path();
[16467]530 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
[13811]531 my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-" . $timestamp . ".zip");
[11110]532 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
[16467]533 if($gsdl_cgi->greenstone_version() != 2) {
534 $java_args .= " gsdl3"; ## must this be done elsewhere as well?
535 }
[11110]536
[19252]537 $ENV{'LANG'} = $lang_env;
538 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args";
539
[11110]540 my $java_output = `$java_command`;
541 my $java_status = $?;
542 if ($java_status > 0) {
543 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
544 }
545
546 # Check that the zip file was created successfully
547 if (!-e $zip_file_path || -z $zip_file_path) {
548 $gsdl_cgi->generate_error("Collection zip file $zip_file_path could not be created.");
549 }
550
[16467]551 &put_file($gsdl_cgi, $zip_file_path, "application/zip"); # file is transferred to client
552 unlink("$zip_file_path") unless $debugging_enabled; # deletes the local intermediate zip file
[11110]553}
554
555
556sub download_collection_archives
557{
[16467]558 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]559
560 my $collection = $gsdl_cgi->clean_param("c");
561 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
562 $gsdl_cgi->generate_error("No collection specified.");
563 }
[22453]564 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
[11110]565
[19172]566 # language and region Environment Variable setting on the client side that was used to zip files.
[19189]567 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
[19172]568 $gsdl_cgi->delete("lr");
569
[11937]570 # Ensure the user is allowed to edit this collection
[16467]571 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]572
[16467]573 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
[11110]574 $gsdl_cgi->checked_chdir($collect_directory);
575
576 # Check that the collection archives exist
577 if (!-d &util::filename_cat($collection, "archives")) {
578 $gsdl_cgi->generate_error("Collection archives do not exist.");
579 }
580
581 # Make sure the collection isn't locked by someone else
[16467]582 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]583
584 # Zip up the collection archives
585 my $java = $gsdl_cgi->get_java_path();
[16467]586 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
[13811]587 my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-archives-" . $timestamp . ".zip");
[11110]588 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
[19252]589 $ENV{'LANG'} = $lang_env;
590 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args";
[11110]591
592 my $java_output = `$java_command`;
593 my $java_status = $?;
594 if ($java_status > 0) {
595 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
596 }
597
598 # Check that the zip file was created successfully
599 if (!-e $zip_file_path || -z $zip_file_path) {
600 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
601 }
602
603 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
604 unlink("$zip_file_path") unless $debugging_enabled;
605}
606
607
608# Collection locking unnecessary because this action isn't related to a particular collection
609sub download_collection_configurations
610{
[16467]611 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[19172]612
613 # language and region Environment Variable setting on the client side that was used to zip files.
[19189]614 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
[19172]615 $gsdl_cgi->delete("lr");
[16467]616
[11937]617 # Users can be in any group to perform this action
[16467]618 my $user_groups = &authenticate_user($gsdl_cgi, $username, "", $site);
[11110]619
[16467]620 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
[11110]621 $gsdl_cgi->checked_chdir($collect_directory);
622
623 # Zip up the collection configurations
624 my $java = $gsdl_cgi->get_java_path();
[16467]625 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
[13811]626 my $zip_file_path = &util::filename_cat($collect_directory, "collection-configurations-" . $timestamp . ".zip");
[11942]627 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$username\" \"$user_groups\"";
[19252]628 $ENV{'LANG'} = $lang_env;
629 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args";
[11110]630 my $java_output = `$java_command`;
631 my $java_status = $?;
632 if ($java_status > 0) {
633 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
634 }
635
636 # Check that the zip file was created successfully
637 if (!-e $zip_file_path || -z $zip_file_path) {
638 $gsdl_cgi->generate_error("Collection configurations zip file $zip_file_path could not be created.");
639 }
[19233]640
[11110]641 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
642 unlink("$zip_file_path") unless $debugging_enabled;
643}
644
[15170]645# Method that will check if the given file exists
646# No error message: all messages generated are OK messages
647# This method will simply state whether the file exists or does not exist.
648sub file_exists
649{
[16467]650 my ($gsdl_cgi, $site) = @_;
[15170]651
652 my $collection = $gsdl_cgi->clean_param("c");
653 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
654 $gsdl_cgi->generate_error("No collection specified.");
655 }
[22453]656 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
657
[15170]658 my $file = $gsdl_cgi->clean_param("file");
659 if ((!defined $file) || ($file =~ m/^\s*$/)) {
660 $gsdl_cgi->generate_error("No file specified.");
661 }
[18649]662 $file = "\"$file\""; # Windows: bookend the relative filepath with quotes in case it contains spaces
663 $file = $gsdl_cgi->decode($file);
[15170]664 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
[18649]665
[16467]666 # Not necessary: checking whether the user is authenticated to query existence of the file
[15170]667 #&authenticate_user($gsdl_cgi, $username, $collection);
668
[16467]669 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
670 $gsdl_cgi->checked_chdir($collection_directory); # cd into the directory of that collection
[15170]671
672 # Check that the collection file exists
673 if (-e $file) {
674 $gsdl_cgi->generate_ok_message("File $file exists.");
675 } else {
676 $gsdl_cgi->generate_ok_message("File $file does not exist.");
677 }
678}
679
[11110]680sub download_collection_file
681{
[16467]682 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]683
684 my $collection = $gsdl_cgi->clean_param("c");
685 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
686 $gsdl_cgi->generate_error("No collection specified.");
687 }
[23197]688 my $collection_tail_name = $collection;
689 $collection_tail_name =~ s/^(.*\|)//;
[22453]690 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
691
[19172]692 # language and region Environment Variable setting on the client side that was used to zip files.
[19189]693 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
[19172]694 $gsdl_cgi->delete("lr");
[11110]695 my $file = $gsdl_cgi->clean_param("file");
696 if ((!defined $file) || ($file =~ m/^\s*$/)) {
697 $gsdl_cgi->generate_error("No file specified.");
698 }
699 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
700
701 # Make sure we don't try to download anything outside the collection
[16467]702 if ($file =~ m/\.\./) {
[11110]703 $gsdl_cgi->generate_error("Illegal file specified.");
704 }
705
[11937]706 # Ensure the user is allowed to edit this collection
[16467]707 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]708
[16467]709 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
[11110]710 $gsdl_cgi->checked_chdir($collection_directory);
711
712 # Check that the collection file exists
713 if (!-e $file) {
[16467]714 $gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
715 die;
[11110]716 }
717
718 # Make sure the collection isn't locked by someone else
[16467]719 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]720
721 # Zip up the collection file
722 my $java = $gsdl_cgi->get_java_path();
[16467]723 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
[22453]724 my $zip_file_path = &util::filename_cat($collection_directory, $collection_tail_name . "-file-" . $timestamp . ".zip");
[11110]725 my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
[19252]726 $ENV{'LANG'} = $lang_env;
727 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
[11110]728
729 my $java_output = `$java_command`;
730 my $java_status = $?;
731 if ($java_status > 0) {
732 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
733 }
734
735 # Check that the zip file was created successfully
736 if (!-e $zip_file_path || -z $zip_file_path) {
737 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
738 }
739
740 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
741 unlink("$zip_file_path") unless $debugging_enabled;
742}
743
[16467]744# download web.xml from the server
745sub download_web_xml_file
746{
747 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]748
[16467]749 # Users can be in any group to perform this action
750 my $user_groups = &authenticate_user($gsdl_cgi, $username, "", $site);
751
[19172]752 # language and region Environment Variable setting on the client side that was used to zip files.
[19189]753 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
[19172]754 $gsdl_cgi->delete("lr");
[16467]755 my $file = $gsdl_cgi->clean_param("file");
756 if ((!defined $file) || ($file =~ m/^\s*$/)) {
757 $gsdl_cgi->generate_error("No file specified.");
758 }
759 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
760
761 # Make sure we don't try to download anything else
762 if ($file =~ m/\.\./) {
763 $gsdl_cgi->generate_error("Illegal file specified.");
764 }
765
766 my $web_inf_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "WEB-INF");
767 $gsdl_cgi->checked_chdir($web_inf_directory);
768
769 # Check that the collection file exists
770 if (!-e $file) {
771 $gsdl_cgi->generate_error("file $file does not exist.");
772 }
773
774 # Zip up the collection file
775 my $java = $gsdl_cgi->get_java_path();
776 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
777 my $zip_file_path = &util::filename_cat($web_inf_directory, "webxml" . $timestamp . ".zip");
778 my $java_args = "\"$zip_file_path\" \"$web_inf_directory\" \"$file\"";
[19252]779 $ENV{'LANG'} = $lang_env;
780 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
[16467]781 my $java_output = `$java_command`;
782
783 my $java_status = $?;
784 if ($java_status > 0) {
785 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
786 }
787
788 # Check that the zip file was created successfully
789 if (!-e $zip_file_path || -z $zip_file_path) {
790 $gsdl_cgi->generate_error("web.xml zip file $zip_file_path could not be created.");
791 }
792
793 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
794
795 unlink("$zip_file_path") unless $debugging_enabled;
796}
797
[11110]798# Collection locking unnecessary because this action isn't related to a particular collection
799sub get_script_options
800{
[16467]801 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]802
803 my $script = $gsdl_cgi->clean_param("script");
804 if ((!defined $script) || ($script =~ m/^\s*$/)) {
805 $gsdl_cgi->generate_error("No script specified.");
806 }
807 $gsdl_cgi->delete("script");
808
[11937]809 # Users can be in any group to perform this action
[16467]810 &authenticate_user($gsdl_cgi, $username, "", $site);
811 $gsdl_cgi->delete("ts"); ## two lines from GS3 version, doesn't seem to harm GS2
812 $gsdl_cgi->delete("pw");
813
[11110]814
815 my $perl_args = "";
816 if ($script eq "classinfo.pl") {
817 $perl_args = $gsdl_cgi->clean_param("classifier") || "";
818 $gsdl_cgi->delete("classifier");
819 }
820 if ($script eq "pluginfo.pl") {
821 $perl_args = $gsdl_cgi->clean_param("plugin") || "";
822 $gsdl_cgi->delete("plugin");
823 }
[19189]824 if ($script eq "downloadinfo.pl") {
825 $perl_args = $gsdl_cgi->clean_param("downloader") || "";
826 $gsdl_cgi->delete("downloader");
827 }
[11110]828
829 foreach my $cgi_arg_name ($gsdl_cgi->param) {
[13179]830 my $cgi_arg_value = $gsdl_cgi->clean_param($cgi_arg_name) || "";
[22471]831
832 # When get_script_options is to launch classinfo.pl or pluginfo.pl, one of the args to be passed to the script
833 # is the collection name. This may be a (collectgroup/)coltailname coming in here as (collectgroup|)coltailname.
834 # Since calling safe_val() below on the collection name value will get rid of \ and |, but preserves /, need to
835 # first replace the | with /, then run safe_val, then convert the / to the OS dependent File separator.
836 $cgi_arg_value =~ s@\|@\/@g if ($cgi_arg_name =~ m/^collection/);
[13179]837 $cgi_arg_value = $gsdl_cgi->safe_val($cgi_arg_value);
[22471]838 $cgi_arg_value =~ s@\/@&util::get_dirsep()@eg if($cgi_arg_name =~ m/^collection/);
[11110]839 if ($cgi_arg_value eq "") {
840 $perl_args = "-$cgi_arg_name " . $perl_args;
841 }
842 else {
843 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
844 }
845 }
846
[14236]847
848 # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
849 # directly out to the page
[16467]850 print STDOUT "Content-type:text/plain\n\n";
851 my $perl_command;
852 if($iis6_mode && $gsdl_cgi->greenstone_version() == 2)
[14236]853 {
854 $perl_command = "perl -S $script $perl_args";
[16467]855 } else {
856 $perl_command = "perl -S $script $perl_args 2>&1";
[14236]857 }
858
[11110]859 my $perl_output = `$perl_command`;
860 my $perl_status = $?;
861 if ($perl_status > 0) {
862 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
863 }
864
[14236]865 if (defined($perl_output))
866 {
867 print STDOUT $perl_output;
868 }
[11110]869}
870
[16467]871# get the names of all sites available on the server
872sub get_site_names
873{
874 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
875 my $sites_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "sites");
[11110]876
[16467]877 my @sites_dir;
878 my @site_dir;
879
880 $gsdl_cgi->checked_chdir($sites_directory);
881 opendir(DIR, $sites_directory);
882 @sites_dir= readdir(DIR);
883 my $sites_dir;
884 my $sub_dir_file;
885
886 print STDOUT "Content-type:text/plain\n\n";
887 foreach $sites_dir(@sites_dir)
888 {
[20253]889 if (!(($sites_dir eq ".") || ($sites_dir eq "..") || ($sites_dir eq "CVS") || ($sites_dir eq ".DS_Store")))
[16467]890 {
891 my $site_dir_path= &util::filename_cat($sites_directory,$sites_dir);
892 $gsdl_cgi->checked_chdir($site_dir_path);
893 opendir(DIR,$site_dir_path);
894 @site_dir=readdir(DIR);
895 closedir(DIR);
896
897 foreach $sub_dir_file(@site_dir)
898 {
899 if ($sub_dir_file eq "siteConfig.xml"){
900 print STDOUT "$sites_dir" . "-----";
901 }
902 }
903 }
904 }
905
906}
907
[11110]908sub move_collection_file
909{
[16467]910 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]911
912 my $collection = $gsdl_cgi->clean_param("c");
913 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
914 $gsdl_cgi->generate_error("No collection specified.");
915 }
[22453]916 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
917
[11110]918 my $source_file = $gsdl_cgi->clean_param("source");
919 if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
920 $gsdl_cgi->generate_error("No source file specified.");
921 }
[19233]922 $source_file = $gsdl_cgi->decode($source_file);
[11110]923 $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
924 my $target_file = $gsdl_cgi->clean_param("target");
925 if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
926 $gsdl_cgi->generate_error("No target file specified.");
927 }
[19233]928 $target_file = $gsdl_cgi->decode($target_file);
[11110]929 $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
930
931 # Make sure we don't try to move anything outside the collection
[16467]932 if ($source_file =~ m/\.\./ || $target_file =~ m/\.\./) {
[11110]933 $gsdl_cgi->generate_error("Illegal file specified.");
934 }
935
[11937]936 # Ensure the user is allowed to edit this collection
[16467]937 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]938
[16467]939 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
[11110]940 $gsdl_cgi->checked_chdir($collection_directory);
941
942 # Check that the collection source file exists
943 if (!-e $source_file) {
944 $gsdl_cgi->generate_error("Collection file $source_file does not exist.");
945 }
946
947 # Make sure the collection isn't locked by someone else
[16467]948 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]949
950 &util::mv($source_file, $target_file);
951
952 # Check that the collection source file was moved
953 if (-e $source_file || !-e $target_file) {
[16467]954 $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file."); # dies
[11110]955 }
956
957 $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
958}
959
960
961sub new_collection_directory
962{
[16467]963 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]964
965 my $collection = $gsdl_cgi->clean_param("c");
966 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
967 $gsdl_cgi->generate_error("No collection specified.");
968 }
[22453]969 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
970
[11110]971 my $directory = $gsdl_cgi->clean_param("directory");
972 if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
973 $gsdl_cgi->generate_error("No directory specified.");
974 }
975 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
976
977 # Make sure we don't try to create anything outside the collection
[16467]978 if ($directory =~ m/\.\./) {
[11110]979 $gsdl_cgi->generate_error("Illegal directory specified.");
980 }
981
[11937]982 # Ensure the user is allowed to edit this collection
[16467]983 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]984
[16467]985 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
[11110]986 $gsdl_cgi->checked_chdir($collection_directory);
987
988 # Check that the collection directory doesn't already exist
[16467]989 # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicitly
[13497]990 # try to create the import directory
[16467]991## log -r13497 for GS2's gliserver.pl, Katherine Don explains:
992# "commented out checking for existence of a directory in new_collection_directory
993# as it throws an error which we don't want"
994 #if($gsdl_cgi->greenstone_version() != 2 && -d $directory) {
995 #$gsdl_cgi->generate_error("Collection directory $directory already exists.");
996 #}
[11110]997
998 # Make sure the collection isn't locked by someone else
[16467]999 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]1000
1001 &util::mk_dir($directory);
1002
1003 # Check that the collection directory was created
1004 if (!-d $directory) {
1005 $gsdl_cgi->generate_error("Could not create collection directory $directory.");
1006 }
1007
1008 $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
1009}
1010
1011
1012sub run_script
1013{
[16467]1014 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
[11110]1015
1016 my $script = $gsdl_cgi->clean_param("script");
1017 if ((!defined $script) || ($script =~ m/^\s*$/)) {
1018 $gsdl_cgi->generate_error("No script specified.");
1019 }
1020 $gsdl_cgi->delete("script");
[16467]1021
[11110]1022 my $collection = $gsdl_cgi->clean_param("c");
1023 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
1024 $gsdl_cgi->generate_error("No collection specified.");
1025 }
[22453]1026 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
[11110]1027 $gsdl_cgi->delete("c");
1028
[16467]1029 # confuse other, so delete timestamp
1030 $gsdl_cgi->delete("ts");
1031
[11937]1032 # Ensure the user is allowed to edit this collection
[16467]1033 &authenticate_user($gsdl_cgi, $username, $collection, $site);
[11110]1034
1035 # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
[16467]1036 &lock_collection($gsdl_cgi, $username, $collection, $site) unless ($script eq "mkcol.pl");
[11110]1037
[15170]1038 # Last argument is the collection name, except for explode_metadata_database.pl and
1039 # replace_srcdoc_with_html (where there's a "file" option followed by the filename. These two preceed the collection name)
[11110]1040 my $perl_args = $collection;
[15170]1041 if ($script eq "explode_metadata_database.pl" || $script eq "replace_srcdoc_with_html.pl") {
[16467]1042 # Last argument is the file to be exploded or it is the file to be replaced with its html version
[13462]1043 my $file = $gsdl_cgi->clean_param("file");
1044 if ((!defined $file) || ($file =~ m/^\s*$/)) {
1045 $gsdl_cgi->generate_error("No file specified.");
1046 }
[18649]1047 $gsdl_cgi->delete("file");
1048 $file = $gsdl_cgi->decode($file);
[16467]1049 $file = "\"$file\""; # Windows: bookend the relative filepath with quotes in case it contains spaces
[20959]1050 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
[16467]1051 $perl_args = $file;
[13462]1052 }
1053
[11110]1054 foreach my $cgi_arg_name ($gsdl_cgi->param) {
1055 my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
1056 if ($cgi_arg_value eq "") {
1057 $perl_args = "-$cgi_arg_name " . $perl_args;
1058 }
1059 else {
1060 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
1061 }
1062 }
1063
[16467]1064 # mkcol.pl and import.pl, buildcol.pl, g2f-import.pl, g2f-buildcol.pl all need the -collectdir option passed
1065 my $import_pl = "import.pl"; # import is a reserved word, need to put it in quotes
1066
1067 if (($script =~ m/$import_pl|buildcol.pl/) || ($script eq "mkcol.pl")) { # || ($script eq "schedule.pl")
[20958]1068 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
1069 $perl_args = "-collectdir \"$collect_directory\" " . $perl_args;
[16467]1070 }
[14236]1071
[11110]1072 my $perl_command = "perl -S $script $perl_args 2>&1";
[14236]1073 # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
1074 # directly out to the page
[16467]1075 if($gsdl_cgi->greenstone_version() == 2 && $iis6_mode)
[14236]1076 {
1077 $perl_command = "perl -S $script $perl_args";
1078 }
[11110]1079 if (!open(PIN, "$perl_command |")) {
1080 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
1081 }
1082
[16467]1083 print STDOUT "Content-type:text/plain\n\n";
1084 print "$perl_command \n";
1085
[11110]1086 while (defined (my $perl_output_line = <PIN>)) {
1087 print STDOUT $perl_output_line;
1088 }
1089 close(PIN);
1090
1091 my $perl_status = $?;
1092 if ($perl_status > 0) {
1093 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
1094 }
1095 elsif ($mail_enabled) {
1096 if ($script eq "buildcol.pl") {
1097 &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
1098 }
1099 }
1100}
1101
1102sub upload_collection_file
1103{
[16467]1104 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
1105
[11110]1106 my $collection = $gsdl_cgi->clean_param("c");
1107 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
1108 $gsdl_cgi->generate_error("No collection specified.");
1109 }
[22453]1110 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
1111
[11110]1112 my $file = $gsdl_cgi->clean_param("file");
1113 if ((!defined $file) || ($file =~ m/^\s*$/)) {
1114 $gsdl_cgi->generate_error("No file specified.");
1115 }
1116 my $directory = $gsdl_cgi->clean_param("directory") || "";
1117 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
1118 my $zip = $gsdl_cgi->clean_param("zip");
1119
[19172]1120 # language and region Environment Variable setting on the client side that was used to
1121 # zip files. This needs to be consistent on both client and server sides, otherwise zip
1122 # and unzip seem to produce different values.
[19189]1123 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
[19172]1124 $gsdl_cgi->delete("lr");
1125
[11110]1126 # Make sure we don't try to upload anything outside the collection
[16467]1127 if ($file =~ m/\.\./) {
[11110]1128 $gsdl_cgi->generate_error("Illegal file specified.");
1129 }
[16467]1130 if ($directory =~ m/\.\./) {
[11110]1131 $gsdl_cgi->generate_error("Illegal directory specified.");
1132 }
1133
[11937]1134 # Ensure the user is allowed to edit this collection
[16467]1135 if($gsdl_cgi->greenstone_version() == 2) { ## Quan commented this out for GS3 in r14325
1136 &authenticate_user($gsdl_cgi, $username, $collection, $site); # site will be undefined for GS2, of course
1137 }
[11110]1138
[16467]1139 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
[11110]1140 $gsdl_cgi->checked_chdir($collection_directory);
1141
1142 # Make sure the collection isn't locked by someone else
[16467]1143 &lock_collection($gsdl_cgi, $username, $collection, $site);
[11110]1144
1145 my $directory_path = &util::filename_cat($collection_directory, $directory);
[13180]1146 if (!-d $directory_path) {
1147 &util::mk_dir($directory_path);
1148 if (!-d $directory_path) {
1149 $gsdl_cgi->generate_error("Could not create directory $directory_path.");
1150 }
[11110]1151 }
1152
[16467]1153 #my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
1154 my $file_path = "";
1155 if($gsdl_cgi->greenstone_version() == 2) {
1156 $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
1157 } else {
1158 $file_path = &util::filename_cat($directory_path, $file);
1159 }
1160
[11110]1161 if (!open(FOUT, ">$file_path")) {
[16467]1162 print STDERR "Unable to write file $file_path\n";
[11110]1163 $gsdl_cgi->generate_error("Unable to write file $file_path");
1164 }
1165
1166 # Read the uploaded data and write it out to file
1167 my $buf;
1168 my $num_bytes = 0;
1169 binmode(FOUT);
[16467]1170 if($gsdl_cgi->greenstone_version() == 2) { ##
1171 # We have to pass the size of the uploaded data in the "fs" argument because IIS 6 seems to be
1172 # completely incapable of working this out otherwise (causing the old code to crash)
1173 my $num_bytes_remaining = $gsdl_cgi->clean_param("fs");
1174 my $bytes_to_read = $num_bytes_remaining;
[14260]1175 if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
[16467]1176
1177 while (read(STDIN, $buf, $bytes_to_read) > 0) {
1178 print FOUT $buf;
1179 $num_bytes += length($buf);
1180 $num_bytes_remaining -= length($buf);
1181 $bytes_to_read = $num_bytes_remaining;
1182 if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
1183 }
1184 } else { # GS3 and later
1185 my $bread;
1186 my $fh = $gsdl_cgi->clean_param("uploaded_file");
1187
1188 if (!defined $fh) {
1189 print STDERR "ERROR. Filehandle undefined. No file uploaded onto GS3 server.\n";
1190 $gsdl_cgi->generate_error("ERROR. Filehandle undefined. No file uploaded (GS3 server).");
1191 } else {
1192 while ($bread=read($fh, $buf, 1024)) {
1193 print FOUT $buf;
1194 }
1195 }
[11110]1196 }
1197 close(FOUT);
[16467]1198
[11110]1199 # If we have downloaded a zip file, unzip it
1200 if (defined $zip) {
1201 my $java = $gsdl_cgi->get_java_path();
[16467]1202 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
[11110]1203 my $java_args = "\"$file_path\" \"$directory_path\"";
[19252]1204 $ENV{'LANG'} = $lang_env;
1205 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
[11110]1206 my $java_output = `$java_command`;
1207 my $java_status = $?;
1208
1209 # Remove the zip file once we have unzipped it, since it is an intermediate file only
[16467]1210 unlink("$file_path") unless $debugging_enabled;
1211
[11110]1212 if ($java_status > 0) {
[16467]1213 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home()); # dies
[11110]1214 }
1215 }
1216
1217 $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
1218}
1219
1220sub put_file
1221{
1222 my $gsdl_cgi = shift(@_);
1223 my $file_path = shift(@_);
1224 my $content_type = shift(@_);
1225
[16467]1226 if(!defined $content_type) { ##
1227 $content_type = "application/zip";
1228 }
1229
[11110]1230 if (open(PIN, "<$file_path")) {
[16467]1231 print STDOUT "Content-type:$content_type\n\n"; ## For GS3: "Content-type:application/zip\n\n";
[11110]1232 my $buf;
1233 my $num_bytes = 0;
1234 binmode(PIN);
1235 while (read(PIN, $buf, 1024) > 0) {
1236 print STDOUT $buf;
1237 $num_bytes += length($buf);
1238 }
1239
1240 close(PIN);
1241 }
1242 else {
1243 $gsdl_cgi->generate_error("Unable to read file $file_path\n $!");
1244 }
1245}
1246
1247sub send_mail
1248{
1249 my $gsdl_cgi = shift(@_);
1250 my $mail_subject = shift(@_);
1251 my $mail_content = shift(@_);
1252
1253 my $sendmail_command = "perl -S sendmail.pl";
1254 $sendmail_command .= " -to \"" . $mail_to_address . "\"";
1255 $sendmail_command .= " -from \"" . $mail_from_address . "\"";
1256 $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
1257 $sendmail_command .= " -subject \"" . $mail_subject . "\"";
1258
1259 if (!open(POUT, "| $sendmail_command")) {
1260 $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
1261 }
1262 print POUT $mail_content . "\n";
1263 close(POUT);
1264}
1265
[16467]1266sub greenstone_server_version
1267{
1268 my $gsdl_cgi = shift(@_);
1269 my $version = $gsdl_cgi->greenstone_version();
1270 $gsdl_cgi->generate_ok_message("Greenstone server version is: $version\n");
1271}
[11110]1272
[16467]1273sub get_library_url_suffix
1274{
1275 my $gsdl_cgi = shift(@_);
1276 my $library_url = $gsdl_cgi->library_url_suffix();
1277 $gsdl_cgi->generate_ok_message("Greenstone library URL suffix is: $library_url\n");
1278}
1279
[11110]1280&main();
Note: See TracBrowser for help on using the repository browser.