source: main/trunk/greenstone2/common-src/cgi-bin/gsdlCGI.pm@ 36794

Last change on this file since 36794 was 36794, checked in by anupama, 19 months ago

Dr Bainbridge fixed a diffcol classifier's subsidiary documents ordering issue: for identical titles under an authorr bookshelf for AZCompactList classifier on Creators in Word-PDF-Basic tutorial model collection, a different order of the documents would appear each time. The solution was 2-fold: Besides the PERL_PERTURB_KEYS environment variable, which we set to 0, there is also the PERL_HASH_SEED (see https://www.perlmonks.org/?node_id=1167787 ), and they both need to be set to 0 to get consistent ordering when calling perl's 'keys' command on a hashmap. The other part of the solution is to initialise AZCompactList's sort property to 'nosort' which then uses an array (thus, having a sense of ordering) instead of AZCompactList's default behaviour of using a hashmap (which does not enforce a sense of ordering). Setting the sort property to nosort had the effect of a consistent order of the same identically Titled documents upon a single build, but no consistent ordering between builds which is what PERL_PERTURB_KEYS in conjunction with PERL_HASH_SEED ensure.

  • Property svn:keywords set to Author Date Id Revision
File size: 25.6 KB
Line 
1package gsdlCGI;
2
3# This file merges Michael Dewsnip's gsdlCGI.pm for GS2 and Quan Qiu's gsdlCGI4gs3.pm (GS3)
4
5use strict;
6no strict 'subs';
7no strict 'refs'; # allow filehandles to be variables and viceversa
8
9use CGI;
10use Cwd;
11use MIME::Base64;
12
13@gsdlCGI::ISA = ( 'CGI' );
14
15our $server_software;
16our $server_version;
17
18sub BEGIN {
19 $server_software = $ENV{'SERVER_SOFTWARE'};
20
21 if (defined $server_software) {
22 if ($server_software =~ m/^Microsoft-IIS\/(.*)$/) {
23 $server_version = $1;
24 }
25 }
26}
27
28
29sub prenew {
30 my $class = shift @_;
31
32 my $version;
33 if (-e "gsdl3site.cfg") {
34 $version = 3;
35 } else {
36 $version = 2;
37 }
38
39 my $self = {};
40
41 if ($version == 2) {
42 $self->{'site_filename'} = "gsdlsite.cfg";
43 $self->{'greenstone_version'} = 2;
44 }
45 elsif ($version == 3) {
46 $self->{'site_filename'} = "gsdl3site.cfg";
47 $self->{'greenstone_version'} = 3;
48 }
49
50 my $bself = bless $self, $class;
51
52 $bself->setup_gsdl();
53
54 return $bself;
55}
56
57
58sub new {
59 my $class = shift @_;
60
61 my $self;
62
63 # We'll determine the correct config file in this constructor itself
64 # and use it to determine the Greenstone server's version.
65 # Perhaps later, another test can be used for finding out what version
66 # of the Greenstone server we are working with.
67 my $version;
68 if (-e "gsdl3site.cfg") {
69 $version = 3;
70 } else {
71 $version = 2;
72 }
73
74 # POST that is URL-encoded (like a GET) is a line that needs to be read from STDIN
75 if ((defined $ENV{'CONTENT_TYPE'}) && ($ENV{'CONTENT_TYPE'} =~ m/form-urlencoded/)) {
76 my $line = <STDIN>;
77 if ((defined $line) && ($line ne "")) {
78 $self = new CGI($line);
79 }
80 }
81
82 # If the conditions above did not hold, then self=new CGI(@_)
83 if (!defined $self) {
84 # It's a GET, or else a POST with Multi-part body
85 $self = new CGI(@_);
86 }
87
88
89 if ($version == 2) {
90 $self->{'site_filename'} = "gsdlsite.cfg";
91 $self->{'greenstone_version'} = 2;
92 }
93 elsif ($version == 3) {
94 $self->{'site_filename'} = "gsdl3site.cfg";
95 $self->{'greenstone_version'} = 3;
96 }
97
98 return bless $self, $class;
99}
100
101
102sub parse_cgi_args
103{
104 my $self = shift @_;
105 my $xml = (defined $self->param("xml")) ? 1 : 0;
106
107 $self->{'xml'} = $xml;
108
109 my @var_names = $self->param;
110 my @arg_list = ();
111 foreach my $n ( @var_names ) {
112 my $arg = "$n=";
113 my $val = $self->param($n);
114 $arg .= $val if (defined $val);
115 push(@arg_list,$arg);
116 }
117
118 $self->{'args'} = join("&",@arg_list);
119}
120
121
122sub clean_param
123{
124 my $self = shift @_;
125 my ($param) = @_;
126
127 my $val = $self->SUPER::param($param);
128 $val =~ s/[\r\n]+$// if (defined $val);
129
130 return $val;
131}
132
133sub safe_val
134{
135 my $self = shift @_;
136 my ($val) = @_;
137
138 # convert any encoded entities to true form
139 $val =~ s/&amp;/&/osg;
140 $val =~ s/&lt;/</osg;
141 $val =~ s/&gt;/>/osg;
142 $val =~ s/&quot;/\"/osg;
143 $val =~ s/&nbsp;/ /osg;
144
145
146 # ensure only alpha-numeric plus a few other special chars remain
147
148 $val =~ s/[^[:alnum:]@\.\/\- :_]//g if (defined $val);
149
150 return $val;
151}
152
153sub generate_message
154{
155 my $self = shift @_;
156 my ($message) = @_;
157
158
159 binmode(STDOUT,":utf8");
160 print STDOUT "Content-type:text/plain\n\n$message";
161}
162
163sub generate_error
164{
165 my $self = shift @_;
166 my ($mess) = @_;
167
168 my $xml = $self->{'xml'};
169
170 my $full_mess;
171 my $args = $self->{'args'};
172
173 if ($xml) {
174 # Make $args XML safe
175 my $args_xml_safe = $args;
176 $args_xml_safe =~ s/&/&amp;/g;
177
178 $full_mess = "<Error>\n";
179 $full_mess .= " $mess\n";
180 $full_mess .= " CGI args were: $args_xml_safe\n";
181 $full_mess .= "</Error>\n";
182 }
183 else {
184 $full_mess = "ERROR: $mess\n ($args)\n";
185 }
186
187 $self->generate_message($full_mess);
188
189 die $full_mess;
190}
191
192sub generate_warning
193{
194 my $self = shift @_;
195 my ($mess) = @_;
196
197 my $xml = $self->{'xml'};
198
199 my $full_mess;
200 my $args = $self->{'args'};
201
202 if ($xml) {
203 # Make $args XML safe
204 my $args_xml_safe = $args;
205 $args_xml_safe =~ s/&/&amp;/g;
206
207 $full_mess = "<Warning>\n";
208 $full_mess .= " $mess\n";
209 $full_mess .= " CGI args were: $args_xml_safe\n";
210 $full_mess .= "</Warning>\n";
211 }
212 else {
213 $full_mess = "Warning: $mess ($args)\n";
214 }
215
216 $self->generate_message($full_mess);
217
218 print STDERR $full_mess;
219}
220
221
222sub generate_ok_message
223{
224 my $self = shift @_;
225 my ($mess) = @_;
226
227 my $xml = $self->{'xml'};
228
229 my $full_mess;
230
231 if ($xml) {
232 $full_mess = "<Accepted>\n";
233 $full_mess .= " $mess\n";
234 $full_mess .= "</Accepted>\n";
235 }
236 else {
237 $full_mess = "$mess";
238 }
239
240 $self->generate_message($full_mess);
241}
242
243
244# Note, this can only be called while we are still in the cgi directory, not after chdir to gsdl.
245sub get_config_info {
246 my $self = shift @_;
247 my ($infotype, $optional) = @_;
248
249 if (! defined $self->{'config_file_content'}) {
250 my $site_filename = $self->{'site_filename'};
251 open (FILEIN, "<$site_filename")
252 || $self->generate_error("Could not open $site_filename, to read $infotype");
253
254 my $config_content = "";
255 while(defined (my $line = <FILEIN>)) {
256 $config_content .= $line;
257 }
258 close(FILEIN);
259 $self->{'config_file_content'} = $config_content;
260 }
261
262# my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
263 my ($loc) = ($self->{'config_file_content'} =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
264 $loc =~ s/\"//g if defined $loc;
265
266 if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
267 if((!defined $optional) || (!$optional)) {
268 $self->generate_error("$infotype is not set in $self->{'site_filename'}");
269 }
270 }
271
272 return $loc;
273}
274
275sub get_gsdl3_src_home{
276 my $self = shift @_;
277 if (defined $self->{'gsdl3srchome'}) {
278 return $self->{'gsdl3srchome'};
279 }
280
281 my $gsdl3srchome = $self->get_config_info("gsdl3srchome");
282
283 if(defined $gsdl3srchome) {
284 $gsdl3srchome =~ s/(\/|\\)$//; # remove trailing slash
285 }
286 $self->{'gsdl3srchome'} = $gsdl3srchome;
287
288 return $gsdl3srchome;
289}
290
291
292sub get_gsdl_home {
293 my $self = shift @_;
294
295 if (defined $self->{'gsdlhome'}) {
296 return $self->{'gsdlhome'};
297 }
298
299 my $gsdlhome = $self->get_config_info("gsdlhome");
300
301 $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
302
303 $self->{'gsdlhome'} = $gsdlhome;
304
305 return $gsdlhome;
306}
307
308sub get_gsdl3_home {
309 my $self = shift @_;
310 my ($optional) = @_;
311
312 if (defined $self->{'gsdl3home'}) {
313 return $self->{'gsdl3home'};
314 }
315
316 my $gsdl3home = $self->get_config_info("gsdl3home", $optional);
317
318 if(defined $gsdl3home) {
319 $gsdl3home =~ s/(\/|\\)$//; # remove trailing slash
320 $self->{'gsdl3home'} = $gsdl3home;
321 }
322 return $gsdl3home;
323}
324
325sub get_java_home {
326 my $self = shift @_;
327 my ($optional) = @_;
328
329 if (defined $self->{'javahome'}) {
330 return $self->{'javahome'};
331 }
332
333 my $javahome = $self->get_config_info("javahome", $optional);
334 if(defined $javahome) {
335 $javahome =~ s/(\/|\\)$//; # remove trailing slash
336 $self->{'javahome'} = $javahome;
337 }
338 return $javahome;
339}
340
341sub get_perl_path {
342 my $self = shift @_;
343 my ($optional) = @_;
344
345 if (defined $self->{'perlpath'}) {
346 return $self->{'perlpath'};
347 }
348
349 my $perlpath = $self->get_config_info("perlpath", $optional);
350
351 if(defined $perlpath) {
352 $perlpath =~ s/(\/|\\)$//; # remove trailing slash
353 $self->{'perlpath'} = $perlpath;
354 }
355 return $perlpath;
356}
357
358sub get_gsdl_os {
359 my $self = shift @_;
360
361 my $os = $^O;
362
363 if ($os =~ m/linux/i) {
364 return "linux";
365 }
366 elsif ($os =~ m/mswin/i) {
367 return "windows";
368 }
369 elsif ($os =~ m/macos/i) {
370 return "darwin";
371 }
372 else {
373 # return as is.
374 return $os;
375 }
376}
377
378sub get_library_url_suffix {
379 my $self = shift @_;
380
381 if (defined $self->{'library_url_suffix'}) {
382 return $self->{'library_url_suffix'};
383 }
384
385 my $optional = 1; # ignore absence of gwcgi if not found
386 my $library_url = $self->get_config_info("gwcgi", $optional);
387 if(defined $library_url) {
388 $library_url =~ s/(\/|\\)$//; # remove trailing slash
389 }
390 else {
391
392 if($self->{'greenstone_version'} == 2) {
393 $library_url = $self->get_config_info("httpprefix", $optional);
394 $library_url = "/greenstone" unless defined $library_url;
395 $library_url = "$library_url/cgi-bin/library.cgi"; # same extension for linux and windows
396 }
397 else { # greenstone 3 or later and gwcgi not defined
398 $library_url = "/greenstone3"; #"/greenstone3/library";
399 }
400 }
401
402 $self->{'library_url_suffix'} = $library_url;
403 return $library_url;
404}
405
406sub get_default_servlet {
407 my $self = shift @_;
408
409 if (defined $self->{'default_servlet'} ){
410 return $self->{'default_servlet'};
411 }
412
413 my $optional = 1; # allows for absence of the field
414 $self->{'default_servlet'} = $self->get_config_info("defaultservlet", $optional);
415 if (!defined $self->{'default_servlet'}) # there was no config param
416 {
417 $self->{'default_servlet'} = ""; # so we don't need to look it up from the file next time
418 }
419 return $self->{'default_servlet'};
420}
421sub setup_fedora_homes {
422 my $self = shift @_;
423 my ($optional) = @_;
424
425 # The following will still allow the FEDORA_HOME and FEDORA_VERSION environment
426 # variables to have been set outside the gsdlsite.cfg file. Existing env vars
427 # are only overwritten if they've *also* been defined in gsdlsite.cfg.
428
429 if (!defined $self->{'fedora_home'}) # Don't need to go through it all again if we'd already done this before
430 {
431 # First look in the gsdlsite.cfg file for the fedora properties to be defined
432 # and set $ENV{FEDORA_HOME} and $ENV{FEDORA_VERSION} if values were provided
433 $self->{'fedora_home'} = $self->get_config_info("fedorahome", $optional);
434
435 if (defined $self->{'fedora_home'}) {
436 $ENV{'FEDORA_HOME'} = $self->{'fedora_home'};
437 }
438 elsif (defined $ENV{'FEDORA_HOME'}) { # check environment variable
439 $self->{'fedora_home'} = $ENV{'FEDORA_HOME'};
440 }
441
442 # if FEDORA_HOME is now defined, we can look for the fedora version that is being used
443 if (defined $ENV{'FEDORA_HOME'})
444 {
445 # first look in the file
446 $self->{'fedora_version'} = $self->get_config_info("fedoraversion", $optional);
447
448 if (defined $self->{'fedora_version'}) {
449 $ENV{'FEDORA_VERSION'} = $self->{'fedora_version'};
450 }
451 elsif (defined $ENV{'FEDORA_VERSION'}) { # then check environment variable
452 $self->{'fedora_version'} = $ENV{'FEDORA_VERSION'};
453 }
454 else { # finally, default to version 3 and warn the user
455 $ENV{'FEDORA_VERSION'} = "3";
456 $self->{'fedora_version'} = $ENV{'FEDORA_VERSION'};
457 #$self->generate_ok_message("FEDORA_HOME is set, but not FEDORA_VERSION, defaulted to: 3.");
458 }
459 }
460 }
461}
462
463# sets optional customisable values to do with Open Office
464sub setup_openoffice {
465 my $self = shift @_;
466 my ($optional) = @_;
467
468 if (!defined $self->{'soffice_home'}) # Don't need to go through it all again if we'd already done this before
469 {
470 # Look in gsdlsite.cfg for whether the openoffice
471 # and jodconverter properties have been defined
472 $self->{'soffice_home'} = $self->get_config_info("soffice_home", $optional);
473 $self->{'soffice_host'} = $self->get_config_info("soffice_host", $optional);
474 $self->{'soffice_port'} = $self->get_config_info("soffice_port", $optional);
475 $self->{'jodconverter_port'} = $self->get_config_info("jodconverter_port", $optional);
476
477 if (defined $self->{'soffice_home'}) {
478 $ENV{'SOFFICE_HOME'} = $self->{'soffice_home'};
479 }
480 if (defined $self->{'soffice_host'}) {
481 $ENV{'SOFFICE_HOST'} = $self->{'soffice_host'};
482 }
483 if (defined $self->{'soffice_port'}) {
484 $ENV{'SOFFICE_PORT'} = $self->{'soffice_port'};
485 }
486 if (defined $self->{'jodconverter_port'}) {
487 $ENV{'JODCONVERTER_PORT'} = $self->{'jodconverter_port'};
488 }
489 }
490}
491
492sub setup_gsdl {
493 my $self = shift @_;
494 my $optional = 1; # ignore absence of specified properties in gsdl(3)site.cfg if not found
495
496 my $gsdlhome = $self->get_gsdl_home();
497 my $gsdlos = $self->get_gsdl_os();
498 $ENV{'GSDLHOME'} = $gsdlhome;
499 $ENV{'GSDLOS'} = $gsdlos;
500
501 if (defined $server_software) {
502 if ($server_software =~ m/^Microsoft-IIS/) {
503 # Printing to STDERR, by default, goes to the web page in IIS
504 # Send it instead to Greenstone's error.txt
505
506 my $error_filename = "$gsdlhome/etc/error.txt"; # OK for Windows
507 open STDERR, ">> $error_filename"
508 or die "Can't write to $error_filename: $!\n";
509 binmode STDERR;
510 }
511 }
512
513 my $library_url = $self->get_library_url_suffix(); # best to have GSDLOS set beforehand
514 $self->{'library_url_suffix'} = $library_url;
515
516 my $cgibin = "cgi-bin/$ENV{'GSDLOS'}";
517 $cgibin = $cgibin.$ENV{'GSDLARCH'} if defined $ENV{'GSDLARCH'};
518
519 unshift(@INC, "$ENV{'GSDLHOME'}/$cgibin"); # This is OK on Windows
520 unshift(@INC, "$ENV{'GSDLHOME'}/perllib");
521 unshift(@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
522 unshift(@INC, "$ENV{'GSDLHOME'}/perllib/cgiactions");
523
524 require util;
525
526 if($self->{'greenstone_version'} == 3) {
527 my $gsdl3srchome = $self->get_gsdl3_src_home();
528 $ENV{'GSDL3SRCHOME'} = $gsdl3srchome;
529
530 my $gsdl3home = $self->get_gsdl3_home($optional);
531 # if a specific location for GS3's web folder is not provided,
532 # assume the GS3 web folder is in the default location
533 if(!defined $gsdl3home) {
534 $gsdl3home = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "web");
535 $self->{'gsdl3home'} = $gsdl3home;
536 }
537 $ENV{'GSDL3HOME'} = $gsdl3home;
538
539 }
540
541 my $gsdl_bin_script = &FileUtils::filenameConcatenate($gsdlhome,"bin","script");
542 &util::envvar_prepend("PATH",$gsdl_bin_script);
543
544 my $gsdl_bin_os = &FileUtils::filenameConcatenate($gsdlhome,"bin",$gsdlos);
545 &util::envvar_prepend("PATH",$gsdl_bin_os);
546
547 # set up ImageMagick for the remote server in parallel to what setup.bash does
548 my $magick_home = &FileUtils::filenameConcatenate($gsdl_bin_os,"imagemagick");
549 if(-e $magick_home) {
550 &util::envvar_prepend("PATH", $magick_home);
551
552 # Doesn't look like 'bin' and 'lib' are used for Windows version anymore,
553 # but that might just be one particular installation pattern, and there's
554 # no harm (that I can see) in keeping them in
555
556 my $magick_bin = &FileUtils::filenameConcatenate($magick_home,"bin");
557 my $magick_lib = &FileUtils::filenameConcatenate($magick_home,"lib");
558
559 &util::envvar_prepend("PATH", $magick_bin);
560
561 if(!defined $ENV{'MAGICK_HOME'} || $ENV{'MAGICK_HOME'} eq "") {
562 $ENV{'MAGICK_HOME'} = $magick_home;
563 }
564
565 if($gsdlos eq "linux") {
566 &util::envvar_prepend("LD_LIBRARY_PATH", $magick_lib);
567 } elsif ($gsdlos eq "darwin") {
568 &util::envvar_prepend("DYLD_LIBRARY_PATH", $magick_lib);
569 }
570
571 }
572
573 # set up GhostScript for the remote server in parallel to what setup.bash does
574 my $ghostscript_home = &FileUtils::filenameConcatenate($gsdl_bin_os,"ghostscript");
575 if(-e $ghostscript_home) {
576 my $ghostscript_bin = &FileUtils::filenameConcatenate($ghostscript_home,"bin");
577 &util::envvar_prepend("PATH", $ghostscript_bin);
578
579 if(!defined $ENV{'GS_LIB'} || $ENV{'GS_LIB'} eq "") {
580 $ENV{'GS_LIB'} = &FileUtils::filenameConcatenate($ghostscript_home,"share","ghostscript","8.63","lib");
581 }
582 if(!defined $ENV{'GS_FONTPATH'} || $ENV{'GS_FONTPATH'} eq "") {
583 $ENV{'GS_FONTPATH'} = &FileUtils::filenameConcatenate($ghostscript_home,"share","ghostscript","8.63","Resource","Font");
584 }
585 }
586
587 # If the "perlpath" property is set in the gsdl(3)site.cfg config file, it is
588 # prepended to PATH only if the same perl bin dir path is not already on PATH env
589 my $perl_bin_dir = $self->get_perl_path($optional);
590 if(defined $perl_bin_dir)
591 {
592 &util::envvar_prepend("PATH", $perl_bin_dir);
593
594 #my ($perl_home) = ($perl_bin_dir =~ m/(.*)[\\|\/]bin[\\|\/]?$/);
595 my ($tailname,$perl_home) = File::Basename::fileparse($perl_bin_dir, "\\.(?:[^\\.]+?)\$");
596 $ENV{'PERL5LIB'} = &FileUtils::filenameConcatenate($perl_home, "lib");
597
598 # add vendor\lib if it exists to PERL5LIB
599 # Strawberry Perl has a perl\vendor\lib folder. Check for it, if it exists add it to PATH for windows
600 # (Windows adds paths to library/dll files to PATH)
601 my $vendor_lib = &FileUtils::filenameConcatenate($perl_home, "vendor", "lib");
602 if(FileUtils::fileExists($vendor_lib)) {
603 &util::envvar_prepend("PATH", $vendor_lib) if $gsdlos eq "windows";
604 &util::envvar_append("PERL5LIB", $vendor_lib);
605 }
606
607 if($gsdlos eq "darwin") {
608 &util::envvar_prepend("DYLD_LIBRARY_PATH", &FileUtils::filenameConcatenate($perl_home,"5.8.9","darwin-thread-multi-2level","CORE"));
609 } elsif($gsdlos eq "linux") {
610 &util::envvar_prepend("LD_LIBRARY_PATH", &FileUtils::filenameConcatenate($perl_home,"5.8.9","i686-linux-thread-multi","CORE"));
611 }
612 }
613 elsif ($gsdlos eq "windows")
614 {
615 # Perl comes installed with the GS Windows Release Kit. However, note that if GS
616 # is from SVN, the user must have their own Perl and put it on the PATH or set
617 # perlpath in the gsdl site config file.
618 $perl_bin_dir = &FileUtils::filenameConcatenate($gsdlhome, "bin", "windows", "perl", "bin");
619 if(-e $perl_bin_dir) {
620 &util::envvar_append("PATH", $perl_bin_dir);
621
622 my ($tailname,$perl_home) = File::Basename::fileparse($perl_bin_dir, "\\.(?:[^\\.]+?)\$");
623 my $vendor_lib = &FileUtils::filenameConcatenate($perl_home, "vendor", "lib");
624 if(FileUtils::fileExists($vendor_lib)) {
625 &util::envvar_prepend("PATH", $vendor_lib) if $gsdlos eq "windows";
626 &util::envvar_append("PERL5LIB", $vendor_lib);
627 }
628 }
629 }
630
631 # If javahome is explicitly set in the gsdl site config file then it will override
632 # any env variable JAVA_HOME. A GS2 server does not set JAVA_HOME, though java is on
633 # the path. Therefore, if Fedora is being used for FLI with GS2, then javahome must
634 # be set in gsdlsite.cfg or the JAVA_HOME env var must be explicitly set by the user.
635 my $java_home = $self->get_java_home($optional);
636 if(defined $java_home) {
637 $ENV{'JAVA_HOME'} = $java_home;
638 }
639
640
641 # Process any extension setup.pl files
642 my @ext_homes = ();
643
644 my $gsdl_ext_home = &FileUtils::filenameConcatenate($gsdlhome,"ext");
645 push(@ext_homes,$gsdl_ext_home);
646
647 if ($self->{'greenstone_version'} == 3) {
648 my $gsdl3srchome = $self->get_gsdl3_src_home();
649 my $gsdl3_ext_home = &FileUtils::filenameConcatenate($gsdl3srchome,"ext");
650 push(@ext_homes,$gsdl3_ext_home);
651 }
652
653 # Don't pass the arguments to gliserver.pl (e.g. cmd=check-installation) to Greenstone extensions' setup files
654 print STDERR "Args: " . join(",", @ARGV)."\n";
655 my @saved_args = @ARGV;
656 if (scalar(@ARGV>0)) {
657 @ARGV=();
658 }
659
660 foreach my $ext_home (@ext_homes) {
661 # Should really think about making this a subroutine
662
663 if (opendir(EXTDIR,$ext_home) ) {
664 my @pot_ext_dir = grep { $_ !~ m/^\./ } readdir(EXTDIR);
665
666 closedir(EXTDIR);
667
668 foreach my $ed (@pot_ext_dir) {
669 my $full_ext_dir = &FileUtils::filenameConcatenate($ext_home,$ed);
670
671 if (-d $full_ext_dir) {
672
673 my $full_ext_perllib_dir = &FileUtils::filenameConcatenate($full_ext_dir,"perllib");
674 if (-d $full_ext_perllib_dir) {
675 unshift (@INC, $full_ext_perllib_dir);
676 }
677
678 my $full_inc_file = &FileUtils::filenameConcatenate($full_ext_dir,
679 "$ed-setup.pl");
680 if (-f $full_inc_file) {
681
682 my $store_cwd = Cwd::cwd();
683
684 chdir($full_ext_dir);
685 require "./$ed-setup.pl";
686 chdir($store_cwd);
687 }
688 }
689 }
690 }
691 }
692
693 # restore the args to gliserver.pl
694 @ARGV = @saved_args;
695 print STDERR "Args: " . join(",", @ARGV)."\n";
696
697 # FEDORA_HOME and FEDORA_VERSION are needed (by scripts g2f-import and g2f-buildcol).
698 $self->setup_fedora_homes($optional);
699
700
701 # Check for any customisations to Open-Office if on Windows
702 if ($gsdlos eq "windows") {
703 $self->setup_openoffice($optional);
704 }
705
706 # If perl_perturb_keys and related perl_hash_seed aren't set, then search results
707 # with remote GS return different documents from the ones that should be returned
708 $ENV{'PERL_PERTURB_KEYS'}=0;
709 $ENV{'PERL_HASH_SEED'}=0;
710 $ENV{'WGETRC'}=&FileUtils::filenameConcatenate($gsdlhome,"bin",$gsdlos,"wgetrc");
711}
712
713sub greenstone_version {
714 my $self = shift @_;
715 return $self->{'greenstone_version'};
716}
717
718sub library_url_suffix {
719 my $self = shift @_;
720 return $self->{'library_url_suffix'};
721}
722
723# Only useful to call this after calling setup_gsdl, as it uses some environment variables
724# Returns the Greenstone collect directory, or a specific collection directory inside collect
725sub get_collection_dir {
726 my $self = shift @_;
727 my ($site, $collection) = @_; # both may be undefined
728
729 my $collection_directory;
730 if($self->{'greenstone_version'} == 2 && defined $ENV{'GSDLHOME'}) {
731 if(defined $collection) {
732 $collection_directory = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "collect", $collection);
733 } else {
734 $collection_directory = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "collect");
735 }
736 }
737 elsif($self->{'greenstone_version'} == 3) {
738 if(defined $ENV{'GSDL3HOME'}) {
739 if(defined $collection) {
740 $collection_directory = &FileUtils::filenameConcatenate($ENV{'GSDL3HOME'}, "sites", $site, "collect", $collection);
741 } else {
742 $collection_directory = &FileUtils::filenameConcatenate($ENV{'GSDL3HOME'}, "sites", $site, "collect");
743 }
744 }
745 elsif(defined $ENV{'GSDL3SRCHOME'}) {
746 if(defined $collection) {
747 $collection_directory = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "web", "sites", $site, "collect", $collection);
748 } else {
749 $collection_directory = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "web", "sites", $site, "collect");
750 }
751 }
752 }
753 return $collection_directory;
754}
755
756sub local_rm_r
757{
758 my $self = shift @_;
759 my ($local_dir) = @_;
760
761 my $prefix_dir = getcwd();
762 my $full_path = &FileUtils::filenameConcatenate($prefix_dir,$local_dir);
763
764 if ($prefix_dir !~ m/collect/) {
765 $self->generate_error("Trying to delete outside of Greenstone collect: $full_path");
766 }
767
768 # Delete recursively
769 if (!-e $full_path) {
770 $self->generate_error("File/Directory does not exist: $full_path");
771 }
772
773 &FileUtils::removeFilesRecursive($full_path);
774}
775
776
777sub get_java_path()
778{
779 # Check the JAVA_HOME environment variable first
780 if (defined $ENV{'JAVA_HOME'}) {
781 my $java_home = $ENV{'JAVA_HOME'};
782 $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
783 return &FileUtils::filenameConcatenate($java_home, "bin", "java");
784 }
785
786 elsif (defined $ENV{'JRE_HOME'}) {
787 my $jre_home = $ENV{'JRE_HOME'};
788 $jre_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
789 return &FileUtils::filenameConcatenate($jre_home, "bin", "java");
790 }
791
792 # Hope that Java is on the PATH
793 return "java";
794}
795
796
797sub check_java_home()
798{
799 # Return a warning unless the JAVA_HOME environment variable is set
800 if (!defined $ENV{'JAVA_HOME'}) {
801 return "JAVA_HOME environment variable not set. Might not be able to find Java unless in PATH (" . $ENV{'PATH'} . ")";
802 }
803
804 return "";
805}
806
807
808sub checked_chdir
809{
810 my $self = shift @_;
811 my ($dir) = @_;
812
813 if (!-e $dir) {
814 $self->generate_error("Directory '$dir' does not exist");
815 }
816
817 chdir $dir
818 || $self->generate_error("Unable to change to directory: $dir");
819}
820
821# used with old GS3 authentication
822sub rot13()
823{
824 my $self = shift @_;
825 my ($password)=@_;
826 my @password_arr=split(//,$password);
827
828 my @encrypt_password;
829 foreach my $str (@password_arr){
830 my $char=unpack("c",$str);
831 if ($char>=97 && $char<=109){
832 $char+=13;
833 }elsif ($char>=110 && $char<=122){
834 $char-=13;
835 }elsif ($char>=65 && $char<=77){
836 $char+=13;
837 }elsif ($char>=78 && $char<=90){
838 $char-=13;
839 }
840 $char=pack("c",$char);
841 push(@encrypt_password,$char);
842 }
843 return join("",@encrypt_password);
844}
845
846# used along with new GS3 authentication
847sub hash_pwd()
848{
849 my $self = shift @_;
850 my ($password)=@_;
851
852 my $gsdl3srchome = $ENV{'GSDL3SRCHOME'};
853
854 my $java = get_java_path();
855 my $java_gsdl3_classpath = &FileUtils::filenameConcatenate($gsdl3srchome, "web", "WEB-INF", "lib", "gsdl3.jar");
856 my $java_remaining_classpath = &FileUtils::filenameConcatenate($gsdl3srchome, "web", "WEB-INF", "lib", "*"); # log4j etc
857 my $java_classpath;
858 my $gsdlos = $ENV{'GSDLOS'};
859 if ($gsdlos !~ m/windows/){
860 $java_classpath = $java_gsdl3_classpath . ":" . $java_remaining_classpath;
861 }else{
862 $java_classpath = $java_gsdl3_classpath . ";" . $java_remaining_classpath;
863 } # can't use util::envvar_prepend(), since the $java_classpath here is not a $ENV type env variable
864
865 my $java_command="\"$java\" -classpath \"$java_classpath\" org.greenstone.gsdl3.service.Authentication \"$password\""; # 2>&1";
866 my $hashedpwd = `$java_command`;
867
868 return $hashedpwd;
869}
870
871sub encrypt_key
872{
873 my $self = shift @_;
874
875 # I think the encryption method used on the key may be the same for GS3 and GS2
876 # (The encryption method used on the pw definitely differs between the two GS versions)
877 if (defined $self->param("ky")) {
878 require "$self->{'gsdlhome'}/perllib/cpan/Crypt/UnixCrypt.pm"; # This is OK on Windows
879 $self->param('-name' => "ky", '-value' => &Crypt::UnixCrypt::crypt($self->clean_param("ky"), "Tp"));
880 }
881}
882
883sub encrypt_password
884{
885 my $self = shift @_;
886
887 if (defined $self->param("pw")) { ##
888 if ($self->{'greenstone_version'} == 3) { # GS3 is in Java, so needs different encryption
889 #$self->param('-name' => "pw", '-value' => $self->rot13($self->clean_param("pw"))); ## when using old GS3 authentication
890
891 my $hashedPwd = $self->hash_pwd($self->clean_param("pw")); # for GS3's new Authentication
892 $self->param('-name' => "pw", '-value' => $hashedPwd);
893 }
894 else { # GS2 (and versions of GS other than 3?)
895 #require "$self->{'gsdlhome'}/perllib/util.pm"; # This is OK on Windows
896 require "$self->{'gsdlhome'}/perllib/cpan/Crypt/UnixCrypt.pm"; # This is OK on Windows
897 $self->param('-name' => "pw", '-value' => &Crypt::UnixCrypt::crypt($self->clean_param("pw"), "Tp"));
898 }
899 }
900}
901
902
903sub decode {
904 my ($self, $text) = @_;
905 $text =~ s/\+/ /g;
906 $text = &MIME::Base64::decode_base64($text);
907
908 return $text;
909}
910
9111;
912
Note: See TracBrowser for help on using the repository browser.