[7956] | 1 |
|
---|
| 2 | package gsdlCGI;
|
---|
| 3 |
|
---|
[14024] | 4 |
|
---|
| 5 |
|
---|
[7956] | 6 | use CGI;
|
---|
[10206] | 7 | use Cwd;
|
---|
[7956] | 8 |
|
---|
| 9 | @ISA = ('CGI');
|
---|
| 10 |
|
---|
| 11 | sub new {
|
---|
| 12 | my $class = shift @_;
|
---|
| 13 |
|
---|
| 14 | my $self;
|
---|
[11148] | 15 | if ((defined $ENV{'REQUEST_METHOD'}) && ($ENV{'REQUEST_METHOD'} eq "POST")) {
|
---|
[7956] | 16 | my $line = <STDIN>;
|
---|
| 17 | if ((defined $line) && ($line ne "")) {
|
---|
| 18 | $self = new CGI($line);
|
---|
| 19 | }
|
---|
| 20 | else {
|
---|
| 21 | $self = new CGI();
|
---|
| 22 | }
|
---|
| 23 | }
|
---|
| 24 | else {
|
---|
| 25 | $self = new CGI();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[10583] | 28 | return bless $self, $class;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | sub parse_cgi_args
|
---|
| 33 | {
|
---|
| 34 | my $self = shift @_;
|
---|
[7956] | 35 | my $xml = (defined $self->param("xml")) ? 1 : 0;
|
---|
| 36 |
|
---|
| 37 | $self->{'xml'} = $xml;
|
---|
| 38 |
|
---|
| 39 | my @var_names = $self->param;
|
---|
| 40 | my @arg_list = ();
|
---|
| 41 | foreach my $n ( @var_names ) {
|
---|
| 42 | my $arg = "$n=";
|
---|
| 43 | my $val = $self->param($n);
|
---|
| 44 | $arg .= $val if (defined $val);
|
---|
| 45 | push(@arg_list,$arg);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | $self->{'args'} = join("&",@arg_list);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | sub clean_param
|
---|
| 53 | {
|
---|
| 54 | my $self = shift @_;
|
---|
| 55 | my ($param) = @_;
|
---|
| 56 |
|
---|
| 57 | my $val = $self->SUPER::param($param);
|
---|
| 58 | $val =~ s/[\r\n]+$// if (defined $val);
|
---|
| 59 |
|
---|
| 60 | return $val;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | sub safe_val
|
---|
| 64 | {
|
---|
| 65 | my $self = shift @_;
|
---|
| 66 | my ($val) = @_;
|
---|
| 67 |
|
---|
| 68 | # ensure only alpha-numeric plus a few other special chars remain
|
---|
| 69 |
|
---|
[9941] | 70 | $val =~ s/[^[:alnum:]@\.\/\- :_]//g if (defined $val);
|
---|
[7956] | 71 |
|
---|
| 72 | return $val;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | sub generate_error
|
---|
| 77 | {
|
---|
| 78 | my $self = shift @_;
|
---|
| 79 | my ($mess) = @_;
|
---|
| 80 |
|
---|
| 81 | my $xml = $self->{'xml'};
|
---|
| 82 |
|
---|
| 83 | my $full_mess;
|
---|
| 84 | my $args = $self->{'args'};
|
---|
| 85 |
|
---|
| 86 | if ($xml) {
|
---|
[13167] | 87 | # Make $args XML safe
|
---|
| 88 | my $args_xml_safe = $args;
|
---|
| 89 | $args_xml_safe =~ s/&/&/g;
|
---|
| 90 |
|
---|
[7956] | 91 | $full_mess = "<Error>\n";
|
---|
| 92 | $full_mess .= " $mess\n";
|
---|
[13167] | 93 | $full_mess .= " CGI args were: $args_xml_safe\n";
|
---|
[7956] | 94 | $full_mess .= "</Error>\n";
|
---|
| 95 | }
|
---|
| 96 | else {
|
---|
[10565] | 97 | $full_mess = "ERROR: $mess\n ($args)\n";
|
---|
[7956] | 98 | }
|
---|
| 99 |
|
---|
| 100 | print STDOUT "Content-type:text/plain\n\n";
|
---|
| 101 | print STDOUT $full_mess;
|
---|
| 102 |
|
---|
[14220] | 103 | exit 0;
|
---|
[7956] | 104 | }
|
---|
| 105 |
|
---|
[10206] | 106 | sub generate_warning
|
---|
| 107 | {
|
---|
| 108 | my $self = shift @_;
|
---|
| 109 | my ($mess) = @_;
|
---|
| 110 |
|
---|
| 111 | my $xml = $self->{'xml'};
|
---|
[7956] | 112 |
|
---|
[10206] | 113 | my $full_mess;
|
---|
| 114 | my $args = $self->{'args'};
|
---|
| 115 |
|
---|
| 116 | if ($xml) {
|
---|
[13167] | 117 | # Make $args XML safe
|
---|
| 118 | my $args_xml_safe = $args;
|
---|
| 119 | $args_xml_safe =~ s/&/&/g;
|
---|
| 120 |
|
---|
[10206] | 121 | $full_mess = "<Warning>\n";
|
---|
| 122 | $full_mess .= " $mess\n";
|
---|
[13167] | 123 | $full_mess .= " CGI args were: $args_xml_safe\n";
|
---|
[10206] | 124 | $full_mess .= "</Warning>\n";
|
---|
| 125 | }
|
---|
| 126 | else {
|
---|
| 127 | $full_mess = "Warning: $mess ($args)\n";
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | print STDOUT "Content-type:text/plain\n\n";
|
---|
| 131 | print STDOUT $full_mess;
|
---|
| 132 |
|
---|
| 133 | print STDERR $full_mess;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[7956] | 137 | sub generate_ok_message
|
---|
| 138 | {
|
---|
| 139 | my $self = shift @_;
|
---|
| 140 | my ($mess) = @_;
|
---|
| 141 |
|
---|
| 142 | my $xml = $self->{'xml'};
|
---|
| 143 |
|
---|
| 144 | my $full_mess;
|
---|
| 145 |
|
---|
| 146 | if ($xml) {
|
---|
| 147 | $full_mess = "<Accepted>\n";
|
---|
| 148 | $full_mess .= " $mess\n";
|
---|
| 149 | $full_mess .= "</Accepted>\n";
|
---|
| 150 | }
|
---|
| 151 | else {
|
---|
| 152 | $full_mess = "$mess\n";
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | print STDOUT "Content-type:text/plain\n\n";
|
---|
| 156 | print STDOUT $full_mess;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | sub get_config_info {
|
---|
| 162 | my $self = shift @_;
|
---|
| 163 | my ($infotype) = @_;
|
---|
| 164 |
|
---|
| 165 | my $site_filename = "gsdlsite.cfg";
|
---|
| 166 | open (FILEIN, "<$site_filename")
|
---|
| 167 | || $self->generate_error("Could not open gsdlsite.cfg");
|
---|
| 168 |
|
---|
| 169 | my $config_content = "";
|
---|
| 170 | while(defined (my $line = <FILEIN>)) {
|
---|
| 171 | $config_content .= $line;
|
---|
| 172 | }
|
---|
| 173 | close(FILEIN);
|
---|
| 174 |
|
---|
[10565] | 175 | my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
|
---|
[7956] | 176 | $loc =~ s/\"//g;
|
---|
| 177 |
|
---|
| 178 | if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
|
---|
| 179 | $self->generate_error("$infotype is not set in gsdlsite.cfg");
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | return $loc;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[10206] | 185 |
|
---|
[9941] | 186 | sub get_gsdl_home {
|
---|
| 187 | my $self = shift @_;
|
---|
[10206] | 188 |
|
---|
| 189 | if (defined $self->{'gsdlhome'}) {
|
---|
| 190 | return $self->{'gsdlhome'};
|
---|
| 191 | }
|
---|
[7956] | 192 |
|
---|
[9941] | 193 | my $gsdlhome = $self->get_config_info("gsdlhome");
|
---|
[7956] | 194 |
|
---|
[10206] | 195 | $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
|
---|
| 196 |
|
---|
| 197 | $self->{'gsdlhome'} = $gsdlhome;
|
---|
| 198 |
|
---|
[9941] | 199 | return $gsdlhome;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[10206] | 202 | sub get_gsdl_os {
|
---|
[7956] | 203 | my $self = shift @_;
|
---|
[10206] | 204 |
|
---|
| 205 | my $os = $^O;
|
---|
[7956] | 206 |
|
---|
[10206] | 207 | if ($os =~ m/linux/i) {
|
---|
| 208 | return "linux";
|
---|
| 209 | }
|
---|
| 210 | elsif ($os =~ /mswin/i) {
|
---|
| 211 | return "windows";
|
---|
| 212 | }
|
---|
| 213 | elsif ($os =~ /macos/i) {
|
---|
| 214 | return "darwin";
|
---|
| 215 | }
|
---|
| 216 | else {
|
---|
| 217 | # return as is.
|
---|
| 218 | return $os;
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
[7956] | 221 |
|
---|
[10206] | 222 | sub setup_gsdl {
|
---|
| 223 | my $self = shift @_;
|
---|
[7956] | 224 |
|
---|
[10212] | 225 | my $gsdlhome = $self->get_gsdl_home();
|
---|
| 226 | my $gsdlos = $self->get_gsdl_os();
|
---|
| 227 |
|
---|
| 228 | $ENV{'GSDLHOME'} = $gsdlhome;
|
---|
| 229 | $ENV{'GSDLOS'} = $gsdlos;
|
---|
| 230 |
|
---|
[14973] | 231 | require "$gsdlhome/perllib/util.pm";
|
---|
| 232 |
|
---|
[10212] | 233 | my $gsdl_bin_script = &util::filename_cat($gsdlhome,"bin","script");
|
---|
| 234 | &util::envvar_append("PATH",$gsdl_bin_script);
|
---|
| 235 |
|
---|
| 236 | my $gsdl_bin_os = &util::filename_cat($gsdlhome,"bin",$gsdlos);
|
---|
| 237 | &util::envvar_append("PATH",$gsdl_bin_os);
|
---|
[12707] | 238 |
|
---|
| 239 | if ($gsdlos eq "windows") {
|
---|
| 240 | my $gsdl_perl_bin_directory = &util::filename_cat($gsdlhome, "bin", "windows", "perl", "bin");
|
---|
| 241 | &util::envvar_append("PATH", $gsdl_perl_bin_directory);
|
---|
| 242 | }
|
---|
[10206] | 243 | }
|
---|
[7956] | 244 |
|
---|
| 245 |
|
---|
[10206] | 246 | sub local_rm_r
|
---|
[9941] | 247 | {
|
---|
| 248 | my $self = shift @_;
|
---|
[10206] | 249 | my ($local_dir) = @_;
|
---|
[9941] | 250 |
|
---|
[10206] | 251 | my $prefix_dir = getcwd();
|
---|
[9941] | 252 |
|
---|
[10206] | 253 | if ($prefix_dir !~ m/collect/) {
|
---|
| 254 | $self->generate_error("Trying to delete outside of Greenstone collect: $full_dir");
|
---|
| 255 | }
|
---|
[9941] | 256 |
|
---|
[10206] | 257 | my $full_dir = &util::filename_cat($prefix_dir,$local_dir);
|
---|
[9941] | 258 |
|
---|
[10206] | 259 | # Delete recursively
|
---|
| 260 | if (!-e $full_dir) {
|
---|
| 261 | $self->generate_error("File/Directory does not exist: $full_dir");
|
---|
| 262 | }
|
---|
[9941] | 263 |
|
---|
[10206] | 264 | &util::rm_r($full_dir);
|
---|
[9941] | 265 | }
|
---|
| 266 |
|
---|
[10206] | 267 |
|
---|
[10584] | 268 | sub get_java_path()
|
---|
[9941] | 269 | {
|
---|
[10584] | 270 | # Check the JAVA_HOME environment variable first
|
---|
[9941] | 271 | if (defined $ENV{'JAVA_HOME'}) {
|
---|
| 272 | my $java_home = $ENV{'JAVA_HOME'};
|
---|
[10584] | 273 | $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
|
---|
| 274 | return &util::filename_cat($java_home, "bin", "java");
|
---|
| 275 | }
|
---|
[10206] | 276 |
|
---|
[10584] | 277 | # Hope that Java is on the PATH
|
---|
| 278 | return "java";
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | sub check_java_home()
|
---|
| 283 | {
|
---|
| 284 | # Return a warning unless the JAVA_HOME enrivonmen variable is set
|
---|
| 285 | if (!defined $ENV{'JAVA_HOME'}) {
|
---|
| 286 | return "JAVA_HOME environment variable not set. Might not be able to find Java unless in PATH (" . $ENV{'PATH'} . ")";
|
---|
[9941] | 287 | }
|
---|
[10206] | 288 |
|
---|
[10584] | 289 | return "";
|
---|
[9941] | 290 | }
|
---|
| 291 |
|
---|
[7956] | 292 |
|
---|
| 293 | sub checked_chdir
|
---|
| 294 | {
|
---|
| 295 | my $self = shift @_;
|
---|
| 296 | my ($dir) = @_;
|
---|
| 297 |
|
---|
| 298 | if (!-e $dir) {
|
---|
| 299 | $self->generate_error("Directory '$dir' does not exist");
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | chdir $dir
|
---|
| 303 | || $self->generate_error("Unable to change to directory: $dir");
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | 1;
|
---|
[10206] | 307 |
|
---|