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

Last change on this file since 24292 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
Line 
1#!/usr/bin/perl -w
2# Need to specify the full path of Perl above
3
4# This file merges Michael Dewsnip's gliserver.pl for GS2 and Quan Qiu's gliserver4gs3.pl (GS3)
5
6use strict;
7no strict 'subs';
8no strict 'refs'; # allow filehandles to be variables and viceversa
9
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
19# Set this to 1 to work around IIS 6 craziness
20my $iis6_mode = 0;
21
22##
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{
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 }
33}
34
35
36# We use require and an eval here (instead of "use package") to catch any errors loading the module (for IIS)
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
46#my $authentication_enabled = 0;
47my $debugging_enabled = 0; # if 1, debugging is enabled and unlinking intermediate files (deleting files) will not happen
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
55{
56 my $gsdl_cgi = new gsdlCGI();
57
58 # Load the Greenstone modules that we need to use
59 $gsdl_cgi->setup_gsdl();
60 my $gsdlhome = $ENV{'GSDLHOME'};
61
62 $gsdl_cgi->checked_chdir($gsdlhome);
63
64 # Encrypt the password
65 $gsdl_cgi->encrypt_password();
66
67 $gsdl_cgi->parse_cgi_args();
68
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
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
79 # The check-installation, greenstone-server-version and get-library-url commands have no arguments
80 if ($cmd eq "check-installation") {
81 &check_installation($gsdl_cgi);
82 return;
83 }
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 }
92
93 # All other commands require a username, for locking and authentication
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
100 # Get then remove the ts (timestamp) argument (since this can mess up other scripts)
101 my $timestamp = $gsdl_cgi->clean_param("ts");
102 if ((!defined $timestamp) || ($timestamp =~ m/^\s*$/)) {
103 $timestamp = time(); # Fall back to using the Perl time() function to generate a timestamp
104 }
105 $gsdl_cgi->delete("ts");
106
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
117 if ($cmd eq "delete-collection") {
118 &delete_collection($gsdl_cgi, $username, $timestamp, $site);
119 }
120 elsif ($cmd eq "download-collection") {
121 &download_collection($gsdl_cgi, $username, $timestamp, $site);
122 }
123 elsif ($cmd eq "download-collection-archives") {
124 &download_collection_archives($gsdl_cgi, $username, $timestamp, $site);
125 }
126 elsif ($cmd eq "download-collection-configurations") {
127 &download_collection_configurations($gsdl_cgi, $username, $timestamp, $site);
128 }
129 elsif ($cmd eq "download-collection-file") {
130 &download_collection_file($gsdl_cgi, $username, $timestamp, $site);
131 }
132 elsif ($cmd eq "delete-collection-file") {
133 &delete_collection_file($gsdl_cgi, $username, $timestamp, $site);
134 }
135 elsif ($cmd eq "get-script-options") {
136 &get_script_options($gsdl_cgi, $username, $timestamp, $site);
137 }
138 elsif ($cmd eq "move-collection-file") {
139 &move_collection_file($gsdl_cgi, $username, $timestamp, $site);
140 }
141 elsif ($cmd eq "new-collection-directory") {
142 &new_collection_directory($gsdl_cgi, $username, $timestamp, $site);
143 }
144 elsif ($cmd eq "run-script") {
145 &run_script($gsdl_cgi, $username, $timestamp, $site);
146 }
147 elsif ($cmd eq "timeout-test") {
148 while (1) { }
149 }
150 elsif ($cmd eq "upload-collection-file") {
151 &upload_collection_file($gsdl_cgi, $username, $timestamp, $site);
152 }
153 elsif ($cmd eq "file-exists") {
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 }
168 else {
169 $gsdl_cgi->generate_error("Unrecognised command: '$cmd'");
170 }
171
172}
173
174
175sub authenticate_user
176{
177 my $gsdl_cgi = shift(@_);
178 my $username = shift(@_);
179 my $collection = shift(@_);
180 my $site = shift(@_);
181
182 # Even if we're not authenticating remove the un and pw arguments, since these can mess up other scripts
183 my $user_password = $gsdl_cgi->clean_param("pw");
184 $gsdl_cgi->delete("pw");
185
186 # Only authenticate if it is enabled
187 # return "all-collections-editor" if (!$authentication_enabled);
188
189 if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) {
190 $gsdl_cgi->generate_error("Authentication failed: no password specified.");
191 }
192
193 my $users_db_content;
194 if($gsdl_cgi->greenstone_version() == 2) {
195 my $etc_directory = &util::filename_cat($ENV{'GSDLHOME'}, "etc");
196 my $users_db_file_path = &util::filename_cat($etc_directory, "users.gdb");
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 }
204 close(USERS_DB);
205 }
206 elsif($gsdl_cgi->greenstone_version() == 3) {
207 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
208
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`;
223 }
224
225 # Get the user account information from the usersDB database
226 my %users_db_data = ();
227 foreach my $users_db_entry (split(/-{70}/, $users_db_content)) {
228 if ($users_db_entry =~ m/\n?\[(.+)\]\n/) {
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
240 my ($valid_user_password) = ($user_data =~ m/\<password\>(.*)/);
241 if ($user_password ne $valid_user_password) {
242 $gsdl_cgi->generate_error("Authentication failed: incorrect password.");
243 }
244
245 # Check group
246 my ($user_groups) = ($user_data =~ m/\<groups\>(.*)/);
247
248 if ($collection eq "") {
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
251 }
252
253 foreach my $user_group (split(/\,/, $user_groups)) {
254 # Does this user have access to all collections?
255 if ($user_group eq "all-collections-editor") {
256 return $user_groups; # Authentication successful
257 }
258 # Does this user have access to personal collections, and is this one?
259 if ($user_group eq "personal-collections-editor" && $collection =~ m/^$username\-/) {
260 return $user_groups; # Authentication successful
261 }
262 # Does this user have access to this collection
263 if ($user_group eq "$collection-collection-editor") {
264 return $user_groups; # Authentication successful
265 }
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(@_);
274 my $username = shift(@_);
275 my $collection = shift(@_);
276 my $site = shift(@_);
277
278 my $steal_lock = $gsdl_cgi->clean_param("steal_lock");
279 $gsdl_cgi->delete("steal_lock");
280
281 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
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
288 my $lock_file_content = "";
289 open(LOCK_FILE, "<$lock_file_name");
290 while (<LOCK_FILE>) {
291 $lock_file_content .= $_;
292 }
293 close(LOCK_FILE);
294
295 # Pick out the owner of the lock file
296 $lock_file_content =~ m/\<User\>(.*?)\<\/User\>/;
297 my $lock_file_owner = $1;
298
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
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
314 open(LOCK_FILE, ">$lock_file_name");
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";
321 close(LOCK_FILE);
322}
323
324
325# ----------------------------------------------------------------------------------------------------
326# ACTIONS
327# ----------------------------------------------------------------------------------------------------
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}
347
348sub check_installation
349{
350 my ($gsdl_cgi) = @_;
351
352 my $installation_ok = 1;
353 my $installation_status = "";
354
355 # Check that Java is installed and accessible
356 my $java = $gsdl_cgi->get_java_path();
357 my $java_command = "\"$java\" -version 2>&1";
358
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
361 if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) { ##
362 print STDOUT "Content-type:text/plain\n\n";
363 $java_command = "\"$java\" -version";
364 }
365
366 my $java_output = `$java_command`;
367
368 my $java_status = $?;
369 if ($java_status < 0) {
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;
373 }
374 else {
375 $installation_status = "Java found: $java_output";
376 }
377
378 # Show the values of some important environment variables
379 $installation_status .= "\n";
380 if($gsdl_cgi->greenstone_version() != 2) {
381 $installation_status .= "GSDL3SRCHOME: " . $ENV{'GSDL3SRCHOME'} . "\n";
382 $installation_status .= "GSDL3HOME: " . $ENV{'GSDL3HOME'} . "\n";
383 }
384 $installation_status .= "GSDLHOME: " . $ENV{'GSDLHOME'} . "\n";
385 $installation_status .= "GSDLOS: " . $ENV{'GSDLOS'} . "\n";
386 $installation_status .= "JAVA_HOME: " . $ENV{'JAVA_HOME'} . "\n" if defined($ENV{'JAVA_HOME'}); # on GS2, Java's only on the PATH
387 $installation_status .= "PATH: " . $ENV{'PATH'} . "\n";
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'};
391 }
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 }
400 else {
401 if($iis6_mode && $gsdl_cgi->greenstone_version() == 2) {
402 print STDOUT $installation_status;
403 } else {
404 $gsdl_cgi->generate_error($installation_status);
405 }
406 }
407}
408
409
410sub delete_collection
411{
412 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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 }
418 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
419
420 # Ensure the user is allowed to edit this collection
421 &authenticate_user($gsdl_cgi, $username, $collection, $site);
422
423
424 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
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
433 &lock_collection($gsdl_cgi, $username, $collection, $site);
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{
449 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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 }
455 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
456
457 my $file = $gsdl_cgi->clean_param("file");
458 if ((!defined $file) || ($file =~ m/^\s*$/)) {
459 $gsdl_cgi->generate_error("No file specified.");
460 }
461 $file = $gsdl_cgi->decode($file);
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
465 if ($file =~ m/\.\./) {
466 $gsdl_cgi->generate_error("Illegal file specified.");
467 }
468
469 # Ensure the user is allowed to edit this collection
470 &authenticate_user($gsdl_cgi, $username, $collection, $site);
471
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
478 $gsdl_cgi->checked_chdir($collection_directory);
479
480 # Make sure the collection isn't locked by someone else
481 &lock_collection($gsdl_cgi, $username, $collection, $site);
482
483 # Check that the collection file exists
484 if (!-e $file) { ## original didn't have 'die', but it was an ok message
485 $gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
486 die;
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{
501 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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 }
507 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
508
509 # language and region Environment Variable setting on the client side that was used to zip files.
510 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
511 $gsdl_cgi->delete("lr");
512
513 # Ensure the user is allowed to edit this collection
514 &authenticate_user($gsdl_cgi, $username, $collection, $site);
515
516 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
517 $gsdl_cgi->checked_chdir($collect_directory);
518
519 # Check that the collection exists
520 if (!-d $collection) {
521 $gsdl_cgi->generate_ok_message("Collection $collection does not exist."); ## original had an error msg (from where it would die)
522 die;
523 }
524
525 # Make sure the collection isn't locked by someone else
526 &lock_collection($gsdl_cgi, $username, $collection, $site);
527
528 # Zip up the collection
529 my $java = $gsdl_cgi->get_java_path();
530 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
531 my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-" . $timestamp . ".zip");
532 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
533 if($gsdl_cgi->greenstone_version() != 2) {
534 $java_args .= " gsdl3"; ## must this be done elsewhere as well?
535 }
536
537 $ENV{'LANG'} = $lang_env;
538 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args";
539
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
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
553}
554
555
556sub download_collection_archives
557{
558 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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 }
564 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
565
566 # language and region Environment Variable setting on the client side that was used to zip files.
567 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
568 $gsdl_cgi->delete("lr");
569
570 # Ensure the user is allowed to edit this collection
571 &authenticate_user($gsdl_cgi, $username, $collection, $site);
572
573 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
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
582 &lock_collection($gsdl_cgi, $username, $collection, $site);
583
584 # Zip up the collection archives
585 my $java = $gsdl_cgi->get_java_path();
586 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
587 my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-archives-" . $timestamp . ".zip");
588 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
589 $ENV{'LANG'} = $lang_env;
590 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args";
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{
611 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
612
613 # language and region Environment Variable setting on the client side that was used to zip files.
614 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
615 $gsdl_cgi->delete("lr");
616
617 # Users can be in any group to perform this action
618 my $user_groups = &authenticate_user($gsdl_cgi, $username, "", $site);
619
620 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
621 $gsdl_cgi->checked_chdir($collect_directory);
622
623 # Zip up the collection configurations
624 my $java = $gsdl_cgi->get_java_path();
625 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
626 my $zip_file_path = &util::filename_cat($collect_directory, "collection-configurations-" . $timestamp . ".zip");
627 my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$username\" \"$user_groups\"";
628 $ENV{'LANG'} = $lang_env;
629 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args";
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 }
640
641 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
642 unlink("$zip_file_path") unless $debugging_enabled;
643}
644
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{
650 my ($gsdl_cgi, $site) = @_;
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 }
656 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
657
658 my $file = $gsdl_cgi->clean_param("file");
659 if ((!defined $file) || ($file =~ m/^\s*$/)) {
660 $gsdl_cgi->generate_error("No file specified.");
661 }
662 $file = "\"$file\""; # Windows: bookend the relative filepath with quotes in case it contains spaces
663 $file = $gsdl_cgi->decode($file);
664 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
665
666 # Not necessary: checking whether the user is authenticated to query existence of the file
667 #&authenticate_user($gsdl_cgi, $username, $collection);
668
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
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
680sub download_collection_file
681{
682 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
683
684 my $collection = $gsdl_cgi->clean_param("c");
685 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
686 $gsdl_cgi->generate_error("No collection specified.");
687 }
688 my $collection_tail_name = $collection;
689 $collection_tail_name =~ s/^(.*\|)//;
690 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
691
692 # language and region Environment Variable setting on the client side that was used to zip files.
693 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
694 $gsdl_cgi->delete("lr");
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
702 if ($file =~ m/\.\./) {
703 $gsdl_cgi->generate_error("Illegal file specified.");
704 }
705
706 # Ensure the user is allowed to edit this collection
707 &authenticate_user($gsdl_cgi, $username, $collection, $site);
708
709 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
710 $gsdl_cgi->checked_chdir($collection_directory);
711
712 # Check that the collection file exists
713 if (!-e $file) {
714 $gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
715 die;
716 }
717
718 # Make sure the collection isn't locked by someone else
719 &lock_collection($gsdl_cgi, $username, $collection, $site);
720
721 # Zip up the collection file
722 my $java = $gsdl_cgi->get_java_path();
723 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
724 my $zip_file_path = &util::filename_cat($collection_directory, $collection_tail_name . "-file-" . $timestamp . ".zip");
725 my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
726 $ENV{'LANG'} = $lang_env;
727 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
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
744# download web.xml from the server
745sub download_web_xml_file
746{
747 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
748
749 # Users can be in any group to perform this action
750 my $user_groups = &authenticate_user($gsdl_cgi, $username, "", $site);
751
752 # language and region Environment Variable setting on the client side that was used to zip files.
753 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
754 $gsdl_cgi->delete("lr");
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\"";
779 $ENV{'LANG'} = $lang_env;
780 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
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
798# Collection locking unnecessary because this action isn't related to a particular collection
799sub get_script_options
800{
801 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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
809 # Users can be in any group to perform this action
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
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 }
824 if ($script eq "downloadinfo.pl") {
825 $perl_args = $gsdl_cgi->clean_param("downloader") || "";
826 $gsdl_cgi->delete("downloader");
827 }
828
829 foreach my $cgi_arg_name ($gsdl_cgi->param) {
830 my $cgi_arg_value = $gsdl_cgi->clean_param($cgi_arg_name) || "";
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/);
837 $cgi_arg_value = $gsdl_cgi->safe_val($cgi_arg_value);
838 $cgi_arg_value =~ s@\/@&util::get_dirsep()@eg if($cgi_arg_name =~ m/^collection/);
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
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
850 print STDOUT "Content-type:text/plain\n\n";
851 my $perl_command;
852 if($iis6_mode && $gsdl_cgi->greenstone_version() == 2)
853 {
854 $perl_command = "perl -S $script $perl_args";
855 } else {
856 $perl_command = "perl -S $script $perl_args 2>&1";
857 }
858
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
865 if (defined($perl_output))
866 {
867 print STDOUT $perl_output;
868 }
869}
870
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");
876
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 {
889 if (!(($sites_dir eq ".") || ($sites_dir eq "..") || ($sites_dir eq "CVS") || ($sites_dir eq ".DS_Store")))
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
908sub move_collection_file
909{
910 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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 }
916 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
917
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 }
922 $source_file = $gsdl_cgi->decode($source_file);
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 }
928 $target_file = $gsdl_cgi->decode($target_file);
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
932 if ($source_file =~ m/\.\./ || $target_file =~ m/\.\./) {
933 $gsdl_cgi->generate_error("Illegal file specified.");
934 }
935
936 # Ensure the user is allowed to edit this collection
937 &authenticate_user($gsdl_cgi, $username, $collection, $site);
938
939 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
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
948 &lock_collection($gsdl_cgi, $username, $collection, $site);
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) {
954 $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file."); # dies
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{
963 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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 }
969 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
970
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
978 if ($directory =~ m/\.\./) {
979 $gsdl_cgi->generate_error("Illegal directory specified.");
980 }
981
982 # Ensure the user is allowed to edit this collection
983 &authenticate_user($gsdl_cgi, $username, $collection, $site);
984
985 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
986 $gsdl_cgi->checked_chdir($collection_directory);
987
988 # Check that the collection directory doesn't already exist
989 # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicitly
990 # try to create the import directory
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 #}
997
998 # Make sure the collection isn't locked by someone else
999 &lock_collection($gsdl_cgi, $username, $collection, $site);
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{
1014 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
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");
1021
1022 my $collection = $gsdl_cgi->clean_param("c");
1023 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
1024 $gsdl_cgi->generate_error("No collection specified.");
1025 }
1026 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
1027 $gsdl_cgi->delete("c");
1028
1029 # confuse other, so delete timestamp
1030 $gsdl_cgi->delete("ts");
1031
1032 # Ensure the user is allowed to edit this collection
1033 &authenticate_user($gsdl_cgi, $username, $collection, $site);
1034
1035 # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
1036 &lock_collection($gsdl_cgi, $username, $collection, $site) unless ($script eq "mkcol.pl");
1037
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)
1040 my $perl_args = $collection;
1041 if ($script eq "explode_metadata_database.pl" || $script eq "replace_srcdoc_with_html.pl") {
1042 # Last argument is the file to be exploded or it is the file to be replaced with its html version
1043 my $file = $gsdl_cgi->clean_param("file");
1044 if ((!defined $file) || ($file =~ m/^\s*$/)) {
1045 $gsdl_cgi->generate_error("No file specified.");
1046 }
1047 $gsdl_cgi->delete("file");
1048 $file = $gsdl_cgi->decode($file);
1049 $file = "\"$file\""; # Windows: bookend the relative filepath with quotes in case it contains spaces
1050 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
1051 $perl_args = $file;
1052 }
1053
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
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")
1068 my $collect_directory = $gsdl_cgi->get_collection_dir($site);
1069 $perl_args = "-collectdir \"$collect_directory\" " . $perl_args;
1070 }
1071
1072 my $perl_command = "perl -S $script $perl_args 2>&1";
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
1075 if($gsdl_cgi->greenstone_version() == 2 && $iis6_mode)
1076 {
1077 $perl_command = "perl -S $script $perl_args";
1078 }
1079 if (!open(PIN, "$perl_command |")) {
1080 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
1081 }
1082
1083 print STDOUT "Content-type:text/plain\n\n";
1084 print "$perl_command \n";
1085
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{
1104 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
1105
1106 my $collection = $gsdl_cgi->clean_param("c");
1107 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
1108 $gsdl_cgi->generate_error("No collection specified.");
1109 }
1110 $collection =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
1111
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
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.
1123 my $lang_env = $gsdl_cgi->clean_param("lr") || "";
1124 $gsdl_cgi->delete("lr");
1125
1126 # Make sure we don't try to upload anything outside the collection
1127 if ($file =~ m/\.\./) {
1128 $gsdl_cgi->generate_error("Illegal file specified.");
1129 }
1130 if ($directory =~ m/\.\./) {
1131 $gsdl_cgi->generate_error("Illegal directory specified.");
1132 }
1133
1134 # Ensure the user is allowed to edit this collection
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 }
1138
1139 my $collection_directory = $gsdl_cgi->get_collection_dir($site, $collection);
1140 $gsdl_cgi->checked_chdir($collection_directory);
1141
1142 # Make sure the collection isn't locked by someone else
1143 &lock_collection($gsdl_cgi, $username, $collection, $site);
1144
1145 my $directory_path = &util::filename_cat($collection_directory, $directory);
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 }
1151 }
1152
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
1161 if (!open(FOUT, ">$file_path")) {
1162 print STDERR "Unable to write file $file_path\n";
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);
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;
1175 if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
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 }
1196 }
1197 close(FOUT);
1198
1199 # If we have downloaded a zip file, unzip it
1200 if (defined $zip) {
1201 my $java = $gsdl_cgi->get_java_path();
1202 my $java_classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "GLIServer.jar");
1203 my $java_args = "\"$file_path\" \"$directory_path\"";
1204 $ENV{'LANG'} = $lang_env;
1205 my $java_command = "\"$java\" -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
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
1210 unlink("$file_path") unless $debugging_enabled;
1211
1212 if ($java_status > 0) {
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
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
1226 if(!defined $content_type) { ##
1227 $content_type = "application/zip";
1228 }
1229
1230 if (open(PIN, "<$file_path")) {
1231 print STDOUT "Content-type:$content_type\n\n"; ## For GS3: "Content-type:application/zip\n\n";
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
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}
1272
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
1280&main();
Note: See TracBrowser for help on using the repository browser.