1 |
|
---|
2 | package gsdlCGI;
|
---|
3 |
|
---|
4 | use CGI;
|
---|
5 | use Cwd;
|
---|
6 |
|
---|
7 | @ISA = ('CGI');
|
---|
8 |
|
---|
9 | sub 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 |
|
---|
47 | sub 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 |
|
---|
58 | sub 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 |
|
---|
71 | sub 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\n ($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 |
|
---|
97 | sub 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 |
|
---|
124 | sub 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 |
|
---|
148 | sub 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 | my ($loc) = ($config_content =~ m/^$infotype\s+((\".+\")|(\S+))\s*\n/m);
|
---|
163 | $loc =~ s/\"//g;
|
---|
164 |
|
---|
165 | if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
|
---|
166 | $self->generate_error("$infotype is not set in gsdlsite.cfg");
|
---|
167 | }
|
---|
168 |
|
---|
169 | return $loc;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | sub get_gsdl_home {
|
---|
174 | my $self = shift @_;
|
---|
175 |
|
---|
176 | if (defined $self->{'gsdlhome'}) {
|
---|
177 | return $self->{'gsdlhome'};
|
---|
178 | }
|
---|
179 |
|
---|
180 | my $gsdlhome = $self->get_config_info("gsdlhome");
|
---|
181 |
|
---|
182 | require "$gsdlhome/perllib/util.pm";
|
---|
183 |
|
---|
184 | $gsdlhome =~ s/(\/|\\)$//; # remove trailing slash
|
---|
185 |
|
---|
186 | $self->{'gsdlhome'} = $gsdlhome;
|
---|
187 |
|
---|
188 | return $gsdlhome;
|
---|
189 | }
|
---|
190 |
|
---|
191 | sub get_gsdl_os {
|
---|
192 | my $self = shift @_;
|
---|
193 |
|
---|
194 | my $os = $^O;
|
---|
195 |
|
---|
196 | if ($os =~ m/linux/i) {
|
---|
197 | return "linux";
|
---|
198 | }
|
---|
199 | elsif ($os =~ /mswin/i) {
|
---|
200 | return "windows";
|
---|
201 | }
|
---|
202 | elsif ($os =~ /macos/i) {
|
---|
203 | return "darwin";
|
---|
204 | }
|
---|
205 | else {
|
---|
206 | # return as is.
|
---|
207 | return $os;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | sub setup_gsdl {
|
---|
212 | my $self = shift @_;
|
---|
213 |
|
---|
214 | my $gsdlhome = $self->get_gsdl_home();
|
---|
215 | my $gsdlos = $self->get_gsdl_os();
|
---|
216 |
|
---|
217 | $ENV{'GSDLHOME'} = $gsdlhome;
|
---|
218 | $ENV{'GSDLOS'} = $gsdlos;
|
---|
219 |
|
---|
220 | my $gsdl_bin_script = &util::filename_cat($gsdlhome,"bin","script");
|
---|
221 | &util::envvar_append("PATH",$gsdl_bin_script);
|
---|
222 |
|
---|
223 | my $gsdl_bin_os = &util::filename_cat($gsdlhome,"bin",$gsdlos);
|
---|
224 | &util::envvar_append("PATH",$gsdl_bin_os);
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | sub local_rm_r
|
---|
229 | {
|
---|
230 | my $self = shift @_;
|
---|
231 | my ($local_dir) = @_;
|
---|
232 |
|
---|
233 | my $prefix_dir = getcwd();
|
---|
234 |
|
---|
235 | if ($prefix_dir !~ m/collect/) {
|
---|
236 | $self->generate_error("Trying to delete outside of Greenstone collect: $full_dir");
|
---|
237 | }
|
---|
238 |
|
---|
239 | my $full_dir = &util::filename_cat($prefix_dir,$local_dir);
|
---|
240 |
|
---|
241 | # Delete recursively
|
---|
242 | if (!-e $full_dir) {
|
---|
243 | $self->generate_error("File/Directory does not exist: $full_dir");
|
---|
244 | }
|
---|
245 |
|
---|
246 | &util::rm_r($full_dir);
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|
251 | sub check_for_java()
|
---|
252 | {
|
---|
253 | my $self = shift @_;
|
---|
254 |
|
---|
255 | my $java = "java";
|
---|
256 |
|
---|
257 | if (defined $ENV{'JAVA_HOME'}) {
|
---|
258 | my $java_home = $ENV{'JAVA_HOME'};
|
---|
259 | # Unix specific
|
---|
260 | $java_home =~ s/\/$//; # remove trailing slash if present
|
---|
261 |
|
---|
262 | $java = &util::filename_cat($java_home,"bin","java");
|
---|
263 | }
|
---|
264 | else {
|
---|
265 | my $path = $ENV{'PATH'};
|
---|
266 | my $mess = "JAVA_HOME environment variable not set. Might not be able to find java unless in PATH=$path";
|
---|
267 |
|
---|
268 | $self->generate_warning($mess);
|
---|
269 | }
|
---|
270 |
|
---|
271 | return $java;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | sub checked_chdir
|
---|
276 | {
|
---|
277 | my $self = shift @_;
|
---|
278 | my ($dir) = @_;
|
---|
279 |
|
---|
280 | if (!-e $dir) {
|
---|
281 | $self->generate_error("Directory '$dir' does not exist");
|
---|
282 | }
|
---|
283 |
|
---|
284 | chdir $dir
|
---|
285 | || $self->generate_error("Unable to change to directory: $dir");
|
---|
286 | }
|
---|
287 |
|
---|
288 | 1;
|
---|
289 |
|
---|