This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtech.actindi.net.lisp
452 lines (418 loc) · 17.3 KB
/
tech.actindi.net.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
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
(in-package :tech.actindi.net)
(defvar *errors* nil)
(defparameter *entries-per-page* 10)
(defvar *session-user* "user" "(id-of user) を設定するセッションキー")
(defun number-to-kanji (num)
(let ((digit #("零" "一" "二" "三" "四" "五" "六" "七" "八" "九"))
(subunit #("" "十" "百" "千"))
(unit #("" "万" "億" "兆")))
(loop for i from 0
initially (if (= num 0) (return (aref digit 0)))
for (n m) = (multiple-value-list (floor num 10000)) ; 4 桁ずつ区切る
until (and (= n 0) (= m 0))
for value = (loop for j from 0 to 3 ; 位
for x = (mod (floor m (expt 10 j)) 10) ; 数字
when (/= x 0) ; 零千 零百 零十 対策
collect (format nil "~A~A"
; 一千 一百 一十 対策
(if (and (= x 1) (> j 0)) "" (aref digit x))
(aref subunit j)))
when value appending (cons (aref unit i) value) into result
do (setf num n)
finally (return (apply #'concatenate (cons 'string (reverse result)))))))
;; RSS
(defaction /rss.xml ()
(setf (content-type) "text/html; charset=utf-8")
(html
(raw "<?xml version=\"1.0\" encoding=\"utf-8\"?>")
(:rss
:version "2.0"
(:channel
(:title "アクトインディ技術部隊報告書")
(:link "http://tech.actindi.net")
(:description "アクトインディ技術部隊報告書")
(raw (format-date-string "<lastBuildDate>%a, %d %b %Y %H:%M:%S +0900</lastBuildDate>"))
(:language "ja")
(loop for x in (zrang :entries 0 nil :from-end t)
do (html
(:item
(:title (post-title x))
(:link (format nil "http://tech.actindi.net~A" (post-path x)))
(:description
(raw "<![CDATA[") (raw (post-body x)) (raw "]]>"))
(raw
(format-date-string "<pubDate>%a, %d %b %Y %H:%M:%S +0900</pubDate>"
(post-date x))))))))))
;; メンバー一覧
(defun top-member ()
(html
(:div :id "member"
(:h2 :class "design" "メンバー一覧")
(:p :class "title"
"アクトインディ技師部隊員名簿")
(:ul
(:li (:a :href "/akiyama" "akiyama"))
(:li (:a :href "/chiba" "chiba"))
(:li (:a :href "/honda" "honda"))
(:li (:a :href "/kawaguchi" "kawaguchi"))
(:li (:a :href "/komatsu" "komatsu"))
(:li (:a :href "/namikata" "namikata"))
(:li (:a :href "/tahara" "tahara")))
(:p :class "title"
"アクトインディ技師部元隊員")
(:ul
(:li (:a :href "/aoki" "aoki"))
(:li (:a :href "/ataka" "ataka"))
(:li (:a :href "/komagata" "komagata"))
(:li (:a :href "/nakanishi" "nakanishi"))
(:li (:a :href "/machida" "machida"))
(:li (:a :href "/masuda" "masuda"))
(:li (:a :href "/oishi" "oishi")))
(:p :class "to_actindi"
(:a :href "http://www.actindi.com"
"アクトインディへ"))
(:a :href "/entry/new" "投稿する"))))
;; お知らせ
(defun block-news ()
(html
(:div :id "news"
(:h2 "技師部隊からの" (:br)
"お知らせ")
#+nil
(:p "子供とおでかけ頑張っています" ((:a :href "http://iko-yo.net/") "iko-yo.net"))
(:p (:a :href "http://www.actindi.com/recruit/programmer" "【求人】エンジニア募集")
"しています。"))))
(defun twitter ()
"こんにちは!!、こんにちは!!")
;; テンプレート
(defmacro with-default-template ((&key (title "アクトインディ技術部隊報告書")) &body contents)
`(html
(raw "<!DOCTYPE html>")
(:html
:lang "ja"
(:head (:meta :charset "UTF-8")
(:title ,title)
(:link :href "/stylesheets/reset.css" :rel "stylesheet" :type "text/css")
(:link :href "/stylesheets/basic.css" :rel "stylesheet" :type "text/css")
(:link :href "/rss.xml" :rel "alternate" :type "application/rss+xml" :title "Actindi Tech blog")
(:script :type "text/javascript"
:src "http://s.hatena.ne.jp/js/HatenaStar.js"
(raw "Hatena.Star.Token = 'cf59d5b3db097a760443474ddf49026f6c5e40f3';"))
;; cf. http://d.hatena.ne.jp/hatenastar/20070707
(:script :type "text/javascript"
(raw
"Hatena.Star.SiteConfig = {
entryNodes: {
'div.content': {
uri: 'h2 a',
title: 'h2',
container: 'h2'
}
}
};"))
(:script (raw "
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-71599025-1', 'auto');
ga('send', 'pageview');
")))
(:body
(:div#header
(:div.inner
(:div.twitter
(:h2.design "twitter")
(:p (twitter)))
(:h1.design_a
(:a :href "" :title "" "タイトル"))
(:ol#bread_crumbs
(:li (:a :href "/" "home")))))
(:div.inner
(:div#columns
(:div#cotents
;; contents
,@contents
(:div.footer nil))
(:div#local_nav
;; news
(block-news)
(:div#counter
(:dl
(:dt "本頁の来客数")
(:dd (format nil "~A名" (number-to-kanji (inc :counter))))))
(top-member)))
;; ads
(:div#foo_ads
(:div#categories
(:h2.design "カテゴリー")
(:ul
(loop for post in (zrang :entries 0 nil :from-end t)
do (html (:li (:a :href (post-path post) (post-title post))))))
(:p.to_actindi (:a :href "http://www.actindi.com" "アクトインディ")))
(:div.poster
(:img :src "/images/poster_01.jpg" :alt "aaaa"))
(:script :type "text/javascript"
(raw "//<![CDATA[
(function() {
var links = document.getElementsByTagName('a');
var query = '?';
for(var i = 0; i < links.length; i++) {
if(links[i].href.indexOf('#disqus_thread') >= 0) {
query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
}
}
document.write('<script charset=\"utf-8\" type=\"text/javascript\" src=\"http://disqus.com/forums/actindi/get_num_replies.js' + query + '\"></' + 'script>');
})();
//]]>")))) ;"
(:div#footer
(:p.center "Copyright " (raw "©") " 2009-2015 アクトインディ All rights reserved."))))))
(defun loginp ()
(unpyo:session *session-user*))
(defmacro with-authorization (&body body)
`(if (loginp)
(progn ,@body)
(redirect "/login")))
(defun pager (current-page total url)
(html
(:div :class "content"
(:p :class "to_top"
(when (< 1 current-page)
(html (:a :href (format nil "~a?page=~d" url (1- current-page))
"前へ")))
" | "
(when (> (/ total *entries-per-page*) current-page)
(html (:a :href (format nil "~a?page=~d" url (1+ current-page))
"次へ")))))))
;; トップページ
(defaction /root (:path "/")
(let* ((page (or (and @page (parse-integer @page :junk-allowed t)) 1))
(start (* *entries-per-page* (1- page)))
(end (1- (+ start *entries-per-page*))))
(with-default-template ()
(loop for x in (zrang :entries start end :from-end t)
do (html
(:div.content
(:h2
(:a :href (post-path x) (post-title x)))
(:dl.date
(:dd (format-date-string "%g%#e年%#m月%#d日(%v) %H時%M分%S秒"
(post-date x)))
(:dt "区分")
(:dd (post-category x))
(:dt "報告者: ")
(:dd (post-author x)))
(:p (raw (post-body x)))
(:p.to_top
(:a :href (format nil "~A#disqus_thread" (post-path x))
">View Comments")
(raw " ")
(:a :href "" "このページの上へ戻る")))))
(pager page (zcard :entries) "/"))))
(defmacro def-member-page (name)
`(defaction ,(intern (format nil "/~:@(~a~)" name)) ()
(let* ((page (or (and @page (parse-integer @page :junk-allowed t)) 1))
(start (* *entries-per-page* (1- page)))
(end (1- (+ start *entries-per-page*))))
(with-default-template (:title (concatenate 'string
(string-downcase ',name)
"の記事 — アクトインディ技術部隊報告書"))
(loop for x in (zrang ,(format nil "author:~a" name) start end :from-end t)
do (html
(:div.content
(:h2
(:a :href (post-path x) (post-title x)))
(:dl.date
(:dd (format-date-string "%g%#e年%#m月%#d日(%v) %H時%M分%S秒"
(post-date x)))
(:dt "区分")
(:dd (post-category x))
(:dt "報告者: ")
(:dd (post-author x)))
(:p (raw (post-body x)))
(:p.to_top
(:a :href (format nil "~A#disqus_thread" (post-path x))
">View Comments")
(raw " ")
(:a :href "" :title "" "このページの上へ戻る")))))
(pager page (zcard ,(format nil "author:~a" name))
(concatenate 'string "/" ,name))))))
(def-member-page "akiyama")
(def-member-page "aoki")
(def-member-page "ataka")
(def-member-page "chiba")
(def-member-page "honda")
(def-member-page "kawaguchi")
(def-member-page "komagata")
(def-member-page "komatsu")
(def-member-page "machida")
(def-member-page "masuda")
(def-member-page "nakanishi")
(def-member-page "namikata")
(def-member-page "oishi")
(def-member-page "tahara")
(defaction /@id (:route-function (lambda (url method)
(declare (ignore method))
(ppcre:register-groups-bind (id) ("^/([0-9]+)$" url)
(setf (param :id) id))))
(let* ((uri (env "REQUEST_URI"))
(id (parse-integer @id))
(post (car (zrang-by-score :entries id id))))
(with-default-template (:title (concatenate 'string
(post-title post)
" — アクトインディ技術部隊報告書"))
(html
(:div.content
(:h2
(:a :href (post-path post) (post-title post)))
(:dl :class "date"
(:dd (format-date-string "%g%#e年%#m月%#d日(%v) %H時%M分%S秒"
(post-date post)))
(:dt "区分")
(:dd (post-category post))
(:dt "報告者: ")
(:dd (post-author post)))
(:p (raw (post-body post)))
(:p :class "to_top"
(:a :href "/" "トップページに戻る"))
(:div.comment
(raw "<div id=\"disqus_thread\"></div><script type=\"text/javascript\" src=\"http://disqus.com/forums/actindi/embed.js\"></script><noscript><a href=\"http://actindi.disqus.com/?url=ref\">View the discussion thread.</a></noscript><a href=\"http://disqus.com\" class=\"dsq-brlink\">blog comments powered by <span class=\"logo-disqus\">Disqus</span></a>"))
(:a.edit :href (concatenate 'string uri "/edit")
"編集"))))))
(defun trim (x)
(and x (string-trim '(#\Space #\Tab #\Newline) x)))
(defun presentp (x)
(and x (trim x) ""))
(defun entry-form (action id title author category body)
(html
(:form :method "POST" :action action
(when id
(html
(:input :type "hidden" :name "id" :value id)))
(:div
(:label :for "title") "題名" (:br)
(:input :type "text" :name "title" :value title))
(:div
(:label :for "author" "報告者") (:br)
(:input :type "text" :name "author" :value author))
(:div
(:label :for "category" "区分") (:br)
(:input :type "text" :name "category" :value category))
(:div
(:label :for "body" "本文") (:br)
(:textarea :cols 30 :rows 15 :name "body" (trim (or body ""))))
(:div (:input :type "submit" :value "投稿")))))
(defaction /entry/new ()
(with-authorization
(with-default-template ()
(html
(:div.content
(:h2 "投稿")
(when *errors*
(format nil "~a" *errors*))
(entry-form "/entry/create" nil @title @author @category @body))))))
(defaction /entry/create ()
(with-authorization
(let* ((post (make-post :title @title
:author @author
:category @category
:body @body))
(date (post-date post)))
(zadd :entries date post)
(zadd (format nil "author:~a" @author) date post))
(redirect "/")))
(defaction /@id/edit ()
(with-authorization
(with-default-template ()
(let* ((id (parse-integer @id))
(post (car (zrang-by-score :entries id id))))
(html
(:div.content
(:h2 "投稿")
(if *errors*
(format nil "~a" *errors*))
(entry-form "/entry/update"
id
(post-title post)
(post-author post)
(post-category post)
(post-body post))))))))
(defaction /entry/update ()
(with-authorization
(let* ((id (parse-integer @id))
(post (car (zrang-by-score :entries id id))))
(setf (post-title post) @title
(post-author post) @author
(post-category post) @category
(post-body post) @body)
(redirect (format nil "/~a" id)))))
(defparameter *oauth-secret-file* (merge-pathnames "google-oauth.lisp" *default-directory*))
(defvar *oauth-client-id* nil)
(defvar *oauth-client-secret* nil)
(defvar *oauth-callback-url* nil)
(defun load-google-oauth ()
(with-open-file (in *oauth-secret-file*)
(let ((x (read in)))
(setf *oauth-client-id* (getf x :client-id)
*oauth-client-secret* (getf x :client-secret)
*oauth-callback-url* (getf x :callback-url)
unpyo::*session-secret* (getf x :unpyo-session-secret)))))
(defaction /login ()
(with-default-template (:title "ログイン")
(:div :style "margin-left: 20px; font-size: 18px;"
(when @noauth
(html (:p "ログイン権限がありません。")))
(:a :href
(format nil "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=~a&redirect_uri=~a&scope=https://www.googleapis.com/auth/userinfo.email"
*oauth-client-id* *oauth-callback-url*)
"Google アカウントでログイン"))))
(defun auth-check (email)
(ppcre:scan "@actindi.net\\z" email))
(defaction /oauth2callback ()
(handler-case
(if @code
(let* ((token (oauth2:request-token
"https://www.googleapis.com/oauth2/v3/token"
@code
:method :post
:redirect-uri *oauth-callback-url*
:other `(("client_id" . ,*oauth-client-id*)
("client_secret" . ,*oauth-client-secret*))))
(userinfo (with-input-from-string
(stream
(map 'string 'code-char
(oauth2:request-resource
"https://www.googleapis.com/oauth2/v2/userinfo"
token)))
(json:decode-json stream)))
(email (cdr (assoc :email userinfo))))
(if (not (auth-check email))
(redirect "/login?noauth=1")
(progn
(setf (unpyo:session *session-user*) email)
(redirect "/"))))
(redirect "/login"))
(oauth2::request-token-error (e)
(print e)
(redirect "/login"))))
(defvar *server*)
(defclass tech-app (application)
())
(defmethod call :around ((tech-app tech-app))
(with-db ((merge-pathnames "lepis/" *default-directory*))
(call-next-method)))
;; start
(defun start-tech.actindi.net (&key (port *http-port*))
(load-google-oauth)
(unless *db*
(setf *db* (open-db (merge-pathnames "lepis/" *default-directory*))))
;; Unpyo
(setf *invoke-debugger-p* nil)
(setq *server* (make-server :app (make-instance 'tech-app) :port port))
(run *server*))
;; stop
(defun stop-tech.actindi.net ()
(stop *server*)
(when *db*
(close-db *db*)
(setf *db* nil)))