-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.lisp
391 lines (357 loc) · 14.9 KB
/
handlers.lisp
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
;;; file: handlers.lisp
;;;
;;; Copyright (c) 2007 Cyrus Harmon ([email protected])
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
(in-package :nuclblog)
(defun entry-html (blog entry)
"Outputs html for a blog entry."
(with-html
(:div :class "nuclblog-entry"
(:div :class "nuclblog-entry-head"
(:div :class "nuclblog-entry-title"
(:h1 (:a :href (make-entry-url blog entry)
(str (blog-entry-title entry)))))
(:div :class "nuclblog-entry-date"
(:h2 (str (hunchentoot::rfc-1123-date
(blog-entry-time entry)))
(unless (< (abs (- (blog-entry-time entry)
(blog-entry-revised-time entry)))
10)
(htm
" revised at: "
(str (hunchentoot::rfc-1123-date
(blog-entry-revised-time entry)))))))
(let ((user (blog-entry-user entry)))
(when user
(htm (:div :class "nuclblog-entry-user"
(:h3 "posted by " (str user)
" in " (:a :href
(make-archives-url
blog (blog-entry-category entry))
(str (blog-entry-category entry)))))))))
(:div :class "nuclblog-entry-contents"
(str (blog-entry-contents entry)))
(:div :class "nuclblog-entry-nav"
(when (hunchentoot-auth:session-realm-user-authenticated-p (blog-realm blog))
(htm (:a :href (make-edit-entry-url blog entry) "edit")
" "
(:a :href (make-delete-entry-url blog entry) "delete")))))))
;;; ugh. who::*downcase-tokens-p* needs to be set at compile time. Let's
;;; try to be polite about how we got about setting this global
;;; flag. I wish there a better way to do this...
(defparameter *tag-state* who::*downcase-tokens-p*)
(progn
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf *tag-state* who::*downcase-tokens-p*)
(setf who::*downcase-tokens-p* nil))
(defun entry-rss (blog entry)
"Outputs RSS 2.0 for a given blog entry."
(with-xml
(:|item|
(:|title| (str (blog-entry-title entry)))
(:|link| (str (make-full-entry-url blog entry)))
(:|description| (str (escape-string (blog-entry-contents entry))))
(:|pubDate| (str (hunchentoot::rfc-1123-date
(blog-entry-time entry))))
(:|guid| (str (make-full-entry-url blog entry))))))
(defun channel-rss (blog &key (limit 10) category)
"Outputs RSS 2.0 for a complete channel."
(let ((latest-change
(loop for entry in
(apply #'sorted-blog-entries blog
(when category
`(:category ,category)))
for i below limit
maximizing (blog-entry-revised-time entry) into latest-change
finally (return latest-change))))
(setf (content-type*) "application/rss+xml")
(hunchentoot::handle-if-modified-since latest-change)
(setf (header-out :last-modified) (hunchentoot::rfc-1123-date latest-change))
(with-xml-output-to-string (*standard-output*)
(htm (:|rss| :|version| 2.0
(:|channel|
(:|title| (str (blog-title blog)))
(:|link| (str (make-full-root-url blog)))
(:|description| (str (blog-subtitle blog)))
(:|pubDate| (str (hunchentoot::rfc-1123-date latest-change)))
(loop for entry in
(apply #'sorted-blog-entries blog
(when category
`(:category ,category)))
for i below limit
do (entry-rss blog entry))))))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf who::*downcase-tokens-p* *tag-state*)))
(defun blog-login-page (blog &optional user password)
(with-blog-page
blog
(format nil "~A: login" (blog-title blog))
(if (or user password)
(with-html
(:p "Login failed. Please try again."))
(with-html
(:p "Please login:")))
(hunchentoot-auth:generate-html-login :user user :password password)))
;;; this only exists for the (deprecated, test) new-browser-auth
;;; handler. this should go away, quickly...
(defmethod check-password ((blog blog) user password)
(hunchentoot-auth:check-password (blog-realm blog) user password))
(defun blog-main (blog)
(with-blog-page
blog
(blog-title blog)
(loop for entry in (sorted-blog-entries blog)
for i below 10
do (entry-html blog entry))))
(defun blog-status (blog)
(with-blog-page
blog
(format nil "~A: status" (blog-title blog))
(if (hunchentoot-auth:session-realm-user-authenticated-p (blog-realm blog))
(with-html
(:p "Logged in as "
(str (hunchentoot-auth:session-realm-user (blog-realm blog)))
" to "
(str (blog-short-name blog))
"."))
(with-html
(:p "Not Logged in.")))))
(defun blog-display (blog &key id)
(with-blog-page
blog
(format nil "~A: display" (blog-title blog))
(if (and id (numberp id))
(let ((entry (get-entry id blog)))
(if entry
(entry-html blog entry)
(with-html
(:p "Invalid entry."))))
(with-html
(:p "Please select a blog entry for display.")))))
(defun blog-archives (blog &key category)
(with-blog-page
blog
(format nil "~A: archives" (blog-title blog))
(loop for entry in (sorted-blog-entries blog)
when (or (null category)
(equal (blog-entry-category entry)
category))
do (entry-html blog entry))))
(defun blog-archives-rss (blog &key limit category)
(apply #'channel-rss blog :limit limit
(when category
`(:category ,category))))
(defun blog-new (blog &key
category
content
title
user
password)
(hunchentoot-auth:authorized-page
((blog-realm blog)
:ssl-port (blog-ssl-port blog)
:login-page-function (lambda ()
(blog-login-page blog user password)))
(with-blog-page
blog
(format nil "~A: new entry" (blog-title blog))
(if (and content title category)
(progn
(create-blog-entry blog category title content user)
(with-html
(:p (str (format nil "Created new blog entry by ~A in ~A:"
user category)))
(:h2 (str title))
(:p (str content))))
(with-html
(:p (:form :method :post
"Category: "
(:select :name "category"
(loop for cat in (blog-categories blog)
for selected = t then nil
do (if selected
(htm (:option :selected t :label cat (str cat)))
(htm (:option :label cat (str cat))))))
(:br)
"Title: "
(:input :type :text :name "title" (when title (str title)))
(:br)
(:textarea :name "content" :rows "20" :cols "60" "")
(:br)
(:input :type :submit :value "Submit"))))))))
(defun blog-edit (blog &key
id
category
content
title
user
password)
(hunchentoot-auth:authorized-page
((blog-realm blog)
:ssl-port (blog-ssl-port blog)
:login-page-function (lambda ()
(blog-login-page blog user password)))
(let ((edited)
(edit-error))
(when (and id content title category)
(let ((entry (get-entry id blog)))
(if entry
(progn
(setf (blog-entry-category entry) category)
(setf (blog-entry-title entry) title)
(setf (blog-entry-contents entry) content)
(setf (blog-entry-revised-time entry) (get-universal-time))
(let ((path (blog-entry-storage-path blog)))
(when path (store-blog-entries blog path)))
(setf edited t))
(setf edit-error t))))
(with-blog-page
blog
(format nil "~A: edit entry" (blog-title blog))
(cond (edited
(with-html
(:p (str (format nil "updated new blog entry by ~A in ~A:"
user category)))
(:h2 (str title))
(:p (str content))))
(edit-error
(with-html
(:p "Entry editing error!")))
(t
(let ((entry (get-entry id blog)))
(when entry
(let ((category (or category (blog-entry-category entry))))
(with-html
(:p (:form :method :post
"Category: "
(:select :name "category"
(loop for cat in (blog-categories blog)
do (if (equal cat category)
(htm (:option :selected t :label cat (str cat)))
(htm (:option :label cat (str cat))))))
(:br)
"Title: "
(:input :type :text :name "title" :value (blog-entry-title entry))
(:input :type :hidden :name "id" :value (princ-to-string (blog-entry-number entry)))
(:br)
(:textarea :name "content" :rows "20" :cols "60"
(esc (blog-entry-contents entry)))
(:br)
(:input :type :submit :value "Submit")))))))))))))
(defun blog-delete (blog &key
id
user
password)
(hunchentoot-auth:authorized-page
((blog-realm blog)
:ssl-port (blog-ssl-port blog)
:login-page-function (lambda ()
(blog-login-page blog user password)))
(let ((deleted))
(when id
(setf deleted (delete-blog-entry blog id)))
(with-blog-page
blog
(if (and id deleted)
(format nil "~A: delete entry" (blog-title blog))
(format nil "~A: error" (blog-title blog)))
(if (and id deleted)
(with-html
(:p "Deleted entry " (str (princ-to-string id))))
(with-html
(:p "Error deleting entry")))))))
(defun blog-login (blog &key user password)
(hunchentoot-auth:authorized-page
((blog-realm blog)
:ssl-port (blog-ssl-port blog)
:login-page-function (lambda ()
(blog-login-page blog user password)))
(with-blog-page
blog
(format nil "~A: login" (blog-title blog))
(with-html
(:p "User " (str user) " successfully logged in.")))))
(defun blog-logout (blog)
(setf (hunchentoot-auth:session-realm-user-authenticated-p (blog-realm blog)) nil)
(setf (hunchentoot-auth:session-realm-user (blog-realm blog)) nil)
(with-blog-page
blog
(format nil "~A: logout" (blog-title blog))
(with-html
(:p "You have successfully logged out."))))
(defun define-blog-handlers (blog)
"Defines the easy handlers for a given blog."
(define-blog-handler (blog)
()
#'blog-main)
(define-blog-handler (blog :uri "/")
()
#'blog-main)
(define-blog-handler (blog :uri "/archives")
(category)
#'blog-archives)
(define-blog-handler (blog :uri "/archives.rss")
((limit :parameter-type 'integer :init-form 10)
category)
#'blog-archives-rss)
(define-blog-handler (blog :uri "/email")
()
(lambda (blog)
(redirect (format nil "mailto:~A" (blog-owner-email blog)))))
(define-blog-handler (blog :uri "/display")
((id :parameter-type 'integer))
#'blog-display)
(define-blog-handler (blog :uri "/new"
:default-request-type :post)
(category
content
title
(user :init-form (hunchentoot-auth:session-realm-user (blog-realm blog)))
password)
#'blog-new)
(define-blog-handler (blog :uri "/edit"
:default-request-type :both)
((id :parameter-type 'integer)
category
content
title
(user :init-form (hunchentoot-auth:session-realm-user (blog-realm blog)))
password)
#'blog-edit)
(define-blog-handler (blog :uri "/delete")
((id :parameter-type 'integer)
(user :init-form (hunchentoot-auth:session-realm-user (blog-realm blog)))
password)
#'blog-delete)
(define-blog-handler (blog :uri "/login"
:default-request-type :post)
((user :init-form (hunchentoot-auth:session-realm-user (blog-realm blog)))
password)
#'blog-login)
(define-blog-handler (blog :uri "/logout") () #'blog-logout)
(define-blog-handler (blog :uri "/status") () #'blog-status))