1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * receptionist.cpp -- a web interface for the gsdl
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | // following line required to get fstream.filedesc() on darwin (Mac OS X)
|
---|
27 | // gcc 2.91 automatically defines this in stream.h
|
---|
28 | #define _STREAM_COMPAT 1
|
---|
29 |
|
---|
30 | #include "receptionist.h"
|
---|
31 | #include "fileutil.h"
|
---|
32 | #include "cgiutils.h"
|
---|
33 | #include "gsdltools.h"
|
---|
34 | #include "gsdltimes.h"
|
---|
35 | #include <assert.h>
|
---|
36 | #include <time.h>
|
---|
37 | #include <stdio.h> // for open()
|
---|
38 | #include <fcntl.h> // for open() flags
|
---|
39 | // following 2 are for printing Last-Modified http header.
|
---|
40 | #include <sys/stat.h>
|
---|
41 | #include <time.h>
|
---|
42 |
|
---|
43 | #if defined (GSDL_USE_IOS_H)
|
---|
44 | #include <fstream.h>
|
---|
45 | #else
|
---|
46 | #include <fstream>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | void recptconf::clear () {
|
---|
50 | gsdlhome.clear();
|
---|
51 | gdbmhome.clear();
|
---|
52 | httpprefix.clear();
|
---|
53 | httpimg = "/images";
|
---|
54 | gwcgi.clear();
|
---|
55 | macrofiles.erase(macrofiles.begin(), macrofiles.end());
|
---|
56 | saveconf.clear();
|
---|
57 | usecookies = false;
|
---|
58 | logcgiargs = false;
|
---|
59 | LogDateFormat = LocalTime;
|
---|
60 |
|
---|
61 | languages.erase(languages.begin(), languages.end());
|
---|
62 | encodings.erase(encodings.begin(), encodings.end());
|
---|
63 |
|
---|
64 | // these default page parameters can always be overriden
|
---|
65 | // in the configuration file
|
---|
66 | pageparams.erase(pageparams.begin(), pageparams.end());
|
---|
67 | pageparams["l"] = "en";
|
---|
68 |
|
---|
69 | #ifdef MACROPRECEDENCE
|
---|
70 | macroprecedence = MACROPRECEDENCE;
|
---|
71 | #else
|
---|
72 | macroprecedence.clear();
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 |
|
---|
76 | void languageinfo_t::clear () {
|
---|
77 | longname.clear();
|
---|
78 | defaultencoding.clear();
|
---|
79 | }
|
---|
80 |
|
---|
81 | receptionist::receptionist () {
|
---|
82 | // create a list of cgi arguments
|
---|
83 | // this must be done before the configuration
|
---|
84 |
|
---|
85 | cgiarginfo ainfo;
|
---|
86 |
|
---|
87 | ainfo.shortname = "e";
|
---|
88 | ainfo.longname = "compressed arguments";
|
---|
89 | ainfo.multiplechar = true;
|
---|
90 | ainfo.defaultstatus = cgiarginfo::good;
|
---|
91 | ainfo.argdefault = "";
|
---|
92 | ainfo.savedarginfo = cgiarginfo::mustnot;
|
---|
93 | argsinfo.addarginfo (NULL, ainfo);
|
---|
94 |
|
---|
95 | ainfo.shortname = "a";
|
---|
96 | ainfo.longname = "action";
|
---|
97 | ainfo.multiplechar = true;
|
---|
98 | ainfo.defaultstatus = cgiarginfo::none;
|
---|
99 | ainfo.argdefault = "";
|
---|
100 | ainfo.savedarginfo = cgiarginfo::must;
|
---|
101 | argsinfo.addarginfo (NULL, ainfo);
|
---|
102 |
|
---|
103 | // w=western
|
---|
104 | ainfo.shortname = "w";
|
---|
105 | ainfo.longname = "encoding";
|
---|
106 | ainfo.multiplechar = true;
|
---|
107 | ainfo.defaultstatus = cgiarginfo::none;
|
---|
108 | ainfo.argdefault = "";
|
---|
109 | ainfo.savedarginfo = cgiarginfo::must;
|
---|
110 | argsinfo.addarginfo (NULL, ainfo);
|
---|
111 |
|
---|
112 | ainfo.shortname = "nw";
|
---|
113 | ainfo.longname = "new encoding";
|
---|
114 | ainfo.multiplechar = true;
|
---|
115 | ainfo.defaultstatus = cgiarginfo::none;
|
---|
116 | ainfo.argdefault = "";
|
---|
117 | ainfo.savedarginfo = cgiarginfo::mustnot;
|
---|
118 | argsinfo.addarginfo (NULL, ainfo);
|
---|
119 |
|
---|
120 | // the interface language name should use the ISO 639
|
---|
121 | // standard
|
---|
122 | ainfo.shortname = "l";
|
---|
123 | ainfo.longname = "interface language";
|
---|
124 | ainfo.multiplechar = true;
|
---|
125 | ainfo.defaultstatus = cgiarginfo::weak;
|
---|
126 | ainfo.argdefault = "en";
|
---|
127 | ainfo.savedarginfo = cgiarginfo::must;
|
---|
128 | argsinfo.addarginfo (NULL, ainfo);
|
---|
129 |
|
---|
130 | ainfo.shortname = "nl";
|
---|
131 | ainfo.longname = "new language";
|
---|
132 | ainfo.multiplechar = false;
|
---|
133 | ainfo.defaultstatus = cgiarginfo::none;
|
---|
134 | ainfo.argdefault = "0";
|
---|
135 | ainfo.savedarginfo = cgiarginfo::mustnot;
|
---|
136 | argsinfo.addarginfo (NULL, ainfo);
|
---|
137 |
|
---|
138 | // the GSDL_UID (cookie)
|
---|
139 | ainfo.shortname = "z";
|
---|
140 | ainfo.longname = "gsdl uid";
|
---|
141 | ainfo.multiplechar = true;
|
---|
142 | ainfo.defaultstatus = cgiarginfo::none;
|
---|
143 | ainfo.argdefault = "";
|
---|
144 | ainfo.savedarginfo = cgiarginfo::mustnot;
|
---|
145 | argsinfo.addarginfo (NULL, ainfo);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | void receptionist::add_action (action *theaction) {
|
---|
150 | // make sure we have an action to add
|
---|
151 | if (theaction == NULL) return;
|
---|
152 |
|
---|
153 | // add this action to the list of actions
|
---|
154 | actions.addaction(theaction);
|
---|
155 |
|
---|
156 | // add the cgi arguments from this action
|
---|
157 | argsinfo.addarginfo (NULL, theaction->getargsinfo());
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | // configure should be called for each line in the
|
---|
162 | // configuration files to configure the receptionist and everything
|
---|
163 | // it contains. The configuration should take place after everything
|
---|
164 | // has been added but before the initialisation.
|
---|
165 |
|
---|
166 | void receptionist::configure (const text_t &key, const text_tarray &cfgline) {
|
---|
167 | // configure the receptionist
|
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 | if (cfgline.size() >= 1) {
|
---|
172 | cgiarginfo *info = NULL;
|
---|
173 | if (key == "gsdlhome") {
|
---|
174 | configinfo.gsdlhome = cfgline[0];
|
---|
175 | if (configinfo.gdbmhome.empty()) configinfo.gdbmhome = cfgline[0];
|
---|
176 | }
|
---|
177 | else if (key == "gdbmhome") configinfo.gdbmhome = cfgline[0];
|
---|
178 | else if (key == "httpprefix") configinfo.httpprefix = cfgline[0];
|
---|
179 | else if (key == "httpimg") configinfo.httpimg = cfgline[0];
|
---|
180 | else if (key == "gwcgi") configinfo.gwcgi = cfgline[0];
|
---|
181 | else if (key == "macrofiles") {
|
---|
182 | // want to append to macrofiles (i.e. may be several config files
|
---|
183 | // contributing, maybe from several collections).
|
---|
184 | text_tarray::const_iterator here = cfgline.begin();
|
---|
185 | text_tarray::const_iterator end = cfgline.end();
|
---|
186 | while (here != end) {
|
---|
187 | configinfo.macrofiles.insert (*here);
|
---|
188 | here ++;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | else if (key == "saveconf") configinfo.saveconf = cfgline[0];
|
---|
192 | else if (key == "usecookies") configinfo.usecookies = (cfgline[0] == "true");
|
---|
193 | else if (key == "logcgiargs") configinfo.logcgiargs = (cfgline[0] == "true");
|
---|
194 | else if (key == "LogDateFormat") {
|
---|
195 | if (cfgline[0] == "UTCTime") configinfo.LogDateFormat = UTCTime;
|
---|
196 | else if (cfgline[0] == "Absolute") configinfo.LogDateFormat = Absolute;
|
---|
197 | }
|
---|
198 | else if (key == "pageparam") {
|
---|
199 | if (cfgline.size() >= 2) configinfo.pageparams[cfgline[0]] = cfgline[1];
|
---|
200 | else configinfo.pageparams[cfgline[0]] = "";
|
---|
201 | }
|
---|
202 | else if (key == "macroprecedence") configinfo.macroprecedence = cfgline[0];
|
---|
203 | else if (key == "cgiarg") {
|
---|
204 | // get shortname
|
---|
205 | bool seen_defaultstatus = false;
|
---|
206 | text_t subkey, subvalue;
|
---|
207 | text_t shortname;
|
---|
208 | text_t::const_iterator cfglinesub_here;
|
---|
209 | text_tarray::const_iterator cfgline_here = cfgline.begin();
|
---|
210 | text_tarray::const_iterator cfgline_end = cfgline.end();
|
---|
211 | while (cfgline_here != cfgline_end) {
|
---|
212 | cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
|
---|
213 | (*cfgline_here).end(), '=', subkey);
|
---|
214 | if (subkey == "shortname") {
|
---|
215 | shortname = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
216 | }
|
---|
217 | cfgline_here++;
|
---|
218 | }
|
---|
219 |
|
---|
220 | // if we found the shortname process the line again filling in values
|
---|
221 | if (!shortname.empty()) {
|
---|
222 | cgiarginfo &chinfo = argsinfo[shortname];
|
---|
223 | chinfo.shortname = shortname; // in case this is a new argument
|
---|
224 |
|
---|
225 | cfgline_here = cfgline.begin();
|
---|
226 | while (cfgline_here != cfgline_end) {
|
---|
227 | cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
|
---|
228 | (*cfgline_here).end(), '=', subkey);
|
---|
229 | subvalue = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
230 |
|
---|
231 | if (subkey == "longname") chinfo.longname = subvalue;
|
---|
232 | else if (subkey == "multiplechar") chinfo.multiplechar = (subvalue == "true");
|
---|
233 | else if (subkey == "defaultstatus") {
|
---|
234 | seen_defaultstatus = true;
|
---|
235 | if (subvalue == "none") chinfo.defaultstatus = cgiarginfo::none;
|
---|
236 | else if (subvalue == "weak") chinfo.defaultstatus = cgiarginfo::weak;
|
---|
237 | else if (subvalue == "good") chinfo.defaultstatus = cgiarginfo::good;
|
---|
238 | else if (subvalue == "config") chinfo.defaultstatus = cgiarginfo::config;
|
---|
239 | else if (subvalue == "imperative") chinfo.defaultstatus = cgiarginfo::imperative;
|
---|
240 | }
|
---|
241 | else if (subkey == "argdefault") {
|
---|
242 | chinfo.argdefault = subvalue;
|
---|
243 | if (!seen_defaultstatus) chinfo.defaultstatus = cgiarginfo::config;
|
---|
244 | }
|
---|
245 | else if (subkey == "savedarginfo") {
|
---|
246 | if (subvalue == "mustnot") chinfo.savedarginfo = cgiarginfo::mustnot;
|
---|
247 | else if (subvalue == "can") chinfo.savedarginfo = cgiarginfo::can;
|
---|
248 | else if (subvalue == "must") chinfo.savedarginfo = cgiarginfo::must;
|
---|
249 | }
|
---|
250 |
|
---|
251 | cfgline_here++;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | } else if (key == "Encoding") {
|
---|
256 |
|
---|
257 | configure_encoding (cfgline);
|
---|
258 |
|
---|
259 | } else if (key == "Language") {
|
---|
260 | text_t subkey, subvalue, shortname;
|
---|
261 | languageinfo_t lang;
|
---|
262 | text_t::const_iterator cfglinesub_here;
|
---|
263 | text_tarray::const_iterator cfgline_here = cfgline.begin();
|
---|
264 | text_tarray::const_iterator cfgline_end = cfgline.end();
|
---|
265 | while (cfgline_here != cfgline_end) {
|
---|
266 | cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
|
---|
267 | (*cfgline_here).end(), '=', subkey);
|
---|
268 | if (subkey == "shortname") {
|
---|
269 | shortname = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
270 | } else if (subkey == "longname") {
|
---|
271 | lang.longname = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
272 | } else if (subkey == "default_encoding") {
|
---|
273 | lang.defaultencoding = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
274 | }
|
---|
275 | cfgline_here++;
|
---|
276 | }
|
---|
277 | if (!shortname.empty()) {
|
---|
278 | if (lang.longname.empty()) lang.longname = shortname;
|
---|
279 | configinfo.languages[shortname] = lang;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | // configure the actions
|
---|
285 | actionptrmap::iterator actionhere = actions.begin ();
|
---|
286 | actionptrmap::iterator actionend = actions.end ();
|
---|
287 |
|
---|
288 | while (actionhere != actionend) {
|
---|
289 | assert ((*actionhere).second.a != NULL);
|
---|
290 | if ((*actionhere).second.a != NULL)
|
---|
291 | (*actionhere).second.a->configure(key, cfgline);
|
---|
292 |
|
---|
293 | actionhere++;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | void receptionist::configure (const text_t &key, const text_t &value) {
|
---|
299 | text_tarray cfgline;
|
---|
300 | cfgline.push_back (value);
|
---|
301 | configure(key, cfgline);
|
---|
302 | }
|
---|
303 |
|
---|
304 | bool receptionist::init (ostream &logout) {
|
---|
305 |
|
---|
306 | // read in the macro files
|
---|
307 | if (!read_macrofiles (logout)) return false;
|
---|
308 |
|
---|
309 | // there must be at least one action defined
|
---|
310 | if (actions.empty()) {
|
---|
311 | logout << "Error: no actions have been added to the receptionist\n";
|
---|
312 | return false;
|
---|
313 | }
|
---|
314 |
|
---|
315 | // create a saveconf string if there isn't one already
|
---|
316 | if (configinfo.saveconf.empty())
|
---|
317 | configinfo.saveconf = create_save_conf_str (argsinfo, logout);
|
---|
318 |
|
---|
319 | // check the saveconf string
|
---|
320 | if (!check_save_conf_str (configinfo.saveconf, argsinfo, logout))
|
---|
321 | return false;
|
---|
322 |
|
---|
323 | // set a random seed
|
---|
324 | srand (time(NULL));
|
---|
325 |
|
---|
326 | // init the actions
|
---|
327 | actionptrmap::iterator actionhere = actions.begin ();
|
---|
328 | actionptrmap::iterator actionend = actions.end ();
|
---|
329 | while (actionhere != actionend) {
|
---|
330 | if (((*actionhere).second.a == NULL) ||
|
---|
331 | !(*actionhere).second.a->init(logout)) return false;
|
---|
332 | actionhere++;
|
---|
333 | }
|
---|
334 |
|
---|
335 | return true;
|
---|
336 | }
|
---|
337 |
|
---|
338 | // get the default encoding for the given language - if it fails for any
|
---|
339 | // reason return ""
|
---|
340 | text_t receptionist::get_default_encoding (const text_t &language) {
|
---|
341 |
|
---|
342 | // make sure language is valid
|
---|
343 | if (configinfo.languages.find(language) == configinfo.languages.end()) return "";
|
---|
344 |
|
---|
345 | text_t default_encoding = configinfo.languages[language].defaultencoding;
|
---|
346 |
|
---|
347 | // make sure the encoding is valid
|
---|
348 | if (converters.find(default_encoding) == converters.end()) return "";
|
---|
349 |
|
---|
350 | return default_encoding;
|
---|
351 | }
|
---|
352 |
|
---|
353 | // parse_cgi_args parses cgi arguments into an argument class.
|
---|
354 | // This function should be called for each page request. It returns false
|
---|
355 | // if there was a major problem with the cgi arguments.
|
---|
356 | bool receptionist::parse_cgi_args (const text_t &argstr, cgiargsclass &args,
|
---|
357 | ostream &logout) {
|
---|
358 |
|
---|
359 | // get an initial list of cgi arguments
|
---|
360 | args.clear();
|
---|
361 | split_cgi_args (argsinfo, argstr, args);
|
---|
362 |
|
---|
363 | // expand the compressed argument (if there was one)
|
---|
364 | if (!expand_save_args (argsinfo, configinfo.saveconf, args, logout)) return false;
|
---|
365 |
|
---|
366 | // add the defaults
|
---|
367 | add_default_args (argsinfo, args, logout);
|
---|
368 |
|
---|
369 | // get the cookie
|
---|
370 | if (configinfo.usecookies) get_cookie(args["z"]);
|
---|
371 |
|
---|
372 | // if we're changing languages, set the encoding to the default for the new language
|
---|
373 | if (args["nl"] == "1") {
|
---|
374 | args["nw"] = get_default_encoding(args["l"]);
|
---|
375 | }
|
---|
376 |
|
---|
377 | // get the input encoding
|
---|
378 | // if encoding isn't set, set it to the default for the current language
|
---|
379 | if ((args.getarg("w") == NULL) || args["w"].empty()) {
|
---|
380 | args["w"] = get_default_encoding(args["l"]);
|
---|
381 | }
|
---|
382 |
|
---|
383 | text_t &arg_w = args["w"];
|
---|
384 |
|
---|
385 | inconvertclass defaultinconvert;
|
---|
386 | inconvertclass *inconvert = converters.get_inconverter (arg_w);
|
---|
387 | if (inconvert == NULL) inconvert = &defaultinconvert;
|
---|
388 |
|
---|
389 | // see if the next page will have a different encoding
|
---|
390 | if (args.getarg("nw") != NULL) arg_w = args["nw"];
|
---|
391 |
|
---|
392 | // convert arguments which aren't in unicode to unicode
|
---|
393 | args_tounicode (args, *inconvert);
|
---|
394 |
|
---|
395 |
|
---|
396 | // decide on the output conversion class (needed for checking the external
|
---|
397 | // cgi arguments)
|
---|
398 | rzwsoutconvertclass defaultoutconverter;
|
---|
399 | rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
|
---|
400 | if (outconverter == NULL) outconverter = &defaultoutconverter;
|
---|
401 | outconverter->reset();
|
---|
402 |
|
---|
403 | // check the arguments for the action
|
---|
404 | action *a = actions.getaction (args["a"]);
|
---|
405 | if (a != NULL) {
|
---|
406 | if (!a->check_cgiargs (argsinfo, args, logout)) return false;
|
---|
407 | } else {
|
---|
408 | // the action was not found!!
|
---|
409 | outconvertclass text_t2ascii;
|
---|
410 | logout << text_t2ascii << "Error: the action \"" << args["a"]
|
---|
411 | << "\" could not be found.\n";
|
---|
412 | return false;
|
---|
413 | }
|
---|
414 |
|
---|
415 | // check external cgi arguments for each action
|
---|
416 | actionptrmap::iterator actionhere = actions.begin ();
|
---|
417 | actionptrmap::iterator actionend = actions.end ();
|
---|
418 | while (actionhere != actionend) {
|
---|
419 | assert ((*actionhere).second.a != NULL);
|
---|
420 | if ((*actionhere).second.a != NULL) {
|
---|
421 | if (!(*actionhere).second.a->check_external_cgiargs (argsinfo, args, *outconverter,
|
---|
422 | configinfo.saveconf, logout))
|
---|
423 | return false;
|
---|
424 | }
|
---|
425 | actionhere++;
|
---|
426 | }
|
---|
427 |
|
---|
428 | // the action might have changed but we will assume that
|
---|
429 | // the cgiargs were checked properly when the change was made
|
---|
430 |
|
---|
431 | return true;
|
---|
432 | }
|
---|
433 |
|
---|
434 | // returns true if cookie already existed, false
|
---|
435 | // if it was generated
|
---|
436 | bool receptionist::get_cookie (text_t &cookie) {
|
---|
437 |
|
---|
438 | text_t cookiestring = gsdl_getenv ("HTTP_COOKIE");
|
---|
439 |
|
---|
440 | text_t::const_iterator end = cookiestring.end();
|
---|
441 | text_t::const_iterator here = findchar ((text_t::const_iterator)cookiestring.begin(), end, 'G');
|
---|
442 |
|
---|
443 | while (here+9 < end) {
|
---|
444 |
|
---|
445 | if (substr(here, here+8) == "GSDL_UID") {
|
---|
446 | cookie = substr (here+9, findchar (here+9, end, ';'));
|
---|
447 | return true;
|
---|
448 | }
|
---|
449 | here = findchar ((text_t::const_iterator)cookiestring.begin(), end, 'G');
|
---|
450 | }
|
---|
451 |
|
---|
452 | cookie.clear();
|
---|
453 | text_t host = gsdl_getenv("REMOTE_ADDR");
|
---|
454 | time_t ttime = time(NULL);
|
---|
455 | if (!host.empty()) {
|
---|
456 | cookie += host;
|
---|
457 | cookie.push_back ('-');
|
---|
458 | }
|
---|
459 | cookie += text_t(ttime);
|
---|
460 |
|
---|
461 | return false;
|
---|
462 | }
|
---|
463 |
|
---|
464 | // as above but just tests if cookie exists
|
---|
465 | bool receptionist::get_cookie () {
|
---|
466 |
|
---|
467 | text_t c = gsdl_getenv("HTTP_COOKIE");
|
---|
468 | if (!c.empty()) {
|
---|
469 | text_t cookiestring = c;
|
---|
470 |
|
---|
471 | text_t::const_iterator end = cookiestring.end();
|
---|
472 | text_t::const_iterator here = findchar ((text_t::const_iterator)cookiestring.begin(), end, 'G');
|
---|
473 |
|
---|
474 | while (here+9 < end) {
|
---|
475 | if (substr(here, here+8) == "GSDL_UID") return true;
|
---|
476 | here = findchar ((text_t::const_iterator)cookiestring.begin(), end, 'G');
|
---|
477 | }
|
---|
478 | }
|
---|
479 | return false;
|
---|
480 | }
|
---|
481 |
|
---|
482 | bool receptionist::log_cgi_args (cgiargsclass &args, ostream &logout) {
|
---|
483 |
|
---|
484 | // see if we want to log the cgi arguments
|
---|
485 | if (!configinfo.logcgiargs) return true;
|
---|
486 |
|
---|
487 | text_t host = gsdl_getenv ("REMOTE_HOST");
|
---|
488 | text_t script_name = gsdl_getenv ("SCRIPT_NAME");
|
---|
489 | if (host.empty()) host = gsdl_getenv ("REMOTE_ADDR");
|
---|
490 | text_t browser = gsdl_getenv ("HTTP_USER_AGENT");
|
---|
491 |
|
---|
492 | cgiargsclass::const_iterator args_here = args.begin();
|
---|
493 | cgiargsclass::const_iterator args_end = args.end();
|
---|
494 |
|
---|
495 | text_t argstr;
|
---|
496 | bool first = true;
|
---|
497 | while (args_here != args_end) {
|
---|
498 | if (!first) argstr += ", ";
|
---|
499 | argstr += (*args_here).first + "=" + (*args_here).second.value;
|
---|
500 | first = false;
|
---|
501 | args_here ++;
|
---|
502 | }
|
---|
503 |
|
---|
504 | text_t logfile = filename_cat (configinfo.gdbmhome, "etc", "usage.txt");
|
---|
505 |
|
---|
506 | text_t logstr = script_name;
|
---|
507 | logstr += " " + host;
|
---|
508 | logstr += " [";
|
---|
509 | if (configinfo.LogDateFormat == UTCTime) {
|
---|
510 | logstr += get_date (false);
|
---|
511 | } else if (configinfo.LogDateFormat == Absolute) {
|
---|
512 | time_t ttime = time(NULL);
|
---|
513 | logstr += ttime;
|
---|
514 | } else {
|
---|
515 | // LocalTime
|
---|
516 | logstr += get_date (true);
|
---|
517 | }
|
---|
518 | logstr += "] (" + argstr + ") \"";
|
---|
519 | logstr += browser;
|
---|
520 | logstr += "\"\n";
|
---|
521 |
|
---|
522 | return append_logstr (logfile, logstr, logout);
|
---|
523 | }
|
---|
524 |
|
---|
525 | bool receptionist::append_logstr (const text_t &filename, const text_t &logstr,
|
---|
526 | ostream &logout) {
|
---|
527 |
|
---|
528 | utf8outconvertclass text_t2utf8;
|
---|
529 | char *lfile = filename.getcstr();
|
---|
530 |
|
---|
531 | int fd = open(lfile, O_WRONLY | O_APPEND);
|
---|
532 |
|
---|
533 | if (fd == -1) {
|
---|
534 | logout << "Error: Couldn't open file " << lfile << "\n";
|
---|
535 | delete lfile;
|
---|
536 | return false;
|
---|
537 | }
|
---|
538 |
|
---|
539 | // lock_val is set to 0 if file is locked successfully
|
---|
540 | int lock_val = 1;
|
---|
541 | GSDL_LOCK_FILE (fd);
|
---|
542 | if (lock_val == 0) {
|
---|
543 | text_t tmp_log_str(logstr); // so we don't pass a const to setinput...
|
---|
544 | text_t2utf8.setinput(&tmp_log_str);
|
---|
545 | char *buffer=new char[logstr.size()];
|
---|
546 | size_t num_chars;
|
---|
547 | convertclass::status_t status;
|
---|
548 | text_t2utf8.convert(buffer, logstr.size(), num_chars, status);
|
---|
549 | // ignore status - assume it is "finished" as buffer is big enough
|
---|
550 | write(fd, buffer, num_chars);
|
---|
551 | GSDL_UNLOCK_FILE (fd);
|
---|
552 | delete buffer;
|
---|
553 | } else {
|
---|
554 | logout << "Error: Couldn't lock file " << lfile << "\n";
|
---|
555 | close(fd);
|
---|
556 | delete lfile;
|
---|
557 | return false;
|
---|
558 | }
|
---|
559 |
|
---|
560 | close(fd);
|
---|
561 |
|
---|
562 | delete lfile;
|
---|
563 | return true;
|
---|
564 | }
|
---|
565 |
|
---|
566 | text_t receptionist::expandmacros (const text_t &astring, cgiargsclass &args,
|
---|
567 | ostream &logout) {
|
---|
568 | text_t outstring;
|
---|
569 | outconvertclass text_t2ascii;
|
---|
570 |
|
---|
571 | action *a = actions.getaction (args["a"]);
|
---|
572 | prepare_page (a, args, text_t2ascii, logout);
|
---|
573 | disp.expandstring ("Global", astring, outstring);
|
---|
574 | return outstring;
|
---|
575 | }
|
---|
576 |
|
---|
577 | // produce_cgi_page will call get_cgihead_info and
|
---|
578 | // produce_content in the appropriate way to output a cgi header and
|
---|
579 | // the page content (if needed). If a page could not be created it
|
---|
580 | // will return false
|
---|
581 | bool receptionist::produce_cgi_page (cgiargsclass &args, ostream &contentout,
|
---|
582 | ostream &logout) {
|
---|
583 | outconvertclass text_t2ascii;
|
---|
584 |
|
---|
585 | response_t response;
|
---|
586 | text_t response_data;
|
---|
587 |
|
---|
588 | // produce cgi header
|
---|
589 | get_cgihead_info (args, response, response_data, logout);
|
---|
590 | if (response == location) {
|
---|
591 | // location response (url may contain macros!!)
|
---|
592 | response_data = expandmacros (response_data, args, logout);
|
---|
593 | contentout << text_t2ascii << "Location: " << response_data << "\n\n";
|
---|
594 | contentout << flush;
|
---|
595 | return true;
|
---|
596 | } else if (response == content) {
|
---|
597 | // content response
|
---|
598 |
|
---|
599 | #ifdef GSDL_NOCACHE
|
---|
600 | contentout << "Expires: Mon, 26 Jul 1997 05:00:00 GMT\n"; // date in the past
|
---|
601 | tm *tm_ptr = NULL;
|
---|
602 | time_t t = time(NULL);
|
---|
603 | tm_ptr = gmtime (&t);
|
---|
604 | if (tm_ptr != NULL) {
|
---|
605 | char *timestr = new char[128];
|
---|
606 | strftime (timestr, 128, "%a, %d %b %Y %H:%M:%S", tm_ptr);
|
---|
607 | contentout << "Last-Modified: " << timestr << " GMT\n"; // always modified
|
---|
608 | delete timestr;
|
---|
609 | }
|
---|
610 | contentout << "Cache-Control: no-cache, must-revalidate\n"; // HTTP/1.1
|
---|
611 | contentout << "Pragma: no-cache\n"; // HTTP/1.0
|
---|
612 |
|
---|
613 | #endif
|
---|
614 |
|
---|
615 | contentout << text_t2ascii << "Content-type: " << response_data << "\n\n";
|
---|
616 | } else {
|
---|
617 | // unknown response
|
---|
618 | logout << "Error: get_cgihead_info returned an unknown response type.\n";
|
---|
619 | return false;
|
---|
620 | }
|
---|
621 |
|
---|
622 | // produce cgi page
|
---|
623 | if (!produce_content (args, contentout, logout)) return false;
|
---|
624 |
|
---|
625 | // flush contentout
|
---|
626 | contentout << flush;
|
---|
627 | return true;
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | // get_cgihead_info determines the cgi header information for
|
---|
632 | // a set of cgi arguments. If response contains location then
|
---|
633 | // response_data contains the redirect address. If reponse
|
---|
634 | // contains content then reponse_data contains the content-type.
|
---|
635 | // Note that images can now be produced by the receptionist.
|
---|
636 | void receptionist::get_cgihead_info (cgiargsclass &args, response_t &response,
|
---|
637 | text_t &response_data, ostream &logout) {
|
---|
638 | outconvertclass text_t2ascii;
|
---|
639 |
|
---|
640 | // get the action
|
---|
641 | action *a = actions.getaction (args["a"]);
|
---|
642 | if (a != NULL) {
|
---|
643 | a->get_cgihead_info (args, response, response_data, logout);
|
---|
644 |
|
---|
645 | } else {
|
---|
646 | // the action was not found!!
|
---|
647 | logout << text_t2ascii << "Error receptionist::get_cgihead_info: the action \""
|
---|
648 | << args["a"] << "\" could not be found.\n";
|
---|
649 | response = content;
|
---|
650 | response_data = "text/html";
|
---|
651 | }
|
---|
652 |
|
---|
653 | // add the encoding information
|
---|
654 | if (response == content) {
|
---|
655 | if (converters.find(args["w"]) != converters.end()) {
|
---|
656 | response_data += "; charset=" + args["w"];
|
---|
657 | } else {
|
---|
658 | // default to latin 1
|
---|
659 | response_data += "; charset=ISO-8859-1";
|
---|
660 | }
|
---|
661 |
|
---|
662 | // add cookie if required
|
---|
663 | if (configinfo.usecookies && !get_cookie())
|
---|
664 | response_data += "\nSet-Cookie: GSDL_UID=" + args["z"]
|
---|
665 | + "; expires=25-Dec-37 00:00:00 GMT";
|
---|
666 | }
|
---|
667 | }
|
---|
668 |
|
---|
669 |
|
---|
670 | // produce the page content
|
---|
671 | bool receptionist::produce_content (cgiargsclass &args, ostream &contentout,
|
---|
672 | ostream &logout) {
|
---|
673 |
|
---|
674 | // decide on the output conversion class
|
---|
675 | text_t &arg_w = args["w"];
|
---|
676 | rzwsoutconvertclass defaultoutconverter;
|
---|
677 | rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
|
---|
678 | if (outconverter == NULL) outconverter = &defaultoutconverter;
|
---|
679 | outconverter->reset();
|
---|
680 |
|
---|
681 | // needed for 16-bit unicode only - big endian marker 0xfeff (RFC 2781)
|
---|
682 | if (arg_w=="utf-16") {
|
---|
683 | contentout << '\xfe' << '\xff' ;
|
---|
684 | }
|
---|
685 |
|
---|
686 | // produce the page using the desired action
|
---|
687 | action *a = actions.getaction (args["a"]);
|
---|
688 | if (a != NULL) {
|
---|
689 | if (a->uses_display(args)) prepare_page (a, args, (*outconverter), logout);
|
---|
690 | if (!a->do_action (args, disp, (*outconverter), contentout, logout))
|
---|
691 | return false;
|
---|
692 | } else {
|
---|
693 | // the action was not found!!
|
---|
694 | outconvertclass text_t2ascii;
|
---|
695 |
|
---|
696 | logout << text_t2ascii << "Error receptionist::produce_content: the action \""
|
---|
697 | << args["a"] << "\" could not be found.\n";
|
---|
698 |
|
---|
699 | contentout << (*outconverter)
|
---|
700 | << "<html>\n"
|
---|
701 | << "<head>\n"
|
---|
702 | << "<title>Error</title>\n"
|
---|
703 | << "</head>\n"
|
---|
704 | << "<body>\n"
|
---|
705 | << "<h2>Oops!</h2>\n"
|
---|
706 | << "Undefined Page. The action \""
|
---|
707 | << args["a"] << "\" could not be found.\n"
|
---|
708 | << "</body>\n"
|
---|
709 | << "</html>\n";
|
---|
710 | }
|
---|
711 | return true;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | // returns the compressed argument ("e") corresponding to the argument
|
---|
716 | // list. This can be used to save preferences between sessions.
|
---|
717 | text_t receptionist::get_compressed_arg (cgiargsclass &args, ostream &logout) {
|
---|
718 | // decide on the output conversion class
|
---|
719 | text_t &arg_w = args["w"];
|
---|
720 | rzwsoutconvertclass defaultoutconverter;
|
---|
721 | rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
|
---|
722 | if (outconverter == NULL) outconverter = &defaultoutconverter;
|
---|
723 | outconverter->reset();
|
---|
724 |
|
---|
725 | text_t compressed_args;
|
---|
726 | if (compress_save_args (argsinfo, configinfo.saveconf, args,
|
---|
727 | compressed_args, *outconverter, logout))
|
---|
728 | return compressed_args;
|
---|
729 |
|
---|
730 | return "";
|
---|
731 | }
|
---|
732 |
|
---|
733 |
|
---|
734 | // will read in all the macro files. If one is not found an
|
---|
735 | // error message will be written to logout and the method will
|
---|
736 | // return false.
|
---|
737 | bool receptionist::read_macrofiles (ostream &logout) {
|
---|
738 | outconvertclass text_t2ascii;
|
---|
739 |
|
---|
740 | // redirect the error output to logout
|
---|
741 | ostream *savedlogout = disp.setlogout (&logout);
|
---|
742 |
|
---|
743 | text_t gsdlmacrodir = filename_cat (configinfo.gsdlhome, "macros");
|
---|
744 |
|
---|
745 | text_tset::iterator arrhere = configinfo.macrofiles.begin();
|
---|
746 | text_tset::iterator arrend = configinfo.macrofiles.end();
|
---|
747 | text_t filename;
|
---|
748 | while (arrhere != arrend) {
|
---|
749 | filename = filename_cat (gsdlmacrodir, *arrhere);
|
---|
750 | if (file_exists (filename)) {
|
---|
751 | disp.loaddefaultmacros(filename);
|
---|
752 | } else {
|
---|
753 | logout << text_t2ascii
|
---|
754 | << "Error: the macro file \"" << *arrhere << "\" could not be found.\n";
|
---|
755 | disp.setlogout (savedlogout);
|
---|
756 | return false;
|
---|
757 | }
|
---|
758 | arrhere ++;
|
---|
759 | }
|
---|
760 |
|
---|
761 | // success
|
---|
762 |
|
---|
763 | // reset logout to what it was
|
---|
764 | disp.setlogout (savedlogout);
|
---|
765 | return true;
|
---|
766 | }
|
---|
767 |
|
---|
768 | // prepare_page sets up page parameters, sets display macros
|
---|
769 | // and opens the page ready for output
|
---|
770 | void receptionist::prepare_page (action *a, cgiargsclass &args,
|
---|
771 | outconvertclass &outconvert,
|
---|
772 | ostream &logout) {
|
---|
773 | // set up page parameters
|
---|
774 | text_t pageparams;
|
---|
775 | bool first = true;
|
---|
776 |
|
---|
777 | text_tmap::iterator params_here = configinfo.pageparams.begin();
|
---|
778 | text_tmap::iterator params_end = configinfo.pageparams.end();
|
---|
779 | while (params_here != params_end) {
|
---|
780 | if (args[(*params_here).first] != (*params_here).second) {
|
---|
781 | if (!first) pageparams += ",";
|
---|
782 | first = false;
|
---|
783 | pageparams += (*params_here).first;
|
---|
784 | pageparams += "=";
|
---|
785 | pageparams += args[(*params_here).first];
|
---|
786 | }
|
---|
787 |
|
---|
788 | params_here++;
|
---|
789 | }
|
---|
790 |
|
---|
791 |
|
---|
792 | // open the page
|
---|
793 | disp.openpage(pageparams, configinfo.macroprecedence);
|
---|
794 |
|
---|
795 | // define external macros for each action
|
---|
796 | actionptrmap::iterator actionhere = actions.begin ();
|
---|
797 | actionptrmap::iterator actionend = actions.end ();
|
---|
798 |
|
---|
799 | while (actionhere != actionend) {
|
---|
800 | assert ((*actionhere).second.a != NULL);
|
---|
801 | if ((*actionhere).second.a != NULL) {
|
---|
802 | (*actionhere).second.a->define_external_macros (disp, args, logout);
|
---|
803 | }
|
---|
804 | actionhere++;
|
---|
805 | }
|
---|
806 |
|
---|
807 | // define internal macros for the current action
|
---|
808 | a->define_internal_macros (disp, args, logout);
|
---|
809 |
|
---|
810 | // define general macros. the defining of general macros is done here so that
|
---|
811 | // the last possible version of the cgi arguments are used
|
---|
812 | define_general_macros (args, outconvert, logout);
|
---|
813 | }
|
---|
814 |
|
---|
815 | void receptionist::define_general_macros (cgiargsclass &args, outconvertclass &/*outconvert*/,
|
---|
816 | ostream &logout) {
|
---|
817 |
|
---|
818 | disp.setmacro ("gsdlhome", "Global", dm_safe(configinfo.gsdlhome));
|
---|
819 | disp.setmacro ("gwcgi", "Global", configinfo.gwcgi);
|
---|
820 | disp.setmacro ("httpimg", "Global", configinfo.httpimg);
|
---|
821 | disp.setmacro ("httpprefix", "Global", configinfo.httpprefix);
|
---|
822 |
|
---|
823 | text_t compressedoptions = get_compressed_arg(args, logout);
|
---|
824 | disp.setmacro ("compressedoptions", "Global", dm_safe(compressedoptions));
|
---|
825 | // need a decoded version of compressedoptions for use within forms
|
---|
826 | // as browsers encode values from forms before sending to server
|
---|
827 | // (e.g. %25 becomes %2525)
|
---|
828 | decode_cgi_arg (compressedoptions);
|
---|
829 | disp.setmacro ("decodedcompressedoptions", "Global", dm_safe(compressedoptions));
|
---|
830 |
|
---|
831 | // set _cgiargX_ macros for each cgi argument
|
---|
832 | cgiargsclass::const_iterator argshere = args.begin();
|
---|
833 | cgiargsclass::const_iterator argsend = args.end();
|
---|
834 | while (argshere != argsend) {
|
---|
835 | disp.setmacro ("cgiarg" + (*argshere).first, "Global", dm_safe((*argshere).second.value));
|
---|
836 | argshere ++;
|
---|
837 | }
|
---|
838 | }
|
---|
839 |
|
---|
840 | // Handles an "Encoding" line from a configuration file - note that the
|
---|
841 | // configinfo.encodings map is a bit of a hack (to be fixed when the
|
---|
842 | // configuration files are tidied up).
|
---|
843 | void receptionist::configure_encoding (const text_tarray &cfgline) {
|
---|
844 |
|
---|
845 | text_t subkey, subvalue, shortname, longname, mapfile;
|
---|
846 | int multibyte = 0;
|
---|
847 | text_t::const_iterator cfglinesub_here;
|
---|
848 | text_tarray::const_iterator cfgline_here = cfgline.begin();
|
---|
849 | text_tarray::const_iterator cfgline_end = cfgline.end();
|
---|
850 | while (cfgline_here != cfgline_end) {
|
---|
851 | if (*cfgline_here == "multibyte") {
|
---|
852 | multibyte = 1;
|
---|
853 | } else {
|
---|
854 | cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
|
---|
855 | (*cfgline_here).end(), '=', subkey);
|
---|
856 | if (subkey == "shortname") {
|
---|
857 | shortname = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
858 | } else if (subkey == "longname") {
|
---|
859 | longname = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
860 | } else if (subkey == "map") {
|
---|
861 | mapfile = substr (cfglinesub_here, (*cfgline_here).end());
|
---|
862 | }
|
---|
863 | }
|
---|
864 | cfgline_here++;
|
---|
865 | }
|
---|
866 | if (!shortname.empty()) {
|
---|
867 | if (longname.empty()) longname = shortname;
|
---|
868 |
|
---|
869 | // add the converter
|
---|
870 | if (shortname == "utf-8") {
|
---|
871 | utf8inconvertclass *utf8inconvert = new utf8inconvertclass();
|
---|
872 | utf8outconvertclass *utf8outconvert = new utf8outconvertclass();
|
---|
873 | utf8outconvert->set_rzws(1);
|
---|
874 | add_converter (shortname, utf8inconvert, utf8outconvert);
|
---|
875 | configinfo.encodings[longname] = shortname;
|
---|
876 |
|
---|
877 | } else if (shortname == "utf-16") {
|
---|
878 | // we use the default input converter as this shouldn't ever be used
|
---|
879 | // for converting from unicode...
|
---|
880 | inconvertclass *inconverter = new inconvertclass();
|
---|
881 | utf16outconvertclass *outconverter = new utf16outconvertclass();
|
---|
882 | add_converter (shortname, inconverter, outconverter);
|
---|
883 | configinfo.encodings[longname] = shortname;
|
---|
884 |
|
---|
885 | } else if (!mapfile.empty()) {
|
---|
886 |
|
---|
887 | if (mapfile == "8859_1.ump") {
|
---|
888 | // iso-8859-1 is a special case as it'll always be supported by the
|
---|
889 | // standard converter class and therefore doesn't need to use its
|
---|
890 | // mapping file
|
---|
891 | inconvertclass *inconvert = new inconvertclass();
|
---|
892 | rzwsoutconvertclass *outconvert = new rzwsoutconvertclass();
|
---|
893 | outconvert->set_rzws(1);
|
---|
894 | add_converter (shortname, inconvert, outconvert);
|
---|
895 | configinfo.encodings[longname] = shortname;
|
---|
896 |
|
---|
897 | } else {
|
---|
898 | text_t to_uc_map = filename_cat(configinfo.gsdlhome, "mappings", "to_uc", mapfile);
|
---|
899 | text_t from_uc_map = filename_cat(configinfo.gsdlhome, "mappings", "from_uc", mapfile);
|
---|
900 | if (file_exists(to_uc_map) && file_exists(from_uc_map)) {
|
---|
901 |
|
---|
902 | mapinconvertclass *mapinconvert = new mapinconvertclass();
|
---|
903 | mapinconvert->setmapfile (to_uc_map, 0x003F);
|
---|
904 | mapinconvert->set_multibyte (multibyte);
|
---|
905 | mapoutconvertclass *mapoutconvert = new mapoutconvertclass();
|
---|
906 | mapoutconvert->setmapfile (from_uc_map, 0x3F);
|
---|
907 | mapoutconvert->set_multibyte (multibyte);
|
---|
908 | mapoutconvert->set_rzws(1);
|
---|
909 | add_converter (shortname, mapinconvert, mapoutconvert);
|
---|
910 | configinfo.encodings[longname] = shortname;
|
---|
911 | }
|
---|
912 | }
|
---|
913 | }
|
---|
914 | }
|
---|
915 | }
|
---|