source: main/trunk/greenstone2/common-src/cgi-bin/gliserver.pl

Last change on this file was 37687, checked in by anupama, 12 months ago

Removing the rename folder utility function from gliserver.pl, as fldv for incremental building is not a client-gli/remoteGS3 feature.

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