X-Git-Url: https://codewiz.org/gitweb?p=geekigeeki.git;a=blobdiff_plain;f=geekigeeki.py;h=651d98db20e984506af833ebf7c810e20a8ae635;hp=5ae1bd3f51d7de73b4a029f3f0ea5505d8231a03;hb=aaa8d1b0c0c584f64ab3f1f08a7b8480c05161d4;hpb=699af67492bf63859b6dedf1afea72a2471c11f8 diff --git a/geekigeeki.py b/geekigeeki.py index 5ae1bd3..651d98d 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -1,8 +1,9 @@ -#! /usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright 1999, 2000 Martin Pool # Copyright 2002 Gerardo Poggiali -# Copyright 2007 Bernardo Innocenti +# Copyright 2007, 2008 Bernie Innocenti # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -22,15 +23,17 @@ __version__ = '$Id$'[4:12] from time import clock start_time = clock() -import cgi, sys, string, os, re, errno, stat -from os import path, environ +import cgi, sys, os, re, errno, stat # Regular expression defining a WikiWord # (but this definition is also assumed in other places) -file_re = re.compile(r"^\b([A-Za-z0-9_\.\-]+)\b$") -word_re = re.compile(r"^\b([A-Z][a-z]+){2,}\b$") -img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg)$", re.IGNORECASE) +word_re = re.compile(r"^\b((([A-Z][a-z0-9]+){2,}/)*([A-Z][a-z0-9]+){2,})\b$") +# FIXME: we accept stuff like foo/../bar and we shouldn't +file_re = re.compile(r"^\b([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)\b$") +img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg|bmp|ico|ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE) +video_re = re.compile(r"^.*\.(ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE) url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$") +link_re = re.compile("(?:\[\[|{{)([^\s\|]+)(?:\s*\|\s*([^\]]+)|)(?:\]\]|}})") title_done = False @@ -38,66 +41,68 @@ title_done = False # CGI stuff --------------------------------------------------------- def script_name(): - return environ.get('SCRIPT_NAME', '') + return os.environ.get('SCRIPT_NAME', '') def privileged_path(): return privileged_url or script_name() def remote_user(): - user = environ.get('REMOTE_USER', '') + user = os.environ.get('REMOTE_USER', '') if user is None or user == '' or user == 'anonymous': user = 'AnonymousCoward' return user def remote_host(): - return environ.get('REMOTE_ADDR', '') + return os.environ.get('REMOTE_ADDR', '') def get_hostname(addr): try: from socket import gethostbyaddr return gethostbyaddr(addr)[0] + ' (' + addr + ')' - except: + except Exception: return addr -# Formatting stuff -------------------------------------------------- +def is_external_url(pathname): + return (url_re.match(pathname) or pathname.startswith('/')) + +def relative_url(pathname, privileged=False): + if not is_external_url(pathname): + if privileged: + url = privileged_path() + else: + url = script_name() + pathname = url + '/' + pathname + return pathname -def emit_header(type="text/html"): - print "Content-type: " + type + "; charset=utf-8" - print +def permalink(s): + return re.sub(' ', '-', re.sub('[^a-z0-9_ ]', '', s.lower()).strip()) -def send_guru(msg, msg_type): - if msg is None or msg == '': return +# Formatting stuff -------------------------------------------------- +def emit_header(mime_type="text/html"): + print "Content-type: " + mime_type + "; charset=utf-8\n" + +def sendfile(dest_file, src_file): + """Efficiently copy file data between file descriptors""" + while 1: + data = src_file.read(65536) + if not data: break + dest_file.write(data) + +def send_guru(msg_text, msg_type): + if not msg_text: return print '
'
     if msg_type == 'error':
         print '    Software Failure.  Press left mouse button to continue.\n'
-    print msg
+    print msg_text
     if msg_type == 'error':
-        print '      Guru Meditation #DEADBEEF.ABADC0DE'
+        print '\n      Guru Meditation #DEADBEEF.ABADC0DE'
     print '
' - # FIXME: This simple JS snippet is harder to pass than ACID 3.0 - print """ - """ + try: + sendfile(sys.stdout, open('gurumeditation.js', 'rb')) + except IOError, err: + pass -def send_title(name, text="Limbo", msg=None, msg_type='error'): +def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=False): global title_done if title_done: return @@ -107,42 +112,59 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'): print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' print '' - site_name = globals().get('site_name', 'Unconfigured Site') print "%s: %s" % (site_name, text) print ' ' if not name: print ' ' - if globals().has_key('css_url'): - print ' ' % css_url + + for meta in meta_urls: + http_equiv, content = meta + print ' ' % (http_equiv, relative_url(content)) + + for link in link_urls: + rel, href = link + print ' ' % (rel, relative_url(href)) + + if name and writable and privileged_url is not None: + print ' ' \ + % (privileged_path() + '?edit=' + name) + + if history_url is not None: + print ' ' \ + % relative_url(history_url + '?a=rss') + print '' # Body - if name and privileged_url is not None: + if name and writable and privileged_url is not None: print '' else: print '' title_done = True - send_guru(msg, msg_type) + send_guru(msg_text, msg_type) # Navbar print '' -def link_tag(params, text=None, ss_class=None, authentication=False): +def send_httperror(status="403 Not Found", query=""): + print "Status: %s" % status + send_title(None, msg_text=("%s: on query '%s'" % (status, query))) + send_footer() + +def link_tag(params, text=None, link_class=None, privileged=False): if text is None: text = params # default - classattr = '' - if ss_class: - classattr += 'class="%s" ' % ss_class - # Prevent crawlers from following links potentially added by spammers or to generated pages - if ss_class == 'external' or ss_class == 'navlink': - classattr += 'rel="nofollow" ' - if authentication: - path = privileged_path() - else: - path = script_name() - return '%s' % (classattr, path, params, text) + elif img_re.match(text): + text = '' + + if not link_class: + if is_external_url(params): + link_class = 'external' + elif file_re.match(params) and Page(params).exists(): + link_class = 'wikilink' + else: + params = nonexist_pfx + params + link_class = 'nonexistent' + + classattr = 'class="%s" ' % link_class + # Prevent crawlers from following links potentially added by spammers or to generated pages + if link_class == 'external' or link_class == 'navlink': + classattr += 'rel="nofollow"' + + return '%s' % (classattr, relative_url(params, privileged=privileged), text) # Search --------------------------------------------------- -def do_fullsearch(needle): +def handle_fullsearch(needle): send_title(None, 'Full text search for "%s"' % (needle)) needle_re = re.compile(needle, re.IGNORECASE) @@ -188,7 +222,7 @@ def do_fullsearch(needle): print "