source: greenstone3/trunk/web/WEB-INF/cgi/gsdlCGI4gs3.pm@ 15960

Last change on this file since 15960 was 15960, checked in by ak19, 16 years ago

Processes new variable javahome that has been simultaneously introduced into gsdl3site.cfg which this uses to set the JAVA_HOME environment variable, so that the doing a gliserver check-installation in the browser will no longer fail with JAVA_HOME not set.

  • Property svn:executable set to *
File size: 7.0 KB
Line 
1
2package gsdlCGI4gs3;
3
4use CGI;
5use Cwd;
6
7@ISA = ('CGI');
8
9sub new {
10 my $class = shift @_;
11
12 my $self;
13 if ((defined $ENV{'REQUEST_METHOD'}) && ($ENV{'REQUEST_METHOD'} eq "POST")) {
14 my $line = <STDIN>;
15 if ((defined $line) && ($line ne "") && ($line !~ /upload-collection-file/)) {
16 $self = new CGI($line);
17 }
18 else {
19 $self = new CGI();
20 }
21 }
22 else {
23 $self = new CGI();
24 }
25
26 return bless $self, $class;
27}
28
29
30sub parse_cgi_args
31{
32 my $self = shift @_;
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
50sub 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
61sub safe_val
62{
63 my $self = shift @_;
64 my ($val) = @_;
65
66 # ensure only alpha-numeric plus a few other special chars remain
67
68 $val =~ s/[^[:alnum:]@\.\/\- :_]//g if (defined $val);
69
70 return $val;
71}
72
73
74sub 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) {
85 # Make $args XML safe
86 my $args_xml_safe = $args;
87 $args_xml_safe =~ s/&/&amp;/g;
88
89 $full_mess = "<Error>\n";
90 $full_mess .= " $mess\n";
91 $full_mess .= " CGI args were: $args_xml_safe\n";
92 $full_mess .= "</Error>\n";
93 }
94 else {
95 $full_mess = "ERROR: $mess\n ($args)\n";
96 }
97
98 print STDOUT "Content-type:text/plain\n\n";
99 print STDOUT $full_mess;
100
101 die $full_mess;
102}
103
104sub generate_warning
105{
106 my $self = shift @_;
107 my ($mess) = @_;
108
109 my $xml = $self->{'xml'};
110
111 my $full_mess;
112 my $args = $self->{'args'};
113
114 if ($xml) {
115 # Make $args XML safe
116 my $args_xml_safe = $args;
117 $args_xml_safe =~ s/&/&amp;/g;
118
119 $full_mess = "<Warning>\n";
120 $full_mess .= " $mess\n";
121 $full_mess .= " CGI args were: $args_xml_safe\n";
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
135sub 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 "<pre>";
154 print STDOUT $full_mess;
155 print "</pre>";
156}
157
158
159
160sub get_config_info {
161 my $self = shift @_;
162 my ($infotype) = @_;
163
164 my $site_filename = "gsdl3site.cfg";
165 open (FILEIN, "<$site_filename")
166 || $self->generate_error("Could not open gsdl3site.cfg");
167
168 my $config_content = "";
169 while(defined (my $line = <FILEIN>)) {
170 $config_content .= $line;
171 }
172 close(FILEIN);
173
174 my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
175 $loc =~ s/\"//g;
176
177 if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
178 $self->generate_error("$infotype is not set in gsdl3site.cfg");
179 }
180
181 return $loc;
182}
183
184sub get_gsdl3_src_home{
185 my $self = shift @_;
186 if (defined $self->{'gsdl3srchome'}) {
187 return $self->{'gsdl3srchome'};
188 }
189
190 my $gsdl3srchome = $self->get_config_info("gsdl3srchome");
191
192 $gsdl3srchome =~ s/(\/|\\)$//; # remove trailing slash
193
194 $self->{'gsdl3srchome'} = $gsdl3srchome;
195
196 return $gsdl3srchome;
197}
198
199
200sub get_gsdl_home {
201 my $self = shift @_;
202
203 if (defined $self->{'gsdlhome'}) {
204 return $self->{'gsdlhome'};
205 }
206
207 my $gsdlhome = $self->get_config_info("gsdlhome");
208
209 #require "$gsdlhome/perllib/util.pm";
210
211 $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
212
213 $self->{'gsdlhome'} = $gsdlhome;
214
215 return $gsdlhome;
216}
217
218sub get_java_home {
219 my $self = shift @_;
220
221 if (defined $self->{'javahome'}) {
222 return $self->{'javahome'};
223 }
224
225 my $javahome = $self->get_config_info("javahome");
226
227 $javahome =~ s/(\/|\\)$//; # remove trailing slash
228
229 $self->{'javahome'} = $javahome;
230
231 return $javahome;
232}
233
234sub get_gsdl_os {
235 my $self = shift @_;
236
237 my $os = $^O;
238
239 if ($os =~ m/linux/i) {
240 return "linux";
241 }
242 elsif ($os =~ /mswin/i) {
243 return "windows";
244 }
245 elsif ($os =~ /macos/i) {
246 return "darwin";
247 }
248 else {
249 # return as is.
250 return $os;
251 }
252}
253
254sub setup_gsdl {
255 my $self = shift @_;
256
257 my $gsdl3srchome = $self->get_gsdl3_src_home();
258 my $gsdlhome = $self->get_gsdl_home();
259 my $gsdlos = $self->get_gsdl_os();
260 my $javahome = $self->get_java_home();
261
262 $ENV{'GSDL3SRCHOME'} = $gsdl3srchome;
263 $ENV{'GSDLHOME'} = $gsdlhome;
264 $ENV{'GSDLOS'} = $gsdlos;
265 $ENV{'JAVA_HOME'} = $javahome;
266
267 require "$gsdlhome/perllib/util.pm";
268
269 my $gsdl_bin_script = &util::filename_cat($gsdlhome,"bin","script");
270 &util::envvar_append("PATH",$gsdl_bin_script);
271
272 my $gsdl_bin_os = &util::filename_cat($gsdlhome,"bin",$gsdlos);
273 &util::envvar_append("PATH",$gsdl_bin_os);
274
275 if ($gsdlos eq "windows") {
276 my $gsdl_perl_bin_directory = &util::filename_cat($gsdlhome, "bin", "windows", "perl", "bin");
277 &util::envvar_append("PATH", $gsdl_perl_bin_directory);
278 }
279}
280
281
282sub local_rm_r
283{
284 my $self = shift @_;
285 my ($local_dir) = @_;
286
287 my $prefix_dir = getcwd();
288
289 if ($prefix_dir !~ m/collect/) {
290 $self->generate_error("Trying to delete outside of Greenstone collect: $full_dir");
291 }
292
293 my $full_dir = &util::filename_cat($prefix_dir,$local_dir);
294
295 # Delete recursively
296 if (!-e $full_dir) {
297 $self->generate_error("File/Directory does not exist: $full_dir");
298 }
299
300 &util::rm_r($full_dir);
301}
302
303
304sub get_java_path()
305{
306 # Check the JAVA_HOME environment variable first
307 if (defined $ENV{'JAVA_HOME'}) {
308 my $java_home = $ENV{'JAVA_HOME'};
309 $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
310 return &util::filename_cat($java_home, "bin", "java");
311 }
312
313 # Hope that Java is on the PATH
314 return "java";
315}
316
317
318sub check_java_home()
319{
320 # Return a warning unless the JAVA_HOME enrivonmen variable is set
321 if (!defined $ENV{'JAVA_HOME'}) {
322 return "JAVA_HOME environment variable not set. Might not be able to find Java unless in PATH (" . $ENV{'PATH'} . ")";
323 }
324
325 return "";
326}
327
328
329sub checked_chdir
330{
331 my $self = shift @_;
332 my ($dir) = @_;
333
334 if (!-e $dir) {
335 $self->generate_error("Directory '$dir' does not exist");
336 }
337
338 chdir $dir
339 || $self->generate_error("Unable to change to directory: $dir");
340}
341
342sub rot13()
343{
344 my $self = shift @_;
345 my ($password)=@_;
346 my @password_arr=split(//,$password);
347
348 my @encrypt_password;
349 foreach my $str (@password_arr){
350 my $char=unpack("c",$str);
351 if ($char>=97 && $char<=109){
352 $char+=13;
353 }elsif ($char>=110 && $char<=122){
354 $char-=13;
355 }elsif ($char>=65 && $char<=77){
356 $char+=13;
357 }elsif ($char>=78 && $char<=90){
358 $char-=13;
359 }
360 $char=pack("c",$char);
361 push(@encrypt_password,$char);
362 }
363 return join("",@encrypt_password);
364}
365
366
3671;
368
Note: See TracBrowser for help on using the repository browser.