Code

4722f7b7e24f87c9626269e7afc27b353d632726
[roundup.git] / cgi-bin / roundup.cgi
1 #!/usr/bin/env python
2 # $Id: roundup.cgi,v 1.8 2001-08-05 07:43:52 richard Exp $
4 # python version check
5 import sys
6 if int(sys.version[0]) < 2:
7     print "Content-Type: text/plain\n"
8     print "Roundup requires Python 2.0 or newer."
9     sys.exit(0)
11 #
12 ##  Configuration
13 #
15 # This indicates where the Roundup instance lives
16 ROUNDUP_INSTANCE_HOMES = {
17     'test': '/tmp/roundup_test',
18 }
20 # Where to log debugging information to. Use an instance of DevNull if you
21 # don't want to log anywhere.
22 class DevNull:
23     def write(self, info):
24         pass
25 LOG = open('/var/log/roundup.cgi.log', 'a')
26 #LOG = DevNull()
28 #
29 ##  end configuration
30 #
33 #
34 # Set up the error handler
35
36 try:
37     import traceback, StringIO, cgi
38     from roundup import cgitb
39 except:
40     print "Content-Type: text/html\n"
41     print "Failed to import cgitb.<pre>"
42     s = StringIO.StringIO()
43     traceback.print_exc(None, s)
44     print cgi.escape(s.getvalue()), "</pre>"
46 def main(instance, out):
47     from roundup import cgi_client
48     db = instance.open('admin')
49     auth = os.environ.get("HTTP_CGI_AUTHORIZATION", None)
50     message = 'Unauthorised'
51     if auth:
52         import binascii
53         l = binascii.a2b_base64(auth.split(' ')[1]).split(':')
54         user = l[0]
55         password = None
56         if len(l) > 1:
57             password = l[1]
58         try:
59             uid = db.user.lookup(user)
60         except KeyError:
61             auth = None
62             message = 'Username not recognised'
63         else:
64             if password != db.user.get(uid, 'password'):
65                 message = 'Incorrect password'
66                 auth = None
67     if not auth:
68         out.write('Content-Type: text/html\n')
69         out.write('Status: 401\n')
70         out.write('WWW-Authenticate: basic realm="Roundup"\n\n')
71         keys = os.environ.keys()
72         keys.sort()
73         out.write(message)
74         return
75     client = instance.Client(out, db, os.environ, user)
76     try:
77         client.main()
78     except cgi_client.Unauthorised:
79         out.write('Content-Type: text/html\n')
80         out.write('Status: 403\n\n')
81         out.write('Unauthorised')
83 #
84 # Now do the actual CGI handling
85
86 out, err = sys.stdout, sys.stderr
87 try:
88     sys.stdout = sys.stderr = LOG
89     import os, string
90     import roundup.instance
91     path = string.split(os.environ['PATH_INFO'], '/')
92     instance = path[1]
93     os.environ['PATH_INFO'] = string.join(path[2:], '/')
94     if ROUNDUP_INSTANCE_HOMES.has_key(instance):
95         instance_home = ROUNDUP_INSTANCE_HOMES[instance]
96         instance = roundup.instance.open(instance_home)
97     else:
98         raise ValueError, 'No such instance "%s"'%instance
99     main(instance, out)
100 except:
101     sys.stdout, sys.stderr = out, err
102     out.write('Content-Type: text/html\n\n')
103     cgitb.handler()
104 sys.stdout.flush()
105 sys.stdout, sys.stderr = out, err
108 # $Log: not supported by cvs2svn $
109 # Revision 1.7  2001/08/03 01:28:33  richard
110 # Used the much nicer load_package, pointed out by Steve Majewski.
112 # Revision 1.6  2001/08/03 00:59:34  richard
113 # Instance import now imports the instance using imp.load_module so that
114 # we can have instance homes of "roundup" or other existing python package
115 # names.
117 # Revision 1.5  2001/07/29 07:01:39  richard
118 # Added vim command to all source so that we don't get no steenkin' tabs :)
120 # Revision 1.4  2001/07/23 04:47:27  anthonybaxter
121 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
122 # sys.exit(0) if python version wrong.
124 # Revision 1.3  2001/07/23 04:33:30  richard
125 # brought the CGI instance config dict in line with roundup-server
127 # Revision 1.2  2001/07/23 04:31:40  richard
128 # Fixed the roundup CGI script for updates to cgi_client.py
130 # Revision 1.1  2001/07/22 11:47:07  richard
131 # More Grande Splite
134 # vim: set filetype=python ts=4 sw=4 et si