source: main/tags/2.61/gsdl/cgi-bin/gsdlCGI.pm@ 25600

Last change on this file since 25600 was 10212, checked in by davidb, 19 years ago

Introduction of envvar_append() to append items into an environment variable
such as PATH in a platform independent way. It is assumed the variable
passed in (typically representing a filename) is already OS compliant
(i.e. has used filename_cat() to construct it).

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