source: greenstone3/trunk/web/WEB-INF/cgi/gliserver.pl@ 16427

Last change on this file since 16427 was 16427, checked in by ak19, 16 years ago

Reinstated the commenting out of a check for pre-existing directory. The lines were commented out in the gliserver.pl for GS2, but not that of GS3 and only cropped up once when running GS3 remote on Windowse so far

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