source: main/trunk/greenstone2/perllib/cgiactions/baseaction.pm@ 23755

Last change on this file since 23755 was 23755, checked in by davidb, 13 years ago

Removal of whitespace

File size: 13.7 KB
Line 
1###########################################################################
2#
3# baseaction.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 2009 New Zealand Digital Library Project
9#
10# This program is free software; you can redistr te it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26
27package baseaction;
28
29use strict;
30
31use inexport;
32
33our $authenication_enabled = 0;
34our $mail_enabled = 0;
35
36
37# change this to get these values from a config file
38my $mail_to_address = "user\@server"; # Set this appropriately
39my $mail_from_address = "user\@server"; # Set this appropriately
40my $mail_smtp_server = "smtp.server"; # Set this appropriately
41
42
43
44# Required CGI arguments: "a" for action
45# "c" for collection
46# Optional CGI arguemnts: "ts" for timestamp (auto generated is missing)
47# "site" (used by Greenstone3)
48
49# allow "un" for username to be optional for now
50
51sub new
52{
53 my $class = shift (@_);
54 my ($action_table,$gsdl_cgi,$iis6_mode) = @_;
55
56 my $self = { 'gsdl_cgi' => $gsdl_cgi,
57 'iis6_mode' => $iis6_mode,
58 'gsdlhome' => $ENV{'GSDLHOME'} };
59
60 # Retrieve the (required) command CGI argument
61 my $action = $gsdl_cgi->clean_param("a");
62
63 if (!defined $action) {
64 my $err_mess = "No action (a=...) specified.\n";
65 $err_mess .= "\nPossible actions are:\n";
66
67 $err_mess .= " check-installation\n\n";
68
69 foreach my $a (keys %$action_table) {
70 $err_mess .= " $a:\n";
71 $err_mess .= " Compulsory args: ";
72 my @comp_args = ("c");
73 push(@comp_args,"un") if ($authenication_enabled);
74 push(@comp_args,@{$action_table->{$a}->{'compulsory-args'}});
75 $err_mess .= join(", ", @comp_args);
76
77 $err_mess .= "\n";
78
79 my @opt_args = ();
80 push(@opt_args,"un") if (!$authenication_enabled);
81 push(@opt_args,@{$action_table->{$a}->{'optional-args'}});
82
83 if (scalar(@opt_args)>0) {
84
85 $err_mess .= " Optional args : ";
86 $err_mess .= join(", ", @opt_args);
87 $err_mess .= "\n";
88 }
89 $err_mess .= "\n";
90 }
91
92 $gsdl_cgi->generate_message($err_mess);
93 exit(-1);
94
95 }
96 $gsdl_cgi->delete("a");
97
98 $self = bless $self, $class;
99
100 # The check-installation command has no arguments
101 if ($action eq "check-installation") {
102 $self->check_installation($gsdl_cgi,$iis6_mode);
103 exit 0;
104 }
105
106
107 if (!defined $action_table->{$action}) {
108 my $valid_actions = join(", ", keys %$action_table);
109
110 my $err_mess = "Unrecognised action (a=$action) specified.\n";
111 $err_mess .= "Valid actions are: $valid_actions\n";
112
113 $gsdl_cgi->generate_error($err_mess);
114 }
115
116
117 my $collect = $gsdl_cgi->clean_param("c");
118 if ((!defined $collect) || ($collect =~ m/^\s*$/)) {
119 $gsdl_cgi->generate_error("No collection specified.");
120 }
121 $gsdl_cgi->delete("c");
122
123 # allow un to be optional for now
124 my $username = $gsdl_cgi->clean_param("un");
125
126
127 # Get then remove the ts (timestamp) argument (since this can mess up
128 # other scripts)
129 my $timestamp = $gsdl_cgi->clean_param("ts");
130 if ((!defined $timestamp) || ($timestamp =~ m/^\s*$/)) {
131 # Fall back to using the Perl time() function to generate a timestamp
132 $timestamp = time();
133 }
134 $gsdl_cgi->delete("ts");
135
136 my $site = undef;
137 if($gsdl_cgi->greenstone_version() != 2) {
138 # all GS versions after 2 may define site
139 $site = $gsdl_cgi->clean_param("site");
140 if (!defined $site) {
141 $gsdl_cgi->generate_error("No site specified.");
142 }
143 $gsdl_cgi->delete("site");
144 }
145
146 $self->{'action'} = $action;
147 $self->{'collect'} = $collect;
148 $self->{'username'} = $username;
149 $self->{'timestamp'} = $timestamp;
150 $self->{'site'} = $site;
151
152 # Locate and store compulsory arguments
153 my $comp_args = $action_table->{$action}->{'compulsory-args'};
154 foreach my $ca (@$comp_args) {
155 if (!defined $gsdl_cgi->param($ca)) {
156 $gsdl_cgi->generate_error("Compulsory argument '$ca' missing");
157 }
158 else {
159 $self->{$ca} = $gsdl_cgi->clean_param($ca);
160 $gsdl_cgi->delete($ca);
161 }
162 }
163
164 # Locate and store optional args if present
165 my $opt_args = $action_table->{$action}->{'optional-args'};
166 foreach my $oa (@$opt_args) {
167 if (defined $gsdl_cgi->param($oa)) {
168 $self->{$oa} = $gsdl_cgi->clean_param($oa);
169 $gsdl_cgi->delete($oa);
170 }
171 }
172
173 # Retrieve infodb-type
174 if (defined $collect) {
175 my $inexport = newCGI inexport(ref $self,$collect);
176 my ($config_filename,$collect_cfg) = $inexport->read_collection_cfg($collect);
177 $self->{'infodbtype'} = $collect_cfg->{'infodbtype'};
178 }
179
180 return $self;
181}
182
183
184sub do_action
185{
186 my $self = shift @_;
187 my $action = $self->{'action'};
188
189 $action =~ s/-/_/g;
190
191
192 $self->$action();
193
194}
195
196
197
198
199sub authenticate_user
200{
201 my $self = shift @_;
202 my $username = shift(@_);
203 my $collection = shift(@_);
204
205 my $gsdl_cgi = $self->{'gsdl_cgi'};
206
207 # Remove the pw argument (since this can mess up other scripts)
208 my $user_password = $gsdl_cgi->clean_param("pw");
209 $gsdl_cgi->delete("pw");
210
211 if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) {
212 $gsdl_cgi->generate_error("Authentication failed: no password specified.");
213 }
214
215 my $gsdlhome = $ENV{'GSDLHOME'};
216 my $etc_directory = &util::filename_cat($gsdlhome, "etc");
217 my $users_db_file_path = &util::filename_cat($etc_directory, "users.db");
218
219 # Use db2txt to get the user accounts information
220 my $users_db_content = "";
221 open(USERS_DB, "db2txt \"$users_db_file_path\" |");
222 while (<USERS_DB>) {
223 $users_db_content .= $_;
224 }
225
226 # Get the user account information from the users.db database
227 my %users_db_data = ();
228 foreach my $users_db_entry (split(/-{70}/, $users_db_content)) {
229 if ($users_db_entry =~ /\n?\[(.+)\]\n/) {
230 $users_db_data{$1} = $users_db_entry;
231 }
232 }
233
234 # Check username
235 my $user_data = $users_db_data{$username};
236 if (!defined $user_data) {
237 $gsdl_cgi->generate_error("Authentication failed: no account for user '$username'.");
238 }
239
240 # Check password
241 my ($valid_user_password) = ($user_data =~ /\<password\>(.*)/);
242 if ($user_password ne $valid_user_password) {
243 $gsdl_cgi->generate_error("Authentication failed: incorrect password.");
244 }
245
246 # Check group
247 my ($user_groups) = ($user_data =~ /\<groups\>(.*)/);
248 if ($collection eq "") {
249 # If we're not editing a collection then the user doesn't need to be in a particular group
250 return $user_groups; # Authentication successful
251 }
252 foreach my $user_group (split(/\,/, $user_groups)) {
253 # Does this user have access to all collections?
254 if ($user_group eq "all-collections-editor") {
255 return $user_groups; # Authentication successful
256 }
257 # Does this user have access to personal collections, and is this one?
258 if ($user_group eq "personal-collections-editor" && $collection =~ /^$username\-/) {
259 return $user_groups; # Authentication successful
260 }
261 # Does this user have access to this collection
262 if ($user_group eq "$collection-collection-editor") {
263 return $user_groups; # Authentication successful
264 }
265 }
266
267 $gsdl_cgi->generate_error("Authentication failed: user is not in the required group.");
268}
269
270
271
272sub check_installation
273{
274 my $self = shift @_;
275 my $iis6_mode = shift(@_);
276
277 my $gsdl_cgi = $self->{'gsdl_cgi'};
278
279 my $installation_ok = 1;
280 my $installation_status = "";
281
282 print STDOUT "Content-type:text/plain\n\n";
283
284 # Check that Java is installed and accessible
285 my $java = $gsdl_cgi->get_java_path();
286 my $java_command = "$java -version 2>&1";
287
288 # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
289 # directly out to the page
290 if ($iis6_mode)
291 {
292 $java_command = "java -version";
293 }
294
295 my $java_output = `$java_command`;
296 my $java_status = $?;
297 if ($java_status < 0) {
298 # The Java command failed
299 $installation_status = "Java failed -- do you have the Java run-time installed?\n" . $gsdl_cgi->check_java_home() . "\n";
300 $installation_ok = 0;
301 }
302 else {
303 $installation_status = "Java found: $java_output";
304 }
305
306 # Show the values of some important environment variables
307 $installation_status .= "\n";
308 $installation_status .= "GSDLHOME: " . $ENV{'GSDLHOME'} . "\n";
309 $installation_status .= "GSDLOS: " . $ENV{'GSDLOS'} . "\n";
310 $installation_status .= "PATH: " . $ENV{'PATH'} . "\n";
311
312 if ($installation_ok) {
313 print STDOUT $installation_status . "\nInstallation OK!";
314 }
315 else {
316 print STDOUT $installation_status;
317 }
318}
319
320sub lock_collection
321{
322 my $self = shift @_;
323 my $username = shift(@_);
324 my $collection = shift(@_);
325
326 my $gsdl_cgi = $self->{'gsdl_cgi'};
327
328 my $steal_lock = $gsdl_cgi->clean_param("steal_lock");
329 $gsdl_cgi->delete("steal_lock");
330
331 if (!defined $username) {
332 # don't have any user details for current user to compare with
333 # even if there is a lock file
334 # For now, allow the current user access. Might want to
335 # revisit this in the future.
336 return;
337 }
338
339 my $gsdlhome = $ENV{'GSDLHOME'};
340 my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
341 $gsdl_cgi->checked_chdir($collection_directory);
342
343 # Check if a lock file already exists for this collection
344 my $lock_file_name = "gli.lck";
345 if (-e $lock_file_name) {
346 # A lock file already exists... check if it's ours
347 my $lock_file_content = "";
348 open(LOCK_FILE, "<$lock_file_name");
349 while (<LOCK_FILE>) {
350 $lock_file_content .= $_;
351 }
352 close(LOCK_FILE);
353
354 # Pick out the owner of the lock file
355 $lock_file_content =~ /\<User\>(.*?)\<\/User\>/;
356 my $lock_file_owner = $1;
357
358 # The lock file is ours, so there is no problem
359 if ($lock_file_owner eq $username) {
360 return;
361 }
362
363 # The lock file is not ours, so throw an error unless "steal_lock" is set
364 unless (defined $steal_lock) {
365 $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner");
366 }
367 }
368
369 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
370 my $current_time = sprintf("%02d/%02d/%d %02d:%02d:%02d", $mday, $mon + 1, $year + 1900, $hour, $min, $sec);
371
372 # Create a lock file for us (in the same format as the GLI) and we're done
373 open(LOCK_FILE, ">$lock_file_name");
374 print LOCK_FILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
375 print LOCK_FILE "<LockFile>\n";
376 print LOCK_FILE " <User>" . $username . "</User>\n";
377 print LOCK_FILE " <Machine>(Remote)</Machine>\n";
378 print LOCK_FILE " <Date>" . $current_time . "</Date>\n";
379 print LOCK_FILE "</LockFile>\n";
380 close(LOCK_FILE);
381}
382
383
384# Release the gli.lck otherwise no one else will be able to use the collection again.
385sub unlock_collection
386{
387 my $self = shift @_;
388 my ($username, $collection) = @_;
389 my $gsdl_cgi = $self->{'gsdl_cgi'};
390
391 # Obtain the path to the collection GLI lock file
392 my $lock_file_path = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "gli.lck");
393
394 # If the lock file does exist, check if it is ours
395 if (-e $lock_file_path)
396 {
397 my $lock_file_content = "";
398 open(LOCK_FILE, "<$lock_file_path");
399 while (<LOCK_FILE>) {
400 $lock_file_content .= $_;
401 }
402 close(LOCK_FILE);
403
404 # Pick out the owner of the lock file
405 $lock_file_content =~ /\<User\>(.*?)\<\/User\>/;
406 my $lock_file_owner = $1;
407
408 # If we are the owner of this lock, we have the right to delete it
409 if ($lock_file_owner eq $username) {
410 unlink($lock_file_path );
411 }
412 else {
413 $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner. Cannot be unlocked");
414 }
415 }
416}
417
418
419sub send_mail
420{
421 my $self = shift @_;
422
423 my ($mail_subject,$mail_content) = @_;
424
425 my $gsdl_cgi = $self->{'gsdl_cgi'};
426
427 my $sendmail_command = "perl -S sendmail.pl";
428 $sendmail_command .= " -to \"" . $mail_to_address . "\"";
429 $sendmail_command .= " -from \"" . $mail_from_address . "\"";
430 $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
431 $sendmail_command .= " -subject \"" . $mail_subject . "\"";
432
433 if (!open(POUT, "| $sendmail_command")) {
434 $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
435 }
436 print POUT $mail_content . "\n";
437 close(POUT);
438}
439
440
441
442
443sub run_script
444{
445 my $self = shift @_;
446
447 my ($collect, $site, $script) = @_;
448
449 my $gsdl_cgi = $self->{'gsdl_cgi'};
450
451 my $perl_args = $collect;
452
453 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
454 $perl_args = "-collectdir \"$collect_dir\" " . $perl_args;
455
456 my $perl_command = "perl -S $script $perl_args";
457
458
459 # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so
460 # we have to let it go directly out to the page
461
462 if (!$self->{'iis6_mode'})
463 {
464 $perl_command .= " 2>&1";
465 }
466
467 if (!open(PIN, "$perl_command |")) {
468 $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
469 }
470
471 print STDOUT "Content-type:text/plain\n\n";
472 print "$perl_command \n";
473
474 while (defined (my $perl_output_line = <PIN>)) {
475 print STDOUT $perl_output_line;
476 }
477 close(PIN);
478
479 my $perl_status = $?;
480 if ($perl_status > 0) {
481 $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
482 }
483}
484
485
486
487
4881;
Note: See TracBrowser for help on using the repository browser.