aboutsummaryrefslogtreecommitdiffstats
path: root/blogthon.cgi
blob: 86becbeea9495b20237e50531c180fce7955a4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.

# Author: Stefan Ritter <xeno@thehappy.de>
# Description: A simple blogging software

import cgi
import os
import time
import glob
import re
import ConfigParser
import md5

configuration = ConfigParser.ConfigParser()
configuration.read('configuration')

blog_title = configuration.get('personal', 'blog_title')
blog_url = configuration.get('personal', 'blog_url')
keywords = configuration.get('personal', 'keywords')
entries_dir = configuration.get('personal', 'entries_dir')
entries_suffix = configuration.get('personal', 'entries_suffix')
staticpages_dir = configuration.get('personal', 'staticpages_dir')
style = configuration.get('look', 'style')
entries_per_page = configuration.getint('look', 'entries_per_page')
monthlist = configuration.get('look', 'monthlist')
staticpages = configuration.get('look', 'staticpages')
linklist = configuration.get('look', 'linklist')
permalinks = configuration.get('look', 'permalinks')
comments = configuration.get('look', 'comments')
newest_first = configuration.get('look', 'newest_first')

action = cgi.FieldStorage()
month_display = action.getvalue('m')
post_display = action.getvalue('p')
static_display = action.getvalue('s')
allentries_display = action.getvalue('a')
feed_display = action.getvalue('feed')
if not month_display: month_display = ""
if not post_display: post_display = ""
if not static_display: static_display = ""
if not allentries_display: allentries_display = ""
if not feed_display: feed_display = ""

# Commentstuff
ctitle = action.getvalue('ctitle')
cname = action.getvalue('cname')
ctext = action.getvalue('ctext')
if not ctitle: ctitle = ""
if not cname: cname = ""
if not ctext: ctext = ""

# Comment to commit?
if cname and ctext and ctitle:
	# Prevent XSS hacks
	cname = cname.replace("<", "&lt;") \
				 .replace(">", "&gt;") \
				 .replace("\"", "&quot;")
	ctext = ctext.replace("<", "&lt;") \
				 .replace(">", "&gt;") \
				 .replace("\"", "&quot;")
	
	# Add comment
	comments_file = glob.glob(entries_dir + ctitle + '.comments')
	if not comments_file:
		content = open(entries_dir + ctitle + '.comments', "w")
		content.close()
		comments_file = glob.glob(entries_dir + ctitle + '.comments')
	content = open(comments_file[0], "a+")
	content.write("-." + cname + "\n")
	content.write("+." + time.asctime() + "\n")
	ctext = ctext.split("\n")
	for line in ctext:
		content.write("." + line + "\n")
	content.close()

# Read entries and store their title and timestamp
entries      = []
entries_list  = glob.glob(entries_dir + '*.' + entries_suffix)

for entry in entries_list:
	timestamp = os.stat(entry)
	timestamp = time.localtime(timestamp[8])
	entry = timestamp, entry
	entries.append(entry)

if newest_first:
	entries.sort(reverse=True)
else:
	entries.sort()

# Generate atom feed
if feed_display == "atom":
	title = str(entries[0][1]).replace('entries/', '', 1).replace('.' + entries_suffix, '')
	date = entries[0][0]

	print 'Content-type: text/html\n'
	print '<?xml version="1.0" encoding="utf-8"?>'
	print '<feed xmlns="http://www.w3.org/2005/Atom">'
	print '    <author>'
	print '        <name>' + blog_title + '</name>'
	print '    </author>'
	print '    <title>' + blog_title + '</title>'
	
	blog_title_md5sum = md5.new(blog_title).hexdigest()
	blog_title_md5sum_1 = blog_title_md5sum[0:8]
	blog_title_md5sum_2 = blog_title_md5sum[8:12]
	blog_title_md5sum_3 = blog_title_md5sum[12:16]
	blog_title_md5sum_4 = blog_title_md5sum[16:20]
	blog_title_md5sum_5 = blog_title_md5sum[20:32]

	print '    <id>urn:uuid:' + blog_title_md5sum_1 + '-' + blog_title_md5sum_2 + '-' + blog_title_md5sum_3 + '-' + blog_title_md5sum_4 + '-' + blog_title_md5sum_5 + '</id>'
	print '    <updated>' + str(date[0]) + '-' + str(date[1]) + '-' + str(date[2]) + 'T' + str(date[3]) + ':' + str(date[4]) + ':' + str(date[5]) + 'Z</updated>'
	print ''
	print '    <entry>'

	title_md5sum = md5.new(title).hexdigest()
	title_md5sum_1 = title_md5sum[0:8]
	title_md5sum_2 = title_md5sum[8:12]
	title_md5sum_3 = title_md5sum[12:16]
	title_md5sum_4 = title_md5sum[16:20]
	title_md5sum_5 = title_md5sum[20:32]

	print '        <title>' + title + '</title>'
	print '        <link href="' + blog_url + "/>'
	print '        <id>urn:uuid:' + title_md5sum_1 + '-' + title_md5sum_2 + '-' + title_md5sum_3 + '-' + title_md5sum_4 + '-' + title_md5sum_5 + '</id>'	
	print '        <updated>' + str(date[0]) + '-' + str(date[1]) + '-' + str(date[2]) + 'T' + str(date[3]) + ':' + str(date[4]) + ':' + str(date[5]) + 'Z</updated>'
	print '    </entry>'
	print '</feed>'

# Generate regular page
else:

	print 'Content-type: text/html\n'
	print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
	print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
	print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'
	print '    <head>'
	print '        <title>' + blog_title + '</title>'
	print '        <meta http-equiv="content-type" content="text/html; charset=utf-8" />'
	print '        <meta name="keywords" content="' + keywords + '" />'
	print '        <meta name="description" content="' + blog_title + '" />'
	print '        <link rel="stylesheet" type="text/css" href="styles/' + style + '" />'
	print '    </head>'
	print '    <body>'
	print '        <div class="title"><a href="?" class="title">' + blog_title + '</a></div>'

	print '        <div class="screen"><div class="sidebar">'

	# Sidebar: Staticpages
	if staticpages == "True":
		staticpages = []
		staticpages_list  = glob.glob(staticpages_dir + '*')
		staticpages_list.sort()
		print '            <div class="sidebarentry">'
		print '                <small>pages</small><br />'
		for staticpage in staticpages_list:
			title = re.sub('\w+?\/\d+?-', '', staticpage)
			link = re.sub('\w+?\/', '', staticpage)
			# The ultimative lookshe-hack *g*
			if title == "lookshe":
				print '                &nbsp;<a href="?s=' + link + '">/me</a>&nbsp;<br />'
			else:
				print '                &nbsp;<a href="?s=' + link + '">' + title + '</a>&nbsp;<br />'

		if monthlist == "True": print '                <br />'
		print '            </div>'

	# Sidebar: Monthlist
	if monthlist == "True":
		olddate = ""
		print '            <div class="sidebarentry">'
		print '                <small>months</small><br />'
		for entry in entries:
			date        = time.strftime("%m%Y", entry[0])
			date_display = time.strftime("%h %Y", entry[0])
			if not olddate == date:
				print '                &nbsp;<a href="?m=' + date + '">' + date_display + '</a>&nbsp;<br />'
			olddate = date
		if linklist == "True": print '                <br />'
		print '            </div>'

	# Sidebar: Linklist
	if linklist == "True":
		print '            <div class="sidebarentry">'
		print '                <small>links</small><br />'
		content = open("linklist", "r")
		for line in content:
			if line.strip() is "":
				print '                <br />'
			else:
				print '                &nbsp;<a href="' + line.split(" ")[0] + '" target="_blank">' + line.split(" ", 1)[1].strip() + '</a>&nbsp;<br />'
		content.close()
		print '            </div>'

	print '        </div>'
	print '        <div class="content">'

	if static_display != "": # Show Staticpage
		content = open(staticpages_dir + static_display, "r")
		print '            <div class="entrytitle">' + re.sub('\d+?-', '', static_display) + '</div>'
		print '            <div class="entry"><p>'
		for line in content:
			print '                ' + line.strip() + '<br />'
		print '            </p></div>'
		content.close()
	else: # Show regular entry
		entry_counter = 0
		for entry in entries:
			date            = time.strftime("%c", entry[0])
			date_to_compare = time.strftime("%m%Y", entry[0]) # Needed for permalinks
			entry           = entry[1]
			title           = entry.replace('entries/', '', 1)
			title           = title.replace('.' + entries_suffix, '')

			if month_display == date_to_compare or not month_display:
				if post_display == title or not post_display:
					if allentries_display == "1" or entry_counter < entries_per_page:
						content = open(entry, "r")
						if permalinks: # Title as permalink...
							print '            <div class="entrytitle"><a href="?p=' + title + '" class="entrytitle">' + title + ' <small>(' + date + ')</small></a></div>'
						else: # ... or not
							print '            <div class="entrytitle">' + title + ' <small>(' + date + ')</small></div>'
						print '            <div class="entry">'
						for line in content:
							print '                ' + line.strip() + '<br />'
						
						# Comments are shown when post_display and comments_file
						comments_file = glob.glob(entries_dir + title + '.comments')
						if post_display:
							if comments_file:
								print '                <br /><hr />'
								comments_file = glob.glob(entries_dir + title + '.comments')
								comments_content = open(comments_file[0], "r")
								for line in comments_content:
									if line.split(".", 1)[0] == "-":
										print '                <br />'
										print '                <b><i>' + line.split(".", 1)[1].strip() + '</i><small> wrote at '
									elif line.split(".", 1)[0] == "+":
										print '                ' + line.split(".", 1)[1].strip() + ':</small></b><br />'
									else:
										line = line.split(".", 1)[1]
										print '                &nbsp;&nbsp;' + line.strip() + '<br />'
								comments_content.close()

							# Form for adding comments
							print '                <br /><br /><br />'
							print '                <form action="" method="post">'
							print '                    <input type="hidden" name="ctitle" value="' + title + '" />'
							print '                    <label for="cname">name:</label><input type="text" id="cname" name="cname" />'
							print '                    <br /><label for="ctext">text:</label><textarea rows="5" cols="80" id="ctext" name="ctext"></textarea>'
							print '                    <br /><input type="submit" id="submit" value="post comment" />'
							print '                </form>'

						if comments == "True":
							comments_file = glob.glob(entries_dir + title + '.comments')
							if not comments_file and not post_display:
								print '                <div class="comment">'
								print '                    <ul><li><a href="?p=' + title + '" class="comment">no comments</a></li></ul>'
								print '                </div>'
							elif comments_file and not post_display:
								comments_content = open(comments_file[0], "r")
								comments_counter = 0
								for line in comments_content:
									if line.split(".", 1)[0] == "-": comments_counter += 1
								print '                <div class="comment">'
								print '                    <ul><li><a href="?p=' + title + '" class="comment">comments (' + str(comments_counter) + ')</a></li></ul>'
								print '                </div>'
								comments_content.close()
								
						print '            </div>'
						print '            <br /><br />'
						content.close()
						entry_counter += 1

		if not month_display and not post_display and not allentries_display and entry_counter == entries_per_page: # Display pagelist
			print '            <div class="entry"><a href=?a=1>View all entries...</a></div>'

	print '        </div></div>'
	print '    </body>'
	print '</html>'

# vim: set tw=0 ts=4: