source: greenstone3/branches/customizingGreenstone3/web/WEB-INF/cgi/gliserver4gs3.pl@ 15787

Last change on this file since 15787 was 15787, checked in by oranfry, 16 years ago

updating from trunk: brought in trunk changes from r15191 to r15785

  • Property svn:executable set to *
File size: 38.1 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 = 1;
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);
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", $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, $username, $timestamp) = @_;
553
554 my ($gsdl_cgi) = @_;
555
556 my $collection = $gsdl_cgi->clean_param("c");
557 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
558 $gsdl_cgi->generate_error("No collection specified.");
559 }
560 my $file = $gsdl_cgi->clean_param("file");
561 if ((!defined $file) || ($file =~ m/^\s*$/)) {
562 $gsdl_cgi->generate_error("No file specified.");
563 }
564 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
565
566 # Not necessary: checking whether the user is authenticated to query existance of the file
567 #&authenticate_user($gsdl_cgi, $username, $collection);
568
569 my $gsdlhome = $ENV{'GSDLHOME'};
570 my $collection_directory = &util::filename_cat($gsdlhome, "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
581sub download_collection_file
582{
583 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
584
585 my $collection = $gsdl_cgi->clean_param("c");
586 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
587 $gsdl_cgi->generate_error("No collection specified.");
588 }
589 my $file = $gsdl_cgi->clean_param("file");
590 if ((!defined $file) || ($file =~ m/^\s*$/)) {
591 $gsdl_cgi->generate_error("No file specified.");
592 }
593 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
594
595 # Make sure we don't try to download anything outside the collection
596 if ($file =~ /\.\./) {
597 $gsdl_cgi->generate_error("Illegal file specified.");
598 }
599
600 # Ensure the user is allowed to edit this collection
601 &authenticate_user($gsdl_cgi, $username, $collection, $site);
602
603 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
604 my $gsdlhome = $ENV{'GSDLHOME'};
605 my $collection_directory = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "collect", $collection);
606 $gsdl_cgi->checked_chdir($collection_directory);
607
608 # Check that the collection file exists
609 if (!-e $file) {
610 $gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
611 die;
612 }
613
614 # Make sure the collection isn't locked by someone else
615 &lock_collection($gsdl_cgi, $username, $collection, $site);
616
617 # Zip up the collection file
618 my $java = $gsdl_cgi->get_java_path();
619 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
620 my $zip_file_path = &util::filename_cat($collection_directory, $collection . "-file-" . $timestamp . ".zip");
621 my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
622 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
623
624 my $java_output = `$java_command`;
625 my $java_status = $?;
626 if ($java_status > 0) {
627 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
628 }
629
630 # Check that the zip file was created successfully
631 if (!-e $zip_file_path || -z $zip_file_path) {
632 $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
633 }
634
635 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
636 unlink("$zip_file_path") unless $debugging_enabled;
637}
638
639# download web.xml from the server
640sub download_web_xml_file
641{
642 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
643
644 # Users can be in any group to perform this action
645 my $user_groups = &authenticate_user($gsdl_cgi, $username, "", $site);
646
647 my $file = $gsdl_cgi->clean_param("file");
648 if ((!defined $file) || ($file =~ m/^\s*$/)) {
649 $gsdl_cgi->generate_error("No file specified.");
650 }
651 $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
652
653 # Make sure we don't try to download anything else
654 if ($file =~ /\.\./) {
655 $gsdl_cgi->generate_error("Illegal file specified.");
656 }
657
658 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
659 my $gsdlhome = $ENV{'GSDLHOME'};
660 my $web_inf_directory = &util::filename_cat($gsdl3srchome, "web", "WEB-INF");
661 $gsdl_cgi->checked_chdir($web_inf_directory);
662
663 # Check that the collection file exists
664 if (!-e $file) {
665 $gsdl_cgi->generate_error("file $file does not exist.");
666 }
667
668 # Zip up the collection file
669 my $java = $gsdl_cgi->get_java_path();
670 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
671 my $zip_file_path = &util::filename_cat($web_inf_directory, "webxml" . $timestamp . ".zip");
672 my $java_args = "\"$zip_file_path\" \"$web_inf_directory\" \"$file\"";
673 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
674 my $java_output = `$java_command`;
675
676 my $java_status = $?;
677 if ($java_status > 0) {
678 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
679 }
680
681 # Check that the zip file was created successfully
682 if (!-e $zip_file_path || -z $zip_file_path) {
683 $gsdl_cgi->generate_error("web.xml zip file $zip_file_path could not be created.");
684 }
685
686 &put_file($gsdl_cgi, $zip_file_path, "application/zip");
687 unlink("$zip_file_path") unless $debugging_enabled;
688}
689
690# Collection locking unnecessary because this action isn't related to a particular collection
691sub get_script_options
692{
693 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
694
695 my $script = $gsdl_cgi->clean_param("script");
696 if ((!defined $script) || ($script =~ m/^\s*$/)) {
697 $gsdl_cgi->generate_error("No script specified.");
698 }
699 $gsdl_cgi->delete("script");
700
701 # Users can be in any group to perform this action
702 &authenticate_user($gsdl_cgi, $username, "", $site);
703 $gsdl_cgi->delete("ts");
704 $gsdl_cgi->delete("pw");
705
706 my $perl_args = "";
707 if ($script eq "classinfo.pl") {
708 $perl_args = $gsdl_cgi->clean_param("classifier") || "";
709 $gsdl_cgi->delete("classifier");
710 }
711 if ($script eq "pluginfo.pl") {
712 $perl_args = $gsdl_cgi->clean_param("plugin") || "";
713 $gsdl_cgi->delete("plugin");
714 }
715
716 foreach my $cgi_arg_name ($gsdl_cgi->param) {
717 my $cgi_arg_value = $gsdl_cgi->clean_param($cgi_arg_name) || "";
718 $cgi_arg_value = $gsdl_cgi->safe_val($cgi_arg_value);
719 if ($cgi_arg_value eq "") {
720 $perl_args = "-$cgi_arg_name " . $perl_args;
721 }
722 else {
723 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
724 }
725 }
726
727 my $perl_command = "perl -S $script $perl_args 2>&1";
728 my $perl_output = `$perl_command`;
729 my $perl_status = $?;
730 if ($perl_status > 0) {
731 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
732 }
733
734 print STDOUT "Content-type:text/plain\n\n";
735 print STDOUT $perl_output;
736}
737
738# get the names of all sites available on the server
739sub get_site_names
740{
741 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
742 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
743 my $gsdlhome = $ENV{'GSDLHOME'};
744 my $sites_directory = &util::filename_cat($gsdl3srchome, "web", "sites");
745
746 my @sites_dir;
747 my @site_dir;
748
749 $gsdl_cgi->checked_chdir($sites_directory);
750 opendir(DIR, $sites_directory);
751 @sites_dir= readdir(DIR);
752 my $sites_dir;
753 my $sub_dir_file;
754
755 print STDOUT "Content-type:text/plain\n\n";
756 foreach $sites_dir(@sites_dir)
757 {
758 if (!(($sites_dir eq ".") || ($sites_dir eq "..") || ($sites_dir eq "CVS")))
759 {
760 my $site_dir_path= &util::filename_cat($sites_directory,$sites_dir);
761 $gsdl_cgi->checked_chdir($site_dir_path);
762 opendir(DIR,$site_dir_path);
763 @site_dir=readdir(DIR);
764 closedir(DIR);
765
766 foreach $sub_dir_file(@site_dir)
767 {
768 if ($sub_dir_file eq "siteConfig.xml"){
769 print STDOUT "$sites_dir" . "-----";
770 }
771 }
772 }
773 }
774
775}
776
777sub move_collection_file
778{
779 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
780
781 my $collection = $gsdl_cgi->clean_param("c");
782 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
783 $gsdl_cgi->generate_error("No collection specified.");
784 }
785 my $source_file = $gsdl_cgi->clean_param("source");
786 if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
787 $gsdl_cgi->generate_error("No source file specified.");
788 }
789 $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
790 my $target_file = $gsdl_cgi->clean_param("target");
791 if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
792 $gsdl_cgi->generate_error("No target file specified.");
793 }
794 $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
795
796 # Make sure we don't try to move anything outside the collection
797 if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
798 $gsdl_cgi->generate_error("Illegal file specified.");
799 }
800
801 # Ensure the user is allowed to edit this collection
802 &authenticate_user($gsdl_cgi, $username, $collection, $site);
803
804 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
805 my $gsdlhome = $ENV{'GSDLHOME'};
806 my $collection_directory = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "collect", $collection);
807 $gsdl_cgi->checked_chdir($collection_directory);
808
809 # Check that the collection source file exists
810 if (!-e $source_file) {
811 $gsdl_cgi->generate_error("Collection file $source_file does not exist.");
812 }
813
814 # Make sure the collection isn't locked by someone else
815 &lock_collection($gsdl_cgi, $username, $collection, $site);
816
817 &util::mv($source_file, $target_file);
818
819 # Check that the collection source file was moved
820 if (-e $source_file || !-e $target_file) {
821 $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
822 }
823
824 $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
825}
826
827
828sub new_collection_directory
829{
830 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
831
832 my $collection = $gsdl_cgi->clean_param("c");
833 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
834 $gsdl_cgi->generate_error("No collection specified.");
835 }
836 my $directory = $gsdl_cgi->clean_param("directory");
837 if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
838 $gsdl_cgi->generate_error("No directory specified.");
839 }
840 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
841
842 # Make sure we don't try to create anything outside the collection
843 if ($directory =~ /\.\./) {
844 $gsdl_cgi->generate_error("Illegal directory specified.");
845 }
846
847 # Ensure the user is allowed to edit this collection
848 &authenticate_user($gsdl_cgi, $username, $collection, $site);
849
850 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
851 my $gsdlhome = $ENV{'GSDLHOME'};
852 my $collection_directory = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "collect", $collection);
853 $gsdl_cgi->checked_chdir($collection_directory);
854
855 # Check that the collection directory doesn't already exist
856 # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicity
857 # try to create the import directory
858 if (-d $directory) {
859 $gsdl_cgi->generate_error("Collection directory $directory already exists.");
860 }
861
862 # Make sure the collection isn't locked by someone else
863 &lock_collection($gsdl_cgi, $username, $collection, $site);
864
865 &util::mk_dir($directory);
866
867 # Check that the collection directory was created
868 if (!-d $directory) {
869 $gsdl_cgi->generate_error("Could not create collection directory $directory.");
870 }
871
872 $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
873}
874
875
876sub run_script
877{
878 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
879
880 my $script = $gsdl_cgi->clean_param("script");
881 if ((!defined $script) || ($script =~ m/^\s*$/)) {
882 $gsdl_cgi->generate_error("No script specified.");
883 }
884 $gsdl_cgi->delete("script");
885 my $collection = $gsdl_cgi->clean_param("c");
886 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
887 $gsdl_cgi->generate_error("No collection specified.");
888 }
889 $gsdl_cgi->delete("c");
890
891 # confuse other, so delete timestamp
892 $gsdl_cgi->delete("ts");
893
894 # Ensure the user is allowed to edit this collection
895 &authenticate_user($gsdl_cgi, $username, $collection, $site);
896
897 # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
898 &lock_collection($gsdl_cgi, $username, $collection, $site) unless ($script eq "mkcol.pl");
899
900 # Last argument is the collection name, except for explode_metadata_database.pl and
901 # replace_srcdoc_with_html (where there's a "file" option followed by the filename. These two preceed the collection name)
902 my $perl_args = $collection;
903 if ($script eq "explode_metadata_database.pl" || $script eq "replace_srcdoc_with_html.pl") {
904 # Last argument is the file to be exploded or it is the file to be replaced with its html version
905 my $file = $gsdl_cgi->clean_param("file");
906 if ((!defined $file) || ($file =~ m/^\s*$/)) {
907 $gsdl_cgi->generate_error("No file specified.");
908 }
909 $gsdl_cgi->delete("file");
910 $file =~ s/ /\\ /g; # escape all spaces in filename with a backslash, i.e. "\ ".
911 $perl_args = $file;
912 }
913
914 foreach my $cgi_arg_name ($gsdl_cgi->param) {
915 my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
916 if ($cgi_arg_value eq "") {
917 $perl_args = "-$cgi_arg_name " . $perl_args;
918 }
919 else {
920 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
921 }
922 }
923
924 if (($script eq "import.pl") || ($script eq "buildcol.pl")){
925 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
926 my $collect_directory = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "collect");
927 $perl_args = $perl_args . " -collectdir " . $collect_directory;
928 }
929
930 if ($script eq "mkcol.pl"){
931 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
932 my $collect_directory = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "collect");
933 $perl_args = $perl_args . " -collectdir " . $collect_directory;
934 }
935
936 my $perl_command = "perl -S $script $perl_args 2>&1";
937 if (!open(PIN, "$perl_command |")) {
938 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
939 }
940
941 print STDOUT "Content-type:text/plain\n\n";
942 print "$perl_command \n";
943
944 while (defined (my $perl_output_line = <PIN>)) {
945 print STDOUT $perl_output_line;
946 }
947 close(PIN);
948
949 my $perl_status = $?;
950 if ($perl_status > 0) {
951 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
952 }
953 elsif ($mail_enabled) {
954 if ($script eq "buildcol.pl") {
955 &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
956 }
957 }
958}
959
960sub upload_collection_file
961{
962 my ($gsdl_cgi, $username, $timestamp, $site) = @_;
963
964 my $collection = $gsdl_cgi->clean_param("c");
965 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
966 $gsdl_cgi->generate_error("No collection specified.");
967 }
968 my $file = $gsdl_cgi->clean_param("file");
969 if ((!defined $file) || ($file =~ m/^\s*$/)) {
970 $gsdl_cgi->generate_error("No file specified.");
971 }
972 my $directory = $gsdl_cgi->clean_param("directory") || "";
973 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
974 my $zip = $gsdl_cgi->clean_param("zip");
975
976 # Make sure we don't try to upload anything outside the collection
977 if ($file =~ /\.\./) {
978 $gsdl_cgi->generate_error("Illegal file specified.");
979 }
980 if ($directory =~ /\.\./) {
981 $gsdl_cgi->generate_error("Illegal directory specified.");
982 }
983
984 # Ensure the user is allowed to edit this collection
985 #&authenticate_user($gsdl_cgi, $username, $collection, $site);
986
987 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
988 my $gsdlhome = $ENV{'GSDLHOME'};
989 my $collection_directory = &util::filename_cat($gsdl3srchome, "web", "sites", $site, "collect", $collection);
990 $gsdl_cgi->checked_chdir($collection_directory);
991
992 # Make sure the collection isn't locked by someone else
993 &lock_collection($gsdl_cgi, $username, $collection, $site);
994
995 my $directory_path = &util::filename_cat($collection_directory, $directory);
996 if (!-d $directory_path) {
997 &util::mk_dir($directory_path);
998 if (!-d $directory_path) {
999 $gsdl_cgi->generate_error("Could not create directory $directory_path.");
1000 }
1001 }
1002
1003 #my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
1004 my $file_path = &util::filename_cat($directory_path, $file);
1005
1006 if (!open(FOUT, ">$file_path")) {
1007 $gsdl_cgi->generate_error("Unable to write file $file_path");
1008 }
1009
1010 # Read the uploaded data and write it out to file
1011 my $buf;
1012 my $num_bytes = 0;
1013 binmode(FOUT);
1014 my $bread;
1015 my $fh=$gsdl_cgi->clean_param("uploaded_file");
1016
1017 while ($bread=read($fh, $buf, 1024)) {
1018 print FOUT $buf;
1019 }
1020
1021 close(FOUT);
1022 # If we have downloaded a zip file, unzip it
1023 if (defined $zip) {
1024 my $java = $gsdl_cgi->get_java_path();
1025 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
1026 my $java_args = "\"$file_path\" \"$directory_path\"";
1027 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
1028
1029 my $java_output = `$java_command`;
1030 my $java_status = $?;
1031
1032 # Remove the zip file once we have unzipped it, since it is an intermediate file only
1033 unlink("$file_path");
1034
1035 if ($java_status > 0) {
1036 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
1037 }
1038 }
1039
1040 $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
1041}
1042
1043sub put_file
1044{
1045 my $gsdl_cgi = shift(@_);
1046 my $file_path = shift(@_);
1047 my $content_type = shift(@_);
1048
1049 if (open(PIN, "<$file_path")) {
1050 print STDOUT "Content-type:application/zip\n\n";
1051 my $buf;
1052 my $num_bytes = 0;
1053 binmode(PIN);
1054 while (read(PIN, $buf, 1024) > 0) {
1055 print STDOUT $buf;
1056 $num_bytes += length($buf);
1057 }
1058
1059 close(PIN);
1060 }
1061 else {
1062 $gsdl_cgi->generate_error("Unable to read file $file_path\n $!");
1063 }
1064}
1065
1066
1067sub send_mail
1068{
1069 my $gsdl_cgi = shift(@_);
1070 my $mail_subject = shift(@_);
1071 my $mail_content = shift(@_);
1072
1073 my $sendmail_command = "perl -S sendmail.pl";
1074 $sendmail_command .= " -to \"" . $mail_to_address . "\"";
1075 $sendmail_command .= " -from \"" . $mail_from_address . "\"";
1076 $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
1077 $sendmail_command .= " -subject \"" . $mail_subject . "\"";
1078
1079 if (!open(POUT, "| $sendmail_command")) {
1080 $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
1081 }
1082 print POUT $mail_content . "\n";
1083 close(POUT);
1084}
Note: See TracBrowser for help on using the repository browser.