X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=geekigeeki.py;h=a6158a3122b41b184930bf0255664a428851858d;hb=fef72da7c35a10fc92c098a9287683e9d71bea1d;hp=ad4fc5dd0683fd99b62fd6a5df85903cb4fa1da9;hpb=9ba86215f478f6ec624522d42e9aeb9f1eb9eb30;p=geekigeeki.git diff --git a/geekigeeki.py b/geekigeeki.py index ad4fc5d..a6158a3 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 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 @@ -22,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$") @@ -56,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 # Head emit_header() - print """ - -""" + print '' + print '' + + site_name = globals().get('site_name', 'Unconfigured Site') print "%s: %s" % (site_name, text) print ' ' if not name: print ' ' - if css_url: - print ' ' % css_url + for css in css_url: + print ' ' % relative_url(css) print '' # Body @@ -122,24 +133,26 @@ 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 = '' @@ -158,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) @@ -187,13 +198,13 @@ def do_fullsearch(needle): for (count, page_name) in hits: print '
  • ' + Page(page_name).link_to() print ' . . . . ' + `count` - print ['match', 'matches'][count <> 1] + print ['match', 'matches'][count != 1] print '

  • ' print "" 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 + '"') @@ -211,44 +222,39 @@ def do_titlesearch(needle): def print_search_stats(hits, searched): print "

    %d hits out of %d pages searched.

    " % (hits, searched) -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(text_dir)) + return filter(word_re.match, os.listdir(data_dir)) def send_footer(name, mod_string=None): - if debug_cgi: + if globals().get('debug_cgi', False): cgi.print_arguments() - cgi.print_form(cgi.FieldStorage()) + cgi.print_form(form) cgi.print_environ() - global __version__ print '