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

Last change on this file since 11451 was 11293, checked in by mdewsnip, 18 years ago

Added a new "check-installation" command to check that Java is installed correctly.

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