source: gsdl/trunk/cgi-bin/gsdlCGI.pm@ 19277

Last change on this file since 19277 was 19277, checked in by davidb, 15 years ago

Additional unshift ops for @INC

  • Property svn:keywords set to Author Date Id Revision
File size: 15.4 KB
RevLine 
[7956]1package gsdlCGI;
2
[16467]3# This file merges Michael Dewsnip's gsdlCGI.pm for GS2 and Quan Qiu's gsdlCGI4gs3.pm (GS3)
[14024]4
[16467]5use strict;
6no strict 'subs';
7no strict 'refs'; # allow filehandles to be variables and viceversa
[14024]8
[7956]9use CGI;
[10206]10use Cwd;
[19141]11use MIME::Base64;
[7956]12
[16467]13@gsdlCGI::ISA = ( 'CGI' );
[7956]14
15sub new {
16 my $class = shift @_;
[16467]17
18 my $self;
19
20 # We'll determine the correct config file in this constructor itself
21 # and use it to determine the Greenstone server's version.
22 # Perhaps later, another test can be used for finding out what version
23 # of the Greenstone server we are working with.
24 my $version;
25 if (-e "gsdl3site.cfg") {
26 $version = 3;
27 } else {
28 $version = 2;
29 }
[7956]30
[11148]31 if ((defined $ENV{'REQUEST_METHOD'}) && ($ENV{'REQUEST_METHOD'} eq "POST")) {
[16467]32
33 # Check if we're dealing with the upload-coll-file cmd. Because it will be a
34 # multipart POST message and must be dealt with by the default CGI() constructor
35 if((defined $ENV{'QUERY_STRING'}) && ($ENV{'QUERY_STRING'} =~ m/upload-collection-file/)) {
36 $self = new CGI();
37 }
38
39 else { # all other POST commands processed using CGI($line)
40 my $line = <STDIN>;
41 if ((defined $line) && ($line ne "")) {
42 $self = new CGI($line);
43 }
[7956]44 }
[16467]45
46 }
47
48 # If one of the conditions above did not hold, then self=new CGI()
49 if (!defined $self) {
[7956]50 $self = new CGI();
51 }
52
[16467]53 if ($version == 2) {
54 $self->{'site_filename'} = "gsdlsite.cfg";
55 $self->{'greenstone_version'} = 2;
56 }
57 elsif ($version == 3) {
58 $self->{'site_filename'} = "gsdl3site.cfg";
59 $self->{'greenstone_version'} = 3;
60 }
61
[10583]62 return bless $self, $class;
63}
64
65
66sub parse_cgi_args
67{
68 my $self = shift @_;
[7956]69 my $xml = (defined $self->param("xml")) ? 1 : 0;
70
71 $self->{'xml'} = $xml;
72
73 my @var_names = $self->param;
74 my @arg_list = ();
75 foreach my $n ( @var_names ) {
76 my $arg = "$n=";
77 my $val = $self->param($n);
78 $arg .= $val if (defined $val);
79 push(@arg_list,$arg);
80 }
81
82 $self->{'args'} = join("&",@arg_list);
83}
84
85
86sub clean_param
87{
88 my $self = shift @_;
89 my ($param) = @_;
90
91 my $val = $self->SUPER::param($param);
92 $val =~ s/[\r\n]+$// if (defined $val);
93
94 return $val;
95}
96
97sub safe_val
98{
99 my $self = shift @_;
100 my ($val) = @_;
101
102 # ensure only alpha-numeric plus a few other special chars remain
103
[9941]104 $val =~ s/[^[:alnum:]@\.\/\- :_]//g if (defined $val);
[7956]105
106 return $val;
107}
108
[16467]109sub generate_message
110{
111 my $self = shift @_;
112 my ($message) = @_;
113
114 #if($self->{'greenstone_version'} == 2) { # plain text, for IIS 6
115 print STDOUT "Content-type:text/plain\n\n$message";
116 #} else {
117 #print "Content-type:text/html\n\n";
118 #print "<pre>";
119 #print STDOUT $message;
120 #print "</pre>";
121 #}
122}
[7956]123
124sub generate_error
125{
126 my $self = shift @_;
127 my ($mess) = @_;
128
129 my $xml = $self->{'xml'};
130
131 my $full_mess;
132 my $args = $self->{'args'};
133
134 if ($xml) {
[13167]135 # Make $args XML safe
136 my $args_xml_safe = $args;
137 $args_xml_safe =~ s/&/&amp;/g;
138
[7956]139 $full_mess = "<Error>\n";
140 $full_mess .= " $mess\n";
[13167]141 $full_mess .= " CGI args were: $args_xml_safe\n";
[7956]142 $full_mess .= "</Error>\n";
143 }
144 else {
[10565]145 $full_mess = "ERROR: $mess\n ($args)\n";
[7956]146 }
147
[16467]148 $self->generate_message($full_mess);
[7956]149
[16467]150 die $full_mess;
[7956]151}
152
[10206]153sub generate_warning
154{
155 my $self = shift @_;
156 my ($mess) = @_;
157
158 my $xml = $self->{'xml'};
[7956]159
[10206]160 my $full_mess;
161 my $args = $self->{'args'};
162
163 if ($xml) {
[13167]164 # Make $args XML safe
165 my $args_xml_safe = $args;
166 $args_xml_safe =~ s/&/&amp;/g;
167
[10206]168 $full_mess = "<Warning>\n";
169 $full_mess .= " $mess\n";
[13167]170 $full_mess .= " CGI args were: $args_xml_safe\n";
[10206]171 $full_mess .= "</Warning>\n";
172 }
173 else {
174 $full_mess = "Warning: $mess ($args)\n";
175 }
176
[16467]177 $self->generate_message($full_mess);
[10206]178
179 print STDERR $full_mess;
180}
181
182
[7956]183sub generate_ok_message
184{
185 my $self = shift @_;
186 my ($mess) = @_;
187
188 my $xml = $self->{'xml'};
189
190 my $full_mess;
191
192 if ($xml) {
193 $full_mess = "<Accepted>\n";
194 $full_mess .= " $mess\n";
195 $full_mess .= "</Accepted>\n";
196 }
197 else {
198 $full_mess = "$mess\n";
199 }
[16467]200
201 $self->generate_message($full_mess);
[7956]202}
203
204
205
206sub get_config_info {
207 my $self = shift @_;
[16467]208 my ($infotype, $optional) = @_;
[7956]209
[16467]210 my $site_filename = $self->{'site_filename'};
[7956]211 open (FILEIN, "<$site_filename")
[16467]212 || $self->generate_error("Could not open $site_filename");
[7956]213
214 my $config_content = "";
215 while(defined (my $line = <FILEIN>)) {
216 $config_content .= $line;
217 }
218 close(FILEIN);
219
[10565]220 my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
[16467]221 $loc =~ s/\"//g if defined $loc;
[7956]222
223 if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
[16467]224 if((!defined $optional) || (!$optional)) {
225 $self->generate_error("$infotype is not set in $site_filename");
226 }
[7956]227 }
228
229 return $loc;
230}
231
[16467]232sub get_gsdl3_src_home{
233 my $self = shift @_;
234 if (defined $self->{'gsdl3srchome'}) {
235 return $self->{'gsdl3srchome'};
236 }
[10206]237
[16467]238 my $gsdl3srchome = $self->get_config_info("gsdl3srchome");
239
240 if(defined $gsdl3srchome) {
241 $gsdl3srchome =~ s/(\/|\\)$//; # remove trailing slash
242 }
243 $self->{'gsdl3srchome'} = $gsdl3srchome;
244
245 return $gsdl3srchome;
246}
247
248
[9941]249sub get_gsdl_home {
250 my $self = shift @_;
[10206]251
252 if (defined $self->{'gsdlhome'}) {
253 return $self->{'gsdlhome'};
254 }
[7956]255
[9941]256 my $gsdlhome = $self->get_config_info("gsdlhome");
[7956]257
[10206]258 $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
259
260 $self->{'gsdlhome'} = $gsdlhome;
261
[9941]262 return $gsdlhome;
263}
264
[16467]265sub get_gsdl3_home {
266 my $self = shift @_;
267 my ($optional) = @_;
268
269 if (defined $self->{'gsdl3home'}) {
270 return $self->{'gsdl3home'};
271 }
272
273 my $gsdl3home = $self->get_config_info("gsdl3home", $optional);
274
275 if(defined $gsdl3home) {
276 $gsdl3home =~ s/(\/|\\)$//; # remove trailing slash
277 $self->{'gsdl3home'} = $gsdl3home;
278 }
279 return $gsdl3home;
280}
281
282sub get_java_home {
283 my $self = shift @_;
284 my ($optional) = @_;
285
286 if (defined $self->{'javahome'}) {
287 return $self->{'javahome'};
288 }
289
290 my $javahome = $self->get_config_info("javahome", $optional);
291 if(defined $javahome) {
292 $javahome =~ s/(\/|\\)$//; # remove trailing slash
293 $self->{'javahome'} = $javahome;
294 }
295 return $javahome;
296}
297
298sub get_perl_path {
299 my $self = shift @_;
[16509]300 my ($optional) = @_;
[16467]301
302 if (defined $self->{'perlpath'}) {
303 return $self->{'perlpath'};
304 }
305
[16509]306 my $perlpath = $self->get_config_info("perlpath", $optional);
[16467]307
308 if(defined $perlpath) {
309 $perlpath =~ s/(\/|\\)$//; # remove trailing slash
310 $self->{'perlpath'} = $perlpath;
311 }
312 return $perlpath;
313}
314
[10206]315sub get_gsdl_os {
[7956]316 my $self = shift @_;
[10206]317
318 my $os = $^O;
[7956]319
[10206]320 if ($os =~ m/linux/i) {
321 return "linux";
322 }
[16467]323 elsif ($os =~ m/mswin/i) {
[10206]324 return "windows";
325 }
[16467]326 elsif ($os =~ m/macos/i) {
[10206]327 return "darwin";
328 }
329 else {
330 # return as is.
331 return $os;
332 }
333}
[7956]334
[16467]335sub get_library_url_suffix {
336 my $self = shift @_;
337
338 if (defined $self->{'library_url_suffix'}) {
339 return $self->{'library_url_suffix'};
340 }
341
342 my $optional = 1; # ignore absence of gwcgi if not found
343 my $library_url = $self->get_config_info("gwcgi", $optional);
344 if(defined $library_url) {
345 $library_url =~ s/(\/|\\)$//; # remove trailing slash
346 }
347 else {
348
349 if($self->{'greenstone_version'} == 2) {
[16971]350 $library_url = $self->get_config_info("httpprefix", $optional);
[18967]351 $library_url = "/greenstone" unless defined $library_url;
[18966]352 $library_url = "$library_url/cgi-bin/library.cgi"; # same extension for linux and windows
[16467]353 }
354 else { # greenstone 3 or later and gwcgi not defined
355 $library_url = "/greenstone3"; #"/greenstone3/library";
356 }
357 }
358
359 $self->{'library_url_suffix'} = $library_url;
360 return $library_url;
361}
362
363sub setup_fedora_homes {
364 my $self = shift @_;
365 my ($optional) = @_;
366
367 # The following will still allow the FEDORA_HOME and FEDORA_VERSION environment
368 # variables to have been set outside the gsdlsite.cfg file. Existing env vars
369 # are only overwritten if they've *also* been defined in gsdlsite.cfg.
370
371 if (!defined $self->{'fedora_home'}) # Don't need to go through it all again if we'd already done this before
372 {
373 # First look in the gsdlsite.cfg file for the fedora properties to be defined
374 # and set $ENV{FEDORA_HOME} and $ENV{FEDORA_VERSION} if values were provided
375 $self->{'fedora_home'} = $self->get_config_info("fedorahome", $optional);
376
377 if (defined $self->{'fedora_home'}) {
378 $ENV{'FEDORA_HOME'} = $self->{'fedora_home'};
379 }
380 elsif (defined $ENV{'FEDORA_HOME'}) { # check environment variable
381 $self->{'fedora_home'} = $ENV{'FEDORA_HOME'};
382 }
383
384 # if FEDORA_HOME is now defined, we can look for the fedora version that is being used
385 if (defined $ENV{'FEDORA_HOME'})
386 {
387 # first look in the file
388 $self->{'fedora_version'} = $self->get_config_info("fedoraversion", $optional);
389
390 if (defined $self->{'fedora_version'}) {
391 $ENV{'FEDORA_VERSION'} = $self->{'fedora_version'};
392 }
393 elsif (defined $ENV{'FEDORA_VERSION'}) { # then check environment variable
394 $self->{'fedora_version'} = $ENV{'FEDORA_VERSION'};
395 }
396 else { # finally, default to version 3 and warn the user
397 $ENV{'FEDORA_VERSION'} = "3";
398 $self->{'fedora_version'} = $ENV{'FEDORA_VERSION'};
399 #$self->generate_ok_message("FEDORA_HOME is set, but not FEDORA_VERSION, defaulted to: 3.");
400 }
401 }
402 }
403}
404
[10206]405sub setup_gsdl {
406 my $self = shift @_;
[16467]407 my $optional = 1; # ignore absence of specified properties in gsdl(3)site.cfg if not found
[7956]408
[10212]409 my $gsdlhome = $self->get_gsdl_home();
410 my $gsdlos = $self->get_gsdl_os();
411 $ENV{'GSDLHOME'} = $gsdlhome;
412 $ENV{'GSDLOS'} = $gsdlos;
413
[19055]414
[16467]415 my $library_url = $self->get_library_url_suffix(); # best to have GSDLOS set beforehand
416 $self->{'library_url_suffix'} = $library_url;
417
[19277]418 unshift(@INC, "$ENV{'GSDLHOME'}/cgi-bin"); # This is OK on Windows
[19055]419 unshift(@INC, "$ENV{'GSDLHOME'}/perllib");
[19277]420 unshift(@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
421
[19055]422 require util;
[14973]423
[16467]424 if($self->{'greenstone_version'} == 3) {
425 my $gsdl3srchome = $self->get_gsdl3_src_home();
426 $ENV{'GSDL3SRCHOME'} = $gsdl3srchome;
427
428 my $gsdl3home = $self->get_gsdl3_home($optional);
429 # if a specific location for GS3's web folder is not provided,
430 # assume the GS3 web folder is in the default location
431 if(!defined $gsdl3home) {
432 $gsdl3home = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web");
433 $self->{'gsdl3home'} = $gsdl3home;
434 }
435 $ENV{'GSDL3HOME'} = $gsdl3home;
436 }
437
438
[10212]439 my $gsdl_bin_script = &util::filename_cat($gsdlhome,"bin","script");
440 &util::envvar_append("PATH",$gsdl_bin_script);
[16467]441
[10212]442 my $gsdl_bin_os = &util::filename_cat($gsdlhome,"bin",$gsdlos);
443 &util::envvar_append("PATH",$gsdl_bin_os);
[16467]444
[16509]445 # If the "perlpath" property is set in the gsdl(3)site.cfg config file, it is
446 # prepended to PATH only if the same perl bin dir path is not already on PATH env
447 my $perl_bin_dir = $self->get_perl_path($optional);
448 if(defined $perl_bin_dir)
449 {
450 &util::envvar_prepend("PATH", $perl_bin_dir);
451 }
452 elsif ($gsdlos eq "windows")
453 {
454 # Perl comes installed with the GS Windows Release Kit. However, note that if GS
455 # is from SVN, the user must have their own Perl and put it on the PATH or set
456 # perlpath in the gsdl site config file.
[16467]457 $perl_bin_dir = &util::filename_cat($gsdlhome, "bin", "windows", "perl", "bin");
458 if(-e $perl_bin_dir) {
459 &util::envvar_append("PATH", $perl_bin_dir);
460 }
[12707]461 }
[16467]462
[16509]463 # If javahome is explicitly set in the gsdl site config file then it will override
464 # any env variable JAVA_HOME. A GS2 server does not set JAVA_HOME, though java is on
465 # the path. Therefore, if Fedora is being used for FLI with GS2, then javahome must
466 # be set in gsdlsite.cfg or the JAVA_HOME env var must be explicitly set by the user.
467 my $java_home = $self->get_java_home($optional);
468 if(defined $java_home) {
469 $ENV{'JAVA_HOME'} = $java_home;
[16467]470 }
471
[16509]472 # FEDORA_HOME and FEDORA_VERSION are needed (by scripts g2f-import and g2f-buildcol).
[16467]473 $self->setup_fedora_homes($optional);
[10206]474}
[7956]475
[16467]476sub greenstone_version {
477 my $self = shift @_;
478 return $self->{'greenstone_version'};
479}
[7956]480
[16467]481sub library_url_suffix {
482 my $self = shift @_;
483 return $self->{'library_url_suffix'};
484}
485
486# Only useful to call this after calling setup_gsdl, as it uses some environment variables
487# Returns the Greenstone collect directory, or a specific collection directory inside collect
488sub get_collection_dir {
489 my $self = shift @_;
490 my ($site, $collection) = @_; # both may be undefined
[19202]491
[16467]492 my $collection_directory;
493 if($self->{'greenstone_version'} == 2 && defined $ENV{'GSDLHOME'}) {
494 if(defined $collection) {
495 $collection_directory = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection);
496 } else {
497 $collection_directory = &util::filename_cat($ENV{'GSDLHOME'}, "collect");
498 }
499 }
500 elsif($self->{'greenstone_version'} == 3 && defined $ENV{'GSDL3SRCHOME'}) {
501 if(defined $collection) {
502 $collection_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "sites", $site, "collect", $collection);
503 } else {
504 $collection_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "sites", $site, "collect");
505 }
506 }
507}
508
[10206]509sub local_rm_r
[9941]510{
511 my $self = shift @_;
[10206]512 my ($local_dir) = @_;
[9941]513
[10206]514 my $prefix_dir = getcwd();
[16467]515 my $full_path = &util::filename_cat($prefix_dir,$local_dir);
516
[10206]517 if ($prefix_dir !~ m/collect/) {
[16467]518 $self->generate_error("Trying to delete outside of Greenstone collect: $full_path");
[10206]519 }
[9941]520
[10206]521 # Delete recursively
[16467]522 if (!-e $full_path) {
523 $self->generate_error("File/Directory does not exist: $full_path");
[10206]524 }
[9941]525
[16467]526 &util::rm_r($full_path);
[9941]527}
528
[10206]529
[10584]530sub get_java_path()
[9941]531{
[10584]532 # Check the JAVA_HOME environment variable first
[9941]533 if (defined $ENV{'JAVA_HOME'}) {
534 my $java_home = $ENV{'JAVA_HOME'};
[10584]535 $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
536 return &util::filename_cat($java_home, "bin", "java");
537 }
[10206]538
[10584]539 # Hope that Java is on the PATH
540 return "java";
541}
542
543
544sub check_java_home()
545{
[16467]546 # Return a warning unless the JAVA_HOME environment variable is set
[10584]547 if (!defined $ENV{'JAVA_HOME'}) {
548 return "JAVA_HOME environment variable not set. Might not be able to find Java unless in PATH (" . $ENV{'PATH'} . ")";
[9941]549 }
[10206]550
[10584]551 return "";
[9941]552}
553
[7956]554
555sub checked_chdir
556{
557 my $self = shift @_;
558 my ($dir) = @_;
559
560 if (!-e $dir) {
561 $self->generate_error("Directory '$dir' does not exist");
562 }
563
564 chdir $dir
565 || $self->generate_error("Unable to change to directory: $dir");
566}
567
[16467]568sub rot13()
569{
570 my $self = shift @_;
571 my ($password)=@_;
572 my @password_arr=split(//,$password);
573
574 my @encrypt_password;
575 foreach my $str (@password_arr){
576 my $char=unpack("c",$str);
577 if ($char>=97 && $char<=109){
578 $char+=13;
579 }elsif ($char>=110 && $char<=122){
580 $char-=13;
581 }elsif ($char>=65 && $char<=77){
582 $char+=13;
583 }elsif ($char>=78 && $char<=90){
584 $char-=13;
585 }
586 $char=pack("c",$char);
587 push(@encrypt_password,$char);
588 }
589 return join("",@encrypt_password);
590}
591
592sub encrypt_password
593{
594 my $self = shift @_;
595
596 if (defined $self->param("pw")) { ##
597 if ($self->{'greenstone_version'} == 3) { # GS3 is in Java, so needs different encryption
598 $self->param('-name' => "pw", '-value' => $self->rot13($self->clean_param("pw")));
599 }
600 else { # GS2 (and versions of GS other than 3?)
601 #require "$self->{'gsdlhome'}/perllib/util.pm"; # This is OK on Windows
602 require "$self->{'gsdlhome'}/perllib/cpan/Crypt/UnixCrypt.pm"; # This is OK on Windows
603 $self->param('-name' => "pw", '-value' => &Crypt::UnixCrypt::crypt($self->clean_param("pw"), "Tp"));
604 }
605 }
606}
607
[18648]608
609sub decode {
610 my ($self, $text) = @_;
611 $text =~ s/\+/ /g;
612 $text = &MIME::Base64::decode_base64($text);
613
614 return $text;
615}
616
[7956]6171;
[10206]618
Note: See TracBrowser for help on using the repository browser.