[7956] | 1 | package gsdlCGI;
|
---|
| 2 |
|
---|
[16467] | 3 | # This file merges Michael Dewsnip's gsdlCGI.pm for GS2 and Quan Qiu's gsdlCGI4gs3.pm (GS3)
|
---|
[14024] | 4 |
|
---|
[16467] | 5 | use strict;
|
---|
| 6 | no strict 'subs';
|
---|
| 7 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[14024] | 8 |
|
---|
[7956] | 9 | use CGI;
|
---|
[10206] | 10 | use Cwd;
|
---|
[19141] | 11 | use MIME::Base64;
|
---|
[7956] | 12 |
|
---|
[16467] | 13 | @gsdlCGI::ISA = ( 'CGI' );
|
---|
[7956] | 14 |
|
---|
[23468] | 15 | our $server_software;
|
---|
| 16 | our $server_version;
|
---|
| 17 |
|
---|
| 18 | sub 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 |
|
---|
[23088] | 29 | sub 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 |
|
---|
[7956] | 58 | sub new {
|
---|
| 59 | my $class = shift @_;
|
---|
[16467] | 60 |
|
---|
[23196] | 61 | my $self;
|
---|
[16467] | 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 | }
|
---|
[7956] | 73 |
|
---|
[23196] | 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 | }
|
---|
[16467] | 81 |
|
---|
[23196] | 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 | }
|
---|
[7956] | 87 |
|
---|
[23070] | 88 |
|
---|
[16467] | 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 |
|
---|
[10583] | 98 | return bless $self, $class;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | sub parse_cgi_args
|
---|
| 103 | {
|
---|
| 104 | my $self = shift @_;
|
---|
[7956] | 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 |
|
---|
| 122 | sub 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 |
|
---|
| 133 | sub safe_val
|
---|
| 134 | {
|
---|
| 135 | my $self = shift @_;
|
---|
| 136 | my ($val) = @_;
|
---|
| 137 |
|
---|
[20573] | 138 | # convert any encoded entities to true form
|
---|
| 139 | $val =~ s/&/&/osg;
|
---|
| 140 | $val =~ s/</</osg;
|
---|
| 141 | $val =~ s/>/>/osg;
|
---|
| 142 | $val =~ s/"/\"/osg;
|
---|
| 143 | $val =~ s/ / /osg;
|
---|
| 144 |
|
---|
| 145 |
|
---|
[7956] | 146 | # ensure only alpha-numeric plus a few other special chars remain
|
---|
| 147 |
|
---|
[9941] | 148 | $val =~ s/[^[:alnum:]@\.\/\- :_]//g if (defined $val);
|
---|
[7956] | 149 |
|
---|
| 150 | return $val;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[16467] | 153 | sub generate_message
|
---|
| 154 | {
|
---|
| 155 | my $self = shift @_;
|
---|
| 156 | my ($message) = @_;
|
---|
[23756] | 157 |
|
---|
| 158 |
|
---|
| 159 | binmode(STDOUT,":utf8");
|
---|
| 160 | print STDOUT "Content-type:text/plain\n\n$message";
|
---|
[16467] | 161 | }
|
---|
[7956] | 162 |
|
---|
| 163 | sub 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) {
|
---|
[13167] | 174 | # Make $args XML safe
|
---|
| 175 | my $args_xml_safe = $args;
|
---|
| 176 | $args_xml_safe =~ s/&/&/g;
|
---|
| 177 |
|
---|
[7956] | 178 | $full_mess = "<Error>\n";
|
---|
| 179 | $full_mess .= " $mess\n";
|
---|
[13167] | 180 | $full_mess .= " CGI args were: $args_xml_safe\n";
|
---|
[7956] | 181 | $full_mess .= "</Error>\n";
|
---|
| 182 | }
|
---|
| 183 | else {
|
---|
[10565] | 184 | $full_mess = "ERROR: $mess\n ($args)\n";
|
---|
[7956] | 185 | }
|
---|
| 186 |
|
---|
[16467] | 187 | $self->generate_message($full_mess);
|
---|
[7956] | 188 |
|
---|
[16467] | 189 | die $full_mess;
|
---|
[7956] | 190 | }
|
---|
| 191 |
|
---|
[10206] | 192 | sub generate_warning
|
---|
| 193 | {
|
---|
| 194 | my $self = shift @_;
|
---|
| 195 | my ($mess) = @_;
|
---|
| 196 |
|
---|
| 197 | my $xml = $self->{'xml'};
|
---|
[7956] | 198 |
|
---|
[10206] | 199 | my $full_mess;
|
---|
| 200 | my $args = $self->{'args'};
|
---|
| 201 |
|
---|
| 202 | if ($xml) {
|
---|
[13167] | 203 | # Make $args XML safe
|
---|
| 204 | my $args_xml_safe = $args;
|
---|
| 205 | $args_xml_safe =~ s/&/&/g;
|
---|
| 206 |
|
---|
[10206] | 207 | $full_mess = "<Warning>\n";
|
---|
| 208 | $full_mess .= " $mess\n";
|
---|
[13167] | 209 | $full_mess .= " CGI args were: $args_xml_safe\n";
|
---|
[10206] | 210 | $full_mess .= "</Warning>\n";
|
---|
| 211 | }
|
---|
| 212 | else {
|
---|
| 213 | $full_mess = "Warning: $mess ($args)\n";
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[16467] | 216 | $self->generate_message($full_mess);
|
---|
[10206] | 217 |
|
---|
| 218 | print STDERR $full_mess;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 |
|
---|
[7956] | 222 | sub 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 {
|
---|
[20573] | 237 | $full_mess = "$mess";
|
---|
[7956] | 238 | }
|
---|
[16467] | 239 |
|
---|
| 240 | $self->generate_message($full_mess);
|
---|
[7956] | 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | sub get_config_info {
|
---|
| 246 | my $self = shift @_;
|
---|
[16467] | 247 | my ($infotype, $optional) = @_;
|
---|
[7956] | 248 |
|
---|
[16467] | 249 | my $site_filename = $self->{'site_filename'};
|
---|
[7956] | 250 | open (FILEIN, "<$site_filename")
|
---|
[16467] | 251 | || $self->generate_error("Could not open $site_filename");
|
---|
[7956] | 252 |
|
---|
| 253 | my $config_content = "";
|
---|
| 254 | while(defined (my $line = <FILEIN>)) {
|
---|
| 255 | $config_content .= $line;
|
---|
| 256 | }
|
---|
| 257 | close(FILEIN);
|
---|
| 258 |
|
---|
[10565] | 259 | my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
|
---|
[16467] | 260 | $loc =~ s/\"//g if defined $loc;
|
---|
[7956] | 261 |
|
---|
| 262 | if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
|
---|
[16467] | 263 | if((!defined $optional) || (!$optional)) {
|
---|
| 264 | $self->generate_error("$infotype is not set in $site_filename");
|
---|
| 265 | }
|
---|
[7956] | 266 | }
|
---|
| 267 |
|
---|
| 268 | return $loc;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[16467] | 271 | sub get_gsdl3_src_home{
|
---|
| 272 | my $self = shift @_;
|
---|
| 273 | if (defined $self->{'gsdl3srchome'}) {
|
---|
| 274 | return $self->{'gsdl3srchome'};
|
---|
| 275 | }
|
---|
[10206] | 276 |
|
---|
[16467] | 277 | my $gsdl3srchome = $self->get_config_info("gsdl3srchome");
|
---|
| 278 |
|
---|
| 279 | if(defined $gsdl3srchome) {
|
---|
| 280 | $gsdl3srchome =~ s/(\/|\\)$//; # remove trailing slash
|
---|
| 281 | }
|
---|
| 282 | $self->{'gsdl3srchome'} = $gsdl3srchome;
|
---|
| 283 |
|
---|
| 284 | return $gsdl3srchome;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 |
|
---|
[9941] | 288 | sub get_gsdl_home {
|
---|
| 289 | my $self = shift @_;
|
---|
[10206] | 290 |
|
---|
| 291 | if (defined $self->{'gsdlhome'}) {
|
---|
| 292 | return $self->{'gsdlhome'};
|
---|
| 293 | }
|
---|
[7956] | 294 |
|
---|
[9941] | 295 | my $gsdlhome = $self->get_config_info("gsdlhome");
|
---|
[7956] | 296 |
|
---|
[10206] | 297 | $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
|
---|
| 298 |
|
---|
| 299 | $self->{'gsdlhome'} = $gsdlhome;
|
---|
| 300 |
|
---|
[9941] | 301 | return $gsdlhome;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[16467] | 304 | sub get_gsdl3_home {
|
---|
| 305 | my $self = shift @_;
|
---|
| 306 | my ($optional) = @_;
|
---|
| 307 |
|
---|
| 308 | if (defined $self->{'gsdl3home'}) {
|
---|
| 309 | return $self->{'gsdl3home'};
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | my $gsdl3home = $self->get_config_info("gsdl3home", $optional);
|
---|
| 313 |
|
---|
| 314 | if(defined $gsdl3home) {
|
---|
| 315 | $gsdl3home =~ s/(\/|\\)$//; # remove trailing slash
|
---|
| 316 | $self->{'gsdl3home'} = $gsdl3home;
|
---|
| 317 | }
|
---|
| 318 | return $gsdl3home;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | sub get_java_home {
|
---|
| 322 | my $self = shift @_;
|
---|
| 323 | my ($optional) = @_;
|
---|
| 324 |
|
---|
| 325 | if (defined $self->{'javahome'}) {
|
---|
| 326 | return $self->{'javahome'};
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | my $javahome = $self->get_config_info("javahome", $optional);
|
---|
| 330 | if(defined $javahome) {
|
---|
| 331 | $javahome =~ s/(\/|\\)$//; # remove trailing slash
|
---|
| 332 | $self->{'javahome'} = $javahome;
|
---|
| 333 | }
|
---|
| 334 | return $javahome;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | sub get_perl_path {
|
---|
| 338 | my $self = shift @_;
|
---|
[16509] | 339 | my ($optional) = @_;
|
---|
[16467] | 340 |
|
---|
| 341 | if (defined $self->{'perlpath'}) {
|
---|
| 342 | return $self->{'perlpath'};
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[16509] | 345 | my $perlpath = $self->get_config_info("perlpath", $optional);
|
---|
[16467] | 346 |
|
---|
| 347 | if(defined $perlpath) {
|
---|
| 348 | $perlpath =~ s/(\/|\\)$//; # remove trailing slash
|
---|
| 349 | $self->{'perlpath'} = $perlpath;
|
---|
| 350 | }
|
---|
| 351 | return $perlpath;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[10206] | 354 | sub get_gsdl_os {
|
---|
[7956] | 355 | my $self = shift @_;
|
---|
[10206] | 356 |
|
---|
| 357 | my $os = $^O;
|
---|
[7956] | 358 |
|
---|
[10206] | 359 | if ($os =~ m/linux/i) {
|
---|
| 360 | return "linux";
|
---|
| 361 | }
|
---|
[16467] | 362 | elsif ($os =~ m/mswin/i) {
|
---|
[10206] | 363 | return "windows";
|
---|
| 364 | }
|
---|
[16467] | 365 | elsif ($os =~ m/macos/i) {
|
---|
[10206] | 366 | return "darwin";
|
---|
| 367 | }
|
---|
| 368 | else {
|
---|
| 369 | # return as is.
|
---|
| 370 | return $os;
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
[7956] | 373 |
|
---|
[16467] | 374 | sub get_library_url_suffix {
|
---|
| 375 | my $self = shift @_;
|
---|
| 376 |
|
---|
| 377 | if (defined $self->{'library_url_suffix'}) {
|
---|
| 378 | return $self->{'library_url_suffix'};
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | my $optional = 1; # ignore absence of gwcgi if not found
|
---|
| 382 | my $library_url = $self->get_config_info("gwcgi", $optional);
|
---|
| 383 | if(defined $library_url) {
|
---|
| 384 | $library_url =~ s/(\/|\\)$//; # remove trailing slash
|
---|
| 385 | }
|
---|
| 386 | else {
|
---|
| 387 |
|
---|
| 388 | if($self->{'greenstone_version'} == 2) {
|
---|
[16971] | 389 | $library_url = $self->get_config_info("httpprefix", $optional);
|
---|
[18967] | 390 | $library_url = "/greenstone" unless defined $library_url;
|
---|
[18966] | 391 | $library_url = "$library_url/cgi-bin/library.cgi"; # same extension for linux and windows
|
---|
[16467] | 392 | }
|
---|
| 393 | else { # greenstone 3 or later and gwcgi not defined
|
---|
| 394 | $library_url = "/greenstone3"; #"/greenstone3/library";
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | $self->{'library_url_suffix'} = $library_url;
|
---|
| 399 | return $library_url;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | sub setup_fedora_homes {
|
---|
| 403 | my $self = shift @_;
|
---|
| 404 | my ($optional) = @_;
|
---|
| 405 |
|
---|
| 406 | # The following will still allow the FEDORA_HOME and FEDORA_VERSION environment
|
---|
| 407 | # variables to have been set outside the gsdlsite.cfg file. Existing env vars
|
---|
| 408 | # are only overwritten if they've *also* been defined in gsdlsite.cfg.
|
---|
| 409 |
|
---|
| 410 | if (!defined $self->{'fedora_home'}) # Don't need to go through it all again if we'd already done this before
|
---|
| 411 | {
|
---|
| 412 | # First look in the gsdlsite.cfg file for the fedora properties to be defined
|
---|
| 413 | # and set $ENV{FEDORA_HOME} and $ENV{FEDORA_VERSION} if values were provided
|
---|
| 414 | $self->{'fedora_home'} = $self->get_config_info("fedorahome", $optional);
|
---|
| 415 |
|
---|
| 416 | if (defined $self->{'fedora_home'}) {
|
---|
| 417 | $ENV{'FEDORA_HOME'} = $self->{'fedora_home'};
|
---|
| 418 | }
|
---|
| 419 | elsif (defined $ENV{'FEDORA_HOME'}) { # check environment variable
|
---|
| 420 | $self->{'fedora_home'} = $ENV{'FEDORA_HOME'};
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | # if FEDORA_HOME is now defined, we can look for the fedora version that is being used
|
---|
| 424 | if (defined $ENV{'FEDORA_HOME'})
|
---|
| 425 | {
|
---|
| 426 | # first look in the file
|
---|
| 427 | $self->{'fedora_version'} = $self->get_config_info("fedoraversion", $optional);
|
---|
| 428 |
|
---|
| 429 | if (defined $self->{'fedora_version'}) {
|
---|
| 430 | $ENV{'FEDORA_VERSION'} = $self->{'fedora_version'};
|
---|
| 431 | }
|
---|
| 432 | elsif (defined $ENV{'FEDORA_VERSION'}) { # then check environment variable
|
---|
| 433 | $self->{'fedora_version'} = $ENV{'FEDORA_VERSION'};
|
---|
| 434 | }
|
---|
| 435 | else { # finally, default to version 3 and warn the user
|
---|
| 436 | $ENV{'FEDORA_VERSION'} = "3";
|
---|
| 437 | $self->{'fedora_version'} = $ENV{'FEDORA_VERSION'};
|
---|
| 438 | #$self->generate_ok_message("FEDORA_HOME is set, but not FEDORA_VERSION, defaulted to: 3.");
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[23732] | 444 | # sets optional customisable values to do with Open Office
|
---|
| 445 | sub setup_openoffice {
|
---|
| 446 | my $self = shift @_;
|
---|
| 447 | my ($optional) = @_;
|
---|
| 448 |
|
---|
| 449 | if (!defined $self->{'soffice_home'}) # Don't need to go through it all again if we'd already done this before
|
---|
| 450 | {
|
---|
[23734] | 451 | # Look in gsdlsite.cfg for whether the openoffice
|
---|
| 452 | # and jodconverter properties have been defined
|
---|
| 453 | $self->{'soffice_home'} = $self->get_config_info("soffice_home", $optional);
|
---|
| 454 | $self->{'soffice_host'} = $self->get_config_info("soffice_host", $optional);
|
---|
| 455 | $self->{'soffice_port'} = $self->get_config_info("soffice_port", $optional);
|
---|
| 456 | $self->{'jodconverter_port'} = $self->get_config_info("jodconverter_port", $optional);
|
---|
| 457 |
|
---|
| 458 | if (defined $self->{'soffice_home'}) {
|
---|
| 459 | $ENV{'SOFFICE_HOME'} = $self->{'soffice_home'};
|
---|
| 460 | }
|
---|
| 461 | if (defined $self->{'soffice_host'}) {
|
---|
| 462 | $ENV{'SOFFICE_HOST'} = $self->{'soffice_host'};
|
---|
| 463 | }
|
---|
| 464 | if (defined $self->{'soffice_port'}) {
|
---|
| 465 | $ENV{'SOFFICE_PORT'} = $self->{'soffice_port'};
|
---|
| 466 | }
|
---|
| 467 | if (defined $self->{'jodconverter_port'}) {
|
---|
| 468 | $ENV{'JODCONVERTER_PORT'} = $self->{'jodconverter_port'};
|
---|
| 469 | }
|
---|
[23732] | 470 | }
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[10206] | 473 | sub setup_gsdl {
|
---|
| 474 | my $self = shift @_;
|
---|
[16467] | 475 | my $optional = 1; # ignore absence of specified properties in gsdl(3)site.cfg if not found
|
---|
[7956] | 476 |
|
---|
[10212] | 477 | my $gsdlhome = $self->get_gsdl_home();
|
---|
| 478 | my $gsdlos = $self->get_gsdl_os();
|
---|
| 479 | $ENV{'GSDLHOME'} = $gsdlhome;
|
---|
| 480 | $ENV{'GSDLOS'} = $gsdlos;
|
---|
| 481 |
|
---|
[23468] | 482 | if (defined $server_software) {
|
---|
| 483 | if ($server_software =~ m/^Microsoft-IIS/) {
|
---|
| 484 | # Printing to STDERR, by default, goes to the web page in IIS
|
---|
| 485 | # Send it instead to Greenstone's error.txt
|
---|
| 486 |
|
---|
| 487 | my $error_filename = "$gsdlhome/etc/error.txt"; # OK for Windows
|
---|
| 488 | open STDERR, ">> $error_filename"
|
---|
| 489 | or die "Can't write to $error_filename: $!\n";
|
---|
| 490 | binmode STDERR;
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
[19055] | 493 |
|
---|
[16467] | 494 | my $library_url = $self->get_library_url_suffix(); # best to have GSDLOS set beforehand
|
---|
| 495 | $self->{'library_url_suffix'} = $library_url;
|
---|
| 496 |
|
---|
[24872] | 497 | my $cgibin = "cgi-bin/$ENV{'GSDLOS'}";
|
---|
| 498 | $cgibin = $cgibin.$ENV{'GSDLARCH'} if defined $ENV{'GSDLARCH'};
|
---|
| 499 |
|
---|
| 500 | unshift(@INC, "$ENV{'GSDLHOME'}/$cgibin"); # This is OK on Windows
|
---|
[19055] | 501 | unshift(@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
[19277] | 502 | unshift(@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
[23403] | 503 | unshift(@INC, "$ENV{'GSDLHOME'}/perllib/cgiactions");
|
---|
[19277] | 504 |
|
---|
[19055] | 505 | require util;
|
---|
[14973] | 506 |
|
---|
[16467] | 507 | if($self->{'greenstone_version'} == 3) {
|
---|
| 508 | my $gsdl3srchome = $self->get_gsdl3_src_home();
|
---|
| 509 | $ENV{'GSDL3SRCHOME'} = $gsdl3srchome;
|
---|
| 510 |
|
---|
| 511 | my $gsdl3home = $self->get_gsdl3_home($optional);
|
---|
| 512 | # if a specific location for GS3's web folder is not provided,
|
---|
| 513 | # assume the GS3 web folder is in the default location
|
---|
| 514 | if(!defined $gsdl3home) {
|
---|
| 515 | $gsdl3home = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web");
|
---|
| 516 | $self->{'gsdl3home'} = $gsdl3home;
|
---|
| 517 | }
|
---|
| 518 | $ENV{'GSDL3HOME'} = $gsdl3home;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 |
|
---|
[10212] | 522 | my $gsdl_bin_script = &util::filename_cat($gsdlhome,"bin","script");
|
---|
[23184] | 523 | &util::envvar_prepend("PATH",$gsdl_bin_script);
|
---|
[16467] | 524 |
|
---|
[10212] | 525 | my $gsdl_bin_os = &util::filename_cat($gsdlhome,"bin",$gsdlos);
|
---|
[23184] | 526 | &util::envvar_prepend("PATH",$gsdl_bin_os);
|
---|
[16467] | 527 |
|
---|
[23236] | 528 | # set up ImageMagick for the remote server in parallel to what setup.bash does
|
---|
| 529 | my $magick_home = &util::filename_cat($gsdl_bin_os,"imagemagick");
|
---|
| 530 | if(-e $magick_home) {
|
---|
[23756] | 531 | &util::envvar_prepend("PATH", $magick_home);
|
---|
| 532 |
|
---|
| 533 | # Doesn't look like 'bin' and 'lib' are used for Windows version anymore,
|
---|
| 534 | # but that might just be one particular installation pattern, and there's
|
---|
| 535 | # no harm (that I can see) in keeping them in
|
---|
| 536 |
|
---|
[23236] | 537 | my $magick_bin = &util::filename_cat($magick_home,"bin");
|
---|
| 538 | my $magick_lib = &util::filename_cat($magick_home,"lib");
|
---|
| 539 |
|
---|
[23756] | 540 | &util::envvar_prepend("PATH", $magick_bin);
|
---|
[23236] | 541 |
|
---|
| 542 | if(!defined $ENV{'MAGICK_HOME'} || $ENV{'MAGICK_HOME'} eq "") {
|
---|
| 543 | $ENV{'MAGICK_HOME'} = $magick_home;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | if($gsdlos eq "linux") {
|
---|
| 547 | &util::envvar_prepend("LD_LIBRARY_PATH", $magick_lib);
|
---|
[23796] | 548 | } elsif ($gsdlos eq "darwin") {
|
---|
[23236] | 549 | &util::envvar_prepend("DYLD_LIBRARY_PATH", $magick_lib);
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | # set up GhostScript for the remote server in parallel to what setup.bash does
|
---|
| 555 | my $ghostscript_home = &util::filename_cat($gsdl_bin_os,"ghostscript");
|
---|
| 556 | if(-e $ghostscript_home) {
|
---|
| 557 | my $ghostscript_bin = &util::filename_cat($ghostscript_home,"bin");
|
---|
| 558 | &util::envvar_prepend("PATH", $ghostscript_bin);
|
---|
| 559 |
|
---|
| 560 | if(!defined $ENV{'GS_LIB'} || $ENV{'GS_LIB'} eq "") {
|
---|
| 561 | $ENV{'GS_LIB'} = &util::filename_cat($ghostscript_home,"share","ghostscript","8.63","lib");
|
---|
| 562 | }
|
---|
| 563 | if(!defined $ENV{'GS_FONTPATH'} || $ENV{'GS_FONTPATH'} eq "") {
|
---|
| 564 | $ENV{'GS_FONTPATH'} = &util::filename_cat($ghostscript_home,"share","ghostscript","8.63","Resource","Font");
|
---|
| 565 | }
|
---|
| 566 | }
|
---|
| 567 |
|
---|
[16509] | 568 | # If the "perlpath" property is set in the gsdl(3)site.cfg config file, it is
|
---|
| 569 | # prepended to PATH only if the same perl bin dir path is not already on PATH env
|
---|
| 570 | my $perl_bin_dir = $self->get_perl_path($optional);
|
---|
[21804] | 571 | if(defined $perl_bin_dir)
|
---|
[16509] | 572 | {
|
---|
[21804] | 573 | &util::envvar_prepend("PATH", $perl_bin_dir);
|
---|
| 574 |
|
---|
| 575 | #my ($perl_home) = ($perl_bin_dir =~ m/(.*)[\\|\/]bin[\\|\/]?$/);
|
---|
| 576 | my ($tailname,$perl_home) = File::Basename::fileparse($perl_bin_dir, "\\.(?:[^\\.]+?)\$");
|
---|
| 577 | $ENV{'PERL5LIB'} = &util::filename_cat($perl_home, "lib");
|
---|
| 578 |
|
---|
| 579 | if($gsdlos eq "darwin") {
|
---|
| 580 | &util::envvar_prepend("DYLD_LIBRARY_PATH", &util::filename_cat($perl_home,"5.8.9","darwin-thread-multi-2level","CORE"));
|
---|
| 581 | } elsif($gsdlos eq "linux") {
|
---|
| 582 | &util::envvar_prepend("LD_LIBRARY_PATH", &util::filename_cat($perl_home,"5.8.9","i686-linux-thread-multi","CORE"));
|
---|
| 583 | }
|
---|
[16509] | 584 | }
|
---|
| 585 | elsif ($gsdlos eq "windows")
|
---|
| 586 | {
|
---|
| 587 | # Perl comes installed with the GS Windows Release Kit. However, note that if GS
|
---|
| 588 | # is from SVN, the user must have their own Perl and put it on the PATH or set
|
---|
| 589 | # perlpath in the gsdl site config file.
|
---|
[16467] | 590 | $perl_bin_dir = &util::filename_cat($gsdlhome, "bin", "windows", "perl", "bin");
|
---|
| 591 | if(-e $perl_bin_dir) {
|
---|
| 592 | &util::envvar_append("PATH", $perl_bin_dir);
|
---|
| 593 | }
|
---|
[12707] | 594 | }
|
---|
[16467] | 595 |
|
---|
[16509] | 596 | # If javahome is explicitly set in the gsdl site config file then it will override
|
---|
| 597 | # any env variable JAVA_HOME. A GS2 server does not set JAVA_HOME, though java is on
|
---|
| 598 | # the path. Therefore, if Fedora is being used for FLI with GS2, then javahome must
|
---|
| 599 | # be set in gsdlsite.cfg or the JAVA_HOME env var must be explicitly set by the user.
|
---|
| 600 | my $java_home = $self->get_java_home($optional);
|
---|
| 601 | if(defined $java_home) {
|
---|
| 602 | $ENV{'JAVA_HOME'} = $java_home;
|
---|
[16467] | 603 | }
|
---|
| 604 |
|
---|
[25078] | 605 |
|
---|
[23184] | 606 | # Process any extension setup.pl files
|
---|
[25078] | 607 | my @ext_homes = ();
|
---|
[23184] | 608 |
|
---|
[25078] | 609 | my $gsdl_ext_home = &util::filename_cat($gsdlhome,"ext");
|
---|
| 610 | push(@ext_homes,$gsdl_ext_home);
|
---|
[23184] | 611 |
|
---|
[25078] | 612 | if ($self->{'greenstone_version'} == 3) {
|
---|
| 613 | my $gsdl3srchome = $self->get_gsdl3_src_home();
|
---|
| 614 | my $gsdl3_ext_home = &util::filename_cat($gsdl3srchome,"ext");
|
---|
| 615 | push(@ext_homes,$gsdl3_ext_home);
|
---|
| 616 | }
|
---|
[23184] | 617 |
|
---|
[25078] | 618 | foreach my $ext_home (@ext_homes) {
|
---|
| 619 | # Should really think about making this a subroutine
|
---|
[23184] | 620 |
|
---|
[25078] | 621 | if (opendir(EXTDIR,$ext_home) ) {
|
---|
| 622 | my @pot_ext_dir = grep { $_ !~ m/^\./ } readdir(EXTDIR);
|
---|
| 623 |
|
---|
| 624 | closedir(EXTDIR);
|
---|
| 625 |
|
---|
| 626 | foreach my $ed (@pot_ext_dir) {
|
---|
| 627 | my $full_ext_dir = &util::filename_cat($ext_home,$ed);
|
---|
[23184] | 628 |
|
---|
[25078] | 629 | if (-d $full_ext_dir) {
|
---|
| 630 |
|
---|
| 631 | my $full_ext_perllib_dir = &util::filename_cat($full_ext_dir,"perllib");
|
---|
| 632 | if (-d $full_ext_perllib_dir) {
|
---|
| 633 | unshift (@INC, $full_ext_perllib_dir);
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | my $full_inc_file = &util::filename_cat($full_ext_dir,
|
---|
| 637 | "$ed-setup.pl");
|
---|
| 638 | if (-f $full_inc_file) {
|
---|
| 639 |
|
---|
| 640 | my $store_cwd = Cwd::cwd();
|
---|
| 641 |
|
---|
| 642 | chdir($full_ext_dir);
|
---|
| 643 | require "./$ed-setup.pl";
|
---|
| 644 | chdir($store_cwd);
|
---|
| 645 | }
|
---|
[23184] | 646 | }
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[16509] | 651 | # FEDORA_HOME and FEDORA_VERSION are needed (by scripts g2f-import and g2f-buildcol).
|
---|
[16467] | 652 | $self->setup_fedora_homes($optional);
|
---|
[23732] | 653 |
|
---|
| 654 |
|
---|
| 655 | # Check for any customisations to Open-Office if on Windows
|
---|
| 656 | if ($gsdlos eq "windows") {
|
---|
| 657 | $self->setup_openoffice($optional);
|
---|
| 658 | }
|
---|
[10206] | 659 | }
|
---|
[7956] | 660 |
|
---|
[16467] | 661 | sub greenstone_version {
|
---|
| 662 | my $self = shift @_;
|
---|
| 663 | return $self->{'greenstone_version'};
|
---|
| 664 | }
|
---|
[7956] | 665 |
|
---|
[16467] | 666 | sub library_url_suffix {
|
---|
| 667 | my $self = shift @_;
|
---|
| 668 | return $self->{'library_url_suffix'};
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | # Only useful to call this after calling setup_gsdl, as it uses some environment variables
|
---|
| 672 | # Returns the Greenstone collect directory, or a specific collection directory inside collect
|
---|
| 673 | sub get_collection_dir {
|
---|
| 674 | my $self = shift @_;
|
---|
| 675 | my ($site, $collection) = @_; # both may be undefined
|
---|
[19202] | 676 |
|
---|
[16467] | 677 | my $collection_directory;
|
---|
| 678 | if($self->{'greenstone_version'} == 2 && defined $ENV{'GSDLHOME'}) {
|
---|
| 679 | if(defined $collection) {
|
---|
| 680 | $collection_directory = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
| 681 | } else {
|
---|
| 682 | $collection_directory = &util::filename_cat($ENV{'GSDLHOME'}, "collect");
|
---|
| 683 | }
|
---|
| 684 | }
|
---|
| 685 | elsif($self->{'greenstone_version'} == 3 && defined $ENV{'GSDL3SRCHOME'}) {
|
---|
| 686 | if(defined $collection) {
|
---|
| 687 | $collection_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "sites", $site, "collect", $collection);
|
---|
| 688 | } else {
|
---|
| 689 | $collection_directory = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web", "sites", $site, "collect");
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
| 692 | }
|
---|
| 693 |
|
---|
[10206] | 694 | sub local_rm_r
|
---|
[9941] | 695 | {
|
---|
| 696 | my $self = shift @_;
|
---|
[10206] | 697 | my ($local_dir) = @_;
|
---|
[9941] | 698 |
|
---|
[10206] | 699 | my $prefix_dir = getcwd();
|
---|
[16467] | 700 | my $full_path = &util::filename_cat($prefix_dir,$local_dir);
|
---|
| 701 |
|
---|
[10206] | 702 | if ($prefix_dir !~ m/collect/) {
|
---|
[16467] | 703 | $self->generate_error("Trying to delete outside of Greenstone collect: $full_path");
|
---|
[10206] | 704 | }
|
---|
[9941] | 705 |
|
---|
[10206] | 706 | # Delete recursively
|
---|
[16467] | 707 | if (!-e $full_path) {
|
---|
| 708 | $self->generate_error("File/Directory does not exist: $full_path");
|
---|
[10206] | 709 | }
|
---|
[9941] | 710 |
|
---|
[16467] | 711 | &util::rm_r($full_path);
|
---|
[9941] | 712 | }
|
---|
| 713 |
|
---|
[10206] | 714 |
|
---|
[10584] | 715 | sub get_java_path()
|
---|
[9941] | 716 | {
|
---|
[10584] | 717 | # Check the JAVA_HOME environment variable first
|
---|
[9941] | 718 | if (defined $ENV{'JAVA_HOME'}) {
|
---|
| 719 | my $java_home = $ENV{'JAVA_HOME'};
|
---|
[10584] | 720 | $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
|
---|
| 721 | return &util::filename_cat($java_home, "bin", "java");
|
---|
| 722 | }
|
---|
[10206] | 723 |
|
---|
[10584] | 724 | # Hope that Java is on the PATH
|
---|
| 725 | return "java";
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 |
|
---|
| 729 | sub check_java_home()
|
---|
| 730 | {
|
---|
[16467] | 731 | # Return a warning unless the JAVA_HOME environment variable is set
|
---|
[10584] | 732 | if (!defined $ENV{'JAVA_HOME'}) {
|
---|
| 733 | return "JAVA_HOME environment variable not set. Might not be able to find Java unless in PATH (" . $ENV{'PATH'} . ")";
|
---|
[9941] | 734 | }
|
---|
[10206] | 735 |
|
---|
[10584] | 736 | return "";
|
---|
[9941] | 737 | }
|
---|
| 738 |
|
---|
[7956] | 739 |
|
---|
| 740 | sub checked_chdir
|
---|
| 741 | {
|
---|
| 742 | my $self = shift @_;
|
---|
| 743 | my ($dir) = @_;
|
---|
| 744 |
|
---|
| 745 | if (!-e $dir) {
|
---|
| 746 | $self->generate_error("Directory '$dir' does not exist");
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | chdir $dir
|
---|
| 750 | || $self->generate_error("Unable to change to directory: $dir");
|
---|
| 751 | }
|
---|
| 752 |
|
---|
[16467] | 753 | sub rot13()
|
---|
| 754 | {
|
---|
| 755 | my $self = shift @_;
|
---|
| 756 | my ($password)=@_;
|
---|
| 757 | my @password_arr=split(//,$password);
|
---|
| 758 |
|
---|
| 759 | my @encrypt_password;
|
---|
| 760 | foreach my $str (@password_arr){
|
---|
| 761 | my $char=unpack("c",$str);
|
---|
| 762 | if ($char>=97 && $char<=109){
|
---|
| 763 | $char+=13;
|
---|
| 764 | }elsif ($char>=110 && $char<=122){
|
---|
| 765 | $char-=13;
|
---|
| 766 | }elsif ($char>=65 && $char<=77){
|
---|
| 767 | $char+=13;
|
---|
| 768 | }elsif ($char>=78 && $char<=90){
|
---|
| 769 | $char-=13;
|
---|
| 770 | }
|
---|
| 771 | $char=pack("c",$char);
|
---|
| 772 | push(@encrypt_password,$char);
|
---|
| 773 | }
|
---|
| 774 | return join("",@encrypt_password);
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | sub encrypt_password
|
---|
| 778 | {
|
---|
| 779 | my $self = shift @_;
|
---|
| 780 |
|
---|
| 781 | if (defined $self->param("pw")) { ##
|
---|
| 782 | if ($self->{'greenstone_version'} == 3) { # GS3 is in Java, so needs different encryption
|
---|
| 783 | $self->param('-name' => "pw", '-value' => $self->rot13($self->clean_param("pw")));
|
---|
| 784 | }
|
---|
| 785 | else { # GS2 (and versions of GS other than 3?)
|
---|
| 786 | #require "$self->{'gsdlhome'}/perllib/util.pm"; # This is OK on Windows
|
---|
| 787 | require "$self->{'gsdlhome'}/perllib/cpan/Crypt/UnixCrypt.pm"; # This is OK on Windows
|
---|
| 788 | $self->param('-name' => "pw", '-value' => &Crypt::UnixCrypt::crypt($self->clean_param("pw"), "Tp"));
|
---|
| 789 | }
|
---|
| 790 | }
|
---|
| 791 | }
|
---|
| 792 |
|
---|
[18648] | 793 |
|
---|
| 794 | sub decode {
|
---|
| 795 | my ($self, $text) = @_;
|
---|
| 796 | $text =~ s/\+/ /g;
|
---|
| 797 | $text = &MIME::Base64::decode_base64($text);
|
---|
| 798 |
|
---|
| 799 | return $text;
|
---|
| 800 | }
|
---|
| 801 |
|
---|
[7956] | 802 | 1;
|
---|
[10206] | 803 |
|
---|