source: trunk/gsdl/cgi-bin/gliserver.pl@ 14024

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

gsdlCGI.pm

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 29.8 KB
Line 
1#!/usr/bin/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 print STDOUT $cgi_arg_value;
570 if ($cgi_arg_value eq "") {
571 $perl_args = "-$cgi_arg_name " . $perl_args;
572 }
573 else {
574 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
575 }
576 }
577
578 my $perl_command = "perl -S $script $perl_args 2>&1";
579 my $perl_output = `$perl_command`;
580 my $perl_status = $?;
581 if ($perl_status > 0) {
582 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
583 }
584
585 #print STDOUT "Content-type:text/plain\n\n";
586 print STDOUT $perl_output;
587 print STDOUT $perl_command;
588
589}
590
591
592sub move_collection_file
593{
594 my ($gsdl_cgi, $username, $timestamp) = @_;
595
596 my $collection = $gsdl_cgi->clean_param("c");
597 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
598 $gsdl_cgi->generate_error("No collection specified.");
599 }
600 my $source_file = $gsdl_cgi->clean_param("source");
601 if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
602 $gsdl_cgi->generate_error("No source file specified.");
603 }
604 $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
605 my $target_file = $gsdl_cgi->clean_param("target");
606 if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
607 $gsdl_cgi->generate_error("No target file specified.");
608 }
609 $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
610
611 # Make sure we don't try to move anything outside the collection
612 if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
613 $gsdl_cgi->generate_error("Illegal file specified.");
614 }
615
616 # Ensure the user is allowed to edit this collection
617 &authenticate_user($gsdl_cgi, $username, $collection);
618
619 my $gsdlhome = $ENV{'GSDLHOME'};
620 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
621 $gsdl_cgi->checked_chdir($collection_directory);
622
623 # Check that the collection source file exists
624 if (!-e $source_file) {
625 $gsdl_cgi->generate_error("Collection file $source_file does not exist.");
626 }
627
628 # Make sure the collection isn't locked by someone else
629 &lock_collection($gsdl_cgi, $username, $collection);
630
631 &util::mv($source_file, $target_file);
632
633 # Check that the collection source file was moved
634 if (-e $source_file || !-e $target_file) {
635 $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
636 }
637
638 $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
639}
640
641
642sub new_collection_directory
643{
644 my ($gsdl_cgi, $username, $timestamp) = @_;
645
646 my $collection = $gsdl_cgi->clean_param("c");
647 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
648 $gsdl_cgi->generate_error("No collection specified.");
649 }
650 my $directory = $gsdl_cgi->clean_param("directory");
651 if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
652 $gsdl_cgi->generate_error("No directory specified.");
653 }
654 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
655
656 # Make sure we don't try to create anything outside the collection
657 if ($directory =~ /\.\./) {
658 $gsdl_cgi->generate_error("Illegal directory specified.");
659 }
660
661 # Ensure the user is allowed to edit this collection
662 &authenticate_user($gsdl_cgi, $username, $collection);
663
664 my $gsdlhome = $ENV{'GSDLHOME'};
665 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
666 $gsdl_cgi->checked_chdir($collection_directory);
667
668 # Check that the collection directory doesn't already exist
669 # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicity
670 # try to create the import directory
671# if (-d $directory) {
672# $gsdl_cgi->generate_error("Collection directory $directory already exists.");
673# }
674
675 # Make sure the collection isn't locked by someone else
676 &lock_collection($gsdl_cgi, $username, $collection);
677
678 &util::mk_dir($directory);
679
680 # Check that the collection directory was created
681 if (!-d $directory) {
682 $gsdl_cgi->generate_error("Could not create collection directory $directory.");
683 }
684
685 $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
686}
687
688
689sub run_script
690{
691 my ($gsdl_cgi, $username, $timestamp) = @_;
692
693 my $script = $gsdl_cgi->clean_param("script");
694 if ((!defined $script) || ($script =~ m/^\s*$/)) {
695 $gsdl_cgi->generate_error("No script specified.");
696 }
697 $gsdl_cgi->delete("script");
698 my $collection = $gsdl_cgi->clean_param("c");
699 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
700 $gsdl_cgi->generate_error("No collection specified.");
701 }
702 $gsdl_cgi->delete("c");
703
704 # Ensure the user is allowed to edit this collection
705 &authenticate_user($gsdl_cgi, $username, $collection);
706
707 # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
708 &lock_collection($gsdl_cgi, $username, $collection) unless ($script eq "mkcol.pl");
709
710 # Last argument is the collection name, except for explode_metadata_database.pl
711 my $perl_args = $collection;
712 if ($script eq "explode_metadata_database.pl") {
713 # Last argument is the file to be exploded
714 my $file = $gsdl_cgi->clean_param("file");
715 if ((!defined $file) || ($file =~ m/^\s*$/)) {
716 $gsdl_cgi->generate_error("No file specified.");
717 }
718 $gsdl_cgi->delete("file");
719 $perl_args = $file;
720 }
721
722 foreach my $cgi_arg_name ($gsdl_cgi->param) {
723 my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
724 if ($cgi_arg_value eq "") {
725 $perl_args = "-$cgi_arg_name " . $perl_args;
726 }
727 else {
728 $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
729 }
730 }
731
732 my $perl_command = "perl -S $script $perl_args 2>&1";
733 if (!open(PIN, "$perl_command |")) {
734 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
735 }
736
737 print STDOUT "Content-type:text/plain\n\n";
738 while (defined (my $perl_output_line = <PIN>)) {
739 print STDOUT $perl_output_line;
740 }
741 close(PIN);
742
743 my $perl_status = $?;
744 if ($perl_status > 0) {
745 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
746 }
747 elsif ($mail_enabled) {
748 if ($script eq "buildcol.pl") {
749 &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
750 }
751 }
752}
753
754
755sub upload_collection_file
756{
757 my ($gsdl_cgi, $username, $timestamp) = @_;
758
759 my $collection = $gsdl_cgi->clean_param("c");
760 if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
761 $gsdl_cgi->generate_error("No collection specified.");
762 }
763 my $file = $gsdl_cgi->clean_param("file");
764 if ((!defined $file) || ($file =~ m/^\s*$/)) {
765 $gsdl_cgi->generate_error("No file specified.");
766 }
767 my $directory = $gsdl_cgi->clean_param("directory") || "";
768 $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
769 my $zip = $gsdl_cgi->clean_param("zip");
770
771 # Make sure we don't try to upload anything outside the collection
772 if ($file =~ /\.\./) {
773 $gsdl_cgi->generate_error("Illegal file specified.");
774 }
775 if ($directory =~ /\.\./) {
776 $gsdl_cgi->generate_error("Illegal directory specified.");
777 }
778
779 # Ensure the user is allowed to edit this collection
780 &authenticate_user($gsdl_cgi, $username, $collection);
781
782 my $gsdlhome = $ENV{'GSDLHOME'};
783 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
784 $gsdl_cgi->checked_chdir($collection_directory);
785
786 # Make sure the collection isn't locked by someone else
787 &lock_collection($gsdl_cgi, $username, $collection);
788
789 my $directory_path = &util::filename_cat($collection_directory, $directory);
790 if (!-d $directory_path) {
791 &util::mk_dir($directory_path);
792 if (!-d $directory_path) {
793 $gsdl_cgi->generate_error("Could not create directory $directory_path.");
794 }
795 }
796
797 my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
798 if (!open(FOUT, ">$file_path")) {
799 $gsdl_cgi->generate_error("Unable to write file $file_path");
800 }
801
802 # Read the uploaded data and write it out to file
803 my $buf;
804 my $num_bytes = 0;
805 binmode(FOUT);
806 while (read(STDIN, $buf, 1024) > 0) {
807 print FOUT $buf;
808 $num_bytes += length($buf);
809 }
810 close(FOUT);
811
812 # If we have downloaded a zip file, unzip it
813 if (defined $zip) {
814 my $java = $gsdl_cgi->get_java_path();
815 my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
816 my $java_args = "\"$file_path\" \"$directory_path\"";
817 my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args";
818
819 my $java_output = `$java_command`;
820 my $java_status = $?;
821
822 # Remove the zip file once we have unzipped it, since it is an intermediate file only
823 unlink("$file_path");
824
825 if ($java_status > 0) {
826 $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
827 }
828 }
829
830 $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
831}
832
833
834sub put_file
835{
836 my $gsdl_cgi = shift(@_);
837 my $file_path = shift(@_);
838 my $content_type = shift(@_);
839
840 if (open(PIN, "<$file_path")) {
841 print STDOUT "Content-type:$content_type\n\n";
842
843 my $buf;
844 my $num_bytes = 0;
845 binmode(PIN);
846 while (read(PIN, $buf, 1024) > 0) {
847 print STDOUT $buf;
848 $num_bytes += length($buf);
849 }
850
851 close(PIN);
852 }
853 else {
854 $gsdl_cgi->generate_error("Unable to read file $file_path\n $!");
855 }
856}
857
858
859sub send_mail
860{
861 my $gsdl_cgi = shift(@_);
862 my $mail_subject = shift(@_);
863 my $mail_content = shift(@_);
864
865 my $sendmail_command = "perl -S sendmail.pl";
866 $sendmail_command .= " -to \"" . $mail_to_address . "\"";
867 $sendmail_command .= " -from \"" . $mail_from_address . "\"";
868 $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
869 $sendmail_command .= " -subject \"" . $mail_subject . "\"";
870
871 if (!open(POUT, "| $sendmail_command")) {
872 $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
873 }
874 print POUT $mail_content . "\n";
875 close(POUT);
876}
877
878
879&main();
Note: See TracBrowser for help on using the repository browser.