source: branches/z3950-branch/gsdl/src/recpt/receptionist.cpp@ 1342

Last change on this file since 1342 was 1342, checked in by johnmcp, 24 years ago

Relatively stable z39.50 implementation now, merged with the mgpp source.
(Still needs a decent interface and query language though...)

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 37.6 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#include "receptionist.h"
27#include "fileutil.h"
28#include "cgiutils.h"
29#include "htmlutils.h"
30#include "gsdltools.h"
31#include "OIDtools.h"
32#include <assert.h>
33#include <time.h>
34#include <stdio.h>
35#if defined (GSDL_USE_IOS_H)
36#include <fstream.h>
37#else
38#include <fstream>
39#endif
40
41#if defined (__WIN32_)
42#include "wincgiutils.h"
43#endif
44
45void recptconf::clear () {
46 gsdlhome.clear();
47 gdbmhome.clear();
48 collectinfo.erase(collectinfo.begin(), collectinfo.end());
49 collection.clear();
50 collectdir.clear();
51 httpprefix.clear();
52 httpimg = "/images";
53 gwcgi.clear();
54 macrofiles.erase(macrofiles.begin(), macrofiles.end());
55 saveconf.clear();
56 usecookies = false;
57 logcgiargs = false;
58
59 // these default page parameters can always be overriden
60 // in the configuration file
61 pageparams.erase(pageparams.begin(), pageparams.end());
62 pageparams["c"] = "";
63 pageparams["l"] = "en";
64
65#ifdef MACROPRECEDENCE
66 macroprecedence = MACROPRECEDENCE;
67#else
68 macroprecedence.clear();
69#endif
70}
71
72void collectioninfo_t::clear () {
73 gsdl_gsdlhome.clear();
74 gsdl_gdbmhome.clear();
75
76 info_loaded = false;
77 info.clear();
78}
79
80receptionist::receptionist () {
81 // create a list of cgi arguments
82 // this must be done before the configuration
83
84 cgiarginfo ainfo;
85
86 ainfo.shortname = "e";
87 ainfo.longname = "compressed arguments";
88 ainfo.multiplechar = true;
89 ainfo.defaultstatus = cgiarginfo::good;
90 ainfo.argdefault = "";
91 ainfo.savedarginfo = cgiarginfo::mustnot;
92 argsinfo.addarginfo (NULL, ainfo);
93
94 ainfo.shortname = "a";
95 ainfo.longname = "action";
96 ainfo.multiplechar = true;
97 ainfo.defaultstatus = cgiarginfo::none;
98 ainfo.argdefault = "";
99 ainfo.savedarginfo = cgiarginfo::must;
100 argsinfo.addarginfo (NULL, ainfo);
101
102 // w=western
103 ainfo.shortname = "w";
104 ainfo.longname = "encoding";
105 ainfo.multiplechar = true;
106 ainfo.defaultstatus = cgiarginfo::weak;
107 ainfo.argdefault = "w";
108 ainfo.savedarginfo = cgiarginfo::must;
109 argsinfo.addarginfo (NULL, ainfo);
110
111 ainfo.shortname = "nw";
112 ainfo.longname = "new encoding";
113 ainfo.multiplechar = true;
114 ainfo.defaultstatus = cgiarginfo::none;
115 ainfo.argdefault = "";
116 ainfo.savedarginfo = cgiarginfo::mustnot;
117 argsinfo.addarginfo (NULL, ainfo);
118
119 ainfo.shortname = "c";
120 ainfo.longname = "collection";
121 ainfo.multiplechar = true;
122 ainfo.defaultstatus = cgiarginfo::none;
123 ainfo.argdefault = "";
124 ainfo.savedarginfo = cgiarginfo::must;
125 argsinfo.addarginfo (NULL, ainfo);
126
127 // the interface language name should use the ISO 639
128 // standard
129 ainfo.shortname = "l";
130 ainfo.longname = "interface language";
131 ainfo.multiplechar = true;
132 ainfo.defaultstatus = cgiarginfo::weak;
133 ainfo.argdefault = "en";
134 ainfo.savedarginfo = cgiarginfo::must;
135 argsinfo.addarginfo (NULL, ainfo);
136
137 // the GSDL_UID (cookie)
138 ainfo.shortname = "z";
139 ainfo.longname = "gsdl uid";
140 ainfo.multiplechar = true;
141 ainfo.defaultstatus = cgiarginfo::none;
142 ainfo.argdefault = "";
143 ainfo.savedarginfo = cgiarginfo::mustnot;
144 argsinfo.addarginfo (NULL, ainfo);
145}
146
147
148void receptionist::add_action (action *theaction) {
149 // make sure we have an action to add
150 if (theaction == NULL) return;
151
152 // add this action to the list of actions
153 actions.addaction(theaction);
154
155 // add the cgi arguments from this action
156 argsinfo.addarginfo (NULL, theaction->getargsinfo());
157}
158
159
160void receptionist::add_browser (browserclass *thebrowser) {
161 // make sure we have a browser to add
162 if (thebrowser == NULL) return;
163
164 // add this browser to the list of browsers
165 browsers.addbrowser(thebrowser);
166}
167
168
169void receptionist::setdefaultbrowser (const text_t &browsername) {
170 browsers.setdefaultbrowser (browsername);
171}
172
173
174// configure should be called for each line in the
175// configuration files to configure the receptionist and everything
176// it contains. The configuration should take place after everything
177// has been added but before the initialisation.
178void receptionist::configure (const text_t &key, const text_tarray &cfgline) {
179 // configure the receptionist
180 if (cfgline.size() >= 1) {
181 cgiarginfo *info = NULL;
182 if (key == "gsdlhome") configinfo.gsdlhome = cfgline[0];
183 else if (key == "collection") {
184 configinfo.collection = cfgline[0];
185 // also need to set the default arg to this collection
186 if ((info = argsinfo.getarginfo("c")) != NULL) {
187 info->defaultstatus = cgiarginfo::good;
188 info->argdefault = cfgline[0];
189 }
190
191 } else if (key == "collectdir") configinfo.collectdir = cfgline[0];
192 else if (key == "httpprefix") configinfo.httpprefix = cfgline[0];
193 else if (key == "httpimg") configinfo.httpimg = cfgline[0];
194 else if (key == "gwcgi") configinfo.gwcgi = cfgline[0];
195 else if (key == "macrofiles") {
196 // want to append to macrofiles (i.e. may be several config files
197 // contributing, maybe from several collections).
198 text_tarray::const_iterator here = cfgline.begin();
199 text_tarray::const_iterator end = cfgline.end();
200 while (here != end) {
201 configinfo.macrofiles.insert (*here);
202 here ++;
203 }
204 }
205 else if (key == "saveconf") configinfo.saveconf = cfgline[0];
206 else if (key == "usecookies") configinfo.usecookies = (cfgline[0] == "true");
207 else if (key == "logcgiargs") configinfo.logcgiargs = (cfgline[0] == "true");
208 else if (key == "pageparam") {
209 if (cfgline.size() >= 2) configinfo.pageparams[cfgline[0]] = cfgline[1];
210 else configinfo.pageparams[cfgline[0]] = "";
211 }
212 else if (key == "macroprecedence") configinfo.macroprecedence = cfgline[0];
213 else if (key == "collectinfo") {
214 if (cfgline.size() >= 3) {
215 collectioninfo_t cinfo;
216 cinfo.gsdl_gsdlhome = cfgline[1];
217 cinfo.gsdl_gdbmhome = cfgline[2];
218 configinfo.collectinfo[cfgline[0]] = cinfo;
219 }
220 }
221
222 else if (key == "cgiarg") {
223 // get shortname
224 bool seen_defaultstatus = false;
225 text_t subkey, subvalue;
226 text_t shortname;
227 text_t::const_iterator cfglinesub_here;
228 text_tarray::const_iterator cfgline_here = cfgline.begin();
229 text_tarray::const_iterator cfgline_end = cfgline.end();
230 while (cfgline_here != cfgline_end) {
231 cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
232 (*cfgline_here).end(), '=', subkey);
233 if (subkey == "shortname") {
234 shortname = substr (cfglinesub_here, (*cfgline_here).end());
235 }
236 cfgline_here++;
237 }
238
239 // if we found the shortname process the line again filling in values
240 if (!shortname.empty()) {
241 cgiarginfo &chinfo = argsinfo[shortname];
242 chinfo.shortname = shortname; // in case this is a new argument
243
244 cfgline_here = cfgline.begin();
245 while (cfgline_here != cfgline_end) {
246 cfglinesub_here = getdelimitstr((*cfgline_here).begin(),
247 (*cfgline_here).end(), '=', subkey);
248 subvalue = substr (cfglinesub_here, (*cfgline_here).end());
249
250 if (subkey == "longname") chinfo.longname = subvalue;
251 else if (subkey == "multiplechar") chinfo.multiplechar = (subvalue == "true");
252 else if (subkey == "defaultstatus") {
253 seen_defaultstatus = true;
254 if (subvalue == "none") chinfo.defaultstatus = cgiarginfo::none;
255 else if (subvalue == "weak") chinfo.defaultstatus = cgiarginfo::weak;
256 else if (subvalue == "good") chinfo.defaultstatus = cgiarginfo::good;
257 else if (subvalue == "config") chinfo.defaultstatus = cgiarginfo::config;
258 else if (subvalue == "imperative") chinfo.defaultstatus = cgiarginfo::imperative;
259 }
260 else if (subkey == "argdefault") {
261 chinfo.argdefault = subvalue;
262 if (!seen_defaultstatus) chinfo.defaultstatus = cgiarginfo::config;
263 }
264 else if (subkey == "savedarginfo") {
265 if (subvalue == "mustnot") chinfo.savedarginfo = cgiarginfo::mustnot;
266 else if (subvalue == "can") chinfo.savedarginfo = cgiarginfo::can;
267 else if (subvalue == "must") chinfo.savedarginfo = cgiarginfo::must;
268 }
269
270 cfgline_here++;
271 }
272 }
273 }
274 }
275
276 // configure the actions
277 actionptrmap::iterator actionhere = actions.begin ();
278 actionptrmap::iterator actionend = actions.end ();
279
280 while (actionhere != actionend) {
281 assert ((*actionhere).second.a != NULL);
282 if ((*actionhere).second.a != NULL)
283 (*actionhere).second.a->configure(key, cfgline);
284
285 actionhere++;
286 }
287
288 // configure the protocols
289 recptprotolistclass::iterator protohere = protocols.begin ();
290 recptprotolistclass::iterator protoend = protocols.end ();
291
292 while (protohere != protoend) {
293 assert ((*protohere).p != NULL);
294 if ((*protohere).p != NULL)
295 (*protohere).p->configure(key, cfgline);
296
297 protohere++;
298 }
299
300 // configure the browsers
301 browserptrmap::iterator browserhere = browsers.begin ();
302 browserptrmap::iterator browserend = browsers.end ();
303
304 while (browserhere != browserend) {
305 assert ((*browserhere).second.b != NULL);
306 if ((*browserhere).second.b != NULL)
307 (*browserhere).second.b->configure(key, cfgline);
308
309 browserhere++;
310 }
311}
312
313
314void receptionist::configure (const text_t &key, const text_t &value) {
315 text_tarray cfgline;
316 cfgline.push_back (value);
317 configure(key, cfgline);
318}
319
320
321// init should be called after all the actions, protocols, and
322// converters have been added to the receptionist and after everything
323// has been configured but before any pages are created.
324// It returns true on success and false on failure. If false is
325// returned getpage should not be called (without producing
326// meaningless output), instead an error page should be
327// produced by the calling code.
328bool receptionist::init (ostream &logout) {
329 // first configure collectdir
330 text_t thecollectdir = configinfo.gsdlhome;
331 if (!configinfo.collection.empty()) {
332 // collection specific mode
333 if (!configinfo.collectdir.empty()) {
334 // has already been configured
335 thecollectdir = configinfo.collectdir;
336 } else {
337 // decide where collectdir is by searching for collect.cfg
338 // look in $GSDLHOME/collect/collection-name/etc/collect.cfg and
339 // then $GSDLHOME/etc/collect.cfg
340 thecollectdir = filename_cat (configinfo.gsdlhome, "collect");
341 thecollectdir = filename_cat (thecollectdir, configinfo.collection);
342 text_t filename = filename_cat (thecollectdir, "etc");
343 filename = filename_cat (filename, "collect.cfg");
344
345 if (!file_exists(filename)) thecollectdir = configinfo.gsdlhome;
346 }
347 }
348 configure("collectdir", thecollectdir);
349
350 // read in the macro files
351 if (!read_macrofiles (logout)) return false;
352
353 // there must be at least one action defined
354 if (actions.empty()) {
355 logout << "Error: no actions have been added to the receptionist\n";
356 return false;
357 }
358
359 // there must be at least one browser defined
360 if (browsers.empty()) {
361 logout << "Error: no browsers have been added to the receptionist\n";
362 return false;
363 }
364
365 // create a saveconf string if there isn't one already
366 if (configinfo.saveconf.empty())
367 configinfo.saveconf = create_save_conf_str (argsinfo, logout);
368
369 // check the saveconf string
370 if (!check_save_conf_str (configinfo.saveconf, argsinfo, logout))
371 return false;
372
373 // set a random seed
374 srand (time(NULL));
375
376 // make the output converters remove all the zero-width spaces
377 convertinfoclass::iterator converthere = converters.begin ();
378 convertinfoclass::iterator convertend = converters.end ();
379 text_t defaultconvertname;
380 while (converthere != convertend) {
381 assert ((*converthere).second.outconverter != NULL);
382 if ((*converthere).second.outconverter != NULL) {
383 (*converthere).second.outconverter->set_rzws(1);
384 if (defaultconvertname.empty())
385 defaultconvertname = (*converthere).second.name;
386 }
387 converthere++;
388 }
389
390 // set default converter if no good one has been defined
391 if (!defaultconvertname.empty()) {
392 cgiarginfo *ainfo = argsinfo.getarginfo ("w");
393 if (ainfo->argdefault != "w") {
394 if ((ainfo != NULL) && (converters.get_outconverter(ainfo->argdefault) == NULL)) {
395 ainfo->defaultstatus = cgiarginfo::good;
396 ainfo->argdefault = defaultconvertname;
397 }
398 }
399 }
400
401 // init the actions
402 actionptrmap::iterator actionhere = actions.begin ();
403 actionptrmap::iterator actionend = actions.end ();
404 while (actionhere != actionend) {
405 if (((*actionhere).second.a == NULL) ||
406 !(*actionhere).second.a->init(logout)) return false;
407 actionhere++;
408 }
409
410 // init the protocols
411 recptprotolistclass::iterator protohere = protocols.begin ();
412 recptprotolistclass::iterator protoend = protocols.end ();
413 while (protohere != protoend) {
414 if (((*protohere).p == NULL) ||
415 !(*protohere).p->init(logout)) return false;
416 protohere++;
417 }
418
419 // init the browsers
420 browserptrmap::iterator browserhere = browsers.begin ();
421 browserptrmap::iterator browserend = browsers.end ();
422 while (browserhere != browserend) {
423 if (((*browserhere).second.b == NULL) ||
424 !(*browserhere).second.b->init(logout)) return false;
425 browserhere++;
426 }
427
428 return true;
429}
430
431
432// parse_cgi_args parses cgi arguments into an argument class.
433// This function should be called for each page request. It returns false
434// if there was a major problem with the cgi arguments.
435bool receptionist::parse_cgi_args (const text_t &argstr, cgiargsclass &args,
436 ostream &logout, text_tmap &fcgienv) {
437 outconvertclass text_t2ascii;
438
439 // get an initial list of cgi arguments
440 args.clear();
441 split_cgi_args (argsinfo, argstr, args);
442
443 // expand the compressed argument (if there was one)
444 if (!expand_save_args (argsinfo, configinfo.saveconf, args, logout)) return false;
445
446 // add the defaults
447 add_default_args (argsinfo, args, logout);
448
449 // get the cookie
450 if (configinfo.usecookies) get_cookie(args["z"], fcgienv);
451
452 // get the input encoding
453 text_t &arg_w = args["w"];
454 inconvertclass defaultinconvert;
455 inconvertclass *inconvert = converters.get_inconverter (arg_w);
456 if (inconvert == NULL) inconvert = &defaultinconvert;
457
458 // see if the next page will have a different encoding
459 if (args.getarg("nw") != NULL) arg_w = args["nw"];
460
461 // convert arguments which aren't in unicode to unicode
462 args_tounicode (args, *inconvert);
463
464
465 // decide on the output conversion class (needed for checking the external
466 // cgi arguments)
467 rzwsoutconvertclass defaultoutconverter;
468 rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
469 if (outconverter == NULL) outconverter = &defaultoutconverter;
470 outconverter->reset();
471
472 // check the main cgi arguments
473 if (!check_mainargs (args, logout)) return false;
474
475 // check the arguments for the action
476 action *a = actions.getaction (args["a"]);
477 if (a != NULL) {
478 if (!a->check_cgiargs (argsinfo, args, logout)) return false;
479 } else {
480 // the action was not found!!
481 logout << text_t2ascii << "Error: the action \"" << args["a"]
482 << "\" could not be found.\n";
483 return false;
484 }
485
486 // check external cgi arguments for each action
487 actionptrmap::iterator actionhere = actions.begin ();
488 actionptrmap::iterator actionend = actions.end ();
489 while (actionhere != actionend) {
490 assert ((*actionhere).second.a != NULL);
491 if ((*actionhere).second.a != NULL) {
492 if (!(*actionhere).second.a->check_external_cgiargs (argsinfo, args, *outconverter,
493 configinfo.saveconf, logout))
494 return false;
495 }
496 actionhere++;
497 }
498
499 // the action might have changed but we will assume that
500 // the cgiargs were checked properly when the change was made
501
502 return true;
503}
504
505// returns true if cookie already existed, false
506// if it was generated
507bool receptionist::get_cookie (text_t &cookie, text_tmap &fcgienv) {
508
509 text_t cookiestring = gsdl_getenv ("HTTP_COOKIE", fcgienv);
510
511 text_t::const_iterator end = cookiestring.end();
512 text_t::const_iterator here = findchar (cookiestring.begin(), end, 'G');
513
514 while (here+9 < end) {
515
516 if (substr(here, here+8) == "GSDL_UID") {
517 cookie = substr (here+9, findchar (here+9, end, ';'));
518 return true;
519 }
520 here = findchar (cookiestring.begin(), end, 'G');
521 }
522
523 cookie.clear();
524 text_t host = gsdl_getenv("REMOTE_ADDR", fcgienv);
525 time_t ttime = time(NULL);
526 if (!host.empty()) {
527 cookie += host;
528 cookie.push_back ('-');
529 }
530 cookie += text_t(ttime);
531
532 return false;
533}
534
535// as above but just tests if cookie exists
536bool receptionist::get_cookie (text_tmap &fcgienv) {
537
538 text_t c = gsdl_getenv("HTTP_COOKIE", fcgienv);
539 if (!c.empty()) {
540 text_t cookiestring = c;
541
542 text_t::const_iterator end = cookiestring.end();
543 text_t::const_iterator here = findchar (cookiestring.begin(), end, 'G');
544
545 while (here+9 < end) {
546 if (substr(here, here+8) == "GSDL_UID") return true;
547 here = findchar (cookiestring.begin(), end, 'G');
548 }
549 }
550 return false;
551}
552
553bool receptionist::log_cgi_args (cgiargsclass &args, ostream &logout, text_tmap &fcgienv) {
554
555 // see if we want to log the cgi arguments
556 if (!configinfo.logcgiargs) return true;
557
558 text_t host = gsdl_getenv ("REMOTE_HOST", fcgienv);
559 text_t script_name = gsdl_getenv ("SCRIPT_NAME", fcgienv);
560 if (host.empty()) host = gsdl_getenv ("REMOTE_ADDR", fcgienv);
561 text_t browser = gsdl_getenv ("HTTP_USER_AGENT", fcgienv);
562 time_t ttime = time(NULL);
563
564 cgiargsclass::const_iterator args_here = args.begin();
565 cgiargsclass::const_iterator args_end = args.end();
566
567 text_t argstr;
568 bool first = true;
569 while (args_here != args_end) {
570 if (!first) argstr += ", ";
571 argstr += (*args_here).first + "=" + (*args_here).second.value;
572 first = false;
573 args_here ++;
574 }
575
576 text_t logfile = filename_cat (configinfo.gsdlhome, "etc");
577 logfile = filename_cat (logfile, "usage.txt");
578
579 text_t logstr = script_name;
580 logstr += " " + host;
581 logstr += " [";
582 logstr += ttime;
583 logstr += "] (" + argstr + ") \"";
584 logstr += browser;
585 logstr += "\"\n";
586
587 return append_logstr (logfile, logstr, logout);
588}
589
590bool receptionist::append_logstr (const text_t &filename, const text_t &logstr,
591 ostream &logout) {
592
593 utf8outconvertclass text_t2utf8;
594 char *lfile = filename.getcstr();
595
596 ofstream log (lfile, ios::app);
597
598 if (!log) {
599 logout << "Error: Couldn't open file " << lfile << "\n";
600 delete lfile;
601 return false;
602 }
603
604 int fd = GSDL_GET_FILEDESC(log);
605
606 // lock_val is set to 0 if file is locked successfully
607 int lock_val = 1;
608 GSDL_LOCK_FILE (fd);
609 if (lock_val == 0) {
610 log << text_t2utf8 << logstr;
611 GSDL_UNLOCK_FILE (fd);
612 } else {
613 logout << "Error: Couldn't lock file " << lfile << "\n";
614 log.close();
615 delete lfile;
616 return false;
617 }
618
619 log.close();
620
621 delete lfile;
622 return true;
623}
624
625text_t receptionist::expandmacros (const text_t &astring, cgiargsclass &args,
626 ostream &logout) {
627 text_t outstring;
628 outconvertclass text_t2ascii;
629
630 action *a = actions.getaction (args["a"]);
631 prepare_page (a, args, text_t2ascii, logout);
632 disp.expandstring ("Global", astring, outstring);
633 return outstring;
634}
635
636// produce_cgi_page will call get_cgihead_info and
637// produce_content in the appropriate way to output a cgi header and
638// the page content (if needed). If a page could not be created it
639// will return false
640bool receptionist::produce_cgi_page (cgiargsclass &args, ostream &contentout,
641 ostream &logout, text_tmap &fcgienv) {
642 outconvertclass text_t2ascii;
643
644 response_t response;
645 text_t response_data;
646
647 // produce cgi header
648 get_cgihead_info (args, response, response_data, logout, fcgienv);
649 if (response == location) {
650 // location response (url may contain macros!!)
651 response_data = expandmacros (response_data, args, logout);
652 contentout << text_t2ascii << "Location: " << response_data << "\n\n";
653 contentout << flush;
654 return true;
655 } else if (response == content) {
656 // content response
657 contentout << text_t2ascii << "Content-type: " << response_data << "\n\n";
658 } else {
659 // unknown response
660 logout << "Error: get_cgihead_info returned an unknown response type.\n";
661 return false;
662 }
663
664 // produce cgi page
665 if (!produce_content (args, contentout, logout)) return false;
666
667 // flush contentout
668 contentout << flush;
669 return true;
670}
671
672
673// get_cgihead_info determines the cgi header information for
674// a set of cgi arguments. If response contains location then
675// response_data contains the redirect address. If reponse
676// contains content then reponse_data contains the content-type.
677// Note that images can now be produced by the receptionist.
678void receptionist::get_cgihead_info (cgiargsclass &args, response_t &response,
679 text_t &response_data, ostream &logout,
680 text_tmap &fcgienv) {
681 outconvertclass text_t2ascii;
682
683 // get the action
684 action *a = actions.getaction (args["a"]);
685 if (a != NULL) {
686 a->get_cgihead_info (args, &protocols, response, response_data, logout);
687
688 } else {
689 // the action was not found!!
690 logout << text_t2ascii << "Error receptionist::get_cgihead_info: the action \""
691 << args["a"] << "\" could not be found.\n";
692 response = content;
693 response_data = "text/html";
694 }
695
696 // add the encoding information
697 if (response == content) {
698 if (args["w"] == "u") {
699 response_data += "; charset=UTF-8";
700 } else if (args["w"] == "g") {
701 response_data += "; charset=GBK";
702 } else if (args["w"] == "a") {
703 response_data += "; charset=windows-1256";
704 } else {
705 response_data += "; charset=ISO-8859-1";
706 }
707
708 // add cookie if required
709 if (configinfo.usecookies && !get_cookie(fcgienv))
710 response_data += "\nSet-Cookie: GSDL_UID=" + args["z"]
711 + "; expires=25-Dec-37 00:00:00 GMT";
712 }
713}
714
715
716// produce the page content
717bool receptionist::produce_content (cgiargsclass &args, ostream &contentout,
718 ostream &logout) {
719
720 // decide on the output conversion class
721 text_t &arg_w = args["w"];
722 rzwsoutconvertclass defaultoutconverter;
723 rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
724 if (outconverter == NULL) outconverter = &defaultoutconverter;
725 outconverter->reset();
726
727
728 recptproto *collectproto = protocols.getrecptproto (args["c"], logout);
729 if (collectproto != NULL) {
730 // get browsers to process OID
731 text_t OID = args["d"];
732 if (OID.empty()) OID = args["cl"];
733 if (!OID.empty()) {
734 text_tset metadata;
735 text_tarray OIDs;
736 OIDs.push_back (OID);
737 if (!is_top(OID)) OIDs.push_back (OID + ".pr");
738 FilterResponse_t response;
739 metadata.insert ("childtype");
740 if (get_info (OIDs, args["c"], metadata, false, collectproto, response, logout)) {
741 text_t classifytype;
742 if (!response.docInfo[0].metadata["childtype"].values[0].empty())
743 classifytype = response.docInfo[0].metadata["childtype"].values[0];
744 else if (!is_top (OID)) {
745 if (!response.docInfo[1].metadata["childtype"].values[0].empty())
746 classifytype = response.docInfo[1].metadata["childtype"].values[0];
747 }
748 browserclass *b = browsers.getbrowser (classifytype);
749 b->processOID (args, collectproto, logout);
750 }
751 }
752
753 // translate "d" and "cl" arguments if required
754 translate_OIDs (args, collectproto, logout);
755 }
756
757 // produce the page using the desired action
758 action *a = actions.getaction (args["a"]);
759 if (a != NULL) {
760 if (a->uses_display(args)) prepare_page (a, args, (*outconverter), logout);
761 if (!a->do_action (args, &protocols, &browsers, disp, (*outconverter), contentout, logout))
762 return false;
763
764 } else {
765 // the action was not found!!
766 outconvertclass text_t2ascii;
767
768 logout << text_t2ascii << "Error receptionist::produce_content: the action \""
769 << args["a"] << "\" could not be found.\n";
770
771 contentout << (*outconverter)
772 << "<html>\n"
773 << "<head>\n"
774 << "<title>Error</title>\n"
775 << "</head>\n"
776 << "<body>\n"
777 << "<h2>Oops!</h2>\n"
778 << "Undefined Page. The action \""
779 << args["a"] << "\" could not be found.\n"
780 << "</body>\n"
781 << "</html>\n";
782 }
783 return true;
784}
785
786
787// returns the compressed argument ("e") corresponding to the argument
788// list. This can be used to save preferences between sessions.
789text_t receptionist::get_compressed_arg (cgiargsclass &args, ostream &logout) {
790 // decide on the output conversion class
791 text_t &arg_w = args["w"];
792 rzwsoutconvertclass defaultoutconverter;
793 rzwsoutconvertclass *outconverter = converters.get_outconverter (arg_w);
794 if (outconverter == NULL) outconverter = &defaultoutconverter;
795 outconverter->reset();
796
797 text_t compressed_args;
798 if (compress_save_args (argsinfo, configinfo.saveconf, args,
799 compressed_args, *outconverter, logout))
800 return compressed_args;
801
802 return "";
803}
804
805
806// will read in all the macro files. If one is not found an
807// error message will be written to logout and the method will
808// return false.
809bool receptionist::read_macrofiles (ostream &logout) {
810 outconvertclass text_t2ascii;
811
812 // redirect the error output to logout
813 ostream *savedlogout = disp.setlogout (&logout);
814
815 // load up the default macro files, the collection directory
816 // is searched first for the file (if this is being used in
817 // collection specific mode) and then the main directory(s)
818 text_t colmacrodir = filename_cat (configinfo.collectdir, "macros");
819
820 text_tset maindirs;
821 text_t gsdlmacrodir = filename_cat (configinfo.gsdlhome, "macros");
822 maindirs.insert (gsdlmacrodir);
823 colinfo_tmap::iterator colhere = configinfo.collectinfo.begin();
824 colinfo_tmap::iterator colend = configinfo.collectinfo.end();
825 while (colhere != colend) {
826 if (!((*colhere).second.gsdl_gsdlhome).empty()) {
827 gsdlmacrodir = filename_cat ((*colhere).second.gsdl_gsdlhome, "macros");
828 maindirs.insert (gsdlmacrodir);
829 }
830 colhere ++;
831 }
832
833 text_tset::iterator arrhere = configinfo.macrofiles.begin();
834 text_tset::iterator arrend = configinfo.macrofiles.end();
835 text_t filename;
836 while (arrhere != arrend) {
837 bool foundfile = false;
838
839 // try in the collection directory if this is being
840 // run in collection specific mode
841 if (!configinfo.collection.empty()) {
842 filename = filename_cat (colmacrodir, *arrhere);
843 if (file_exists (filename)) {
844 disp.loaddefaultmacros(filename);
845 foundfile = true;
846 }
847 }
848
849 // if we haven't found the macro file yet try in
850 // the main macro directory(s)
851 // if file is found in more than one main directory
852 // we'll load all copies
853 if (!foundfile) {
854 text_tset::const_iterator dirhere = maindirs.begin();
855 text_tset::const_iterator dirend = maindirs.end();
856 while (dirhere != dirend) {
857 filename = filename_cat (*dirhere, *arrhere);
858 if (file_exists (filename)) {
859 disp.loaddefaultmacros(filename);
860 foundfile = true;
861 }
862 dirhere ++;
863 }
864 }
865
866 // see if we found the file or not
867 if (!foundfile) {
868 logout << text_t2ascii
869 << "Error: the macro file \"" << *arrhere << "\" could not be found.\n";
870 if (configinfo.collection.empty()) {
871 text_t dirs;
872 joinchar (maindirs, ", ", dirs);
873 logout << text_t2ascii
874 << "It should be in either of the following directories ("
875 << dirs << ").\n\n";
876
877 } else {
878 logout << text_t2ascii
879 << "It should be in either " << colmacrodir << " or in "
880 << gsdlmacrodir << ".\n\n";
881 }
882 // reset logout to what it was
883 disp.setlogout (savedlogout);
884 return false;
885 }
886 arrhere++;
887 }
888
889 // success
890
891 // reset logout to what it was
892 disp.setlogout (savedlogout);
893 return true;
894}
895
896
897// check_mainargs will check all the main arguments. If a major
898// error is found it will return false and no cgi page should
899// be created using the arguments.
900bool receptionist::check_mainargs (cgiargsclass &args, ostream &logout) {
901 // if this receptionist is running in collection dependant mode
902 // then it should always set the collection argument to the
903 // collection
904 if (!configinfo.collection.empty()) args["c"] = configinfo.collection;
905
906 // if current collection uses ccscols make sure
907 // "ccs" argument is set and make "cc" default to
908 // all collections in "ccs"
909 if (!args["c"].empty()) {
910
911 text_t &arg_c = args["c"];
912 recptproto *collectproto = protocols.getrecptproto (arg_c, logout);
913 if (collectproto == NULL) {
914 // oops, this collection isn't valid
915 outconvertclass text_t2ascii;
916 logout << text_t2ascii << "ERROR: Invalid collection: " << arg_c << "\n";
917 args["c"].clear();
918
919 } else {
920
921 ColInfoResponse_t *cinfo = get_collectinfo_ptr (collectproto, arg_c, logout);
922
923 if (cinfo != NULL) {
924 if (!cinfo->ccsCols.empty()) {
925 args["ccs"] = 1;
926 if (args["cc"].empty()) {
927 text_tarray::const_iterator col_here = cinfo->ccsCols.begin();
928 text_tarray::const_iterator col_end = cinfo->ccsCols.end();
929 bool first = true;
930 while (col_here != col_end) {
931 // make sure it's a valid collection
932 if (protocols.getrecptproto (*col_here, logout) != NULL) {
933 if (!first) args["cc"].push_back (',');
934 args["cc"] += *col_here;
935 first = false;
936 }
937 col_here ++;
938 }
939 }
940 }
941 } else {
942 logout << "ERROR (receptionist::check_mainargs): get_collectinfo_ptr returned NULL\n";
943 }
944 }
945 }
946
947 // argument "v" can only be 0 or 1. Use the default value
948 // if it is out of range
949 int arg_v = args.getintarg ("v");
950 if (arg_v != 0 && arg_v != 1) {
951 cgiarginfo *vinfo = argsinfo.getarginfo ("v");
952 if (vinfo != NULL) args["v"] = vinfo->argdefault;
953 }
954
955 // argument "f" can only be 0 or 1. Use the default value
956 // if it is out of range
957 int arg_f = args.getintarg ("f");
958 if (arg_f != 0 && arg_f != 1) {
959 cgiarginfo *finfo = argsinfo.getarginfo ("f");
960 if (finfo != NULL) args["f"] = finfo->argdefault;
961 }
962
963 return true;
964}
965
966// translate_OIDs translates the "d" and "cl" arguments to their correct values
967// if they use the tricky ".fc", ".lc" type syntax.
968void receptionist::translate_OIDs (cgiargsclass &args, recptproto *collectproto,
969 ostream &logout) {
970
971 FilterResponse_t response;
972 FilterRequest_t request;
973 comerror_t err;
974 text_t &arg_d = args["d"];
975 text_t &arg_cl = args["cl"];
976 text_t &collection = args["c"];
977
978 // do a call to translate OIDs if required
979 request.filterName = "NullFilter";
980 request.filterResultOptions = FROID;
981 if (!arg_d.empty() && needs_translating (arg_d)) {
982 request.docSet.push_back (arg_d);
983 collectproto->filter (collection, request, response, err, logout);
984 arg_d = response.docInfo[0].OID;
985 request.clear();
986 }
987 // we'll also check here that the "cl" argument has a "classify" doctype
988 // (in case ".fc" or ".lc" have screwed up)
989 if (needs_translating (arg_cl)) {
990 request.fields.insert ("doctype");
991 request.docSet.push_back (arg_cl);
992 request.filterResultOptions = FRmetadata;
993 collectproto->filter (collection, request, response, err, logout);
994 // set to original value (without .xx stuff) if doctype isn't "classify"
995 if (response.docInfo[0].metadata["doctype"].values[0] != "classify")
996 strip_suffix (arg_cl);
997 else
998 arg_cl = response.docInfo[0].OID;
999 }
1000}
1001
1002// prepare_page sets up page parameters, sets display macros
1003// and opens the page ready for output
1004void receptionist::prepare_page (action *a, cgiargsclass &args,
1005 outconvertclass &outconvert,
1006 ostream &logout) {
1007 // set up page parameters
1008 text_t pageparams;
1009 bool first = true;
1010
1011 text_tmap::iterator params_here = configinfo.pageparams.begin();
1012 text_tmap::iterator params_end = configinfo.pageparams.end();
1013 while (params_here != params_end) {
1014 if (args[(*params_here).first] != (*params_here).second) {
1015 if (!first) pageparams += ",";
1016 first = false;
1017 pageparams += (*params_here).first;
1018 pageparams += "=";
1019 pageparams += args[(*params_here).first];
1020 }
1021
1022 params_here++;
1023 }
1024
1025
1026 // open the page
1027 disp.openpage(pageparams, configinfo.macroprecedence);
1028
1029
1030 // define external macros for each action
1031 actionptrmap::iterator actionhere = actions.begin ();
1032 actionptrmap::iterator actionend = actions.end ();
1033
1034 while (actionhere != actionend) {
1035 assert ((*actionhere).second.a != NULL);
1036 if ((*actionhere).second.a != NULL)
1037 (*actionhere).second.a->define_external_macros (disp, args, &protocols, logout);
1038 actionhere++;
1039 }
1040
1041 // define internal macros for the current action
1042 a->define_internal_macros (disp, args, &protocols, logout);
1043
1044 // define general macros. the defining of general macros is done here so that
1045 // the last possible version of the cgi arguments are used
1046 define_general_macros (args, outconvert, logout);
1047}
1048
1049void receptionist::define_general_macros (cgiargsclass &args, outconvertclass &/*outconvert*/,
1050 ostream &logout) {
1051
1052 text_t &collection = args["c"];
1053
1054 disp.setmacro ("gsdlhome", "Global", dm_safe(configinfo.gsdlhome));
1055 disp.setmacro ("gwcgi", "Global", configinfo.gwcgi);
1056 disp.setmacro ("httpimg", "Global", configinfo.httpimg);
1057 disp.setmacro ("httpprefix", "Global", configinfo.httpprefix);
1058 text_t compressedoptions = get_compressed_arg(args, logout);
1059 disp.setmacro ("compressedoptions", "Global", dm_safe(compressedoptions));
1060 // need a decoded version of compressedoptions for use within forms
1061 // as browsers encode values from forms before sending to server
1062 // (e.g. %25 becomes %2525)
1063 decode_cgi_arg (compressedoptions);
1064 disp.setmacro ("decodedcompressedoptions", "Global", dm_safe(compressedoptions));
1065
1066#if defined (__WIN32__)
1067 disp.setmacro ("win32", "Global", "1");
1068#endif
1069
1070 // set macron macros if encoding is utf8
1071 if (args["w"] == "u") {
1072 disp.setmacro ("Amn", "Global", "&#256;");
1073 disp.setmacro ("amn", "Global", "&#257;");
1074 disp.setmacro ("Emn", "Global", "&#274;");
1075 disp.setmacro ("emn", "Global", "&#275;");
1076 disp.setmacro ("Imn", "Global", "&#298;");
1077 disp.setmacro ("imn", "Global", "&#299;");
1078 disp.setmacro ("Omn", "Global", "&#332;");
1079 disp.setmacro ("omn", "Global", "&#333;");
1080 disp.setmacro ("Umn", "Global", "&#362;");
1081 disp.setmacro ("umn", "Global", "&#363;");
1082 }
1083
1084 // set _cgiargX_ macros for each cgi argument
1085 cgiargsclass::const_iterator argshere = args.begin();
1086 cgiargsclass::const_iterator argsend = args.end();
1087 while (argshere != argsend) {
1088 if (((*argshere).first == "q") ||
1089 ((*argshere).first == "qa") ||
1090 ((*argshere).first == "qtt") ||
1091 ((*argshere).first == "qty") ||
1092 ((*argshere).first == "qp") ||
1093 ((*argshere).first == "qpl") ||
1094 ((*argshere).first == "qr") ||
1095 ((*argshere).first == "q2"))
1096 // need to escape special characters from query string
1097 disp.setmacro ("cgiarg" + (*argshere).first,
1098 "Global", html_safe((*argshere).second.value));
1099 else
1100 disp.setmacro ("cgiarg" + (*argshere).first, "Global", dm_safe((*argshere).second.value));
1101 argshere ++;
1102 }
1103
1104 // display text right to left if language is arabic (and if browser can support it)
1105 if (args["l"] == "ar")
1106 disp.setmacro ("htmlextra", "Global", " dir=rtl");
1107
1108 // set collection specific macros
1109 if (!collection.empty()) {
1110 recptproto *collectproto = protocols.getrecptproto (collection, logout);
1111 if (collectproto != NULL) {
1112 FilterResponse_t response;
1113 text_tset metadata;
1114 get_info ("collection", collection, metadata, false,
1115 collectproto, response, logout);
1116
1117 if (!response.docInfo[0].metadata.empty()) {
1118 MetadataInfo_tmap::const_iterator here = response.docInfo[0].metadata.begin();
1119 MetadataInfo_tmap::const_iterator end = response.docInfo[0].metadata.end();
1120 while (here != end) {
1121 if (((*here).first != "haschildren") && ((*here).first != "hasnext") &&
1122 ((*here).first != "hasprevious")) {
1123 disp.setmacro ((*here).first, "Global", (*here).second.values[0]);
1124 }
1125 here ++;
1126 }
1127 }
1128 }
1129 }
1130}
1131
1132// gets collection info from cache if found or
1133// calls collection server (and updates cache)
1134// returns NULL if there's an error
1135ColInfoResponse_t *receptionist::get_collectinfo_ptr (recptproto *collectproto,
1136 const text_t &collection,
1137 ostream &logout) {
1138
1139 // check the cache
1140 colinfo_tmap::iterator it = configinfo.collectinfo.find (collection);
1141 if ((it != configinfo.collectinfo.end()) && ((*it).second.info_loaded)) {
1142 // found it
1143 return &((*it).second.info);
1144 }
1145
1146 // not cached, get info from collection server
1147 if (collectproto == NULL) {
1148 logout << "ERROR: receptionist::get_collectinfo_ptr passed null collectproto\n";
1149 return NULL;
1150 }
1151
1152 comerror_t err;
1153 if (it == configinfo.collectinfo.end()) {
1154 collectioninfo_t cinfo;
1155 collectproto->get_collectinfo (collection, cinfo.info, err, logout);
1156 if (err != noError) {
1157 outconvertclass text_t2ascii;
1158 logout << text_t2ascii << "ERROR (receptionist::getcollectinfo_ptr): \""
1159 << get_comerror_string (err) << "\"while getting collectinfo\n";
1160 return NULL;
1161 }
1162 cinfo.info_loaded = true;
1163 configinfo.collectinfo[collection] = cinfo;
1164 return &(configinfo.collectinfo[collection].info);
1165 } else {
1166 collectproto->get_collectinfo (collection, (*it).second.info, err, logout);
1167 if (err != noError) {
1168 outconvertclass text_t2ascii;
1169 logout << text_t2ascii << "ERROR (receptionist::getcollectinfo_ptr): \""
1170 << get_comerror_string (err) << "\"while getting collectinfo\n";
1171 return NULL;
1172 }
1173 (*it).second.info_loaded = true;
1174 return &((*it).second.info);
1175 }
1176}
Note: See TracBrowser for help on using the repository browser.