source: main/trunk/greenstone2/runtime-src/src/recpt/receptionist.cpp@ 22766

Last change on this file since 22766 was 22231, checked in by mdewsnip, 14 years ago

Fix assert() failure that looks like a segmentation fault when users enter "a=" (without any action name).

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 56.7 KB
Line 
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 "recptprototools.h"
32#include "fileutil.h"
33#include "cgiutils.h"
34#include "htmlutils.h"
35#include "gsdltools.h"
36#include "gsdltimes.h"
37#include "OIDtools.h"
38#include <assert.h>
39#include <time.h>
40#include <stdio.h> // for open()
41#include <fcntl.h> // for open() flags
42// following 2 are for printing Last-Modified http header.
43#include <sys/stat.h>
44#include <time.h>
45
46#if defined (GSDL_USE_IOS_H)
47#include <fstream.h>
48#else
49#include <fstream>
50#endif
51
52void recptconf::clear () {
53 gsdlhome.clear();
54 collecthome.clear();
55 dbhome.clear();
56 collectinfo.erase(collectinfo.begin(), collectinfo.end());
57 collection.clear();
58 collectdir.clear();
59 httpprefix.clear();
60 httpweb.clear();
61 gwcgi.clear();
62 macrofiles.erase(macrofiles.begin(), macrofiles.end());
63 saveconf.clear();
64 usecookies = false;
65 logcgiargs = false;
66 LogDateFormat = LocalTime;
67
68 maintainer.clear();
69 MailServer.clear();
70 LogEvents = Disabled;
71 EmailEvents = Disabled;
72 EmailUserEvents = false;
73
74 languages.erase(languages.begin(), languages.end());
75 encodings.erase(encodings.begin(), encodings.end());
76
77 site_auth = false;
78 HomePageType = "images";
79 HomePageCols = 3;
80
81 // these default page parameters can always be overriden
82 // in the configuration file
83 pageparams.erase(pageparams.begin(), pageparams.end());
84 pageparams["c"] = "";
85 pageparams["l"] = "en";
86
87#ifdef MACROPRECEDENCE
88 macroprecedence = MACROPRECEDENCE;
89#else
90 macroprecedence.clear();
91#endif
92}
93
94
95void collectioninfo_t::clear () {
96 gsdl_gsdlhome.clear();
97 gsdl_dbhome.clear();
98
99 info_loaded = false;
100 info.clear();
101}
102
103void languageinfo_t::clear () {
104 longname.clear();
105 defaultencoding.clear();
106}
107
108receptionist::receptionist () {
109 // create a list of cgi arguments
110 // this must be done before the configuration
111
112 cgiarginfo ainfo;
113
114 ainfo.shortname = "e";
115 ainfo.longname = "compressed arguments";
116 ainfo.multiplechar = true;
117 ainfo.defaultstatus = cgiarginfo::good;
118 ainfo.argdefault = g_EmptyText;
119 ainfo.savedarginfo = cgiarginfo::mustnot;
120 argsinfo.addarginfo (NULL, ainfo);
121
122 ainfo.shortname = "a";
123 ainfo.longname = "action";
124 ainfo.multiplechar = true;
125 ainfo.defaultstatus = cgiarginfo::none;
126 ainfo.argdefault = g_EmptyText;
127 ainfo.savedarginfo = cgiarginfo::must;
128 argsinfo.addarginfo (NULL, ainfo);
129
130 // w=western
131 ainfo.shortname = "w";
132 ainfo.longname = "encoding";
133 ainfo.multiplechar = true;
134 ainfo.defaultstatus = cgiarginfo::none;
135 ainfo.argdefault = g_EmptyText;
136 ainfo.savedarginfo = cgiarginfo::must;
137 argsinfo.addarginfo (NULL, ainfo);
138
139 ainfo.shortname = "nw";
140 ainfo.longname = "new encoding";
141 ainfo.multiplechar = true;
142 ainfo.defaultstatus = cgiarginfo::none;
143 ainfo.argdefault = g_EmptyText;
144 ainfo.savedarginfo = cgiarginfo::mustnot;
145 argsinfo.addarginfo (NULL, ainfo);
146
147 ainfo.shortname = "c";
148 ainfo.longname = "collection";
149 ainfo.multiplechar = true;
150 ainfo.defaultstatus = cgiarginfo::none;
151 ainfo.argdefault = g_EmptyText;
152 ainfo.savedarginfo = cgiarginfo::must;
153 argsinfo.addarginfo (NULL, ainfo);
154
155 // the interface language name should use the ISO 639
156 // standard
157 ainfo.shortname = "l";
158 ainfo.longname = "interface language";
159 ainfo.multiplechar = true;
160 ainfo.defaultstatus = cgiarginfo::weak;
161 ainfo.argdefault = "en";
162 ainfo.savedarginfo = cgiarginfo::must;
163 argsinfo.addarginfo (NULL, ainfo);
164
165 ainfo.shortname = "nl";
166 ainfo.longname = "new language";
167 ainfo.multiplechar = false;
168 ainfo.defaultstatus = cgiarginfo::none;
169 ainfo.argdefault = "0";
170 ainfo.savedarginfo = cgiarginfo::mustnot;
171 argsinfo.addarginfo (NULL, ainfo);
172
173 // the GSDL_UID (cookie)
174 ainfo.shortname = "z";
175 ainfo.longname = "gsdl uid";
176 ainfo.multiplechar = true;
177 ainfo.defaultstatus = cgiarginfo::none;
178 ainfo.argdefault = g_EmptyText;
179 ainfo.savedarginfo = cgiarginfo::mustnot;
180 argsinfo.addarginfo (NULL, ainfo);
181}
182
183
184void receptionist::add_action (action *theaction) {
185 // make sure we have an action to add
186 if (theaction == NULL) return;
187
188 // add this action to the list of actions
189 actions.addaction(theaction);
190
191 // add the cgi arguments from this action
192 argsinfo.addarginfo (NULL, *(theaction->getargsinfo()));
193}
194
195
196void receptionist::add_browser (browserclass *thebrowser) {
197 // make sure we have a browser to add
198 if (thebrowser == NULL) return;
199
200 // add this browser to the list of browsers
201 browsers.addbrowser(thebrowser);
202}
203
204
205void receptionist::setdefaultbrowser (const text_t &browsername) {
206 browsers.setdefaultbrowser (browsername);
207}
208
209
210// configure should be called for each line in the
211// configuration files to configure the receptionist and everything
212// it contains. The configuration should take place after everything
213// has been added but before the initialisation.
214
215void receptionist::configure (const text_t &key, const text_tarray &cfgline) {
216 // configure the receptionist
217
218
219
220 if (cfgline.size() >= 1) {
221 cgiarginfo *info = NULL;
222 if (key == "gsdlhome") {
223 configinfo.gsdlhome = cfgline[0];
224 if (configinfo.dbhome.empty()) configinfo.dbhome = cfgline[0];
225 }
226 else if (key == "collecthome") configinfo.collecthome = cfgline[0];
227 else if (key == "gdbmhome") configinfo.dbhome = cfgline[0];
228 else if (key == "collection") {
229 configinfo.collection = cfgline[0];
230 // also need to set the default arg to this collection
231 if ((info = argsinfo.getarginfo("c")) != NULL) {
232 info->defaultstatus = cgiarginfo::good;
233 info->argdefault = cfgline[0];
234 }
235
236 }
237 else if (key == "collectdir") configinfo.collectdir = cfgline[0];
238 else if (key == "httpprefix") configinfo.httpprefix = cfgline[0];
239 else if (key == "httpweb") configinfo.httpweb = cfgline[0];
240 else if (key == "gwcgi") configinfo.gwcgi = cfgline[0];
241 else if (key == "macrofiles") {
242 // want to append to macrofiles (i.e. may be several config files
243 // contributing, maybe from several collections).
244 text_tarray::const_iterator here = cfgline.begin();
245 text_tarray::const_iterator end = cfgline.end();
246 while (here != end) {
247 configinfo.macrofiles.insert (*here);
248 ++here;
249 }
250 }
251 else if (key == "saveconf") configinfo.saveconf = cfgline[0];
252 else if (key == "usecookies") configinfo.usecookies = (cfgline[0] == "true");
253 else if (key == "logcgiargs") configinfo.logcgiargs = (cfgline[0] == "true");
254 else if (key == "maintainer") configinfo.maintainer = cfgline[0];
255 else if (key == "MailServer") configinfo.MailServer = cfgline[0];
256 else if (key == "LogDateFormat") {
257 if (cfgline[0] == "UTCTime") configinfo.LogDateFormat = UTCTime;
258 else if (cfgline[0] == "Absolute") configinfo.LogDateFormat = Absolute;
259 }
260 else if (key == "LogEvents") {
261 if (cfgline[0] == "CollectorEvents") configinfo.LogEvents = CollectorEvents;
262 else if (cfgline[0] == "AllEvents") configinfo.LogEvents = AllEvents;
263 }
264 else if (key == "EmailEvents") {
265 if (cfgline[0] == "CollectorEvents") configinfo.EmailEvents = CollectorEvents;
266 else if (cfgline[0] == "AllEvents") configinfo.EmailEvents = AllEvents;
267 }
268 else if (key == "EmailUserEvents") configinfo.EmailUserEvents = (cfgline[0] == "true");
269 else if (key == "pageparam") {
270 if (cfgline.size() >= 2) configinfo.pageparams[cfgline[0]] = cfgline[1];
271 else configinfo.pageparams[cfgline[0]] = "";
272 }
273 else if (key == "macroprecedence") configinfo.macroprecedence = cfgline[0];
274 else if (key == "collectinfo") {
275 if (cfgline.size() == 3) {
276 // for backwards compatability with older collections that only use
277 // gsdlhome and dbhome
278 collectioninfo_t cinfo;
279 cinfo.gsdl_gsdlhome = cfgline[1];
280 cinfo.gsdl_collecthome = filename_cat(cfgline[1],"collect");
281 cinfo.gsdl_dbhome = cfgline[2];
282 configinfo.collectinfo[cfgline[0]] = cinfo;
283 }
284 else if (cfgline.size() >= 4) {
285 collectioninfo_t cinfo;
286 cinfo.gsdl_gsdlhome = cfgline[1];
287 cinfo.gsdl_collecthome = cfgline[2];
288 cinfo.gsdl_dbhome = cfgline[3];
289 configinfo.collectinfo[cfgline[0]] = cinfo;
290 }
291 }
292
293 // Read in the value for the site_auth directive either true or false
294 else if (key == "site_auth") configinfo.site_auth = (cfgline[0] == "true");
295
296 else if (key == "site_group")
297 joinchar(cfgline,',',configinfo.site_group);
298
299 else if (key == "SiteFormat") {
300 if (cfgline[0] == "HomePageType") {
301 configinfo.HomePageType = cfgline[1];
302 } else if (cfgline[0] == "HomePageCols") {
303 configinfo.HomePageCols = cfgline[1].getint();
304 }
305 }
306
307 else if (key == "cgiarg") {
308 // get shortname
309 bool seen_defaultstatus = false;
310 text_t subkey, subvalue;
311 text_t shortname;
312 text_t::const_iterator cfglinesub_here;
313 text_tarray::const_iterator cfgline_here = cfgline.begin();
314 text_tarray::const_iterator cfgline_end = cfgline.end();
315 while (cfgline_here != cfgline_end) {
316 cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
317 (*cfgline_here).end(), '=', subkey);
318 if (subkey == "shortname") {
319 shortname = substr (cfglinesub_here, (*cfgline_here).end());
320 }
321 ++cfgline_here;
322 }
323
324 // if we found the shortname process the line again filling in values
325 if (!shortname.empty()) {
326 cgiarginfo &chinfo = argsinfo[shortname];
327 chinfo.shortname = shortname; // in case this is a new argument
328
329 cfgline_here = cfgline.begin();
330 while (cfgline_here != cfgline_end) {
331 cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
332 (*cfgline_here).end(), '=', subkey);
333 subvalue = substr (cfglinesub_here, (*cfgline_here).end());
334
335 if (subkey == "longname") chinfo.longname = subvalue;
336 else if (subkey == "multiplechar") chinfo.multiplechar = (subvalue == "true");
337 else if (subkey == "multiplevalue") chinfo.multiplevalue = (subvalue == "true");
338 else if (subkey == "defaultstatus") {
339 seen_defaultstatus = true;
340 if (subvalue == "none") chinfo.defaultstatus = cgiarginfo::none;
341 else if (subvalue == "weak") chinfo.defaultstatus = cgiarginfo::weak;
342 else if (subvalue == "good") chinfo.defaultstatus = cgiarginfo::good;
343 else if (subvalue == "config") chinfo.defaultstatus = cgiarginfo::config;
344 else if (subvalue == "imperative") chinfo.defaultstatus = cgiarginfo::imperative;
345 }
346 else if (subkey == "argdefault") {
347 chinfo.argdefault = subvalue;
348 if (!seen_defaultstatus) chinfo.defaultstatus = cgiarginfo::config;
349 }
350 else if (subkey == "savedarginfo") {
351 if (subvalue == "mustnot") chinfo.savedarginfo = cgiarginfo::mustnot;
352 else if (subvalue == "can") chinfo.savedarginfo = cgiarginfo::can;
353 else if (subvalue == "must") chinfo.savedarginfo = cgiarginfo::must;
354 }
355
356 ++cfgline_here;
357 }
358 }
359
360 } else if (key == "Encoding") {
361
362 configure_encoding (cfgline);
363
364 } else if (key == "Language") {
365 text_t subkey, subvalue, shortname;
366 languageinfo_t lang;
367 text_t::const_iterator cfglinesub_here;
368 text_tarray::const_iterator cfgline_here = cfgline.begin();
369 text_tarray::const_iterator cfgline_end = cfgline.end();
370 while (cfgline_here != cfgline_end) {
371 cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
372 (*cfgline_here).end(), '=', subkey);
373 if (subkey == "shortname") {
374 shortname = substr (cfglinesub_here, (*cfgline_here).end());
375 } else if (subkey == "longname") {
376 lang.longname = substr (cfglinesub_here, (*cfgline_here).end());
377 } else if (subkey == "default_encoding") {
378 lang.defaultencoding = substr (cfglinesub_here, (*cfgline_here).end());
379 }
380 ++cfgline_here;
381 }
382 if (!shortname.empty()) {
383 if (lang.longname.empty()) lang.longname = shortname;
384 configinfo.languages[shortname] = lang;
385 }
386 }
387 }
388
389 // configure the actions
390 actionptrmap::iterator actionhere = actions.begin ();
391 actionptrmap::iterator actionend = actions.end ();
392
393 while (actionhere != actionend) {
394 assert ((*actionhere).second.a != NULL);
395 if ((*actionhere).second.a != NULL)
396 (*actionhere).second.a->configure(key, cfgline);
397
398 ++actionhere;
399 }
400
401 // configure the protocols
402 recptprotolistclass::iterator protohere = protocols.begin ();
403 recptprotolistclass::iterator protoend = protocols.end ();
404
405 while (protohere != protoend) {
406 assert ((*protohere).p != NULL);
407 comerror_t err;
408 if ((*protohere).p != NULL)
409 (*protohere).p->configure(key, cfgline, err);
410
411 ++protohere;
412 }
413
414 // configure the browsers
415 browserptrmap::iterator browserhere = browsers.begin ();
416 browserptrmap::iterator browserend = browsers.end ();
417
418 while (browserhere != browserend) {
419 assert ((*browserhere).second.b != NULL);
420 if ((*browserhere).second.b != NULL)
421 (*browserhere).second.b->configure(key, cfgline);
422
423 ++browserhere;
424 }
425}
426
427
428void receptionist::configure (const text_t &key, const text_t &value) {
429 text_tarray cfgline;
430 cfgline.push_back (value);
431 configure(key, cfgline);
432}
433
434
435// init should be called after all the actions and protocols have been
436// added to the receptionist and after everything has been configured but
437// before any pages are created. It returns true on success and false on
438// failure. If false is returned getpage should not be called (without
439// producing meaningless output), instead an error page should be produced
440// by the calling code.
441bool receptionist::init (ostream &logout) {
442
443 // first configure collectdir
444 if (!configinfo.collection.empty()) {
445
446 // collection specific mode
447
448 text_t collectdir = configinfo.gsdlhome;
449
450 if (!configinfo.collectdir.empty()) {
451 // has already been configured
452 collectdir = configinfo.collectdir;
453 } else {
454
455 // decide where collectdir is by searching for collect.cfg
456 // look in $GSDLHOME/collect/collection-name/etc/collect.cfg and
457 // then $GSDLHOME/etc/collect.cfg
458 collectdir = filename_cat (configinfo.gsdlhome, "collect");
459 collectdir = filename_cat (collectdir, configinfo.collection);
460 text_t filename = filename_cat (collectdir, "etc");
461 filename = filename_cat (filename, "collect.cfg");
462
463 if (!file_exists(filename)) collectdir = configinfo.gsdlhome;
464 }
465
466 configure("collectdir", collectdir);
467
468 }
469 else {
470
471 text_t collecthome;
472 if (configinfo.collecthome.empty()) {
473 collecthome = filename_cat(configinfo.gsdlhome,"collect");
474 }
475 else {
476 collecthome = configinfo.collecthome;
477 }
478
479 configure("collecthome", collecthome);
480
481 // for backwards compatability collectdir set to gsdlhome
482 // (possible it could now be removed)
483 configure("collectdir", configinfo.gsdlhome);
484 }
485
486
487 // read in the macro files
488 if (!read_macrofiles (logout)) return false;
489
490 // there must be at least one action defined
491 if (actions.empty()) {
492 logout << "Error: no actions have been added to the receptionist\n";
493 return false;
494 }
495
496 // there must be at least one browser defined
497 if (browsers.empty()) {
498 logout << "Error: no browsers have been added to the receptionist\n";
499 return false;
500 }
501
502 // create a saveconf string if there isn't one already
503 if (configinfo.saveconf.empty())
504 configinfo.saveconf = create_save_conf_str (argsinfo, logout);
505
506 // check the saveconf string
507 if (!check_save_conf_str (configinfo.saveconf, argsinfo, logout))
508 return false;
509
510 // set a random seed
511 srand (time(NULL));
512
513 // if maintainer email address is something dodgy (for now I'll define
514 // dodgy as being anything that doesn't contain '@') disable EmailEvents
515 // and EmailUserEvents (we don't strictly need to disable EmailUserEvents
516 // in this case but we will as it seems likely that MailServer will also
517 // be screwed up if maintainer is).
518 text_t::const_iterator maintainer_end = configinfo.maintainer.end ();
519 text_t::const_iterator maintainer_here = findchar ((text_t::const_iterator)configinfo.maintainer.begin(),
520 maintainer_end, '@');
521 if (maintainer_here == maintainer_end) {
522 configinfo.EmailEvents = Disabled;
523 configinfo.EmailUserEvents = Disabled;
524 } else {
525 // if MailServer isn't set it should default to mail.maintainer-domain
526 if (configinfo.MailServer.empty()) {
527 configinfo.MailServer = "mail." + substr (maintainer_here+1, maintainer_end);
528 }
529 }
530
531 // init the actions
532 actionptrmap::iterator actionhere = actions.begin ();
533 actionptrmap::iterator actionend = actions.end ();
534 while (actionhere != actionend) {
535 if (((*actionhere).second.a == NULL) ||
536 !(*actionhere).second.a->init(logout)) return false;
537 ++actionhere;
538 }
539
540 // init the protocols
541 recptprotolistclass::iterator protohere = protocols.begin ();
542 recptprotolistclass::iterator protoend = protocols.end ();
543 while (protohere != protoend) {
544 comerror_t err;
545 if (((*protohere).p == NULL) ||
546 !(*protohere).p->init(err, logout)) return false;
547 ++protohere;
548 }
549
550 // init the browsers
551 browserptrmap::iterator browserhere = browsers.begin ();
552 browserptrmap::iterator browserend = browsers.end ();
553 while (browserhere != browserend) {
554 if (((*browserhere).second.b == NULL) ||
555 !(*browserhere).second.b->init(logout)) return false;
556 ++browserhere;
557 }
558
559 return true;
560}
561
562// get the default encoding for the given language - if it fails for any
563// reason return ""
564text_t receptionist::get_default_encoding (const text_t &language) {
565
566 // make sure language is valid
567 if (configinfo.languages.find(language) == configinfo.languages.end()) return "";
568
569 text_t default_encoding = configinfo.languages[language].defaultencoding;
570
571 // make sure the encoding is valid
572 if (converters.find(default_encoding) == converters.end()) {
573 // we don't support the encoding specified as default for this language
574 if (configinfo.encodings.size()==1) {
575 // only 1 encoding specified in main.cfg, so use it
576 return configinfo.encodings.begin()->second;
577 }
578 return "";
579 }
580
581 return default_encoding;
582}
583
584// parse_cgi_args parses cgi arguments into an argument class.
585// This function should be called for each page request. It returns false
586// if there was a major problem with the cgi arguments.
587bool receptionist::parse_cgi_args (const text_t &argstr,
588 fileupload_tmap &fileuploads,
589 cgiargsclass &args,
590 ostream &logout, text_tmap &fcgienv) {
591
592 // get an initial list of cgi arguments
593 args.clear();
594 split_cgi_args (argsinfo, argstr, args);
595
596 // expand the compressed argument (if there was one)
597 if (!expand_save_args (argsinfo, configinfo.saveconf, args, logout)) return false;
598
599 // add the defaults
600 add_default_args (argsinfo, args, logout);
601
602 // add any file upload arguments
603 add_fileupload_args(argsinfo, args, fileuploads, logout);
604
605 // get the cookie
606 if (configinfo.usecookies) get_cookie(args["z"], fcgienv);
607
608 // if we're changing languages, set the encoding to the default for the new language
609 if (args["nl"] == "1") {
610 args["nw"] = get_default_encoding(args["l"]);
611 }
612
613 // get the input encoding
614 // if encoding isn't set, set it to the default for the current language
615 if ((args.getarg("w") == NULL) || args["w"].empty()) {
616 args["w"] = get_default_encoding(args["l"]);
617 }
618
619 text_t &arg_w = args["w"];
620
621 inconvertclass defaultinconvert;
622 inconvertclass *inconvert = converters.get_inconverter (arg_w);
623 if (inconvert == NULL) inconvert = &defaultinconvert;
624
625 // see if the next page will have a different encoding
626 if (args.getarg("nw") != NULL) arg_w = args["nw"];
627
628 // convert arguments which aren't in unicode to unicode
629 args_tounicode (args, *inconvert);
630
631
632 // decide on the output conversion class (needed for checking the external
633 // cgi arguments)
634 rzwsoutconvertclass defaultoutconverter;
635 rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
636 if (outconverter == NULL) outconverter = &defaultoutconverter;
637 outconverter->reset();
638
639 // check the main cgi arguments
640 if (!check_mainargs (args, logout)) return false;
641
642 // check the arguments for the action
643 action *a = actions.getaction (args["a"]);
644 if (a != NULL) {
645 if (!a->check_cgiargs (argsinfo, args, &protocols, logout)) return false;
646 } else {
647 // the action was not found!!
648 outconvertclass text_t2ascii;
649 logout << text_t2ascii << "Error: the action \"" << args["a"]
650 << "\" could not be found.\n";
651 return false;
652 }
653
654 // check external cgi arguments for each action
655 actionptrmap::iterator actionhere = actions.begin ();
656 actionptrmap::iterator actionend = actions.end ();
657 while (actionhere != actionend) {
658 assert ((*actionhere).second.a != NULL);
659 if ((*actionhere).second.a != NULL) {
660 if (!(*actionhere).second.a->check_external_cgiargs (argsinfo, args, *outconverter,
661 configinfo.saveconf, logout))
662 return false;
663 }
664 ++actionhere;
665 }
666
667 // the action might have changed but we will assume that
668 // the cgiargs were checked properly when the change was made
669
670 return true;
671}
672
673
674// Returns true if cookie already existed, false if it was generated
675bool receptionist::get_cookie (text_t &cookie, text_tmap &fcgienv)
676{
677 // See if we can get the GSDL_UID cookie
678 text_t cookiestring = gsdl_getenv ("HTTP_COOKIE", fcgienv);
679 if (!cookiestring.empty()) // This should really be handled by the findword function...
680 {
681 // Check if the cookie contains GSDL_UID
682 text_t gsdl_uid = "GSDL_UID=";
683 text_t::iterator gsdl_uid_start = findword(cookiestring.begin(), cookiestring.end(), gsdl_uid);
684 if (gsdl_uid_start != cookiestring.end())
685 {
686 // Yes, so extract its value
687 cookie = substr(gsdl_uid_start + gsdl_uid.size(), findchar(gsdl_uid_start + gsdl_uid.size(), cookiestring.end(), ';'));
688 return true;
689 }
690 }
691
692 // Generate a new key "[host]-[epoch time]", e.g. test.com-1256764496
693 cookie.clear();
694 text_t host = gsdl_getenv("REMOTE_ADDR", fcgienv);
695 time_t ttime = time(NULL);
696 if (!host.empty())
697 {
698 cookie += host;
699 cookie.push_back ('-');
700 }
701 cookie += text_t(ttime);
702
703 return false;
704}
705
706
707// Same as above but just tests if cookie exists
708bool receptionist::get_cookie (text_tmap &fcgienv)
709{
710 text_t cookie_jar = "";
711 return get_cookie(cookie_jar, fcgienv);
712}
713
714
715bool receptionist::log_cgi_args (cgiargsclass &args, ostream &logout, text_tmap &fcgienv) {
716
717 // see if we want to log the cgi arguments
718 if (!configinfo.logcgiargs) return true;
719
720 text_t host = gsdl_getenv ("REMOTE_HOST", fcgienv);
721 text_t script_name = gsdl_getenv ("SCRIPT_NAME", fcgienv);
722 if (host.empty()) host = gsdl_getenv ("REMOTE_ADDR", fcgienv);
723 text_t browser = gsdl_getenv ("HTTP_USER_AGENT", fcgienv);
724
725 cgiargsclass::const_iterator args_here = args.begin();
726 cgiargsclass::const_iterator args_end = args.end();
727
728 text_t argstr;
729 bool first = true;
730 while (args_here != args_end) {
731 if (!first) argstr += ", ";
732 argstr += (*args_here).first + "=" + (*args_here).second.value;
733 first = false;
734 ++args_here;
735 }
736
737 text_t logfile = filename_cat (configinfo.dbhome, "etc", "usage.txt");
738
739 text_t logstr = script_name;
740 logstr += " " + host;
741 logstr += " [";
742 if (configinfo.LogDateFormat == UTCTime) {
743 logstr += get_date (false);
744 } else if (configinfo.LogDateFormat == Absolute) {
745 time_t ttime = time(NULL);
746 logstr += ttime;
747 } else {
748 // LocalTime
749 logstr += get_date (true);
750 }
751 logstr += "] (" + argstr + ") \"";
752 logstr += browser;
753 logstr += "\"\n";
754
755 return append_logstr (logfile, logstr, logout);
756}
757
758bool receptionist::append_logstr (const text_t &filename, const text_t &logstr,
759 ostream &logout) {
760
761 utf8outconvertclass text_t2utf8;
762 char *lfile = filename.getcstr();
763
764 int fd = open(lfile, O_WRONLY | O_APPEND);
765
766 if (fd == -1) {
767 logout << "Error: Couldn't open file " << lfile << "\n";
768 delete []lfile;
769 return false;
770 }
771
772 // lock_val is set to 0 if file is locked successfully
773 int lock_val = 1;
774 GSDL_LOCK_FILE (fd);
775 if (lock_val == 0) {
776 text_t tmp_log_str(logstr); // so we don't pass a const to setinput...
777 text_t2utf8.setinput(&tmp_log_str);
778 char *buffer=new char[logstr.size()];
779 size_t num_chars;
780 convertclass::status_t status;
781 text_t2utf8.convert(buffer, logstr.size(), num_chars, status);
782 // ignore status - assume it is "finished" as buffer is big enough
783 write(fd, buffer, num_chars);
784 GSDL_UNLOCK_FILE (fd);
785 delete []buffer;
786 } else {
787 logout << "Error: Couldn't lock file " << lfile << "\n";
788 close(fd);
789 delete []lfile;
790 return false;
791 }
792
793 close(fd);
794
795 delete []lfile;
796 return true;
797}
798
799text_t receptionist::expandmacros (const text_t &astring, cgiargsclass &args,
800 ostream &logout) {
801 text_t outstring;
802 outconvertclass text_t2ascii;
803
804 action *a = actions.getaction (args["a"]);
805 if (a != NULL)
806 {
807 prepare_page (a, args, text_t2ascii, logout);
808 }
809 disp.expandstring (displayclass::defaultpackage, astring, outstring);
810 return outstring;
811}
812
813// produce_cgi_page will call get_cgihead_info and
814// produce_content in the appropriate way to output a cgi header and
815// the page content (if needed). If a page could not be created it
816// will return false
817bool receptionist::produce_cgi_page (cgiargsclass &args, ostream &contentout,
818 ostream &logout, text_tmap &fcgienv) {
819 outconvertclass text_t2ascii;
820
821 response_t response;
822 text_t response_data;
823
824 // produce cgi header
825 get_cgihead_info (args, response, response_data, logout, fcgienv);
826 if (response == location) {
827 // location response (url may contain macros!!)
828 response_data = expandmacros (response_data, args, logout);
829
830 contentout << text_t2ascii << "Location: " << response_data << "\n\n";
831 contentout << flush;
832
833 return true;
834 } else if (response == content) {
835 // content response
836
837#ifdef GSDL_NOCACHE
838 contentout << "Expires: Mon, 26 Jul 1997 05:00:00 GMT\n"; // date in the past
839 tm *tm_ptr = NULL;
840 time_t t = time(NULL);
841 tm_ptr = gmtime (&t);
842 if (tm_ptr != NULL) {
843 char *timestr = new char[128];
844 strftime (timestr, 128, "%a, %d %b %Y %H:%M:%S", tm_ptr);
845 contentout << "Last-Modified: " << timestr << " GMT\n"; // always modified
846 delete []timestr;
847 }
848 contentout << "Cache-Control: no-cache, must-revalidate\n"; // HTTP/1.1
849 contentout << "Pragma: no-cache\n"; // HTTP/1.0
850
851#else
852
853 // use the later of build.cfg and collect.cfg modification times
854 // as the Last-Modified: header, for caching values
855 struct stat file_info;
856 time_t latest=0;
857
858 text_t collectname="";
859 collectname=args["c"];
860 if (collectname != "") {
861
862 text_t collecthome;
863 if (!configinfo.collecthome.empty()) {
864 collecthome = configinfo.collecthome;
865 }
866 else {
867 collecthome=filename_cat(configinfo.gsdlhome,"collect");
868 }
869 text_t collectdir=filename_cat(collecthome,collectname);
870
871 text_t buildcfg=filename_cat(collectdir,"index");
872 buildcfg=filename_cat(buildcfg,"build.cfg");
873 char *buildcfg_ptr=buildcfg.getcstr();
874 text_t collectcfg=filename_cat(collectdir,"etc");
875 collectcfg=filename_cat(collectcfg,"collect.cfg");
876 char *collectcfg_ptr=collectcfg.getcstr();
877
878 if (stat(buildcfg_ptr, &file_info)) {
879 // we got an error. Currently don't handle error :(
880 // logout <<
881 } else {
882 latest=file_info.st_mtime;
883 }
884
885 if (stat(collectcfg_ptr, &file_info)) {
886 // error - unhandled for now
887 } else {
888 if (latest<file_info.st_mtime) latest=file_info.st_mtime;
889 }
890 delete []buildcfg_ptr;
891 delete []collectcfg_ptr;
892
893 if (latest>0) {
894 // print out modified time, "DDD, dd MMM YYYY hh:mm:ss" format
895 // c library takes care of mem for this string... (has \n at end!!!!)
896 // latest is currently local time, convert to UTC.
897 struct tm* utc_latest;
898 utc_latest=gmtime(&latest);
899 contentout << "Last-Modified: " << asctime(utc_latest);
900 }
901 } // end of collection != ""
902
903#endif
904
905 contentout << text_t2ascii << "Content-type: " << response_data << "\n\n";
906 }
907 else if (response == undecided_location) {
908 // Wait until later to output the target location
909 // Used for the "I'm feeling lucky" functionality
910 }
911 else {
912 // unknown response
913 logout << "Error: get_cgihead_info returned an unknown response type.\n";
914 return false;
915 }
916
917 // produce cgi page
918 if (!produce_content (args, contentout, logout)) return false;
919
920 // flush contentout
921 contentout << flush;
922 return true;
923}
924
925
926// get_cgihead_info determines the cgi header information for
927// a set of cgi arguments. If response contains location then
928// response_data contains the redirect address. If reponse
929// contains content then reponse_data contains the content-type.
930// Note that images can now be produced by the receptionist.
931// Note also, alternative for get_cgihead_info below which
932// stores the information in a text_tmap so it is more easily digested
933
934void receptionist::get_cgihead_info (cgiargsclass &args, response_t &response,
935 text_t &response_data, ostream &logout,
936 text_tmap &fcgienv) {
937 outconvertclass text_t2ascii;
938
939 // get the action
940 action *a = actions.getaction (args["a"]);
941 if (a != NULL) {
942 a->get_cgihead_info (args, &protocols, response, response_data, logout);
943
944 } else {
945 // the action was not found!!
946 logout << text_t2ascii << "Error receptionist::get_cgihead_info: the action \""
947 << args["a"] << "\" could not be found.\n";
948 response = content;
949 response_data = "text/html";
950 }
951
952 // add the encoding information
953 if (response == content) {
954 if (converters.find(args["w"]) != converters.end()) {
955 response_data += "; charset=" + args["w"];
956 } else {
957 // default to latin 1
958 response_data += "; charset=ISO-8859-1";
959 }
960
961 // add cookie if required
962 if (configinfo.usecookies && !get_cookie(fcgienv))
963 response_data += "\nSet-Cookie: GSDL_UID=" + args["z"]
964 + "; expires=Fri, 25-Dec-2037 00:00:00 GMT";
965 }
966}
967
968
969// Alternative version of get_cgihead_info, stores fielded infomation
970// in text_tmap rather than concatenated string
971void receptionist::get_cgihead_info (cgiargsclass &args, text_tmap &headers,
972 ostream &logout, text_tmap &fcgienv) {
973
974 response_t response;
975 text_t response_data;
976
977 // get the action
978 action *a = actions.getaction (args["a"]);
979 if (a != NULL) {
980 a->get_cgihead_info (args, &protocols, response, response_data, logout);
981
982 } else {
983 // the action was not found!!
984 outconvertclass text_t2ascii;
985 logout << text_t2ascii << "Error receptionist::get_cgihead_info: the action \""
986 << args["a"] << "\" could not be found.\n";
987 response = content;
988 response_data = "text/html";
989 }
990
991 if (response == location) {
992 response_data = expandmacros(response_data, args, logout);
993 headers["Location"] = response_data;
994 return;
995 }
996
997 // add the encoding information
998 if (response == content) {
999
1000 if (converters.find(args["w"]) != converters.end()) {
1001 headers["content-encoding"] = args["w"];
1002 response_data += "; charset=" + args["w"];
1003 } else {
1004 // default to utf-8
1005 headers["content-encoding"] = "utf-8";
1006 response_data += "; charset=utf-8";
1007 }
1008
1009 headers["content-type"] = response_data;
1010
1011 }
1012
1013}
1014
1015
1016
1017// produce the page content
1018bool receptionist::produce_content (cgiargsclass &args, ostream &contentout,
1019 ostream &logout) {
1020
1021 // decide on the output conversion class
1022 text_t &arg_w = args["w"];
1023 rzwsoutconvertclass defaultoutconverter;
1024 rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
1025 if (outconverter == NULL) outconverter = &defaultoutconverter;
1026 outconverter->reset();
1027
1028 // needed for 16-bit unicode only - big endian marker 0xfeff (RFC 2781)
1029 if (arg_w=="utf-16be") {
1030 contentout << '\xfe' << '\xff' ;
1031 }
1032
1033 recptproto *collectproto = protocols.getrecptproto (args["c"], logout);
1034 if (collectproto != NULL) {
1035 // get browsers to process OID
1036 text_t OID = args["d"];
1037 if (OID.empty()) OID = args["cl"];
1038 if (!OID.empty()) {
1039 text_tset metadata;
1040 text_tarray OIDs;
1041 OIDs.push_back (OID);
1042 if (!is_top(OID)) OIDs.push_back (OID + ".pr");
1043 FilterResponse_t response;
1044 metadata.insert ("childtype");
1045 if (get_info (OIDs, args["c"], args["l"], metadata, false, collectproto, response, logout)) {
1046 text_t classifytype;
1047 if (!response.docInfo[0].metadata["childtype"].values[0].empty())
1048 classifytype = response.docInfo[0].metadata["childtype"].values[0];
1049 else if (!is_top (OID)) {
1050 // not sure why this is occasionally not set, but it will
1051 // cause a segfault... possibly if built with no_text? jrm21
1052 if (response.docInfo[1].metadata.find("childtype")
1053 == response.docInfo[1].metadata.end()) {
1054 cerr << "receptionist: no childtype element in metadata map!"
1055 << endl;
1056 } else {
1057 if (!response.docInfo[1].metadata["childtype"].values[0].empty())
1058 classifytype = response.docInfo[1].metadata["childtype"].values[0];
1059 }
1060 }
1061 browserclass *b = browsers.getbrowser (classifytype);
1062 b->processOID (args, collectproto, logout);
1063 }
1064 }
1065
1066 // translate "d" and "cl" arguments if required
1067 translate_OIDs (args, collectproto, logout);
1068 }
1069
1070 // produce the page using the desired action
1071 action *a = actions.getaction (args["a"]);
1072 if (a != NULL) {
1073 if (a->uses_display(args)) prepare_page (a, args, (*outconverter), logout);
1074 if (!a->do_action (args, &protocols, &browsers, disp, (*outconverter), contentout, logout))
1075 return false;
1076 } else {
1077 // the action was not found!!
1078 outconvertclass text_t2ascii;
1079
1080 logout << text_t2ascii << "Error receptionist::produce_content: the action \""
1081 << args["a"] << "\" could not be found.\n";
1082
1083 contentout << (*outconverter)
1084 << "<html>\n"
1085 << "<head>\n"
1086 << "<title>Error</title>\n"
1087 << "</head>\n"
1088 << "<body>\n"
1089 << "<h2>Oops!</h2>\n"
1090 << "Undefined Page. The action \""
1091 << args["a"] << "\" could not be found.\n"
1092 << "</body>\n"
1093 << "</html>\n";
1094 }
1095 return true;
1096}
1097
1098
1099// returns the compressed argument ("e") corresponding to the argument
1100// list. This can be used to save preferences between sessions.
1101text_t receptionist::get_compressed_arg (cgiargsclass &args, ostream &logout) {
1102 // decide on the output conversion class
1103 text_t &arg_w = args["w"];
1104 rzwsoutconvertclass defaultoutconverter;
1105 rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
1106 if (outconverter == NULL) outconverter = &defaultoutconverter;
1107 outconverter->reset();
1108
1109 text_t compressed_args;
1110 if (compress_save_args (argsinfo, configinfo.saveconf, args,
1111 compressed_args, *outconverter, logout))
1112 return compressed_args;
1113
1114 return g_EmptyText;
1115}
1116
1117
1118// will read in all the macro files. If one is not found an
1119// error message will be written to logout and the method will
1120// return false.
1121bool receptionist::read_macrofiles (ostream &logout) {
1122 outconvertclass text_t2ascii;
1123
1124 // redirect the error output to logout
1125 ostream *savedlogout = disp.setlogout (&logout);
1126
1127 // unload any macros that were previously loaded - this allows us to call
1128 // this function a second time to reload all the macro files (useful for
1129 // reading in changed macro files in server versions of greenstone)
1130 disp.unloaddefaultmacros();
1131
1132 // load up the default macro files, the collection directory
1133 // is searched first for the file (if this is being used in
1134 // collection specific mode) and then the main directory(s)
1135 text_t colmacrodir = filename_cat (configinfo.collectdir, "macros");
1136
1137 text_tset maindirs;
1138 text_t gsdlmacrodir = filename_cat (configinfo.gsdlhome, "macros");
1139 maindirs.insert (gsdlmacrodir);
1140 colinfo_tmap::iterator colhere = configinfo.collectinfo.begin();
1141 colinfo_tmap::iterator colend = configinfo.collectinfo.end();
1142 while (colhere != colend) {
1143 if (!((*colhere).second.gsdl_gsdlhome).empty()) {
1144 gsdlmacrodir = filename_cat ((*colhere).second.gsdl_gsdlhome, "macros");
1145 maindirs.insert (gsdlmacrodir);
1146 }
1147 ++colhere;
1148 }
1149
1150 text_tset::iterator arrhere = configinfo.macrofiles.begin();
1151 text_tset::iterator arrend = configinfo.macrofiles.end();
1152 text_t filename;
1153 while (arrhere != arrend) {
1154 bool foundfile = false;
1155
1156 // try in the collection directory if this is being
1157 // run in collection specific mode
1158 if (!configinfo.collection.empty()) {
1159 filename = filename_cat (colmacrodir, *arrhere);
1160 if (file_exists (filename)) {
1161 disp.loaddefaultmacros(filename);
1162 foundfile = true;
1163 }
1164 }
1165
1166 // if we haven't found the macro file yet try in
1167 // the main macro directory(s)
1168 // if file is found in more than one main directory
1169 // we'll load all copies
1170 if (!foundfile) {
1171 text_tset::const_iterator dirhere = maindirs.begin();
1172 text_tset::const_iterator dirend = maindirs.end();
1173 while (dirhere != dirend) {
1174 filename = filename_cat (*dirhere, *arrhere);
1175 if (file_exists (filename)) {
1176 disp.loaddefaultmacros(filename);
1177 foundfile = true;
1178 }
1179 ++dirhere;
1180 }
1181 }
1182
1183 // see if we found the file or not
1184 if (!foundfile) {
1185 logout << text_t2ascii
1186 << "Error: the macro file \"" << *arrhere << "\" could not be found.\n";
1187 if (configinfo.collection.empty()) {
1188 text_t dirs;
1189 joinchar (maindirs, ", ", dirs);
1190 logout << text_t2ascii
1191 << "It should be in either of the following directories ("
1192 << dirs << ").\n\n";
1193
1194 } else {
1195 logout << text_t2ascii
1196 << "It should be in either " << colmacrodir << " or in "
1197 << gsdlmacrodir << ".\n\n";
1198 }
1199 // don't crap out if a macro file is missing
1200 //disp.setlogout (savedlogout);
1201 //return false;
1202 }
1203 ++arrhere;
1204 }
1205
1206 // success
1207
1208 // reset logout to what it was
1209 disp.setlogout (savedlogout);
1210 return true;
1211}
1212
1213
1214
1215
1216// Go through the list of macro files looking to see
1217// if any exist in the collectoin specific area. If they
1218// do then read them in and add them to the set of existing
1219// current macros
1220
1221void receptionist::read_collection_macrofiles (const text_t& collection, ostream &logout)
1222{
1223 outconvertclass text_t2ascii;
1224
1225 // disp.unloadcollectionmacros();
1226
1227 // redirect the error output to logout
1228 ostream *savedlogout = disp.setlogout (&logout);
1229
1230 text_t colmacrodir
1231 = filename_cat (configinfo.collecthome,collection, "macros");
1232
1233 if (directory_exists (colmacrodir)) {
1234
1235 text_tset::iterator arrhere = configinfo.macrofiles.begin();
1236 text_tset::iterator arrend = configinfo.macrofiles.end();
1237 text_t filename;
1238 while (arrhere != arrend) {
1239
1240 filename = filename_cat (colmacrodir, *arrhere);
1241 if (file_exists (filename)) {
1242 disp.loadcollectionmacros(filename);
1243 }
1244
1245 ++arrhere;
1246 }
1247 }
1248
1249 // reset logout to what it was
1250 disp.setlogout (savedlogout);
1251}
1252
1253
1254
1255
1256// check_mainargs will check all the main arguments. If a major
1257// error is found it will return false and no cgi page should
1258// be created using the arguments.
1259
1260bool receptionist::check_mainargs (cgiargsclass &args, ostream &logout) {
1261
1262 if(configinfo.site_auth)
1263 {
1264 args["uan"] = "1";
1265 args["ug"] = configinfo.site_group;
1266 }
1267
1268
1269 // if this receptionist is running in collection dependant mode
1270 // then it should always set the collection argument to the
1271 // collection
1272 if (!configinfo.collection.empty()) args["c"] = configinfo.collection;
1273
1274 // if current collection uses ccscols make sure
1275 // "ccs" argument is set and make "cc" default to
1276 // all collections in "ccs"
1277 if (args["a"] != "config" && !args["c"].empty()) {
1278
1279 text_t &arg_c = args["c"];
1280 recptproto *collectproto = protocols.getrecptproto (arg_c, logout);
1281 if (collectproto == NULL) {
1282 // oops, this collection isn't valid
1283 outconvertclass text_t2ascii;
1284 logout << text_t2ascii << "ERROR: Invalid collection: " << arg_c << "\n";
1285 // args["c"].clear();
1286
1287 } else {
1288
1289 ColInfoResponse_t *cinfo = get_collectinfo_ptr (collectproto, arg_c, logout);
1290
1291 if(cinfo->authenticate == "collection")
1292 {
1293 args["uan"] = "1";
1294 args["ug"] = cinfo->auth_group;
1295 }
1296
1297
1298 if (cinfo != NULL) {
1299 if (!cinfo->ccsCols.empty()) {
1300 args["ccs"] = 1;
1301 if (args["cc"].empty()) {
1302 text_tarray::const_iterator col_here = cinfo->ccsCols.begin();
1303 text_tarray::const_iterator col_end = cinfo->ccsCols.end();
1304 bool first = true;
1305 while (col_here != col_end) {
1306 // make sure it's a valid collection
1307 if (protocols.getrecptproto (*col_here, logout) != NULL) {
1308 if (!first) args["cc"].push_back (',');
1309 args["cc"] += *col_here;
1310 first = false;
1311 }
1312 ++col_here;
1313 }
1314 }
1315 }
1316 } else {
1317 logout << "ERROR (receptionist::check_mainargs): get_collectinfo_ptr returned NULL\n";
1318 }
1319 }
1320 }
1321
1322 // argument "v" can only be 0 or 1. Use the default value
1323 // if it is out of range
1324 int arg_v = args.getintarg ("v");
1325 if (arg_v != 0 && arg_v != 1) {
1326 cgiarginfo *vinfo = argsinfo.getarginfo ("v");
1327 if (vinfo != NULL) args["v"] = vinfo->argdefault;
1328 }
1329
1330 // argument "f" can only be 0 or 1. Use the default value
1331 // if it is out of range
1332 int arg_f = args.getintarg ("f");
1333 if (arg_f != 0 && arg_f != 1) {
1334 cgiarginfo *finfo = argsinfo.getarginfo ("f");
1335 if (finfo != NULL) args["f"] = finfo->argdefault;
1336 }
1337
1338 return true;
1339}
1340
1341// translate_OIDs translates the "d" and "cl" arguments to their correct values
1342// if they use the tricky ".fc", ".lc" type syntax.
1343void receptionist::translate_OIDs (cgiargsclass &args, recptproto *collectproto,
1344 ostream &logout) {
1345
1346 FilterResponse_t response;
1347 FilterRequest_t request;
1348 comerror_t err;
1349 text_t &arg_d = args["d"];
1350 text_t &arg_cl = args["cl"];
1351 text_t &collection = args["c"];
1352
1353 // do a call to translate OIDs if required
1354 request.filterName = "NullFilter";
1355 request.filterResultOptions = FROID;
1356 if (!arg_d.empty() && needs_translating (arg_d)) {
1357 request.docSet.push_back (arg_d);
1358 collectproto->filter (collection, request, response, err, logout);
1359 arg_d = response.docInfo[0].OID;
1360 request.clear();
1361 }
1362 // we'll also check here that the "cl" argument has a "classify" doctype
1363 // (in case ".fc" or ".lc" have screwed up)
1364 if (needs_translating (arg_cl)) {
1365 request.fields.insert ("doctype");
1366 request.docSet.push_back (arg_cl);
1367 request.filterResultOptions = FRmetadata;
1368 collectproto->filter (collection, request, response, err, logout);
1369 // set to original value (without .xx stuff) if doctype isn't "classify"
1370 if (response.docInfo[0].metadata["doctype"].values[0] != "classify")
1371 strip_suffix (arg_cl);
1372 else
1373 arg_cl = response.docInfo[0].OID;
1374 }
1375}
1376
1377// prepare_page sets up page parameters, sets display macros
1378// and opens the page ready for output
1379void receptionist::prepare_page (action *a, cgiargsclass &args,
1380 outconvertclass &outconvert,
1381 ostream &logout) {
1382 // set up page parameters
1383 text_t pageparams;
1384 bool first = true;
1385
1386 text_tmap::iterator params_here = configinfo.pageparams.begin();
1387 text_tmap::iterator params_end = configinfo.pageparams.end();
1388 while (params_here != params_end) {
1389 // page params are those from main.cfg (eg pageparam v 0) plus
1390 // two defaults set in recptconf.clear() (c="" and l=en)
1391 // This used to check if the current value of the page param
1392 // == the default value, then don't add in it the list
1393 // but if l=en, and there is a macro with [l=en], then it doesn't
1394 // find it.
1395 // so now all page params will go into the list. I assume this will
1396 // mean more attempts to find each macro, but nothing worsee than
1397 // that. --kjdon
1398 //if (args[(*params_here).first] != (*params_here).second) {
1399 if (first)
1400 first = false;
1401 else
1402 pageparams += ",";
1403
1404 pageparams += (*params_here).first;
1405 pageparams += "=";
1406 pageparams += args[(*params_here).first];
1407 // }
1408
1409 ++params_here;
1410 }
1411
1412
1413 // open the page
1414 disp.openpage(pageparams, configinfo.macroprecedence);
1415
1416 disp.unloadcollectionmacros();
1417
1418 text_t collection = args["c"];
1419 if (!collection.empty()) {
1420 read_collection_macrofiles(collection,logout);
1421 }
1422
1423 // define external macros for each action
1424 actionptrmap::iterator actionhere = actions.begin ();
1425 actionptrmap::iterator actionend = actions.end ();
1426
1427 while (actionhere != actionend) {
1428 assert ((*actionhere).second.a != NULL);
1429 if ((*actionhere).second.a != NULL) {
1430 (*actionhere).second.a->define_external_macros (disp, args, &protocols, logout);
1431 }
1432 ++actionhere;
1433 }
1434
1435
1436 // define internal macros for the current action
1437 a->define_internal_macros (disp, args, &protocols, logout);
1438
1439 // define general macros. the defining of general macros is done here so that
1440 // the last possible version of the cgi arguments are used
1441 define_general_macros (args, outconvert, logout);
1442}
1443
1444
1445void receptionist::define_general_macros (cgiargsclass &args, outconvertclass &/*outconvert*/,
1446 ostream &logout) {
1447
1448 text_t &collection = args["c"];
1449
1450 disp.setmacro ("gsdlhome", displayclass::defaultpackage, dm_safe(configinfo.gsdlhome));
1451 disp.setmacro ("gwcgi", displayclass::defaultpackage, configinfo.gwcgi);
1452 disp.setmacro ("httpweb", displayclass::defaultpackage, configinfo.httpweb);
1453 disp.setmacro ("httpprefix", displayclass::defaultpackage, configinfo.httpprefix);
1454
1455 text_t compressedoptions = get_compressed_arg(args, logout);
1456 disp.setmacro ("compressedoptions", displayclass::defaultpackage, dm_safe(compressedoptions));
1457 // need a decoded version of compressedoptions for use within forms
1458 // as browsers encode values from forms before sending to server
1459 // (e.g. %25 becomes %2525)
1460 decode_cgi_arg (compressedoptions);
1461 if (args["w"] == "utf-8") { // if the encoding was utf-8, then compressed options was utf-8, and we need unicode.
1462 // if encoding wasn't utf-8, then compressed opotions may be screwed up, but seems to work for 8 bit encodings?
1463 compressedoptions = to_uni(compressedoptions);
1464 }
1465 disp.setmacro ("decodedcompressedoptions", displayclass::defaultpackage, dm_safe(compressedoptions));
1466
1467#if defined (__WIN32__)
1468 disp.setmacro ("win32", displayclass::defaultpackage, "1");
1469#endif
1470
1471 // set _cgiargX_ macros for each cgi argument
1472 cgiargsclass::const_iterator argshere = args.begin();
1473 cgiargsclass::const_iterator argsend = args.end();
1474 while (argshere != argsend) {
1475 if (((*argshere).first == "q") ||
1476 ((*argshere).first == "qa") ||
1477 ((*argshere).first == "qtt") ||
1478 ((*argshere).first == "qty") ||
1479 ((*argshere).first == "qp") ||
1480 ((*argshere).first == "qpl") ||
1481 ((*argshere).first == "qr") ||
1482 ((*argshere).first == "q2"))
1483 // need to escape special characters from query string
1484 disp.setmacro ("cgiarg" + (*argshere).first,
1485 displayclass::defaultpackage, html_safe((*argshere).second.value));
1486 else if ((*argshere).first == "hp") {
1487 disp.setmacro ("cgiarg" + (*argshere).first, displayclass::defaultpackage, (*argshere).second.value);
1488 } else {
1489 disp.setmacro ("cgiarg" + (*argshere).first, displayclass::defaultpackage, dm_safe((*argshere).second.value));
1490 }
1491 ++argshere;
1492 }
1493
1494 // set collection specific macros
1495 if (!collection.empty()) {
1496 recptproto *collectproto = protocols.getrecptproto (collection, logout);
1497 if (collectproto != NULL) {
1498 FilterResponse_t response;
1499 text_tset metadata;
1500 get_info ("collection", collection, args["l"], metadata, false,
1501 collectproto, response, logout);
1502
1503 if (!response.docInfo[0].metadata.empty()) {
1504 MetadataInfo_tmap::const_iterator here = response.docInfo[0].metadata.begin();
1505 MetadataInfo_tmap::const_iterator end = response.docInfo[0].metadata.end();
1506 while (here != end) {
1507 if (((*here).first != "haschildren") && ((*here).first != "hasnext") &&
1508 ((*here).first != "hasprevious")) {
1509 // check for args in form name:lang
1510 text_t name = g_EmptyText;
1511 text_t lang = g_EmptyText;
1512 bool colonfound=false;
1513 text_t::const_iterator a = (*here).first.begin();
1514 text_t::const_iterator b = (*here).first.end();
1515 while (a !=b) {
1516 if (*a==':') {
1517 colonfound=true;
1518 }
1519 else {
1520 if (colonfound)
1521 lang.push_back(*a);
1522 else name.push_back(*a);
1523 }
1524 ++a;
1525 }
1526 if (!lang.empty()) {
1527 if (args["l"]==lang) {
1528 disp.setcollectionmacro(displayclass::defaultpackage, name, "", (*here).second.values[0]);
1529 }
1530 }
1531 else { // the default one
1532 disp.setcollectionmacro(displayclass::defaultpackage, (*here).first, "", (*here).second.values[0]);
1533 }
1534 }
1535 ++here;
1536 }
1537 }
1538
1539 text_t iconcollection;
1540 disp.expandstring (displayclass::defaultpackage, "_iconcollection_", iconcollection);
1541 if (!iconcollection.empty())
1542 {
1543 ColInfoResponse_t cinfo;
1544 comerror_t err;
1545 collectproto->get_collectinfo (collection, cinfo, err, logout);
1546 if (iconcollection[0]=='/' && !cinfo.httpdomain.empty())
1547 {
1548 // local but with full path
1549 iconcollection = "http://" + cinfo.httpdomain + iconcollection;
1550 disp.setmacro("iconcollection", displayclass::defaultpackage, iconcollection);
1551 }
1552 }
1553 }
1554 }
1555
1556 if (!collection.empty()) {
1557 ColInfoResponse_t cinfo;
1558 comerror_t err;
1559 recptproto *collectproto = protocols.getrecptproto (collection, logout);
1560 if (collectproto != NULL) {
1561 collectproto->get_collectinfo (collection, cinfo, err, logout);
1562
1563
1564 // This part of the code used to use "cinfo.httpprefix" regardless
1565 // of the value it contained. Since
1566 // this can come back with an empty (in the case of gsdl_mod), the
1567 // URL produced was invalid.
1568 //
1569 // Changed to test for empty first, and use configinfo.httpprefix as
1570 // a "backup"
1571 //
1572 // Point to consider: since configinfo.httpprefix has been offically
1573 // set as "httpprefix" in macros, it seems to make more sense to use
1574 // always use that version and not the cinfo version at all.
1575
1576 text_t httpprefix
1577 = (!cinfo.httpprefix.empty()) ? cinfo.httpprefix : configinfo.httpprefix;
1578
1579 text_t httpcollection;
1580 if (!cinfo.httpdomain.empty()) httpcollection = "http://";
1581 httpcollection += cinfo.httpdomain + httpprefix + "/collect/"
1582 + collection;
1583 disp.setmacro ("httpcollection", displayclass::defaultpackage,
1584 httpcollection);
1585
1586 // as of gsdl 2.53, collect.cfg can specify macros
1587 if (cinfo.collection_macros.size() > 0) {
1588 collectionmeta_map::const_iterator this_macro=cinfo.collection_macros.begin();
1589 collectionmeta_map::const_iterator done_macro=cinfo.collection_macros.end();
1590 while (this_macro != done_macro) {
1591 text_t package = "Global";
1592 text_t macroname = this_macro->first;
1593 // if this macro name is AAA:bbb then extract the package name
1594 text_t::const_iterator thischar, donechar;
1595 thischar = macroname.begin();
1596 donechar = macroname.end();
1597 while (thischar < donechar) {
1598 if (*thischar == ':') {
1599 package = substr(macroname.begin(),thischar);
1600 macroname = substr(thischar+1,donechar);
1601 break;
1602 }
1603 ++thischar;
1604 }
1605
1606 text_tmap params_map = this_macro->second;
1607 text_tmap::const_iterator this_param = params_map.begin();
1608 text_tmap::const_iterator done_param = params_map.end();
1609 while (this_param != done_param) {
1610 disp.setcollectionmacro(package,
1611 macroname,
1612 this_param->first,
1613 this_param->second);
1614 ++this_param;
1615 }
1616
1617 ++this_macro;
1618 }
1619 } // col macros
1620 } // collectproto != NULL
1621 }
1622
1623}
1624
1625// gets collection info from cache if found or
1626// calls collection server (and updates cache)
1627// returns NULL if there's an error
1628ColInfoResponse_t *receptionist::get_collectinfo_ptr (recptproto *collectproto,
1629 const text_t &collection,
1630 ostream &logout) {
1631
1632 // check the cache
1633 colinfo_tmap::iterator it = configinfo.collectinfo.find (collection);
1634 if ((it != configinfo.collectinfo.end()) && ((*it).second.info_loaded)) {
1635 // found it
1636 return &((*it).second.info);
1637 }
1638
1639 // not cached, get info from collection server
1640 if (collectproto == NULL) {
1641 logout << "ERROR: receptionist::get_collectinfo_ptr passed null collectproto\n";
1642 return NULL;
1643 }
1644
1645 comerror_t err;
1646 if (it == configinfo.collectinfo.end()) {
1647 collectioninfo_t cinfo;
1648 collectproto->get_collectinfo (collection, cinfo.info, err, logout);
1649 if (err != noError) {
1650 outconvertclass text_t2ascii;
1651 logout << text_t2ascii << "ERROR (receptionist::getcollectinfo_ptr): \""
1652 << get_comerror_string (err) << "\"while getting collectinfo\n";
1653 return NULL;
1654 }
1655 cinfo.info_loaded = true;
1656 configinfo.collectinfo[collection] = cinfo;
1657 return &(configinfo.collectinfo[collection].info);
1658 } else {
1659 collectproto->get_collectinfo (collection, (*it).second.info, err, logout);
1660 if (err != noError) {
1661 outconvertclass text_t2ascii;
1662 logout << text_t2ascii << "ERROR (receptionist::getcollectinfo_ptr): \""
1663 << get_comerror_string (err) << "\"while getting collectinfo\n";
1664 return NULL;
1665 }
1666 (*it).second.info_loaded = true;
1667 return &((*it).second.info);
1668 }
1669}
1670
1671// removes a collection from the cache so that the next
1672// call to get_collectinfo_ptr() for that collection will
1673// retrieve the collection info from the collection server
1674void receptionist::uncache_collection (const text_t &collection) {
1675
1676 colinfo_tmap::iterator it = configinfo.collectinfo.find (collection);
1677 if ((it != configinfo.collectinfo.end()) && ((*it).second.info_loaded)) {
1678
1679 (*it).second.info_loaded = false;
1680
1681 }
1682}
1683
1684// Handles an "Encoding" line from a configuration file - note that the
1685// configinfo.encodings map is a bit of a hack (to be fixed when the
1686// configuration files are tidied up).
1687void receptionist::configure_encoding (const text_tarray &cfgline) {
1688
1689 text_t subkey, subvalue, shortname, longname, mapfile;
1690 int multibyte = 0;
1691 text_t::const_iterator cfglinesub_here;
1692 text_tarray::const_iterator cfgline_here = cfgline.begin();
1693 text_tarray::const_iterator cfgline_end = cfgline.end();
1694 while (cfgline_here != cfgline_end) {
1695 if (*cfgline_here == "multibyte") {
1696 multibyte = 1;
1697 } else {
1698 cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
1699 (*cfgline_here).end(), '=', subkey);
1700 if (subkey == "shortname") {
1701 shortname = substr (cfglinesub_here, (*cfgline_here).end());
1702 } else if (subkey == "longname") {
1703 longname = substr (cfglinesub_here, (*cfgline_here).end());
1704 } else if (subkey == "map") {
1705 mapfile = substr (cfglinesub_here, (*cfgline_here).end());
1706 }
1707 }
1708 ++cfgline_here;
1709 }
1710 if (!shortname.empty()) {
1711 if (longname.empty()) longname = shortname;
1712
1713 // add the converter
1714 if (shortname == "utf-8") {
1715 utf8inconvertclass *utf8inconvert = new utf8inconvertclass();
1716 utf8outconvertclass *utf8outconvert = new utf8outconvertclass();
1717 utf8outconvert->set_rzws(1);
1718 add_converter (shortname, utf8inconvert, utf8outconvert);
1719 configinfo.encodings[longname] = shortname;
1720
1721 } else if (shortname == "utf-16be") {
1722 // we use the default input converter as this shouldn't ever be used
1723 // for converting from unicode...
1724 inconvertclass *inconverter = new inconvertclass();
1725 utf16outconvertclass *outconverter = new utf16outconvertclass();
1726 add_converter (shortname, inconverter, outconverter);
1727 configinfo.encodings[longname] = shortname;
1728
1729 } else if (!mapfile.empty()) {
1730
1731 if (mapfile == "8859_1.ump") {
1732 // iso-8859-1 is a special case as it'll always be supported by the
1733 // standard converter class and therefore doesn't need to use its
1734 // mapping file
1735 inconvertclass *inconvert = new inconvertclass();
1736 rzwsoutconvertclass *outconvert = new rzwsoutconvertclass();
1737 outconvert->set_rzws(1);
1738 add_converter (shortname, inconvert, outconvert);
1739 configinfo.encodings[longname] = shortname;
1740
1741 } else {
1742 text_t to_uc_map = filename_cat(configinfo.gsdlhome, "mappings", "to_uc", mapfile);
1743 text_t from_uc_map = filename_cat(configinfo.gsdlhome, "mappings", "from_uc", mapfile);
1744 if (file_exists(to_uc_map) && file_exists(from_uc_map)) {
1745
1746 mapinconvertclass *mapinconvert = new mapinconvertclass();
1747 mapinconvert->setmapfile (to_uc_map, 0x003F);
1748 mapinconvert->set_multibyte (multibyte);
1749 mapoutconvertclass *mapoutconvert = new mapoutconvertclass();
1750 mapoutconvert->setmapfile (from_uc_map, 0x3F);
1751 mapoutconvert->set_multibyte (multibyte);
1752 mapoutconvert->set_rzws(1);
1753 add_converter (shortname, mapinconvert, mapoutconvert);
1754 configinfo.encodings[longname] = shortname;
1755 }
1756 }
1757 }
1758 }
1759}
Note: See TracBrowser for help on using the repository browser.