Code

i18n'ification
[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.23 2002-01-05 02:19:03 richard Exp $
21 # python version check
22 from roundup import version_check
23 from roundup.i18n import _
25 #
26 ##  Configuration
27 #
29 # Configuration can also be provided through the OS environment (or via
30 # the Apache "SetEnv" configuration directive). If the variables
31 # documented below are set, they _override_ any configuation defaults
32 # given in this file. 
34 # ROUNDUP_INSTANCE_HOMES is a list of instances, in the form
35 # "NAME=DIR<sep>NAME2=DIR2<sep>...", where <sep> is the directory path
36 # separator (";" on Windows, ":" on Unix). 
38 # ROUNDUP_LOG is the name of the logfile; if it's empty or does not exist,
39 # logging is turned off (unless you changed the default below). 
41 # ROUNDUP_DEBUG is a debug level, currently only 0 (OFF) and 1 (ON) are
42 # used in the code. Higher numbers means more debugging output. 
44 # This indicates where the Roundup instance lives
45 ROUNDUP_INSTANCE_HOMES = {
46     'demo': '/var/roundup/instances/demo',
47 }
49 # Where to log debugging information to. Use an instance of DevNull if you
50 # don't want to log anywhere.
51 class DevNull:
52     def write(self, info):
53         pass
54     def close():
55         pass
56 #LOG = open('/var/log/roundup.cgi.log', 'a')
57 LOG = DevNull()
59 #
60 ##  end configuration
61 #
64 #
65 # Set up the error handler
66
67 try:
68     import traceback, StringIO, cgi
69     from roundup import cgitb
70 except:
71     print "Content-Type: text/html\n"
72     print _("Failed to import cgitb.<pre>")
73     s = StringIO.StringIO()
74     traceback.print_exc(None, s)
75     print cgi.escape(s.getvalue()), "</pre>"
78 #
79 # Check environment for config items
80 #
81 def checkconfig():
82     import os, string
83     global ROUNDUP_INSTANCE_HOMES, LOG
85     homes = os.environ.get('ROUNDUP_INSTANCE_HOMES', '')
86     if homes:
87         ROUNDUP_INSTANCE_HOMES = {}
88         for home in string.split(homes, os.pathsep):
89             try:
90                 name, dir = string.split(home, '=', 1)
91             except ValueError:
92                 # ignore invalid definitions
93                 continue
94             if name and dir:
95                 ROUNDUP_INSTANCE_HOMES[name] = dir
96                 
97     logname = os.environ.get('ROUNDUP_LOG', '')
98     if logname:
99         LOG = open(logname, 'a')
101     # ROUNDUP_DEBUG is checked directly in "roundup.cgi_client"
105 # Provide interface to CGI HTTP response handling
107 class RequestWrapper:
108     '''Used to make the CGI server look like a BaseHTTPRequestHandler
109     '''
110     def __init__(self, wfile):
111         self.wfile = wfile
112     def write(self, data):
113         self.wfile.write(data)
114     def send_response(self, code):
115         self.write('Status: %s\r\n'%code)
116     def send_header(self, keyword, value):
117         self.write("%s: %s\r\n" % (keyword, value))
118     def end_headers(self):
119         self.write("\r\n")
122 # Main CGI handler
124 def main(out, err):
125     import os, string
126     import roundup.instance
127     path = string.split(os.environ.get('PATH_INFO', '/'), '/')
128     instance = path[1]
129     os.environ['INSTANCE_NAME'] = instance
130     os.environ['PATH_INFO'] = string.join(path[2:], '/')
131     request = RequestWrapper(out)
132     if ROUNDUP_INSTANCE_HOMES.has_key(instance):
133         # redirect if we need a trailing '/'
134         if len(path) == 2:
135             request.send_response(301)
136             absolute_url = 'http://%s%s/'%(os.environ['HTTP_HOST'],
137                 os.environ['REQUEST_URI'])
138             request.send_header('Location', absolute_url)
139             request.end_headers()
140             out.write('Moved Permanently')
141         else:
142             instance_home = ROUNDUP_INSTANCE_HOMES[instance]
143             instance = roundup.instance.open(instance_home)
144             from roundup import cgi_client
145             client = instance.Client(instance, request, os.environ)
146             try:
147                 client.main()
148             except cgi_client.Unauthorised:
149                 request.send_response(403)
150                 request.send_header('Content-Type', 'text/html')
151                 request.end_headers()
152                 out.write('Unauthorised')
153             except cgi_client.NotFound:
154                 request.send_response(404)
155                 request.send_header('Content-Type', 'text/html')
156                 request.end_headers()
157                 out.write('Not found: %s'%client.path)
159     else:
160         import urllib
161         request.send_response(200)
162         request.send_header('Content-Type', 'text/html')
163         request.end_headers()
164         w = request.write
165         w(_('<html><head><title>Roundup instances index</title></head>\n'))
166         w(_('<body><h1>Roundup instances index</h1><ol>\n'))
167         homes = ROUNDUP_INSTANCE_HOMES.keys()
168         homes.sort()
169         for instance in homes:
170             w(_('<li><a href="%(instance_url)s/index">%(instance_name)s</a>\n')%{
171                 'instance_url': os.environ['SCRIPT_NAME']+'/'+urllib.quote(instance),
172                 'instance_name': cgi.escape(instance)})
173         w(_('</ol></body></html>'))
176 # Now do the actual CGI handling
178 out, err = sys.stdout, sys.stderr
179 try:
180     # force input/output to binary (important for file up/downloads)
181     if sys.platform == "win32":
182         import os, msvcrt
183         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
184         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
185     checkconfig()
186     sys.stdout = sys.stderr = LOG
187     main(out, err)
188 except SystemExit:
189     pass
190 except:
191     sys.stdout, sys.stderr = out, err
192     out.write('Content-Type: text/html\n\n')
193     cgitb.handler()
194 sys.stdout.flush()
195 sys.stdout, sys.stderr = out, err
196 LOG.close()
199 # $Log: not supported by cvs2svn $
200 # Revision 1.22  2001/12/13 00:20:01  richard
201 #  . Centralised the python version check code, bumped version to 2.1.1 (really
202 #    needs to be 2.1.2, but that isn't released yet :)
204 # Revision 1.21  2001/12/02 05:06:16  richard
205 # . We now use weakrefs in the Classes to keep the database reference, so
206 #   the close() method on the database is no longer needed.
207 #   I bumped the minimum python requirement up to 2.1 accordingly.
208 # . #487480 ] roundup-server
209 # . #487476 ] INSTALL.txt
211 # I also cleaned up the change message / post-edit stuff in the cgi client.
212 # There's now a clearly marked "TODO: append the change note" where I believe
213 # the change note should be added there. The "changes" list will obviously
214 # have to be modified to be a dict of the changes, or somesuch.
216 # More testing needed.
218 # Revision 1.20  2001/11/26 22:55:56  richard
219 # Feature:
220 #  . Added INSTANCE_NAME to configuration - used in web and email to identify
221 #    the instance.
222 #  . Added EMAIL_SIGNATURE_POSITION to indicate where to place the roundup
223 #    signature info in e-mails.
224 #  . Some more flexibility in the mail gateway and more error handling.
225 #  . Login now takes you to the page you back to the were denied access to.
227 # Fixed:
228 #  . Lots of bugs, thanks Roché and others on the devel mailing list!
230 # Revision 1.19  2001/11/22 00:25:10  richard
231 # quick fix for file uploads on windows in roundup.cgi
233 # Revision 1.18  2001/11/06 22:10:11  jhermann
234 # Added env config; fixed request wrapper & index list; sort list by key
236 # Revision 1.17  2001/11/06 21:51:19  richard
237 # Fixed HTTP headers for top-level index in CGI script
239 # Revision 1.16  2001/11/01 22:04:37  richard
240 # Started work on supporting a pop3-fetching server
241 # Fixed bugs:
242 #  . bug #477104 ] HTML tag error in roundup-server
243 #  . bug #477107 ] HTTP header problem
245 # Revision 1.15  2001/10/29 23:55:44  richard
246 # Fix to CGI top-level index (thanks Juergen Hermann)
248 # Revision 1.14  2001/10/27 00:22:35  richard
249 # Fixed some URL issues in roundup.cgi, again thanks Juergen Hermann.
251 # Revision 1.13  2001/10/05 02:23:24  richard
252 #  . roundup-admin create now prompts for property info if none is supplied
253 #    on the command-line.
254 #  . hyperdb Class getprops() method may now return only the mutable
255 #    properties.
256 #  . Login now uses cookies, which makes it a whole lot more flexible. We can
257 #    now support anonymous user access (read-only, unless there's an
258 #    "anonymous" user, in which case write access is permitted). Login
259 #    handling has been moved into cgi_client.Client.main()
260 #  . The "extended" schema is now the default in roundup init.
261 #  . The schemas have had their page headings modified to cope with the new
262 #    login handling. Existing installations should copy the interfaces.py
263 #    file from the roundup lib directory to their instance home.
264 #  . Incorrectly had a Bizar Software copyright on the cgitb.py module from
265 #    Ping - has been removed.
266 #  . Fixed a whole bunch of places in the CGI interface where we should have
267 #    been returning Not Found instead of throwing an exception.
268 #  . Fixed a deviation from the spec: trying to modify the 'id' property of
269 #    an item now throws an exception.
271 # Revision 1.12  2001/10/01 05:55:41  richard
272 # Fixes to the top-level index
274 # Revision 1.11  2001/09/29 13:27:00  richard
275 # CGI interfaces now spit up a top-level index of all the instances they can
276 # serve.
278 # Revision 1.10  2001/08/07 00:24:42  richard
279 # stupid typo
281 # Revision 1.9  2001/08/07 00:15:51  richard
282 # Added the copyright/license notice to (nearly) all files at request of
283 # Bizar Software.
285 # Revision 1.8  2001/08/05 07:43:52  richard
286 # Instances are now opened by a special function that generates a unique
287 # module name for the instances on import time.
289 # Revision 1.7  2001/08/03 01:28:33  richard
290 # Used the much nicer load_package, pointed out by Steve Majewski.
292 # Revision 1.6  2001/08/03 00:59:34  richard
293 # Instance import now imports the instance using imp.load_module so that
294 # we can have instance homes of "roundup" or other existing python package
295 # names.
297 # Revision 1.5  2001/07/29 07:01:39  richard
298 # Added vim command to all source so that we don't get no steenkin' tabs :)
300 # Revision 1.4  2001/07/23 04:47:27  anthonybaxter
301 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
302 # sys.exit(0) if python version wrong.
304 # Revision 1.3  2001/07/23 04:33:30  richard
305 # brought the CGI instance config dict in line with roundup-server
307 # Revision 1.2  2001/07/23 04:31:40  richard
308 # Fixed the roundup CGI script for updates to cgi_client.py
310 # Revision 1.1  2001/07/22 11:47:07  richard
311 # More Grande Splite
314 # vim: set filetype=python ts=4 sw=4 et si