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

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