source: trunk/gsdl/cgi-bin/gsdlCGI.pm@ 12707

Last change on this file since 12707 was 12707, checked in by mdewsnip, 18 years ago

On Windows the perl "bin" path has to be added to the PATH for gliserver.pl commands to work.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1
2package gsdlCGI;
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 "")) {
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 $full_mess = "<Error>\n";
86 $full_mess .= " $mess\n";
87 $full_mess .= " CGI args were: $args\n";
88 $full_mess .= "</Error>\n";
89 }
90 else {
91 $full_mess = "ERROR: $mess\n ($args)\n";
92 }
93
94 print STDOUT "Content-type:text/plain\n\n";
95 print STDOUT $full_mess;
96
97 die $full_mess;
98}
99
100sub generate_warning
101{
102 my $self = shift @_;
103 my ($mess) = @_;
104
105 my $xml = $self->{'xml'};
106
107 my $full_mess;
108 my $args = $self->{'args'};
109
110 if ($xml) {
111 $full_mess = "<Warning>\n";
112 $full_mess .= " $mess\n";
113 $full_mess .= " CGI args were: $args\n";
114 $full_mess .= "</Warning>\n";
115 }
116 else {
117 $full_mess = "Warning: $mess ($args)\n";
118 }
119
120 print STDOUT "Content-type:text/plain\n\n";
121 print STDOUT $full_mess;
122
123 print STDERR $full_mess;
124}
125
126
127sub generate_ok_message
128{
129 my $self = shift @_;
130 my ($mess) = @_;
131
132 my $xml = $self->{'xml'};
133
134 my $full_mess;
135
136 if ($xml) {
137 $full_mess = "<Accepted>\n";
138 $full_mess .= " $mess\n";
139 $full_mess .= "</Accepted>\n";
140 }
141 else {
142 $full_mess = "$mess\n";
143 }
144
145 print STDOUT "Content-type:text/plain\n\n";
146 print STDOUT $full_mess;
147}
148
149
150
151sub get_config_info {
152 my $self = shift @_;
153 my ($infotype) = @_;
154
155 my $site_filename = "gsdlsite.cfg";
156 open (FILEIN, "<$site_filename")
157 || $self->generate_error("Could not open gsdlsite.cfg");
158
159 my $config_content = "";
160 while(defined (my $line = <FILEIN>)) {
161 $config_content .= $line;
162 }
163 close(FILEIN);
164
165 my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
166 $loc =~ s/\"//g;
167
168 if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
169 $self->generate_error("$infotype is not set in gsdlsite.cfg");
170 }
171
172 return $loc;
173}
174
175
176sub get_gsdl_home {
177 my $self = shift @_;
178
179 if (defined $self->{'gsdlhome'}) {
180 return $self->{'gsdlhome'};
181 }
182
183 my $gsdlhome = $self->get_config_info("gsdlhome");
184
185 require "$gsdlhome/perllib/util.pm";
186
187 $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
188
189 $self->{'gsdlhome'} = $gsdlhome;
190
191 return $gsdlhome;
192}
193
194sub get_gsdl_os {
195 my $self = shift @_;
196
197 my $os = $^O;
198
199 if ($os =~ m/linux/i) {
200 return "linux";
201 }
202 elsif ($os =~ /mswin/i) {
203 return "windows";
204 }
205 elsif ($os =~ /macos/i) {
206 return "darwin";
207 }
208 else {
209 # return as is.
210 return $os;
211 }
212}
213
214sub setup_gsdl {
215 my $self = shift @_;
216
217 my $gsdlhome = $self->get_gsdl_home();
218 my $gsdlos = $self->get_gsdl_os();
219
220 $ENV{'GSDLHOME'} = $gsdlhome;
221 $ENV{'GSDLOS'} = $gsdlos;
222
223 my $gsdl_bin_script = &util::filename_cat($gsdlhome,"bin","script");
224 &util::envvar_append("PATH",$gsdl_bin_script);
225
226 my $gsdl_bin_os = &util::filename_cat($gsdlhome,"bin",$gsdlos);
227 &util::envvar_append("PATH",$gsdl_bin_os);
228
229 if ($gsdlos eq "windows") {
230 my $gsdl_perl_bin_directory = &util::filename_cat($gsdlhome, "bin", "windows", "perl", "bin");
231 &util::envvar_append("PATH", $gsdl_perl_bin_directory);
232 }
233}
234
235
236sub local_rm_r
237{
238 my $self = shift @_;
239 my ($local_dir) = @_;
240
241 my $prefix_dir = getcwd();
242
243 if ($prefix_dir !~ m/collect/) {
244 $self->generate_error("Trying to delete outside of Greenstone collect: $full_dir");
245 }
246
247 my $full_dir = &util::filename_cat($prefix_dir,$local_dir);
248
249 # Delete recursively
250 if (!-e $full_dir) {
251 $self->generate_error("File/Directory does not exist: $full_dir");
252 }
253
254 &util::rm_r($full_dir);
255}
256
257
258sub get_java_path()
259{
260 # Check the JAVA_HOME environment variable first
261 if (defined $ENV{'JAVA_HOME'}) {
262 my $java_home = $ENV{'JAVA_HOME'};
263 $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
264 return &util::filename_cat($java_home, "bin", "java");
265 }
266
267 # Hope that Java is on the PATH
268 return "java";
269}
270
271
272sub check_java_home()
273{
274 # Return a warning unless the JAVA_HOME enrivonmen variable is set
275 if (!defined $ENV{'JAVA_HOME'}) {
276 return "JAVA_HOME environment variable not set. Might not be able to find Java unless in PATH (" . $ENV{'PATH'} . ")";
277 }
278
279 return "";
280}
281
282
283sub checked_chdir
284{
285 my $self = shift @_;
286 my ($dir) = @_;
287
288 if (!-e $dir) {
289 $self->generate_error("Directory '$dir' does not exist");
290 }
291
292 chdir $dir
293 || $self->generate_error("Unable to change to directory: $dir");
294}
295
2961;
297
Note: See TracBrowser for help on using the repository browser.