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

Last change on this file since 14326 was 14326, checked in by qq6, 17 years ago

updated gliserver4gs3.pl

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