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

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

deleted a duplicated output in check-installation

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