source: main/tags/2.71/gsdl/cgi-bin/gliserver.pl@ 25574

Last change on this file since 25574 was 11943, checked in by mdewsnip, 18 years ago

It is no longer an error to try to delete a file that doesn't exist. This happens, for example, when trying to delete the index directory after building a new collection for the first time.

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