1 |
|
---|
2 | package gsdlCGI;
|
---|
3 |
|
---|
4 | use CGI;
|
---|
5 | use Cwd;
|
---|
6 |
|
---|
7 | @ISA = ('CGI');
|
---|
8 |
|
---|
9 | sub 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 |
|
---|
30 | sub 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 |
|
---|
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 |
|
---|
68 | $val =~ s/[^[:alnum:]@\.\/\- :_]//g if (defined $val);
|
---|
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) {
|
---|
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 |
|
---|
100 | sub 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 |
|
---|
127 | sub 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 |
|
---|
151 | sub 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 |
|
---|
176 | sub 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 |
|
---|
194 | sub 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 |
|
---|
214 | sub 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 |
|
---|
230 |
|
---|
231 | sub local_rm_r
|
---|
232 | {
|
---|
233 | my $self = shift @_;
|
---|
234 | my ($local_dir) = @_;
|
---|
235 |
|
---|
236 | my $prefix_dir = getcwd();
|
---|
237 |
|
---|
238 | if ($prefix_dir !~ m/collect/) {
|
---|
239 | $self->generate_error("Trying to delete outside of Greenstone collect: $full_dir");
|
---|
240 | }
|
---|
241 |
|
---|
242 | my $full_dir = &util::filename_cat($prefix_dir,$local_dir);
|
---|
243 |
|
---|
244 | # Delete recursively
|
---|
245 | if (!-e $full_dir) {
|
---|
246 | $self->generate_error("File/Directory does not exist: $full_dir");
|
---|
247 | }
|
---|
248 |
|
---|
249 | &util::rm_r($full_dir);
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | sub get_java_path()
|
---|
254 | {
|
---|
255 | # Check the JAVA_HOME environment variable first
|
---|
256 | if (defined $ENV{'JAVA_HOME'}) {
|
---|
257 | my $java_home = $ENV{'JAVA_HOME'};
|
---|
258 | $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
|
---|
259 | return &util::filename_cat($java_home, "bin", "java");
|
---|
260 | }
|
---|
261 |
|
---|
262 | # Hope that Java is on the PATH
|
---|
263 | return "java";
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | sub check_java_home()
|
---|
268 | {
|
---|
269 | # Return a warning unless the JAVA_HOME enrivonmen variable is set
|
---|
270 | if (!defined $ENV{'JAVA_HOME'}) {
|
---|
271 | return "JAVA_HOME environment variable not set. Might not be able to find Java unless in PATH (" . $ENV{'PATH'} . ")";
|
---|
272 | }
|
---|
273 |
|
---|
274 | return "";
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | sub checked_chdir
|
---|
279 | {
|
---|
280 | my $self = shift @_;
|
---|
281 | my ($dir) = @_;
|
---|
282 |
|
---|
283 | if (!-e $dir) {
|
---|
284 | $self->generate_error("Directory '$dir' does not exist");
|
---|
285 | }
|
---|
286 |
|
---|
287 | chdir $dir
|
---|
288 | || $self->generate_error("Unable to change to directory: $dir");
|
---|
289 | }
|
---|
290 |
|
---|
291 | 1;
|
---|
292 |
|
---|