-
Notifications
You must be signed in to change notification settings - Fork 6
/
uiki.rkt
500 lines (371 loc) · 14.1 KB
/
uiki.rkt
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#lang racket
; Uiki: A simple academic wiki.
; Copyright (C) 2015 Matthew Might
; 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
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
; DEPENDENCIES:
; + racket 6.1.1+ (earlier versions might work)
; + git
; + multimarkdown
; + sed
; + htpasswd (bundled with apache)
; FEATURES:
; + Markdown support via multimarkdown
; + LaTeX support via MathJax
; + Syntax highlighting support via code-prettify
; + Backup/versioning support via git
; KNOWN BUGS:
; + No warning on racing edits.
; TODO:
; + Include suffices in links, e.g., [[cat]]s renders as `cats`
; + Add web interface for version control
; + Add support for local install of MathJax
; + Add support for local install of code-prettify
; + Add support for git over ssh access to page repos
; + Add support for git over http access to page repos (git-http-backend)
; + Add support for site-wide LaTeX macro files inclusion
; + Add support for HTTP authentication
; + Allow uplodaing images
; + Allow uploading other file types (pdf)
; + Add support for citation management with .bib files
; SECURITY ISSUES:
; + Strip out problematic (all?) HTML tags: script, style, etc.
; + Verify absence of injection for shell/system/process uses.
; CONSIDER:
; + Could git submodules allow database to be giant module?
; Would this allow checking out entire wiki db locally?
(require web-server/servlet
web-server/servlet-env)
(require web-server/private/mime-types)
(require xml)
(require parser-tools/lex)
(require (prefix-in : parser-tools/lex-sre))
(require net/uri-codec)
(require file/sha1)
(require net/base64)
; Import configuration variables:
(include "config.rkt")
; Shell interaction:
(define ($ command)
; run a shell command, then
; print out stdout, stderr as needed:
(match (process command)
[`(,stdout ,stdin ,exit ,stderr ,proc)
(define cmd-out (port->string stdout))
(define cmd-err (port->string stderr))
(printf "$ command~n~a~n" cmd-out)
(when (not (equal? cmd-err ""))
(printf "~nerror:~n~a~n" cmd-err))]))
; Wiki text mark-up routines:
(define wikify-text
; convert a port into a list of strings,
; converting wiki mark-ups in the process:
(lexer
[(:: "[[" (complement (:: any-string "]]" any-string)) "]]")
(begin
(define text (substring lexeme 2 (- (string-length lexeme) 2)))
(define target:text (string-split text "|"))
(define link
(match target:text
[`(,target) (wikify-link target)]
[`(,target ,text) (wikify-link target text)]))
(cons link (wikify-text input-port)))]
[any-char
(cons lexeme (wikify-text input-port))]
[(eof)
'()]))
(define (wikify-target target)
; sanitize a link target:
(string-replace (string-downcase target) #px"[\\W]" "-"))
(define (wikify-link target [text #f])
; create an anchor tag:
(define safe-target (wikify-target target))
(string-append
"<a href=\"/wiki/" safe-target "\">"
(if text text target)
"</a>"))
; Page-generation helpers:
(define (generate-head-xexpr
#:title [title "no title"]
#:style [style ""])
; generate a page header:
`(head
(title ,title)
(style
,(string-append default-style "\n" style))))
; Basic HTTP request processing helpers:
(define ext=>mime-type (read-mime-types mime-types-file))
; HTTP Basic Authentication:
(define (htpasswd-credentials-valid?
passwd-file
username
password)
; checks if the given credentials match those in the database
; it assumes all entries as SHA1-encoded as in `htpasswd -s`.
; read in the lines from the password file:
(define lines (call-with-input-file passwd-file
(λ (port) (port->lines port))))
; convert the password to sha1:
(define sha1-pass (sha1-bytes (open-input-bytes password)))
; then to base64 encoding:
(define sha1-pass-b64
(bytes->string/utf-8 (base64-encode sha1-pass #"")))
; check if both the username and the password match:
(define (password-matches? line)
(define user:hash (string-split line ":"))
(define user (car user:hash))
(define hash (cadr user:hash))
(match (string->list hash)
; check for SHA1 prefix
[`(#\{ #\S #\H #\A #\} . ,hashpass-chars)
(define hashpass (list->string hashpass-chars))
(and (equal? username (string->bytes/utf-8 user))
(equal? hashpass sha1-pass-b64))]))
; check to see if any line validates:
(ormap password-matches? lines))
(define (authenticated? passwd-file req)
; checks if a request has valid credentials:
(match (request->basic-credentials req)
[(cons user pass)
(htpasswd-credentials-valid? passwd-file user pass)]
[else #f]))
; A handler for static files:
(define (handle-file-request req docroot path)
; NOTE: In practice, static requests can be replaced by
; providing an #:extra-files-paths to serve/servlet.
; identify the requested file:
(define file (string-append docroot "/" (string-join path "/")))
(cond
[(file-exists? file)
; =>
(define extension (string->symbol (bytes->string/utf-8 (filename-extension file))))
(define content-type
(hash-ref ext=>mime-type extension
(λ () TEXT/HTML-MIME-TYPE)))
; send the requested file back:
(response
200 #"OK" ; code & message
(current-seconds) ; timestamp
content-type ; content-type
'() ; additional headers
(λ (client-out)
(write-bytes (file->bytes file) client-out)))]
[else
; =>
(response/xexpr
#:preamble #"<!DOCTYPE html>"
#:code 404
#:message #"Not found"
`(html
,(generate-head-xexpr
#:title #"Not found")
(body
(p "Not found"))))]))
(define (handle-git-version-changes dir-path)
; if the git repository doesn't exist, create it:
(when (not (directory-exists? (string-append dir-path "/.git")))
; TODO/WARNING/SECURITY: Injection attack vulnerability
; Need to verify that wikilink-name escapes path.
(define git-init-cmd
(string-append "git -C '" dir-path "' init;"
"git -C '" dir-path "' add content.md;"
"git -C '" dir-path "' commit -m 'Initial commit.'"))
($ git-init-cmd))
; commit changes:
(define git-commit-cmd
(string-append "git -C '" dir-path "' add content.md;"
; TODO: Let the user set the update comment.
"git -C '" dir-path "' commit -m 'Updated page.'"))
($ git-commit-cmd))
; Wiki-specific requests:
(define (handle-wiki-content-put-request req page)
; modifies the contents of the specified page.
; directory location:
(define dir-path (string-append database-dir "/" (wikify-target page)))
; create the directory if it does not exist:
(define created? #f)
(when (not (directory-exists? dir-path))
(set! created? #t)
(make-directory dir-path))
; location of the markdown file:
(define md-file-path (string-append dir-path "/" "content.md"))
; grab the new contents:
(define post-data (request-post-data/raw req))
(define param-string (bytes->string/utf-8 post-data))
(define params (form-urlencoded->alist param-string))
(define contents (cdr (assq 'content params)))
; edit the content file:
(call-with-output-file
md-file-path
#:exists 'replace
(λ (out)
(write-string contents out)))
; Notify git of changes.
(handle-git-version-changes dir-path)
; Render the page.
(handle-wiki-view-request
req page
#:message (if created? "Page created." "Page edited.")))
; A handler to render the interface for editing a page:
(define (handle-wiki-edit-request req page)
; creates a page to allow editing.
(define dir-path (string-append database-dir "/" page))
(define md-file-path (string-append dir-path "/" "content.md"))
(cond
[(file-exists? md-file-path)
; =>
(response/xexpr
#:preamble #"<!DOCTYPE html>"
`(html
,(generate-head-xexpr
#:title (string-append "edit: " page)
#:style "
textarea#content {
width: 50em;
height: 30em;
}
")
(body
(form ((method "POST")
(action "./"))
(textarea
((id "content")
(name "content"))
,(file->string md-file-path))
(br)
(input ([type "submit"] [value "submit changes"]))))))]))
; A handler to render pages:
(define (handle-wiki-view-request req page
#:message [message #f])
; directory containing page contents:
(define dir-path (string-append database-dir "/" page))
; markdown file containing the page:
(define md-file-path (string-append dir-path "/" "content.md"))
(cond
[(file-exists? md-file-path)
(response
200 #"OK" ; code & message
(current-seconds) ; timestamp
TEXT/HTML-MIME-TYPE ; content type
'() ; additional headers
(λ (client-out)
; render the top:
(write-bytes #"<!DOCTYPE>\n<html>\n" client-out)
; render the header:
(define head (generate-head-xexpr
#:title (string-append page " :: " uiki-name)))
(write-string (xexpr->string head) client-out)
; render the body:
(write-bytes #"<body>" client-out)
; enable MathJax for LaTeX support:
(write-bytes #"<script type=\"text/x-mathjax-config\">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']]}
});
</script>" client-out)
(write-bytes #"<script src=\"https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>" client-out)
; Enable prettify for syntax highlighting:
(write-bytes #"<script src=\"https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js\"></script>" client-out)
; Include a message, if any:
(when message
(write-string message client-out)
(write-string "<hr />" client-out))
; Render the menu bar:
(define wiki-top-bar
(string->bytes/utf-8 (string-append "
<p>
[<a href=\"/wiki/" (wikify-target page) "/edit\">edit</a>]
</p>
<hr />")))
; Render the menu bar:
(write-bytes wiki-top-bar client-out)
; Pass content.md through through a markdown formatter, and then
; through the wikifier to convert wiki syntax:
(match (process (string-append markdown-preprocess-command " " md-file-path " | " markdown-command))
[(list in out exit err interrupt)
; Convert contents to wiki:
(define wikified (apply string-append (wikify-text in)))
(write-string wikified client-out)])
; Write the footer:
(write-bytes #"</body>" client-out)
(write-bytes #"</html>" client-out)))]
; Or, if the page is not found:
[else
; =>
(response/xexpr
#:preamble #"<!DOCTYPE html>"
`(html
,(generate-head-xexpr
#:title "page does not yet exist")
(body
(p "Page does not exist")
(form ([method "POST"] [action ,(string-append "/wiki/" page)])
(input ([type "hidden"] [name "content"] [value "Blank page"]))
(input ([type "submit"] [value "Create page"]))))))]))
(define (handle-wiki-request req resource)
; handle a top level /wiki/ request:
(when (equal? (last resource) "")
(set! resource (reverse (cdr (reverse resource)))))
(match resource
; view the page:
[`(,page)
#:when (equal? (request-method req) #"GET")
(handle-wiki-view-request req page)]
; modify the page contents:
[`(,page)
#:when (equal? (request-method req) #"POST")
(handle-wiki-content-put-request req page)]
; edit the page:
[`(,page "edit")
(handle-wiki-edit-request req page)]))
(define (start req)
; extract the uri from the request:
(define uri (request-uri req))
; extract the path from the uri:
(define path (map path/param-path (url-path uri)))
; The first element of the path determines the service;
; choices are "wiki" or "file":
(define service (car path))
(define resource (cdr path))
; NOTE: This could be simplified with a higher-level
; interface for web-server/dispatch:
; http://docs.racket-lang.org/web-server/dispatch.html
(cond
[(and auth-db-path (not (authenticated? auth-db-path req)))
(response
401 #"Unauthorized"
(current-seconds)
TEXT/HTML-MIME-TYPE
(list
(make-basic-auth-header
"Authentication required"
))
void)]
[(equal? service "file")
(handle-file-request req document-root resource)]
[(equal? service "wiki")
(handle-wiki-request req resource)]
[else (response/xexpr
#:preamble #"<!DOCTYPE html>"
`(html
(head
(title "can't handle service: " ,service)
(body
(p "Unhandled service type: " ,service)))))]))
(serve/servlet start
#:port uiki-port
#:servlet-path "/wiki/main"
#:servlet-regexp #rx""
#:launch-browser? #f
#:ssl? use-ssl?
#:ssl-cert ssl-cert-path
#:ssl-key ssl-private-key-path
#;end)