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

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

added a uer_validation method

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