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

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

Better use of @INC to include Greenstone's perllib areas so 'use' and 'require' statements can be simpler

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