X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=geekigeeki.py;h=a6158a3122b41b184930bf0255664a428851858d;hb=fef72da7c35a10fc92c098a9287683e9d71bea1d;hp=7193d5520b50af8bb709453f3cb53f721d9c7efd;hpb=e3f54bfe72a742afbe81e363bacc1869959f50c9;p=geekigeeki.git diff --git a/geekigeeki.py b/geekigeeki.py index 7193d55..a6158a3 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -3,7 +3,7 @@ # # Copyright 1999, 2000 Martin Pool # Copyright 2002 Gerardo Poggiali -# Copyright 2007 Bernardo Innocenti +# Copyright 2007, 2008 Bernardo 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 @@ -23,13 +23,14 @@ __version__ = '$Id$'[4:12] from time import clock start_time = clock() -import cgi, sys, string, os, re, errno, stat +import cgi, sys, os, re, errno, stat from os import path, environ # 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$") +word_re = re.compile(r"^\b((([A-Z][a-z]+){2,}/)*([A-Z][a-z]+){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)$", re.IGNORECASE) url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$") @@ -57,25 +58,33 @@ def get_hostname(addr): try: from socket import gethostbyaddr return gethostbyaddr(addr)[0] + ' (' + addr + ')' - except: + except Exception, er: return addr +def relative_url(pathname, privileged=False): + if not (url_re.match(pathname) or pathname.startswith('/')): + if privileged: + url = privileged_path() + else: + url = script_name() + pathname = url + '/' + pathname + return pathname + # Formatting stuff -------------------------------------------------- -def emit_header(type="text/html"): - print "Content-type: " + type + "; charset=utf-8" - print +def emit_header(mime_type="text/html"): + print "Content-type: " + mime_type + "; charset=utf-8\n" -def send_guru(msg, msg_type): - if msg is None or msg == '': return +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 '
' - # FIXME: This simple JS snippet is harder to pass than ACID 3.0 + # FIXME: This little JS snippet is harder to pass than ACID 3.0 print """ """ -def send_title(name, text="Limbo", msg=None, msg_type='error'): +def send_title(name, text="Limbo", msg_text=None, msg_type='error'): global title_done if title_done: return @@ -114,7 +123,7 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'): if not name: print ' ' for css in css_url: - print ' ' % css + print ' ' % relative_url(css) print '' # Body @@ -124,7 +133,7 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'): 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 link_tag(params, text=None, ss_class=None, privileged=False): if text is None: text = params # default classattr = '' @@ -162,15 +171,13 @@ def link_tag(params, text=None, ss_class=None, authentication=False): # 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 url_re.match(params): + 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) @@ -197,7 +204,7 @@ def do_fullsearch(needle): print_search_stats(len(hits), len(all_pages)) -def do_titlesearch(needle): +def handle_titlesearch(needle): # TODO: check needle is legal -- but probably we can just accept any RE send_title(None, "Title search for \"" + needle + '"') @@ -215,35 +222,30 @@ def do_titlesearch(needle): def print_search_stats(hits, searched): print "

%d hits out of %d pages searched.

" % (hits, searched) -#TODO: merge into do_savepage() -def do_edit(pagename): - Page(pagename).send_editor() - -def do_raw(pagename): +def handle_raw(pagename): Page(pagename).send_raw() -def do_savepage(pagename): - global form +def handle_edit(pagename): pg = Page(pagename) - if 'preview' in form: - pg.send_editor(form['savetext'].value) - elif 'save' in form: - pg.save_text(form['savetext'].value) + if 'save' in form: + if form['file'].value: + pg.save(form['file'].file.read()) + else: + pg.save(form['savetext'].value.replace('\r\n', '\n')) pg.send_page() elif 'cancel' in form: - pg.msg = 'Editing canceled' + pg.msg_text = 'Editing canceled' pg.msg_type = 'notice' pg.send_page() - else: - raise 'What did you press?' + else: # preview or edit + text = None + if 'preview' in form: + text = form['savetext'].value + pg.send_editor(text) def make_index_key(): - s = '

' - links = map(lambda ch: '%s' % (ch, ch), - string.lowercase) - s = s + string.join(links, ' | ') - s = s + '

' - return s + links = map(lambda ch: '%s' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz') + return '

'+ ' | '.join(links) + '

' def page_list(): return filter(word_re.match, os.listdir(data_dir)) @@ -251,9 +253,8 @@ def page_list(): def send_footer(name, mod_string=None): if globals().get('debug_cgi', False): cgi.print_arguments() - cgi.print_form(cgi.FieldStorage()) + cgi.print_form(form) cgi.print_environ() - global __version__ print '