Code

5dbf3d50719f4669d7ac6dc3df5a74dc6b3627f5
[roundup.git] / cgi-bin / roundup.cgi
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
4 # This module is free software, and you may redistribute it and/or modify
5 # under the same terms as Python, so long as this copyright message and
6 # disclaimer are retained in their original form.
7 #
8 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
9 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
10 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
11 # POSSIBILITY OF SUCH DAMAGE.
12 #
13 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
18
19 # $Id: roundup.cgi,v 1.11 2001-09-29 13:27:00 richard Exp $
21 # python version check
22 import sys
23 if int(sys.version[0]) < 2:
24     print "Content-Type: text/plain\n"
25     print "Roundup requires Python 2.0 or newer."
26     sys.exit(0)
28 #
29 ##  Configuration
30 #
32 # This indicates where the Roundup instance lives
33 ROUNDUP_INSTANCE_HOMES = {
34     'test': '/tmp/roundup_test',
35 }
37 # Where to log debugging information to. Use an instance of DevNull if you
38 # don't want to log anywhere.
39 class DevNull:
40     def write(self, info):
41         pass
42 LOG = open('/var/log/roundup.cgi.log', 'a')
43 #LOG = DevNull()
45 #
46 ##  end configuration
47 #
50 #
51 # Set up the error handler
52
53 try:
54     import traceback, StringIO, cgi
55     from roundup import cgitb
56 except:
57     print "Content-Type: text/html\n"
58     print "Failed to import cgitb.<pre>"
59     s = StringIO.StringIO()
60     traceback.print_exc(None, s)
61     print cgi.escape(s.getvalue()), "</pre>"
63 def main(instance, out):
64     from roundup import cgi_client
65     db = instance.open('admin')
66     auth = os.environ.get("HTTP_CGI_AUTHORIZATION", None)
67     message = 'Unauthorised'
68     if auth:
69         import binascii
70         l = binascii.a2b_base64(auth.split(' ')[1]).split(':')
71         user = l[0]
72         password = None
73         if len(l) > 1:
74             password = l[1]
75         try:
76             uid = db.user.lookup(user)
77         except KeyError:
78             auth = None
79             message = 'Username not recognised'
80         else:
81             if password != db.user.get(uid, 'password'):
82                 message = 'Incorrect password'
83                 auth = None
84     if not auth:
85         out.write('Content-Type: text/html\n')
86         out.write('Status: 401\n')
87         out.write('WWW-Authenticate: basic realm="Roundup"\n\n')
88         keys = os.environ.keys()
89         keys.sort()
90         out.write(message)
91         return
92     client = instance.Client(out, db, os.environ, user)
93     try:
94         client.main()
95     except cgi_client.Unauthorised:
96         out.write('Content-Type: text/html\n')
97         out.write('Status: 403\n\n')
98         out.write('Unauthorised')
100 def index(out):
101     ''' Print up an index of the available instances
102     '''
103     w = out.write
104     w("Content-Type: text/html\n\n")
105     w('<html><head><title>Roundup instances index</title><head>\n')
106     w('<body><h1>Roundup instances index</h1><ol>\n')
107     for instance in ROUNDUP_INSTANCE_HOMES.keys():
108         w('<li><a href="%s/index">%s</a>\n'%(urllib.quote(instance),
109             instance))
110     w('</ol></body></html>')
113 # Now do the actual CGI handling
114
115 out, err = sys.stdout, sys.stderr
116 try:
117     sys.stdout = sys.stderr = LOG
118     import os, string
119     import roundup.instance
120     path = string.split(os.environ['PATH_INFO'], '/')
121     instance = path[1]
122     os.environ['PATH_INFO'] = string.join(path[2:], '/')
123     if ROUNDUP_INSTANCE_HOMES.has_key(instance):
124         instance_home = ROUNDUP_INSTANCE_HOMES[instance]
125         instance = roundup.instance.open(instance_home)
126         main(instance, out)
127     else:
128         index()
129 except:
130     sys.stdout, sys.stderr = out, err
131     out.write('Content-Type: text/html\n\n')
132     cgitb.handler()
133 sys.stdout.flush()
134 sys.stdout, sys.stderr = out, err
137 # $Log: not supported by cvs2svn $
138 # Revision 1.10  2001/08/07 00:24:42  richard
139 # stupid typo
141 # Revision 1.9  2001/08/07 00:15:51  richard
142 # Added the copyright/license notice to (nearly) all files at request of
143 # Bizar Software.
145 # Revision 1.8  2001/08/05 07:43:52  richard
146 # Instances are now opened by a special function that generates a unique
147 # module name for the instances on import time.
149 # Revision 1.7  2001/08/03 01:28:33  richard
150 # Used the much nicer load_package, pointed out by Steve Majewski.
152 # Revision 1.6  2001/08/03 00:59:34  richard
153 # Instance import now imports the instance using imp.load_module so that
154 # we can have instance homes of "roundup" or other existing python package
155 # names.
157 # Revision 1.5  2001/07/29 07:01:39  richard
158 # Added vim command to all source so that we don't get no steenkin' tabs :)
160 # Revision 1.4  2001/07/23 04:47:27  anthonybaxter
161 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
162 # sys.exit(0) if python version wrong.
164 # Revision 1.3  2001/07/23 04:33:30  richard
165 # brought the CGI instance config dict in line with roundup-server
167 # Revision 1.2  2001/07/23 04:31:40  richard
168 # Fixed the roundup CGI script for updates to cgi_client.py
170 # Revision 1.1  2001/07/22 11:47:07  richard
171 # More Grande Splite
174 # vim: set filetype=python ts=4 sw=4 et si