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

Last change on this file since 13497 was 13497, checked in by kjdon, 17 years ago

commented out checking for existence of a directory in new_collection_directory as it throws an error which we don't want

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