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

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

added the cgi directory

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