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

Last change on this file since 24873 was 24872, checked in by ak19, 12 years ago

First set of changes to do with migration of cgi-bin into common-src, so that upon make install, common-src\cgi-bin will be installed in cgi-bin\GSDLOS(GSDLARCH). After this commit (to do with the contents of the files), cgi-bin itself will be moved.

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