source: gsdl/branches/protos-branch/cgi-bin/gliserver.pl@ 18554

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

gliserver.pl

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