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

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

Now escapes the '&' characters in the CGI arguments in XML errors or warnings, to prevent XML parsing errors.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 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 # 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 STDOUT "Content-type:text/plain\n\n";
154 print STDOUT $full_mess;
155}
156
157
158
159sub 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
173 my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
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
183
184sub get_gsdl_home {
185 my $self = shift @_;
186
187 if (defined $self->{'gsdlhome'}) {
188 return $self->{'gsdlhome'};
189 }
190
191 my $gsdlhome = $self->get_config_info("gsdlhome");
192
193 require "$gsdlhome/perllib/util.pm";
194
195 $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
196
197 $self->{'gsdlhome'} = $gsdlhome;
198
199 return $gsdlhome;
200}
201
202sub get_gsdl_os {
203 my $self = shift @_;
204
205 my $os = $^O;
206
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}
221
222sub setup_gsdl {
223 my $self = shift @_;
224
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);
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 }
241}
242
243
244sub local_rm_r
245{
246 my $self = shift @_;
247 my ($local_dir) = @_;
248
249 my $prefix_dir = getcwd();
250
251 if ($prefix_dir !~ m/collect/) {
252 $self->generate_error("Trying to delete outside of Greenstone collect: $full_dir");
253 }
254
255 my $full_dir = &util::filename_cat($prefix_dir,$local_dir);
256
257 # Delete recursively
258 if (!-e $full_dir) {
259 $self->generate_error("File/Directory does not exist: $full_dir");
260 }
261
262 &util::rm_r($full_dir);
263}
264
265
266sub get_java_path()
267{
268 # Check the JAVA_HOME environment variable first
269 if (defined $ENV{'JAVA_HOME'}) {
270 my $java_home = $ENV{'JAVA_HOME'};
271 $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
272 return &util::filename_cat($java_home, "bin", "java");
273 }
274
275 # Hope that Java is on the PATH
276 return "java";
277}
278
279
280sub 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'} . ")";
285 }
286
287 return "";
288}
289
290
291sub 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
3041;
305
Note: See TracBrowser for help on using the repository browser.