Code

793a0d8472b32b4f183910948e953e83e2c005a4
[roundup.git] / roundup / cgi / client.py
1 """WWW request handler (also used in the stand-alone server).
2 """
3 __docformat__ = 'restructuredtext'
5 import base64, binascii, cgi, codecs, mimetypes, os
6 import quopri, random, re, rfc822, stat, sys, time
7 import socket, errno
9 from roundup import roundupdb, date, hyperdb, password
10 from roundup.cgi import templating, cgitb, TranslationService
11 from roundup.cgi.actions import *
12 from roundup.exceptions import *
13 from roundup.cgi.exceptions import *
14 from roundup.cgi.form_parser import FormParser
15 from roundup.mailer import Mailer, MessageSendError, encode_quopri
16 from roundup.cgi import accept_language
17 from roundup import xmlrpc
19 from roundup.anypy.cookie_ import CookieError, BaseCookie, SimpleCookie, \
20     get_cookie_date
21 from roundup.anypy.io_ import StringIO
22 from roundup.anypy import http_
23 from roundup.anypy import urllib_
25 def initialiseSecurity(security):
26     '''Create some Permissions and Roles on the security object
28     This function is directly invoked by security.Security.__init__()
29     as a part of the Security object instantiation.
30     '''
31     p = security.addPermission(name="Web Access",
32         description="User may access the web interface")
33     security.addPermissionToRole('Admin', p)
35     # doing Role stuff through the web - make sure Admin can
36     # TODO: deprecate this and use a property-based control
37     p = security.addPermission(name="Web Roles",
38         description="User may manipulate user Roles through the web")
39     security.addPermissionToRole('Admin', p)
41 # used to clean messages passed through CGI variables - HTML-escape any tag
42 # that isn't <a href="">, <i>, <b> and <br> (including XHTML variants) so
43 # that people can't pass through nasties like <script>, <iframe>, ...
44 CLEAN_MESSAGE_RE = r'(<(/?(.*?)(\s*href="[^"]")?\s*/?)>)'
45 def clean_message(message, mc=re.compile(CLEAN_MESSAGE_RE, re.I)):
46     return mc.sub(clean_message_callback, message)
47 def clean_message_callback(match, ok={'a':1,'i':1,'b':1,'br':1}):
48     """ Strip all non <a>,<i>,<b> and <br> tags from a string
49     """
50     if match.group(3).lower() in ok:
51         return match.group(1)
52     return '&lt;%s&gt;'%match.group(2)
55 error_message = ''"""<html><head><title>An error has occurred</title></head>
56 <body><h1>An error has occurred</h1>
57 <p>A problem was encountered processing your request.
58 The tracker maintainers have been notified of the problem.</p>
59 </body></html>"""
62 class LiberalCookie(SimpleCookie):
63     """ Python's SimpleCookie throws an exception if the cookie uses invalid
64         syntax.  Other applications on the same server may have done precisely
65         this, preventing roundup from working through no fault of roundup.
66         Numerous other python apps have run into the same problem:
68         trac: http://trac.edgewall.org/ticket/2256
69         mailman: http://bugs.python.org/issue472646
71         This particular implementation comes from trac's solution to the
72         problem. Unfortunately it requires some hackery in SimpleCookie's
73         internals to provide a more liberal __set method.
74     """
75     def load(self, rawdata, ignore_parse_errors=True):
76         if ignore_parse_errors:
77             self.bad_cookies = []
78             self._BaseCookie__set = self._loose_set
79         SimpleCookie.load(self, rawdata)
80         if ignore_parse_errors:
81             self._BaseCookie__set = self._strict_set
82             for key in self.bad_cookies:
83                 del self[key]
85     _strict_set = BaseCookie._BaseCookie__set
87     def _loose_set(self, key, real_value, coded_value):
88         try:
89             self._strict_set(key, real_value, coded_value)
90         except CookieError:
91             self.bad_cookies.append(key)
92             dict.__setitem__(self, key, None)
95 class Session:
96     """
97     Needs DB to be already opened by client
99     Session attributes at instantiation:
101     - "client" - reference to client for add_cookie function
102     - "session_db" - session DB manager
103     - "cookie_name" - name of the cookie with session id
104     - "_sid" - session id for current user
105     - "_data" - session data cache
107     session = Session(client)
108     session.set(name=value)
109     value = session.get(name)
111     session.destroy()  # delete current session
112     session.clean_up() # clean up session table
114     session.update(set_cookie=True, expire=3600*24*365)
115                        # refresh session expiration time, setting persistent
116                        # cookie if needed to last for 'expire' seconds
118     """
120     def __init__(self, client):
121         self._data = {}
122         self._sid  = None
124         self.client = client
125         self.session_db = client.db.getSessionManager()
127         # parse cookies for session id
128         self.cookie_name = 'roundup_session_%s' % \
129             re.sub('[^a-zA-Z]', '', client.instance.config.TRACKER_NAME)
130         cookies = LiberalCookie(client.env.get('HTTP_COOKIE', ''))
131         if self.cookie_name in cookies:
132             if not self.session_db.exists(cookies[self.cookie_name].value):
133                 self._sid = None
134                 # remove old cookie
135                 self.client.add_cookie(self.cookie_name, None)
136             else:
137                 self._sid = cookies[self.cookie_name].value
138                 self._data = self.session_db.getall(self._sid)
140     def _gen_sid(self):
141         """ generate a unique session key """
142         while 1:
143             s = '%s%s'%(time.time(), random.random())
144             s = binascii.b2a_base64(s).strip()
145             if not self.session_db.exists(s):
146                 break
148         # clean up the base64
149         if s[-1] == '=':
150             if s[-2] == '=':
151                 s = s[:-2]
152             else:
153                 s = s[:-1]
154         return s
156     def clean_up(self):
157         """Remove expired sessions"""
158         self.session_db.clean()
160     def destroy(self):
161         self.client.add_cookie(self.cookie_name, None)
162         self._data = {}
163         self.session_db.destroy(self._sid)
164         self.client.db.commit()
166     def get(self, name, default=None):
167         return self._data.get(name, default)
169     def set(self, **kwargs):
170         self._data.update(kwargs)
171         if not self._sid:
172             self._sid = self._gen_sid()
173             self.session_db.set(self._sid, **self._data)
174             # add session cookie
175             self.update(set_cookie=True)
177             # XXX added when patching 1.4.4 for backward compatibility
178             # XXX remove
179             self.client.session = self._sid
180         else:
181             self.session_db.set(self._sid, **self._data)
182             self.client.db.commit()
184     def update(self, set_cookie=False, expire=None):
185         """ update timestamp in db to avoid expiration
187             if 'set_cookie' is True, set cookie with 'expire' seconds lifetime
188             if 'expire' is None - session will be closed with the browser
189              
190             XXX the session can be purged within a week even if a cookie
191                 lifetime is longer
192         """
193         self.session_db.updateTimestamp(self._sid)
194         self.client.db.commit()
196         if set_cookie:
197             self.client.add_cookie(self.cookie_name, self._sid, expire=expire)
201 class Client:
202     """Instantiate to handle one CGI request.
204     See inner_main for request processing.
206     Client attributes at instantiation:
208     - "path" is the PATH_INFO inside the instance (with no leading '/')
209     - "base" is the base URL for the instance
210     - "form" is the cgi form, an instance of FieldStorage from the standard
211       cgi module
212     - "additional_headers" is a dictionary of additional HTTP headers that
213       should be sent to the client
214     - "response_code" is the HTTP response code to send to the client
215     - "translator" is TranslationService instance
217     During the processing of a request, the following attributes are used:
219     - "db" 
220     - "error_message" holds a list of error messages
221     - "ok_message" holds a list of OK messages
222     - "session" is deprecated in favor of session_api (XXX remove)
223     - "session_api" is the interface to store data in session
224     - "user" is the current user's name
225     - "userid" is the current user's id
226     - "template" is the current :template context
227     - "classname" is the current class context name
228     - "nodeid" is the current context item id
230     User Identification:
231      Users that are absent in session data are anonymous and are logged
232      in as that user. This typically gives them all Permissions assigned to the
233      Anonymous Role.
235      Every user is assigned a session. "session_api" is the interface to work
236      with session data.
238     Special form variables:
239      Note that in various places throughout this code, special form
240      variables of the form :<name> are used. The colon (":") part may
241      actually be one of either ":" or "@".
242     """
244     # charset used for data storage and form templates
245     # Note: must be in lower case for comparisons!
246     # XXX take this from instance.config?
247     STORAGE_CHARSET = 'utf-8'
249     #
250     # special form variables
251     #
252     FV_TEMPLATE = re.compile(r'[@:]template')
253     FV_OK_MESSAGE = re.compile(r'[@:]ok_message')
254     FV_ERROR_MESSAGE = re.compile(r'[@:]error_message')
256     # Note: index page stuff doesn't appear here:
257     # columns, sort, sortdir, filter, group, groupdir, search_text,
258     # pagesize, startwith
260     # list of network error codes that shouldn't be reported to tracker admin
261     # (error descriptions from FreeBSD intro(2))
262     IGNORE_NET_ERRORS = (
263         # A write on a pipe, socket or FIFO for which there is
264         # no process to read the data.
265         errno.EPIPE,
266         # A connection was forcibly closed by a peer.
267         # This normally results from a loss of the connection
268         # on the remote socket due to a timeout or a reboot.
269         errno.ECONNRESET,
270         # Software caused connection abort.  A connection abort
271         # was caused internal to your host machine.
272         errno.ECONNABORTED,
273         # A connect or send request failed because the connected party
274         # did not properly respond after a period of time.
275         errno.ETIMEDOUT,
276     )
278     def __init__(self, instance, request, env, form=None, translator=None):
279         # re-seed the random number generator
280         random.seed()
281         self.start = time.time()
282         self.instance = instance
283         self.request = request
284         self.env = env
285         self.setTranslator(translator)
286         self.mailer = Mailer(instance.config)
288         # save off the path
289         self.path = env['PATH_INFO']
291         # this is the base URL for this tracker
292         self.base = self.instance.config.TRACKER_WEB
294         # check the tracker_we setting
295         if not self.base.endswith('/'):
296             self.base = self.base + '/'
298         # this is the "cookie path" for this tracker (ie. the path part of
299         # the "base" url)
300         self.cookie_path = urllib_.urlparse(self.base)[2]
301         # cookies to set in http responce
302         # {(path, name): (value, expire)}
303         self._cookies = {}
305         # see if we need to re-parse the environment for the form (eg Zope)
306         if form is None:
307             self.form = cgi.FieldStorage(fp=request.rfile, environ=env)
308         else:
309             self.form = form
311         # turn debugging on/off
312         try:
313             self.debug = int(env.get("ROUNDUP_DEBUG", 0))
314         except ValueError:
315             # someone gave us a non-int debug level, turn it off
316             self.debug = 0
318         # flag to indicate that the HTTP headers have been sent
319         self.headers_done = 0
321         # additional headers to send with the request - must be registered
322         # before the first write
323         self.additional_headers = {}
324         self.response_code = 200
326         # default character set
327         self.charset = self.STORAGE_CHARSET
329         # parse cookies (used for charset lookups)
330         # use our own LiberalCookie to handle bad apps on the same
331         # server that have set cookies that are out of spec
332         self.cookie = LiberalCookie(self.env.get('HTTP_COOKIE', ''))
334         self.user = None
335         self.userid = None
336         self.nodeid = None
337         self.classname = None
338         self.template = None
340     def setTranslator(self, translator=None):
341         """Replace the translation engine
343         'translator'
344            is TranslationService instance.
345            It must define methods 'translate' (TAL-compatible i18n),
346            'gettext' and 'ngettext' (gettext-compatible i18n).
348            If omitted, create default TranslationService.
349         """
350         if translator is None:
351             translator = TranslationService.get_translation(
352                 language=self.instance.config["TRACKER_LANGUAGE"],
353                 tracker_home=self.instance.config["TRACKER_HOME"])
354         self.translator = translator
355         self._ = self.gettext = translator.gettext
356         self.ngettext = translator.ngettext
358     def main(self):
359         """ Wrap the real main in a try/finally so we always close off the db.
360         """
361         try:
362             if self.env.get('CONTENT_TYPE') == 'text/xml':
363                 self.handle_xmlrpc()
364             else:
365                 self.inner_main()
366         finally:
367             if hasattr(self, 'db'):
368                 self.db.close()
371     def handle_xmlrpc(self):
373         # Pull the raw XML out of the form.  The "value" attribute
374         # will be the raw content of the POST request.
375         assert self.form.file
376         input = self.form.value
377         # So that the rest of Roundup can query the form in the
378         # usual way, we create an empty list of fields.
379         self.form.list = []
381         # Set the charset and language, since other parts of
382         # Roundup may depend upon that.
383         self.determine_charset()
384         self.determine_language()
385         # Open the database as the correct user.
386         self.determine_user()
387         self.check_anonymous_access()
389         # Call the appropriate XML-RPC method.
390         handler = xmlrpc.RoundupDispatcher(self.db,
391                                            self.instance.actions,
392                                            self.translator,
393                                            allow_none=True)
394         output = handler.dispatch(input)
396         self.setHeader("Content-Type", "text/xml")
397         self.setHeader("Content-Length", str(len(output)))
398         self.write(output)
399         
400     def inner_main(self):
401         """Process a request.
403         The most common requests are handled like so:
405         1. look for charset and language preferences, set up user locale
406            see determine_charset, determine_language
407         2. figure out who we are, defaulting to the "anonymous" user
408            see determine_user
409         3. figure out what the request is for - the context
410            see determine_context
411         4. handle any requested action (item edit, search, ...)
412            see handle_action
413         5. render a template, resulting in HTML output
415         In some situations, exceptions occur:
417         - HTTP Redirect  (generally raised by an action)
418         - SendFile       (generally raised by determine_context)
419           serve up a FileClass "content" property
420         - SendStaticFile (generally raised by determine_context)
421           serve up a file from the tracker "html" directory
422         - Unauthorised   (generally raised by an action)
423           the action is cancelled, the request is rendered and an error
424           message is displayed indicating that permission was not
425           granted for the action to take place
426         - templating.Unauthorised   (templating action not permitted)
427           raised by an attempted rendering of a template when the user
428           doesn't have permission
429         - NotFound       (raised wherever it needs to be)
430           percolates up to the CGI interface that called the client
431         """
432         self.ok_message = []
433         self.error_message = []
434         try:
435             self.determine_charset()
436             self.determine_language()
438             try:
439                 # make sure we're identified (even anonymously)
440                 self.determine_user()
442                 # figure out the context and desired content template
443                 self.determine_context()
445                 # if we've made it this far the context is to a bit of
446                 # Roundup's real web interface (not a file being served up)
447                 # so do the Anonymous Web Acess check now
448                 self.check_anonymous_access()
450                 # possibly handle a form submit action (may change self.classname
451                 # and self.template, and may also append error/ok_messages)
452                 html = self.handle_action()
454                 if html:
455                     self.write_html(html)
456                     return
458                 # now render the page
459                 # we don't want clients caching our dynamic pages
460                 self.additional_headers['Cache-Control'] = 'no-cache'
461                 # Pragma: no-cache makes Mozilla and its ilk
462                 # double-load all pages!!
463                 #            self.additional_headers['Pragma'] = 'no-cache'
465                 # pages with messages added expire right now
466                 # simple views may be cached for a small amount of time
467                 # TODO? make page expire time configurable
468                 # <rj> always expire pages, as IE just doesn't seem to do the
469                 # right thing here :(
470                 date = time.time() - 1
471                 #if self.error_message or self.ok_message:
472                 #    date = time.time() - 1
473                 #else:
474                 #    date = time.time() + 5
475                 self.additional_headers['Expires'] = rfc822.formatdate(date)
477                 # render the content
478                 self.write_html(self.renderContext())
479             except SendFile, designator:
480                 # The call to serve_file may result in an Unauthorised
481                 # exception or a NotModified exception.  Those
482                 # exceptions will be handled by the outermost set of
483                 # exception handlers.
484                 self.serve_file(designator)
485             except SendStaticFile, file:
486                 self.serve_static_file(str(file))
487             except IOError:
488                 # IOErrors here are due to the client disconnecting before
489                 # recieving the reply.
490                 pass
492         except SeriousError, message:
493             self.write_html(str(message))
494         except Redirect, url:
495             # let's redirect - if the url isn't None, then we need to do
496             # the headers, otherwise the headers have been set before the
497             # exception was raised
498             if url:
499                 self.additional_headers['Location'] = str(url)
500                 self.response_code = 302
501             self.write_html('Redirecting to <a href="%s">%s</a>'%(url, url))
502         except LoginError, message:
503             # The user tried to log in, but did not provide a valid
504             # username and password.  If we support HTTP
505             # authorization, send back a response that will cause the
506             # browser to prompt the user again.
507             if self.instance.config.WEB_HTTP_AUTH:
508                 self.response_code = http_.client.UNAUTHORIZED
509                 realm = self.instance.config.TRACKER_NAME
510                 self.setHeader("WWW-Authenticate",
511                                "Basic realm=\"%s\"" % realm)
512             else:
513                 self.response_code = http_.client.FORBIDDEN
514             self.renderFrontPage(message)
515         except Unauthorised, message:
516             # users may always see the front page
517             self.response_code = 403
518             self.renderFrontPage(message)
519         except NotModified:
520             # send the 304 response
521             self.response_code = 304
522             self.header()
523         except NotFound, e:
524             self.response_code = 404
525             self.template = '404'
526             try:
527                 cl = self.db.getclass(self.classname)
528                 self.write_html(self.renderContext())
529             except KeyError:
530                 # we can't map the URL to a class we know about
531                 # reraise the NotFound and let roundup_server
532                 # handle it
533                 raise NotFound(e)
534         except FormError, e:
535             self.error_message.append(self._('Form Error: ') + str(e))
536             self.write_html(self.renderContext())
537         except:
538             # Something has gone badly wrong.  Therefore, we should
539             # make sure that the response code indicates failure.
540             if self.response_code == http_.client.OK:
541                 self.response_code = http_.client.INTERNAL_SERVER_ERROR
542             # Help the administrator work out what went wrong.
543             html = ("<h1>Traceback</h1>"
544                     + cgitb.html(i18n=self.translator)
545                     + ("<h1>Environment Variables</h1><table>%s</table>"
546                        % cgitb.niceDict("", self.env)))
547             if not self.instance.config.WEB_DEBUG:
548                 exc_info = sys.exc_info()
549                 subject = "Error: %s" % exc_info[1]
550                 self.send_html_to_admin(subject, html)
551                 self.write_html(self._(error_message))
552             else:
553                 self.write_html(html)
555     def clean_sessions(self):
556         """Deprecated
557            XXX remove
558         """
559         self.clean_up()
561     def clean_up(self):
562         """Remove expired sessions and One Time Keys.
564            Do it only once an hour.
565         """
566         hour = 60*60
567         now = time.time()
569         # XXX: hack - use OTK table to store last_clean time information
570         #      'last_clean' string is used instead of otk key
571         last_clean = self.db.getOTKManager().get('last_clean', 'last_use', 0)
572         if now - last_clean < hour:
573             return
575         self.session_api.clean_up()
576         self.db.getOTKManager().clean()
577         self.db.getOTKManager().set('last_clean', last_use=now)
578         self.db.commit(fail_ok=True)
580     def determine_charset(self):
581         """Look for client charset in the form parameters or browser cookie.
583         If no charset requested by client, use storage charset (utf-8).
585         If the charset is found, and differs from the storage charset,
586         recode all form fields of type 'text/plain'
587         """
588         # look for client charset
589         charset_parameter = 0
590         if '@charset' in self.form:
591             charset = self.form['@charset'].value
592             if charset.lower() == "none":
593                 charset = ""
594             charset_parameter = 1
595         elif 'roundup_charset' in self.cookie:
596             charset = self.cookie['roundup_charset'].value
597         else:
598             charset = None
599         if charset:
600             # make sure the charset is recognized
601             try:
602                 codecs.lookup(charset)
603             except LookupError:
604                 self.error_message.append(self._('Unrecognized charset: %r')
605                     % charset)
606                 charset_parameter = 0
607             else:
608                 self.charset = charset.lower()
609         # If we've got a character set in request parameters,
610         # set the browser cookie to keep the preference.
611         # This is done after codecs.lookup to make sure
612         # that we aren't keeping a wrong value.
613         if charset_parameter:
614             self.add_cookie('roundup_charset', charset)
616         # if client charset is different from the storage charset,
617         # recode form fields
618         # XXX this requires FieldStorage from Python library.
619         #   mod_python FieldStorage is not supported!
620         if self.charset != self.STORAGE_CHARSET:
621             decoder = codecs.getdecoder(self.charset)
622             encoder = codecs.getencoder(self.STORAGE_CHARSET)
623             re_charref = re.compile('&#([0-9]+|x[0-9a-f]+);', re.IGNORECASE)
624             def _decode_charref(matchobj):
625                 num = matchobj.group(1)
626                 if num[0].lower() == 'x':
627                     uc = int(num[1:], 16)
628                 else:
629                     uc = int(num)
630                 return unichr(uc)
632             for field_name in self.form:
633                 field = self.form[field_name]
634                 if (field.type == 'text/plain') and not field.filename:
635                     try:
636                         value = decoder(field.value)[0]
637                     except UnicodeError:
638                         continue
639                     value = re_charref.sub(_decode_charref, value)
640                     field.value = encoder(value)[0]
642     def determine_language(self):
643         """Determine the language"""
644         # look for language parameter
645         # then for language cookie
646         # last for the Accept-Language header
647         if "@language" in self.form:
648             language = self.form["@language"].value
649             if language.lower() == "none":
650                 language = ""
651             self.add_cookie("roundup_language", language)
652         elif "roundup_language" in self.cookie:
653             language = self.cookie["roundup_language"].value
654         elif self.instance.config["WEB_USE_BROWSER_LANGUAGE"]:
655             hal = self.env.get('HTTP_ACCEPT_LANGUAGE')
656             language = accept_language.parse(hal)
657         else:
658             language = ""
660         self.language = language
661         if language:
662             self.setTranslator(TranslationService.get_translation(
663                     language,
664                     tracker_home=self.instance.config["TRACKER_HOME"]))
666     def determine_user(self):
667         """Determine who the user is"""
668         self.opendb('admin')
670         # get session data from db
671         # XXX: rename
672         self.session_api = Session(self)
674         # take the opportunity to cleanup expired sessions and otks
675         self.clean_up()
677         user = None
678         # first up, try http authorization if enabled
679         if self.instance.config['WEB_HTTP_AUTH']:
680             if 'REMOTE_USER' in self.env:
681                 # we have external auth (e.g. by Apache)
682                 user = self.env['REMOTE_USER']
683             elif self.env.get('HTTP_AUTHORIZATION', ''):
684                 # try handling Basic Auth ourselves
685                 auth = self.env['HTTP_AUTHORIZATION']
686                 scheme, challenge = auth.split(' ', 1)
687                 if scheme.lower() == 'basic':
688                     try:
689                         decoded = base64.decodestring(challenge)
690                     except TypeError:
691                         # invalid challenge
692                         pass
693                     username, password = decoded.split(':')
694                     try:
695                         login = self.get_action_class('login')(self)
696                         login.verifyLogin(username, password)
697                     except LoginError, err:
698                         self.make_user_anonymous()
699                         raise
700                     user = username
702         # if user was not set by http authorization, try session lookup
703         if not user:
704             user = self.session_api.get('user')
705             if user:
706                 # update session lifetime datestamp
707                 self.session_api.update()
709         # if no user name set by http authorization or session lookup
710         # the user is anonymous
711         if not user:
712             user = 'anonymous'
714         # sanity check on the user still being valid,
715         # getting the userid at the same time
716         try:
717             self.userid = self.db.user.lookup(user)
718         except (KeyError, TypeError):
719             user = 'anonymous'
721         # make sure the anonymous user is valid if we're using it
722         if user == 'anonymous':
723             self.make_user_anonymous()
724         else:
725             self.user = user
727         # reopen the database as the correct user
728         self.opendb(self.user)
730     def check_anonymous_access(self):
731         """Check that the Anonymous user is actually allowed to use the web
732         interface and short-circuit all further processing if they're not.
733         """
734         # allow Anonymous to use the "login" and "register" actions (noting
735         # that "register" has its own "Register" permission check)
736         if ':action' in self.form:
737             action = self.form[':action'].value.lower()
738         elif '@action' in self.form:
739             action = self.form['@action'].value.lower()
740         else:
741             action = None
742         if action in ('login', 'register'):
743             return
745         # allow Anonymous to view the "user" "register" template if they're
746         # allowed to register
747         if (self.db.security.hasPermission('Register', self.userid, 'user')
748                 and self.classname == 'user' and self.template == 'register'):
749             return
751         # otherwise for everything else
752         if self.user == 'anonymous':
753             if not self.db.security.hasPermission('Web Access', self.userid):
754                 raise Unauthorised(self._("Anonymous users are not "
755                     "allowed to use the web interface"))
757     def opendb(self, username):
758         """Open the database and set the current user.
760         Opens a database once. On subsequent calls only the user is set on
761         the database object the instance.optimize is set. If we are in
762         "Development Mode" (cf. roundup_server) then the database is always
763         re-opened.
764         """
765         # don't do anything if the db is open and the user has not changed
766         if hasattr(self, 'db') and self.db.isCurrentUser(username):
767             return
769         # open the database or only set the user
770         if not hasattr(self, 'db'):
771             self.db = self.instance.open(username)
772         else:
773             if self.instance.optimize:
774                 self.db.setCurrentUser(username)
775             else:
776                 self.db.close()
777                 self.db = self.instance.open(username)
778                 # The old session API refers to the closed database;
779                 # we can no longer use it.
780                 self.session_api = Session(self)
781  
783     def determine_context(self, dre=re.compile(r'([^\d]+)0*(\d+)')):
784         """Determine the context of this page from the URL:
786         The URL path after the instance identifier is examined. The path
787         is generally only one entry long.
789         - if there is no path, then we are in the "home" context.
790         - if the path is "_file", then the additional path entry
791           specifies the filename of a static file we're to serve up
792           from the instance "html" directory. Raises a SendStaticFile
793           exception.(*)
794         - if there is something in the path (eg "issue"), it identifies
795           the tracker class we're to display.
796         - if the path is an item designator (eg "issue123"), then we're
797           to display a specific item.
798         - if the path starts with an item designator and is longer than
799           one entry, then we're assumed to be handling an item of a
800           FileClass, and the extra path information gives the filename
801           that the client is going to label the download with (ie
802           "file123/image.png" is nicer to download than "file123"). This
803           raises a SendFile exception.(*)
805         Both of the "*" types of contexts stop before we bother to
806         determine the template we're going to use. That's because they
807         don't actually use templates.
809         The template used is specified by the :template CGI variable,
810         which defaults to:
812         - only classname suplied:          "index"
813         - full item designator supplied:   "item"
815         We set:
817              self.classname  - the class to display, can be None
819              self.template   - the template to render the current context with
821              self.nodeid     - the nodeid of the class we're displaying
822         """
823         # default the optional variables
824         self.classname = None
825         self.nodeid = None
827         # see if a template or messages are specified
828         template_override = ok_message = error_message = None
829         for key in self.form:
830             if self.FV_TEMPLATE.match(key):
831                 template_override = self.form[key].value
832             elif self.FV_OK_MESSAGE.match(key):
833                 ok_message = self.form[key].value
834                 ok_message = clean_message(ok_message)
835             elif self.FV_ERROR_MESSAGE.match(key):
836                 error_message = self.form[key].value
837                 error_message = clean_message(error_message)
839         # see if we were passed in a message
840         if ok_message:
841             self.ok_message.append(ok_message)
842         if error_message:
843             self.error_message.append(error_message)
845         # determine the classname and possibly nodeid
846         path = self.path.split('/')
847         if not path or path[0] in ('', 'home', 'index'):
848             if template_override is not None:
849                 self.template = template_override
850             else:
851                 self.template = ''
852             return
853         elif path[0] in ('_file', '@@file'):
854             raise SendStaticFile(os.path.join(*path[1:]))
855         else:
856             self.classname = path[0]
857             if len(path) > 1:
858                 # send the file identified by the designator in path[0]
859                 raise SendFile(path[0])
861         # see if we got a designator
862         m = dre.match(self.classname)
863         if m:
864             self.classname = m.group(1)
865             self.nodeid = m.group(2)
866             try:
867                 klass = self.db.getclass(self.classname)
868             except KeyError:
869                 raise NotFound('%s/%s'%(self.classname, self.nodeid))
870             if not klass.hasnode(self.nodeid):
871                 raise NotFound('%s/%s'%(self.classname, self.nodeid))
872             # with a designator, we default to item view
873             self.template = 'item'
874         else:
875             # with only a class, we default to index view
876             self.template = 'index'
878         # make sure the classname is valid
879         try:
880             self.db.getclass(self.classname)
881         except KeyError:
882             raise NotFound(self.classname)
884         # see if we have a template override
885         if template_override is not None:
886             self.template = template_override
888     def serve_file(self, designator, dre=re.compile(r'([^\d]+)(\d+)')):
889         """ Serve the file from the content property of the designated item.
890         """
891         m = dre.match(str(designator))
892         if not m:
893             raise NotFound(str(designator))
894         classname, nodeid = m.group(1), m.group(2)
896         try:
897             klass = self.db.getclass(classname)
898         except KeyError:
899             # The classname was not valid.
900             raise NotFound(str(designator))
901             
902         # perform the Anonymous user access check
903         self.check_anonymous_access()
905         # make sure we have the appropriate properties
906         props = klass.getprops()
907         if 'type' not in props:
908             raise NotFound(designator)
909         if 'content' not in props:
910             raise NotFound(designator)
912         # make sure we have permission
913         if not self.db.security.hasPermission('View', self.userid,
914                 classname, 'content', nodeid):
915             raise Unauthorised(self._("You are not allowed to view "
916                 "this file."))
918         mime_type = klass.get(nodeid, 'type')
919         # Can happen for msg class:
920         if not mime_type:
921             mime_type = 'text/plain'
923         # if the mime_type is HTML-ish then make sure we're allowed to serve up
924         # HTML-ish content
925         if mime_type in ('text/html', 'text/x-html'):
926             if not self.instance.config['WEB_ALLOW_HTML_FILE']:
927                 # do NOT serve the content up as HTML
928                 mime_type = 'application/octet-stream'
930         # If this object is a file (i.e., an instance of FileClass),
931         # see if we can find it in the filesystem.  If so, we may be
932         # able to use the more-efficient request.sendfile method of
933         # sending the file.  If not, just get the "content" property
934         # in the usual way, and use that.
935         content = None
936         filename = None
937         if isinstance(klass, hyperdb.FileClass):
938             try:
939                 filename = self.db.filename(classname, nodeid)
940             except AttributeError:
941                 # The database doesn't store files in the filesystem
942                 # and therefore doesn't provide the "filename" method.
943                 pass
944             except IOError:
945                 # The file does not exist.
946                 pass
947         if not filename:
948             content = klass.get(nodeid, 'content')
949         
950         lmt = klass.get(nodeid, 'activity').timestamp()
952         self._serve_file(lmt, mime_type, content, filename)
954     def serve_static_file(self, file):
955         """ Serve up the file named from the templates dir
956         """
957         # figure the filename - try STATIC_FILES, then TEMPLATES dir
958         for dir_option in ('STATIC_FILES', 'TEMPLATES'):
959             prefix = self.instance.config[dir_option]
960             if not prefix:
961                 continue
962             # ensure the load doesn't try to poke outside
963             # of the static files directory
964             prefix = os.path.normpath(prefix)
965             filename = os.path.normpath(os.path.join(prefix, file))
966             if os.path.isfile(filename) and filename.startswith(prefix):
967                 break
968         else:
969             raise NotFound(file)
971         # last-modified time
972         lmt = os.stat(filename)[stat.ST_MTIME]
974         # detemine meta-type
975         file = str(file)
976         mime_type = mimetypes.guess_type(file)[0]
977         if not mime_type:
978             if file.endswith('.css'):
979                 mime_type = 'text/css'
980             else:
981                 mime_type = 'text/plain'
983         self._serve_file(lmt, mime_type, '', filename)
985     def _serve_file(self, lmt, mime_type, content=None, filename=None):
986         """ guts of serve_file() and serve_static_file()
987         """
989         # spit out headers
990         self.additional_headers['Content-Type'] = mime_type
991         self.additional_headers['Last-Modified'] = rfc822.formatdate(lmt)
993         ims = None
994         # see if there's an if-modified-since...
995         # XXX see which interfaces set this
996         #if hasattr(self.request, 'headers'):
997             #ims = self.request.headers.getheader('if-modified-since')
998         if 'HTTP_IF_MODIFIED_SINCE' in self.env:
999             # cgi will put the header in the env var
1000             ims = self.env['HTTP_IF_MODIFIED_SINCE']
1001         if ims:
1002             ims = rfc822.parsedate(ims)[:6]
1003             lmtt = time.gmtime(lmt)[:6]
1004             if lmtt <= ims:
1005                 raise NotModified
1007         if filename:
1008             self.write_file(filename)
1009         else:
1010             self.additional_headers['Content-Length'] = str(len(content))
1011             self.write(content)
1013     def send_html_to_admin(self, subject, content):
1015         to = [self.mailer.config.ADMIN_EMAIL]
1016         message = self.mailer.get_standard_message(to, subject)
1017         # delete existing content-type headers
1018         del message['Content-type']
1019         message['Content-type'] = 'text/html; charset=utf-8'
1020         message.set_payload(content)
1021         encode_quopri(message)
1022         self.mailer.smtp_send(to, str(message))
1023     
1024     def renderFrontPage(self, message):
1025         """Return the front page of the tracker."""
1026     
1027         self.classname = self.nodeid = None
1028         self.template = ''
1029         self.error_message.append(message)
1030         self.write_html(self.renderContext())
1032     def renderContext(self):
1033         """ Return a PageTemplate for the named page
1034         """
1035         name = self.classname
1036         extension = self.template
1038         # catch errors so we can handle PT rendering errors more nicely
1039         args = {
1040             'ok_message': self.ok_message,
1041             'error_message': self.error_message
1042         }
1043         try:
1044             pt = self.instance.templates.get(name, extension)
1045             # let the template render figure stuff out
1046             result = pt.render(self, None, None, **args)
1047             self.additional_headers['Content-Type'] = pt.content_type
1048             if self.env.get('CGI_SHOW_TIMING', ''):
1049                 if self.env['CGI_SHOW_TIMING'].upper() == 'COMMENT':
1050                     timings = {'starttag': '<!-- ', 'endtag': ' -->'}
1051                 else:
1052                     timings = {'starttag': '<p>', 'endtag': '</p>'}
1053                 timings['seconds'] = time.time()-self.start
1054                 s = self._('%(starttag)sTime elapsed: %(seconds)fs%(endtag)s\n'
1055                     ) % timings
1056                 if hasattr(self.db, 'stats'):
1057                     timings.update(self.db.stats)
1058                     s += self._("%(starttag)sCache hits: %(cache_hits)d,"
1059                         " misses %(cache_misses)d."
1060                         " Loading items: %(get_items)f secs."
1061                         " Filtering: %(filtering)f secs."
1062                         "%(endtag)s\n") % timings
1063                 s += '</body>'
1064                 result = result.replace('</body>', s)
1065             return result
1066         except templating.NoTemplate, message:
1067             return '<strong>%s</strong>'%message
1068         except templating.Unauthorised, message:
1069             raise Unauthorised(str(message))
1070         except:
1071             # everything else
1072             if self.instance.config.WEB_DEBUG:
1073                 return cgitb.pt_html(i18n=self.translator)
1074             exc_info = sys.exc_info()
1075             try:
1076                 # If possible, send the HTML page template traceback
1077                 # to the administrator.
1078                 subject = "Templating Error: %s" % exc_info[1]
1079                 self.send_html_to_admin(subject, cgitb.pt_html())
1080                 # Now report the error to the user.
1081                 return self._(error_message)
1082             except:
1083                 # Reraise the original exception.  The user will
1084                 # receive an error message, and the adminstrator will
1085                 # receive a traceback, albeit with less information
1086                 # than the one we tried to generate above.
1087                 raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
1089     # these are the actions that are available
1090     actions = (
1091         ('edit',        EditItemAction),
1092         ('editcsv',     EditCSVAction),
1093         ('new',         NewItemAction),
1094         ('register',    RegisterAction),
1095         ('confrego',    ConfRegoAction),
1096         ('passrst',     PassResetAction),
1097         ('login',       LoginAction),
1098         ('logout',      LogoutAction),
1099         ('search',      SearchAction),
1100         ('retire',      RetireAction),
1101         ('show',        ShowAction),
1102         ('export_csv',  ExportCSVAction),
1103     )
1104     def handle_action(self):
1105         """ Determine whether there should be an Action called.
1107             The action is defined by the form variable :action which
1108             identifies the method on this object to call. The actions
1109             are defined in the "actions" sequence on this class.
1111             Actions may return a page (by default HTML) to return to the
1112             user, bypassing the usual template rendering.
1114             We explicitly catch Reject and ValueError exceptions and
1115             present their messages to the user.
1116         """
1117         if ':action' in self.form:
1118             action = self.form[':action'].value.lower()
1119         elif '@action' in self.form:
1120             action = self.form['@action'].value.lower()
1121         else:
1122             return None
1124         try:
1125             action_klass = self.get_action_class(action)
1127             # call the mapped action
1128             if isinstance(action_klass, type('')):
1129                 # old way of specifying actions
1130                 return getattr(self, action_klass)()
1131             else:
1132                 return action_klass(self).execute()
1134         except (ValueError, Reject), err:
1135             self.error_message.append(str(err))
1137     def get_action_class(self, action_name):
1138         if (hasattr(self.instance, 'cgi_actions') and
1139                 action_name in self.instance.cgi_actions):
1140             # tracker-defined action
1141             action_klass = self.instance.cgi_actions[action_name]
1142         else:
1143             # go with a default
1144             for name, action_klass in self.actions:
1145                 if name == action_name:
1146                     break
1147             else:
1148                 raise ValueError('No such action "%s"'%action_name)
1149         return action_klass
1151     def _socket_op(self, call, *args, **kwargs):
1152         """Execute socket-related operation, catch common network errors
1154         Parameters:
1155             call: a callable to execute
1156             args, kwargs: call arguments
1158         """
1159         try:
1160             call(*args, **kwargs)
1161         except socket.error, err:
1162             err_errno = getattr (err, 'errno', None)
1163             if err_errno is None:
1164                 try:
1165                     err_errno = err[0]
1166                 except TypeError:
1167                     pass
1168             if err_errno not in self.IGNORE_NET_ERRORS:
1169                 raise
1170         except IOError:
1171             # Apache's mod_python will raise IOError -- without an
1172             # accompanying errno -- when a write to the client fails.
1173             # A common case is that the client has closed the
1174             # connection.  There's no way to be certain that this is
1175             # the situation that has occurred here, but that is the
1176             # most likely case.
1177             pass
1179     def write(self, content):
1180         if not self.headers_done:
1181             self.header()
1182         if self.env['REQUEST_METHOD'] != 'HEAD':
1183             self._socket_op(self.request.wfile.write, content)
1185     def write_html(self, content):
1186         if not self.headers_done:
1187             # at this point, we are sure about Content-Type
1188             if 'Content-Type' not in self.additional_headers:
1189                 self.additional_headers['Content-Type'] = \
1190                     'text/html; charset=%s' % self.charset
1191             self.header()
1193         if self.env['REQUEST_METHOD'] == 'HEAD':
1194             # client doesn't care about content
1195             return
1197         if self.charset != self.STORAGE_CHARSET:
1198             # recode output
1199             content = content.decode(self.STORAGE_CHARSET, 'replace')
1200             content = content.encode(self.charset, 'xmlcharrefreplace')
1202         # and write
1203         self._socket_op(self.request.wfile.write, content)
1205     def http_strip(self, content):
1206         """Remove HTTP Linear White Space from 'content'.
1208         'content' -- A string.
1210         returns -- 'content', with all leading and trailing LWS
1211         removed."""
1213         # RFC 2616 2.2: Basic Rules
1214         #
1215         # LWS = [CRLF] 1*( SP | HT )
1216         return content.strip(" \r\n\t")
1218     def http_split(self, content):
1219         """Split an HTTP list.
1221         'content' -- A string, giving a list of items.
1223         returns -- A sequence of strings, containing the elements of
1224         the list."""
1226         # RFC 2616 2.1: Augmented BNF
1227         #
1228         # Grammar productions of the form "#rule" indicate a
1229         # comma-separated list of elements matching "rule".  LWS
1230         # is then removed from each element, and empty elements
1231         # removed.
1233         # Split at commas.
1234         elements = content.split(",")
1235         # Remove linear whitespace at either end of the string.
1236         elements = [self.http_strip(e) for e in elements]
1237         # Remove any now-empty elements.
1238         return [e for e in elements if e]
1239         
1240     def handle_range_header(self, length, etag):
1241         """Handle the 'Range' and 'If-Range' headers.
1243         'length' -- the length of the content available for the
1244         resource.
1246         'etag' -- the entity tag for this resources.
1248         returns -- If the request headers (including 'Range' and
1249         'If-Range') indicate that only a portion of the entity should
1250         be returned, then the return value is a pair '(offfset,
1251         length)' indicating the first byte and number of bytes of the
1252         content that should be returned to the client.  In addition,
1253         this method will set 'self.response_code' to indicate Partial
1254         Content.  In all other cases, the return value is 'None'.  If
1255         appropriate, 'self.response_code' will be
1256         set to indicate 'REQUESTED_RANGE_NOT_SATISFIABLE'.  In that
1257         case, the caller should not send any data to the client."""
1259         # RFC 2616 14.35: Range
1260         #
1261         # See if the Range header is present.
1262         ranges_specifier = self.env.get("HTTP_RANGE")
1263         if ranges_specifier is None:
1264             return None
1265         # RFC 2616 14.27: If-Range
1266         #
1267         # Check to see if there is an If-Range header.
1268         # Because the specification says:
1269         #
1270         #  The If-Range header ... MUST be ignored if the request
1271         #  does not include a Range header, we check for If-Range
1272         #  after checking for Range.
1273         if_range = self.env.get("HTTP_IF_RANGE")
1274         if if_range:
1275             # The grammar for the If-Range header is:
1276             # 
1277             #   If-Range = "If-Range" ":" ( entity-tag | HTTP-date )
1278             #   entity-tag = [ weak ] opaque-tag
1279             #   weak = "W/"
1280             #   opaque-tag = quoted-string
1281             #
1282             # We only support strong entity tags.
1283             if_range = self.http_strip(if_range)
1284             if (not if_range.startswith('"')
1285                 or not if_range.endswith('"')):
1286                 return None
1287             # If the condition doesn't match the entity tag, then we
1288             # must send the client the entire file.
1289             if if_range != etag:
1290                 return
1291         # The grammar for the Range header value is:
1292         #
1293         #   ranges-specifier = byte-ranges-specifier
1294         #   byte-ranges-specifier = bytes-unit "=" byte-range-set
1295         #   byte-range-set = 1#( byte-range-spec | suffix-byte-range-spec )
1296         #   byte-range-spec = first-byte-pos "-" [last-byte-pos]
1297         #   first-byte-pos = 1*DIGIT
1298         #   last-byte-pos = 1*DIGIT
1299         #   suffix-byte-range-spec = "-" suffix-length
1300         #   suffix-length = 1*DIGIT
1301         #
1302         # Look for the "=" separating the units from the range set.
1303         specs = ranges_specifier.split("=", 1)
1304         if len(specs) != 2:
1305             return None
1306         # Check that the bytes-unit is in fact "bytes".  If it is not,
1307         # we do not know how to process this range.
1308         bytes_unit = self.http_strip(specs[0])
1309         if bytes_unit != "bytes":
1310             return None
1311         # Seperate the range-set into range-specs.
1312         byte_range_set = self.http_strip(specs[1])
1313         byte_range_specs = self.http_split(byte_range_set)
1314         # We only handle exactly one range at this time.
1315         if len(byte_range_specs) != 1:
1316             return None
1317         # Parse the spec.
1318         byte_range_spec = byte_range_specs[0]
1319         pos = byte_range_spec.split("-", 1)
1320         if len(pos) != 2:
1321             return None
1322         # Get the first and last bytes.
1323         first = self.http_strip(pos[0])
1324         last = self.http_strip(pos[1])
1325         # We do not handle suffix ranges.
1326         if not first:
1327             return None
1328        # Convert the first and last positions to integers.
1329         try:
1330             first = int(first)
1331             if last:
1332                 last = int(last)
1333             else:
1334                 last = length - 1
1335         except:
1336             # The positions could not be parsed as integers.
1337             return None
1338         # Check that the range makes sense.
1339         if (first < 0 or last < 0 or last < first):
1340             return None
1341         if last >= length:
1342             # RFC 2616 10.4.17: 416 Requested Range Not Satisfiable
1343             #
1344             # If there is an If-Range header, RFC 2616 says that we
1345             # should just ignore the invalid Range header.
1346             if if_range:
1347                 return None
1348             # Return code 416 with a Content-Range header giving the
1349             # allowable range.
1350             self.response_code = http_.client.REQUESTED_RANGE_NOT_SATISFIABLE
1351             self.setHeader("Content-Range", "bytes */%d" % length)
1352             return None
1353         # RFC 2616 10.2.7: 206 Partial Content
1354         #
1355         # Tell the client that we are honoring the Range request by
1356         # indicating that we are providing partial content.
1357         self.response_code = http_.client.PARTIAL_CONTENT
1358         # RFC 2616 14.16: Content-Range
1359         #
1360         # Tell the client what data we are providing.
1361         #
1362         #   content-range-spec = byte-content-range-spec
1363         #   byte-content-range-spec = bytes-unit SP
1364         #                             byte-range-resp-spec "/"
1365         #                             ( instance-length | "*" )
1366         #   byte-range-resp-spec = (first-byte-pos "-" last-byte-pos)
1367         #                          | "*"
1368         #   instance-length      = 1 * DIGIT
1369         self.setHeader("Content-Range",
1370                        "bytes %d-%d/%d" % (first, last, length))
1371         return (first, last - first + 1)
1373     def write_file(self, filename):
1374         """Send the contents of 'filename' to the user."""
1376         # Determine the length of the file.
1377         stat_info = os.stat(filename)
1378         length = stat_info[stat.ST_SIZE]
1379         # Assume we will return the entire file.
1380         offset = 0
1381         # If the headers have not already been finalized, 
1382         if not self.headers_done:
1383             # RFC 2616 14.19: ETag
1384             #
1385             # Compute the entity tag, in a format similar to that
1386             # used by Apache.
1387             etag = '"%x-%x-%x"' % (stat_info[stat.ST_INO],
1388                                    length,
1389                                    stat_info[stat.ST_MTIME])
1390             self.setHeader("ETag", etag)
1391             # RFC 2616 14.5: Accept-Ranges
1392             #
1393             # Let the client know that we will accept range requests.
1394             self.setHeader("Accept-Ranges", "bytes")
1395             # RFC 2616 14.35: Range
1396             #
1397             # If there is a Range header, we may be able to avoid
1398             # sending the entire file.
1399             content_range = self.handle_range_header(length, etag)
1400             if content_range:
1401                 offset, length = content_range
1402             # RFC 2616 14.13: Content-Length
1403             #
1404             # Tell the client how much data we are providing.
1405             self.setHeader("Content-Length", str(length))
1406             # Send the HTTP header.
1407             self.header()
1408         # If the client doesn't actually want the body, or if we are
1409         # indicating an invalid range.
1410         if (self.env['REQUEST_METHOD'] == 'HEAD'
1411             or self.response_code == http_.client.REQUESTED_RANGE_NOT_SATISFIABLE):
1412             return
1413         # Use the optimized "sendfile" operation, if possible.
1414         if hasattr(self.request, "sendfile"):
1415             self._socket_op(self.request.sendfile, filename, offset, length)
1416             return
1417         # Fallback to the "write" operation.
1418         f = open(filename, 'rb')
1419         try:
1420             if offset:
1421                 f.seek(offset)
1422             content = f.read(length)
1423         finally:
1424             f.close()
1425         self.write(content)
1427     def setHeader(self, header, value):
1428         """Override a header to be returned to the user's browser.
1429         """
1430         self.additional_headers[header] = value
1432     def header(self, headers=None, response=None):
1433         """Put up the appropriate header.
1434         """
1435         if headers is None:
1436             headers = {'Content-Type':'text/html; charset=utf-8'}
1437         if response is None:
1438             response = self.response_code
1440         # update with additional info
1441         headers.update(self.additional_headers)
1443         if headers.get('Content-Type', 'text/html') == 'text/html':
1444             headers['Content-Type'] = 'text/html; charset=utf-8'
1446         headers = list(headers.items())
1448         for ((path, name), (value, expire)) in self._cookies.iteritems():
1449             cookie = "%s=%s; Path=%s;"%(name, value, path)
1450             if expire is not None:
1451                 cookie += " expires=%s;"%get_cookie_date(expire)
1452             headers.append(('Set-Cookie', cookie))
1454         self._socket_op(self.request.start_response, headers, response)
1456         self.headers_done = 1
1457         if self.debug:
1458             self.headers_sent = headers
1460     def add_cookie(self, name, value, expire=86400*365, path=None):
1461         """Set a cookie value to be sent in HTTP headers
1463         Parameters:
1464             name:
1465                 cookie name
1466             value:
1467                 cookie value
1468             expire:
1469                 cookie expiration time (seconds).
1470                 If value is empty (meaning "delete cookie"),
1471                 expiration time is forced in the past
1472                 and this argument is ignored.
1473                 If None, the cookie will expire at end-of-session.
1474                 If omitted, the cookie will be kept for a year.
1475             path:
1476                 cookie path (optional)
1478         """
1479         if path is None:
1480             path = self.cookie_path
1481         if not value:
1482             expire = -1
1483         self._cookies[(path, name)] = (value, expire)
1485     def set_cookie(self, user, expire=None):
1486         """Deprecated. Use session_api calls directly
1488         XXX remove
1489         """
1491         # insert the session in the session db
1492         self.session_api.set(user=user)
1493         # refresh session cookie
1494         self.session_api.update(set_cookie=True, expire=expire)
1496     def make_user_anonymous(self):
1497         """ Make us anonymous
1499             This method used to handle non-existence of the 'anonymous'
1500             user, but that user is mandatory now.
1501         """
1502         self.userid = self.db.user.lookup('anonymous')
1503         self.user = 'anonymous'
1505     def standard_message(self, to, subject, body, author=None):
1506         """Send a standard email message from Roundup.
1508         "to"      - recipients list
1509         "subject" - Subject
1510         "body"    - Message
1511         "author"  - (name, address) tuple or None for admin email
1513         Arguments are passed to the Mailer.standard_message code.
1514         """
1515         try:
1516             self.mailer.standard_message(to, subject, body, author)
1517         except MessageSendError, e:
1518             self.error_message.append(str(e))
1519             return 0
1520         return 1
1522     def parsePropsFromForm(self, create=0):
1523         return FormParser(self).parse(create=create)
1525 # vim: set et sts=4 sw=4 :