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

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

encrypt the password

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