Code

511e08b7fb46f6d4b02ec0f3a77f7409e0da7817
[roundup.git] / cgi-bin / roundup.cgi
1 #!/usr/bin/env python
3 # $Id: roundup.cgi,v 1.2 2001-07-23 04:31:40 richard Exp $
5 # python version check
6 import sys
7 if int(sys.version[0]) < 2:
8     print "Content-Type: text/plain\n"
9     print "Roundup requires Python 2.0 or newer."
11 #
12 ##  Configuration
13 #
15 # This indicates where the Roundup instance lives
16 ROUNDUPS = {
17     'roundup_test': '/tmp/',
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     path = string.split(os.environ['PATH_INFO'], '/')
91     instance = path[1]
92     os.environ['PATH_INFO'] = string.join(path[2:], '/')
93     if ROUNDUPS.has_key(instance):
94         instance_home = ROUNDUPS[instance]
95         sys.path.insert(0, instance_home)
96         try:
97             instance = __import__(instance)
98         finally:
99             del sys.path[0]
100     else:
101         raise ValueError, 'No such instance "%s"'%instance
102     main(instance, out)
103 except:
104     sys.stdout, sys.stderr = out, err
105     out.write('Content-Type: text/html\n\n')
106     cgitb.handler()
107 sys.stdout.flush()
108 sys.stdout, sys.stderr = out, err
111 # $Log: not supported by cvs2svn $
112 # Revision 1.1  2001/07/22 11:47:07  richard
113 # More Grande Splite