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