source: greenstone3/trunk/web/WEB-INF/cgi/gliserver4gs3.pl@ 16512

Last change on this file since 16512 was 16479, checked in by ak19, 14 years ago

Putting these back on svn since GathererApplet4gs3.java of GLI makes use of them

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