From 01057766514a73e0df07470e71c94b0508b2e5bf Mon Sep 17 00:00:00 2001 From: Bernardo Innocenti Date: Mon, 10 Sep 2007 03:38:42 +0200 Subject: [PATCH] Initial authentication support. --- geekigeeki.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/geekigeeki.py b/geekigeeki.py index 99234c5..1909644 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -23,8 +23,6 @@ __version__ = '$Revision: 1.63+gerry+bernie $'[11:-2]; import cgi, sys, string, os, re, errno, time, stat from os import path, environ -from socket import gethostbyaddr -from time import localtime, strftime # Regular expression defining a WikiWord # (but this definition is also assumed in other places) @@ -35,22 +33,31 @@ url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$") title_done = False -def emit_header(type="text/html"): - print "Content-type: " + type + "; charset=utf-8" - print - -# Formatting stuff -------------------------------------------------- +# CGI stuff --------------------------------------------------------- def get_scriptname(): return environ.get('SCRIPT_NAME', '') +def remote_user(): + return environ.get('REMOTE_USER', 'AnonymousCoward') + +def remote_host(): + return environ.get('REMOTE_ADDR', '') + def get_hostname(addr): try: + from socket import gethostbyaddr return gethostbyaddr(addr)[0] + ' (' + addr + ')' except: return addr; +# Formatting stuff -------------------------------------------------- + +def emit_header(type="text/html"): + print "Content-type: " + type + "; charset=utf-8" + print + def send_guru(msg, msg_type): if msg is None or len(msg) == 0: return print '
'
@@ -111,7 +118,7 @@ def send_title(name, text="Limbo", msg=None, msg_type = 'error'):
 
     # Navbar
     print ''
 
+    user = remote_user()
+    if user != 'AnonymousCoward':
+        print ' | logged in as ' + cgi.escape(user) + ''
+
+    print ''
     title_done = True
 
 
@@ -488,8 +500,6 @@ class PageFormatter:
 class Page:
     def __init__(self, page_name):
         self.page_name = page_name
-        self.remote_user = 'AnonymousCoward'
-        self.remote_host = environ.get('REMOTE_ADDR', '')
         self.msg = ''
         self.msg_type = 'error'
         self.attrs = {}
@@ -499,15 +509,12 @@ class Page:
         # and insert a space there
         return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
 
-
     def _text_filename(self):
         return path.join(text_dir, self.page_name)
 
-
     def _tmp_filename(self):
         return path.join(text_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#'))
 
-
     def exists(self):
         try:
             os.stat(self._text_filename())
@@ -563,7 +570,7 @@ class Page:
             acl = attrs["acl"]
             for rule in acl.split():
                 (user,perms) = acl.split(':')
-                if user == self.remote_user or user == "All":
+                if user == remote_user() or user == "All":
                     if 'write' in perms.split(','):
                         return True
             return False
@@ -582,6 +589,7 @@ class Page:
     def _last_modified(self):
         if not self.exists():
             return None
+        from time import localtime, strftime
         modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
         return strftime(datetime_fmt, modtime)
 
@@ -589,8 +597,8 @@ class Page:
         send_title(None, 'Edit ' + self.split_title(), msg=self.msg, msg_type=self.msg_type)
 
         print ('

Editing ' + self.page_name - + ' for ' + self.remote_user - + ' from ' + get_hostname(self.remote_host) + + ' for ' + cgi.escape(remote_user()) + + ' from ' + cgi.escape(get_hostname(remote_host())) + '

') print '
' % (get_scriptname(), self.page_name) print '' % (self.page_name) @@ -629,10 +637,11 @@ class Page: self._write_file(newtext) rc = 0 if post_edit_hook: + # FIXME: what's the std way to perform shell quoting in python? cmd = ( post_edit_hook - + ' ' + text_dir + '/' + self.page_name - + ' ' + self.remote_user - + ' ' + self.remote_host + + " '" + text_dir + '/' + self.page_name + + "' '" + remote_user() + + "' '" + remote_host() + "'" ) out = os.popen(cmd) msg = out.read() -- 2.25.1