-
Notifications
You must be signed in to change notification settings - Fork 2
/
awki.cgi
executable file
·421 lines (368 loc) · 13.6 KB
/
awki.cgi
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/awk -f
################################################################################
# awkiawki - wikiwiki clone written in (n|g|m)awk
# $Id: awki.cgi,v 1.45 2004/07/13 16:34:45 olt Exp $
################################################################################
# Copyright (c) 2002 Oliver Tonnhofer (olt@bogosoft.com)
# See the file `COPYING' for copyright notice.
################################################################################
BEGIN {
# --- default options ---
# --- use awki.conf to override default settings ---
#
# img_tag: HTML img tag for picture in page header.
localconf["img_tag"] = "<img src=\"/awki.png\" width=\"48\" height=\"39\" align=\"left\">"
# datadir: Directory for raw pagedata (must be writeable for the script).
localconf["datadir"] = "./data/"
# parser: Parsing script.
localconf["parser"] = "./parser.awk"
# special_parser: Parser for special_* functions.
localconf["special_parser"] = "./special_parser.awk"
# default_page: Name of the default_page.
localconf["default_page"] = "FrontPage"
# show_changes: Number of changes listed by RecentChanges
localconf["show_changes"] = 10
# max_post: Bytes accept by POST requests (to avoid DOS).
localconf["max_post"] = 100000
# write_protection: Regex for write protected files
# e.g.: "*", "PageOne|PageTwo|^.*NonEditable"
# HINT: to edit these protected pages, upload a .htaccess
# protected awki.cgi script with write_protection = ""
localconf["write_protection"] = ""
# css: HTTP URL for external CSS file.
localconf["css"] = ""
# always_convert_spaces: If true, convert runs of 8 spaces to tab automatical.
localconf["always_convert_spaces"] = 0
# date_cmd: Command for current date.
localconf["date_cmd"] = "date '+%e %b. %G %R:%S %Z'"
# rcs: If true, rcs is used for revisioning.
localconf["rcs"] = 0
# path: add path to PATH environment
localconf["path"] = ""
# --- default options ---
scriptname = ENVIRON["SCRIPT_NAME"]
if (localconf["path"])
ENVIRON["PATH"] = localconf["path"] ":" ENVIRON["PATH"]
#load external configfile
load_config(scriptname)
# PATH_INFO contains page name
if (ENVIRON["PATH_INFO"]) {
query["page"] = ENVIRON["PATH_INFO"]
}
if (ENVIRON["REQUEST_METHOD"] == "POST") {
if (ENVIRON["CONTENT_TYPE"] == "application/x-www-form-urlencoded") {
if (ENVIRON["CONTENT_LENGTH"] < localconf["max_post"])
bytes = ENVIRON["CONTENT_LENGTH"]
else
bytes = localconf["max_post"]
cmd = "F=`mktemp`; " \
"dd ibs=" bytes " status=noxfer count=1 of=$F" \
">/dev/null 2>/dev/null && " \
"cat $F && " \
"rm -f $F"
cmd | getline query_str
close (cmd)
}
if (ENVIRON["QUERY_STRING"])
query_str = query_str "&" ENVIRON["QUERY_STRING"]
} else {
if (ENVIRON["QUERY_STRING"])
query_str = ENVIRON["QUERY_STRING"]
}
n = split(query_str, querys, "&")
for (i=1; i<=n; i++) {
split(querys[i], data, "=")
query[data[1]] = data[2]
}
# (IMPORTANT for security!)
query["page"] = clear_pagename(query["page"])
query["revision"] = clear_revision(query["revision"])
query["revision2"] = clear_revision(query["revision2"])
query["string"] = clear_str(decode(query["string"]))
if (!localconf["rcs"])
query["revision"] = 0
if (query["page"] == "")
query["page"] = localconf["default_page"]
query["filename"] = localconf["datadir"] query["page"]
#check if page is editable
special_pages = "FullSearch|PageList|RecentChanges|ChangesRSS"
if (query["page"] ~ "("special_pages")") {
special_page = 1
} else if (!localconf["write_protection"] || query["page"] !~ "("localconf["write_protection"]")") {
page_editable = 1
}
if(query["page"] == "ChangesRSS")
rss_page()
else
html_page()
}
function html_page() {
header(query["page"])
if (query["edit"] && page_editable)
edit(query["page"], query["filename"], query["revision"])
else if (query["save"] && query["text"] && page_editable)
save(query["page"], query["text"], query["string"], query["filename"])
else if (query["page"] ~ "PageList")
special_index(localconf["datadir"])
else if (query["page"] ~ "RecentChanges")
special_changes(localconf["datadir"])
else if (query["page"] ~ "FullSearch")
special_search(query["string"],localconf["datadir"])
else if (query["page"] && query["history"])
special_history(query["page"], query["filename"])
else if (query["page"] && query["diff"] && query["revision"])
special_diff(query["page"], query["filename"], query["revision"], query["revision2"])
else
parse(query["page"], query["filename"], query["revision"])
footer(query["page"])
}
function rss_page( i,fn,date,link,host,proto,base_url) {
host = ENVIRON["HTTP_HOST"]
if("HTTPS" in ENVIRON)
proto = "https"
else
proto = "http"
base_url = proto "://" host scriptname
#print "Content-Type: text/plain; charset=utf-8\n"
print "Content-Type: application/rss+xml; charset=utf-8\n"
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
print "<rss version=\"2.0\"><channel>"
print "<title>AwkiAwki at " base_url "</title>"
print "<link>" base_url "</link>"
print "<description>Recent Changes RSS feed for wiki</description>"
i = 0
while(i < 10 && ("ls -tlL "localconf["datadir"] | getline) > 0) {
if($9 ~ /^[A-Z][a-z]+[A-Z][A-Za-z]*$/) {
i++
fn = $9
date = $6 " " $7 " " $8 # ls -l's timstamp format can die in a fire.
link = base_url "/" fn
print "<item><title>" fn "</title>"
# FIXME: Getting ls to *portably* spit out an RFC-822 date isn't
# possible. feedvalidator.org doesn't seem to mind, so we'll ignore
# the pubDate tag.
#print "<pubDate>" date "</pubDate>"
print "<author>awki@"host" (AwkiAwki users)</author>"
print "<description>"fn" update at "date"</description>"
gsub(/[^0-9a-zA-Z]/, "_", date)
print "<link>"link"</link><guid>"link"?"date"</guid>"
print "</item>"
}
}
print "</channel></rss>\n"
}
# print header
function header(page) {
print "Content-type: text/html; charset=utf-8\n"
print "<html>\n<head>\n<title>" page "</title>"
print "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"Wiki Changes RSS\" href=\""scriptname"/ChangesRSS\"></link>"
if (localconf["css"])
print "<link rel=\"stylesheet\" href=\"" localconf["css"] "\">"
if (query["save"])
print "<meta http-equiv=\"refresh\" content=\"2,URL="scriptname"/"page"\">"
print "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\">"
print "</head>\n<body>"
print "<h1>"localconf["img_tag"]
print "<a href=\""scriptname"/FullSearch?string="page"\">"page"</a></h1><hr>"
}
# print footer
function footer(page) {
print "<hr>"
if (page_editable)
print "<a href=\""scriptname"?edit=true&page="page"\">Edit "page"</a>"
print "<a href=\""scriptname"/"localconf["default_page"]"\">"localconf["default_page"]"</a>"
print "<a href=\""scriptname"/PageList\">PageList</a>"
print "<a href=\""scriptname"/RecentChanges\">RecentChanges</a>"
print "<a href=\""scriptname"/ChangesRSS\">ChangesRSS</a>"
if (localconf["rcs"] && !special_page)
print "<a href=\""scriptname"/"page"?history=true\">PageHistory</a>"
print "<form action=\""scriptname"/FullSearch\" method=\"GET\" align=\"right\">"
print "<input type=\"text\" name=\"string\">"
print "<input type=\"submit\" value=\"search\">"
print "</form>\n</body>\n</html>"
}
# send page to parser script
function parse(name, filename, revision) {
if (system("[ -f "filename" ]") == 0 ) {
if (revision) {
print "<em>Displaying old version ("revision") of <a href=\""scriptname"/" name "\">"name"</a>.</em>"
system("co -q -p'"revision"' " filename " | "localconf["parser"] " -v datadir='"localconf["datadir"] "'")
} else
system(localconf["parser"] " -v datadir='"localconf["datadir"] "' " filename)
}
}
function special_diff(page, filename, revision, revision2, revisions) {
if (system("[ -f "filename" ]") == 0) {
print "<em>Displaying diff between "revision
if (revision2)
print " and "revision2
else
print " and current version"
print " of <a href=\""scriptname"/"page "\">"page"</a>.</em>"
if (revision2)
revisions = "-r" revision " -r" revision2
else
revisions = "-r" revision
system("rcsdiff "revisions" -u "filename" | " localconf["special_parser"] " -v special_diff='"page"'")
}
}
# print edit form
function edit(page, filename, revision, cmd) {
if (revision)
print "<p><small><em>If you save previous versions, you'll overwrite the current page.</em></small>"
print "<form action=\""scriptname"?save=true&page="page"\" method=\"POST\">"
print "<textarea name=\"text\" rows=25 cols=80>"
# insert current page into textarea
if (revision) {
cmd = "co -q -p'"revision"' " filename
while ((cmd | getline) >0)
print
close(cmd)
} else {
while ((getline < filename) >0)
print
close(filename)
}
print "</textarea><br />"
print "<input type=\"submit\" value=\"save\">"
if (localconf["rcs"])
print "Comment: <input type=\"text\" name=\"string\" maxlength=80 size=50>"
if (! localconf["always_convert_spaces"])
print "<br>Convert runs of 8 spaces to Tab <input type=\"checkbox\" name=\"convertspaces\" checked>"
print "</form>"
print "<small><strong>Emphases:</strong> ''<em>italic</em>''; '''<strong>bold</strong>'''; \
'''''<strong><em>bold italic</em></strong>'''''; ''<em>mixed '''<strong>bold</strong>'''\
and italic</em>''<br>\
<strong>Horizontal Rule:</strong> ----<br><strong>Paragraph:</strong> blank line<br>\
<strong>Headings:</strong> -Title 1 ; --Title 2 ; ---Title 3<br>\
<strong>Preformatted Text:</strong> *space*Text<br>\
<strong>Lists:</strong> tab(s) and one of * bullets; 1 numbered items<br>\
<strong>Links:</strong> JoinCapitalizedWords; url (http, https, ftp, gopher, mailto, news)<br>\
<strong>Quotes:</strong> > text</small>"
}
# save page
function save(page, text, comment, filename, dtext, date) {
dtext = decode(text);
if ( localconf["always_convert_spaces"] || query["convertspaces"] == "on")
gsub(/ /, "\t", dtext);
print dtext > filename
if (localconf["rcs"]) {
localconf["date_cmd"] | getline date
system("ci -q -t-"page" -l -m'"ENVIRON["REMOTE_ADDR"] ";;" date ";;"comment"' " filename)
}
print "saved <a href=\""scriptname"/"page"\">"page"</a>"
}
# list all pages
function special_index(datadir) {
system("ls -1 " datadir " | " localconf["special_parser"] " -v special_index=yes")
}
# list pages with last modified time (sorted by date)
function special_changes(datadir, date) {
localconf["date_cmd"] | getline date
print "<p>current date:", date "<p>"
system("ls -tlL "datadir" | " localconf["special_parser"] " -v special_changes=" localconf["show_changes"])
}
function special_search(name,datadir) {
system("grep -il '"name"' "datadir"* | " localconf["special_parser"] " -v special_search=yes")
}
function special_history(name, filename) {
print "<p>last changes on <a href=\""scriptname"/" name "\">"name"</a><p>"
system("rlog " filename " | " localconf["special_parser"] " -v special_history="name)
print "<p>Show diff between:"
print "<form action=\""scriptname"/\" method=\"GET\">"
print "<input type=\"hidden\" name=\"page\" value=\""name"\">"
print "<input type=\"hidden\" name=\"diff\" value=\"true\">"
print "<input type=\"text\" name=\"revision\" size=5>"
print "<input type=\"text\" name=\"revision2\" size=5>"
print "<input type=\"submit\" value=\"diff\">"
print "</form></p>"
}
# remove '"` characters from string
# *** !Important for Security! ***
function clear_str(str) {
gsub(/['`"]/, "", str)
if (length(str) > 80)
return substr(str, 1, 80)
else
return str
}
# retrun the pagename
# *** !Important for Security! ***
function clear_pagename(str) {
if (match(str, /[A-Z][a-z]+[A-Z][A-Za-z]*/))
return substr(str, RSTART, RLENGTH)
else
return ""
}
# return revision numbers
# *** !Important for Security! ***
function clear_revision(str) {
if (match(str, /[1-9]\.[0-9]+/))
return substr(str, RSTART, RLENGTH)
else
return ""
}
# decode urlencoded string
function decode(text, hex, i, hextab, decoded, len, c, c1, c2, code) {
split("0 1 2 3 4 5 6 7 8 9 a b c d e f", hex, " ")
for (i=0; i<16; i++) hextab[hex[i+1]] = i
# urldecode function from Heiner Steven
# http://www.shelldorado.com/scripts/cmds/urldecode
# decode %xx to ASCII char
decoded = ""
i = 1
len = length(text)
while ( i <= len ) {
c = substr (text, i, 1)
if ( c == "%" ) {
if ( i+2 <= len ) {
c1 = tolower(substr(text, i+1, 1))
c2 = tolower(substr(text, i+2, 1))
if ( hextab [c1] != "" || hextab [c2] != "" ) {
if ( (c1 >= 2 && c1 != 7) || (c1 == 7 && c2 != "f") || (c1 == 0 && c2 ~ "[9acd]") ){
code = 0 + hextab [c1] * 16 + hextab [c2] + 0
c = sprintf ("%c", code)
} else {
c = " "
}
i = i + 2
}
}
} else if ( c == "+" ) { # special handling: "+" means " "
c = " "
}
decoded = decoded c
++i
}
# change linebreaks to \n
gsub(/\r\n/, "\n", decoded)
# remove last linebreak
sub(/[\n\r]*$/,"",decoded)
return decoded
}
#load configfile
function load_config(script, configfile,key,value) {
configfile = script
#remove trailing / ('/awki/awki.cgi/' -> '/awki/awki.cgi')
sub(/\/$/, "", configfile)
#remove path ('/awki/awki.cgi' -> 'awki.cgi')
sub(/^.*\//, "", configfile)
#remove suffix ('awki.cgi' -> 'awki')
sub(/\.[^.]*$/,"", configfile)
# append .conf suffix
configfile = configfile ".conf"
#read configfile
while((getline < configfile) >0) {
if ($0 ~ /^#/) continue #ignore comments
if (match($0,/[ \t]*=[ \t]*/)) {
key = substr($0, 1, RSTART-1)
sub(/^[ \t]*/, "", key)
value = substr($0, RSTART+RLENGTH)
sub(/[ \t]*$/, "", value)
if (sub(/^"/, "", value))
sub(/"$/, "", value)
#set localconf variables
localconf[key] = value
}
}
}