]> git.plutz.net Git - webtxt/blob - index.cgi
list recent pages; git revisioning
[webtxt] / index.cgi
1 #!/bin/sh
2
3 _EXEC="${_EXEC:-${0%/*}/}"
4 _DATA="${_DATA:-.}"
5
6 . "$_EXEC"/cgilite/cgilite.sh
7 . "$_EXEC"/cgilite/session.sh
8 . "$_EXEC"/cgilite/file.sh
9 . "$_EXEC"/session_lock.sh
10
11 git init "$_DATA"
12
13 yield_page(){
14   page="$1"
15   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
16                   "Content-Security-Policy: script-src 'none'" \
17                   ''
18   { printf '[html
19     [head
20       [meta name="viewport" content="width=device-width"]
21       [link rel="stylesheet" type="text/css" href="common.css"]
22       [link rel="stylesheet" type="text/css" href="webnote.css"]
23       [title Webnote]
24     ] [body class="%s"
25   ' "$page"
26   cat
27   printf '] ]'
28   } |"$_EXEC/cgilite/html-sh.sed" -u
29 }
30
31 case ${PATH_INFO##*/} in
32   favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
33   common.css) FILE "$_EXEC/cgilite/common.css";;
34   webnote.css) FILE "$_EXEC/webnote.css";;
35   '') yield_page <<-EOF
36         [form .new action=new [button type=submit New Note]
37           $(if [ "$(COOKIE pages)" ]; then
38             printf '[h2 Recent Pages][ul .recent'
39             { COOKIE pages; echo; } |tr \  \\n |while read page; do
40               [ "$(printf %s "$page" |checkid)" ] && printf '[li [a href="./%s" . %s]]' "$page" "$page"
41             done
42             fi)]
43         ]
44         EOF
45     return 0
46     ;;
47   new)
48     newid="$(timeid)"
49     touch "$_DATA/$newid"
50     git -C "$_DATA" add "$newid"
51     git -C "$_DATA" commit -m "new note \"${newid}\"" "$newid"
52     REDIRECT "./$newid"
53     ;;
54 esac
55
56 doc="$_DATA/$(printf %s ${PATH_INFO##*/} |checkid)"
57
58 if [ ! -f "$doc" ]; then
59   REDIRECT "${PATH_INFO%/*}/"
60   return 0
61 fi
62
63 [ $REQUEST_METHOD = POST ] && case $(POST action) in
64   edit)
65     if temp=$(SLOCK "$doc"); then
66       yield_page <<-EOF
67         [form method=POST
68           [input type=hidden name=session_key value="$SESSION_KEY"]
69           [button type=submit name=action value=cancel Cancel]
70           [button type=submit name=action value=update Update]
71           [textarea name=document . $(HTML <"$doc")]
72         ]
73         EOF
74     else
75       yield_page <<-EOF
76         [p .error .locked Someone else is already editing this Dokument. Wait a few minutes and try again. [a href="$PATH_INFO" Nothing else I can do.]]
77         EOF
78     fi
79     return 0
80     ;;
81   cancel)
82     RELEASE_SLOCK "$doc"
83     REDIRECT "$PATH_INFO"
84     ;;
85   update)
86     if temp=$(CHECK_SLOCK "$doc"); then
87       RELEASE_SLOCK "$doc"
88       POST document >"${doc}"
89       git -C "$_DATA" commit -m "update note ${doc##*/} \"$(sed '1{s;\r;;; q;}' "$doc")\"" "${doc##*/}" >/dev/null
90       REDIRECT "$PATH_INFO"
91     else
92       yield_page <<-EOF
93         [p .error .stolen Your edit took too long and someone else is now editing this file. [a href="$PATH_INFO" Dang, I must be quicker next time!]]
94         [p Copy your Content for reference:]
95         [div .text . $(POST document |HTML)]
96         EOF
97     fi
98     return 0
99     ;;
100 esac
101
102 COOKIE pages |tr \  \\n |grep -qF "${doc##*/}" \
103 || SET_COOKIE +$((90 * 86400)) pages="$(COOKIE pages && printf ' %s' "${doc##*/}" || printf '%s' "${doc##*/}")"
104
105 yield_page <<-EOF
106         [form method=POST
107           [input type=hidden name=session_key value="$SESSION_KEY"]
108           [button type=submit name=action value=edit Edit]
109         ]
110         [div .text . $(HTML <"$doc")]
111         EOF