~ubuntu-branches/ubuntu/feisty/pyblosxom/feisty

« back to all changes in this revision

Viewing changes to contrib/weblog-add.py

  • Committer: Bazaar Package Importer
  • Author(s): Charles Majola
  • Date: 2005-02-21 14:38:10 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050221143810-c1r07ibzr5hr863u
Tags: 1.0.0-2ubuntu1
* Rebuild for python2.4
* Fixed patches also 2.3 -> 2.4 
* Standards Version 3.6.1.1 

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
include_html_syntax = 1
33
33
 
34
34
def user2Link(user): 
35
 
        """given a username, return some representation of that user
36
 
        Generally, this will be an anchor ref of a mailto URL
37
 
        """ 
38
 
        # could also look up mail addrs via a table lookup, etc
39
 
        return '<a href="mailto:%(user)s@somewebsite.com">%(user)s</a>' % {"user": user}
 
35
    """given a username, return some representation of that user
 
36
    Generally, this will be an anchor ref of a mailto URL
 
37
    """ 
 
38
    # could also look up mail addrs via a table lookup, etc
 
39
    return '<a href="mailto:%(user)s@somewebsite.com">%(user)s</a>' % {"user": user}
40
40
 
41
41
# Nothing below here should need to be tailored...
42
42
 
43
43
def get_blog_dirs():
44
 
        logdirs=[]
45
 
        os.path.walk(blog_root, lambda arg, dirPath, paths: logdirs.append(dirPath[len(blog_root)+1:]), None)
46
 
        logdirs.sort()
47
 
        return logdirs
 
44
    logdirs=[]
 
45
    os.path.walk(blog_root, lambda arg, dirPath, paths: logdirs.append(dirPath[len(blog_root)+1:]), None)
 
46
    logdirs.sort()
 
47
    return logdirs
48
48
 
49
49
def getUser(): return os.environ.get("REMOTE_USER", None)
50
50
 
51
51
def validPath(category, filename):
52
 
        if category == ".": 
53
 
                category = ""           # special case for main directory
54
 
        categories=get_blog_dirs()
55
 
        if category not in categories:
56
 
                # XXX: I should escape any html in category to prevent
57
 
                # error page from cross site scripting (of course, it's
58
 
                # assumed that the person submitting has been authenticated
59
 
                # and is trusted...)
60
 
                raise RuntimeError, "Category `%s' does not exist" % category
61
 
        for hostile_char in r"/.`$&*?|;":
62
 
                if hostile_char in filename:
63
 
                        raise RuntimeError, "Invalid character `%s' in filename.  Try to stay with alphanumerics, space, and underscore." % hostile_char
64
 
        return os.path.join(blog_root, category, filename +".txt")
65
 
        
 
52
    if category == ".": 
 
53
        category = ""        # special case for main directory
 
54
    categories=get_blog_dirs()
 
55
    if category not in categories:
 
56
        # XXX: I should escape any html in category to prevent
 
57
        # error page from cross site scripting (of course, it's
 
58
        # assumed that the person submitting has been authenticated
 
59
        # and is trusted...)
 
60
        raise RuntimeError, "Category `%s' does not exist" % category
 
61
    for hostile_char in r"/.`$&*?|;":
 
62
        if hostile_char in filename:
 
63
            raise RuntimeError, "Invalid character `%s' in filename.  Try to stay with alphanumerics, space, and underscore." % hostile_char
 
64
    return os.path.join(blog_root, category, filename +".txt")
 
65
    
66
66
def genFormPage():
67
 
        categories=get_blog_dirs()
68
 
        print """\
 
67
    categories=get_blog_dirs()
 
68
    print """\
69
69
<html>
70
70
<head>
71
71
<title>content creation</title>
74
74
<form action="weblog-add.py">
75
75
<b>Category:</b>
76
76
<select name="category">"""
77
 
        for path in categories:
78
 
                name=path
79
 
                if path == "":          # special case for root
80
 
                        path="."
81
 
                        name="MAIN"
82
 
                print """<option value="%s">%s</option>""" %(path, name)
83
 
        print """\
 
77
    for path in categories:
 
78
        name=path
 
79
        if path == "":       # special case for root
 
80
            path="."
 
81
            name="MAIN"
 
82
        print """<option value="%s">%s</option>""" %(path, name)
 
83
    print """\
84
84
</select>
85
85
<p> 
86
86
<b>Title:</b>
87
 
<br>
 
87
<br />
88
88
<input type=text name="title" size=80 value=""> 
89
89
<p>
90
90
<b>Filename (no path and no extension):</b>
91
 
<br>
 
91
<br />
92
92
<input type=text name="filename" size=40 value=""> 
93
93
<p>
94
94
"""
95
95
 
96
 
        if include_html_syntax:
97
 
                print """\
 
96
    if include_html_syntax:
 
97
        print """\
98
98
<b>HTML Summary:</b>
99
 
<br>
 
99
<br />
100
100
<table border=1>
101
101
<tr><th>Hypertext link</th><td>&lt;a href="URL"&gt;linked text&lt;/a&gt;</td></tr>
102
102
<tr><th>Paragraph</th><td>&lt;p&gt;Text&lt;/p&gt;</td></tr>
105
105
<tr><th>Strongly emphasized text</th><td>&lt;strong&gt;<strong>Text to emphasize</strong>&lt;/strong&gt;</td></tr>
106
106
</table>
107
107
<p>"""
108
 
        print """\
 
108
    print """\
109
109
<b>Content:</b> 
110
 
<br>
 
110
<br />
111
111
<textarea cols=80 rows=10 name="text"></textarea>
112
 
<br>
 
112
<br />
113
113
<input type=submit> 
114
114
</body>
115
115
</html>
116
116
"""
117
117
 
118
118
def error(msg):
119
 
        print "<html><head><title>Content Error!</title></head><body><h1>Content Error!</h1>%s</body></html>" % msg
120
 
        sys.exit(0)
 
119
    print "<html><head><title>Content Error!</title></head><body><h1>Content Error!</h1>%s</body></html>" % msg
 
120
    sys.exit(0)
121
121
 
122
122
def addContent(form):
123
 
        try:
124
 
                filename=validPath(form.getfirst("category"), form.getfirst("filename"))
125
 
        except RuntimeError, msg:
126
 
                error(msg)
127
 
        # XXX: should perhaps do more error checking here
128
 
        datafile=open(filename, "w")
129
 
        print >>datafile, form.getfirst("title")
130
 
        print >>datafile, '#author %s' % user2Link(getUser())
131
 
        print >>datafile, form.getfirst("text")
132
 
        datafile.close()
133
 
        print '<html><body><h1>Posted!</h1><a href="/">Return to webroot</a></body></html>'
134
 
        # XXX: return page should probably link to page to which 
135
 
        # content was added...  HTTP_REFERRER seems to be broken for
136
 
        # for me though...
 
123
    try:
 
124
        filename=validPath(form.getfirst("category"), form.getfirst("filename"))
 
125
    except RuntimeError, msg:
 
126
        error(msg)
 
127
    # XXX: should perhaps do more error checking here
 
128
    datafile=open(filename, "w")
 
129
    print >>datafile, form.getfirst("title")
 
130
    print >>datafile, '#author %s' % user2Link(getUser())
 
131
    print >>datafile, form.getfirst("text")
 
132
    datafile.close()
 
133
    print '<html><body><h1>Posted!</h1><a href="/">Return to webroot</a></body></html>'
 
134
    # XXX: return page should probably link to page to which 
 
135
    # content was added...  HTTP_REFERRER seems to be broken for
 
136
    # for me though...
137
137
 
138
138
if __name__ == '__main__':
139
 
        form = cgi.FieldStorage()
140
 
        print "Content-type: text/html\n"
141
 
        if not getUser(): 
142
 
                error("User not authenticated.")
143
 
        if form.has_key("text"):
144
 
                addContent(form)
145
 
                sys.exit(0)
146
 
        genFormPage()
147
 
        sys.exit(0)
 
139
    form = cgi.FieldStorage()
 
140
    print "Content-type: text/html\n"
 
141
    if not getUser(): 
 
142
        error("User not authenticated.")
 
143
    if form.has_key("text"):
 
144
        addContent(form)
 
145
        sys.exit(0)
 
146
    genFormPage()
 
147
    sys.exit(0)