]> git.plutz.net Git - webtxt/blob - index.cgi
Merge commit '04f6da6f71cf26025036c5cb4d561f1414fc82c9'
[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" >/dev/null &
12
13 yield_page(){
14   title="${1:-WebTXT}" page="$2"
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 %s]
24     ] [body class="%s"
25   ' "$title" "$page"
26   cat
27   printf '] ]'
28   } |"$_EXEC/cgilite/html-sh.sed" -u
29 }
30
31 pagename() {
32   local name page="${1##*/}"
33   name="$(sed -nE "/[a-zA-Z0-9].[^\r    ]/{s;\r$;;; p; q;};" "$_DATA/$page")"
34   [ "$name" ] && printf %s "$name" \
35               || printf %s "$page"
36 }
37
38 recent="$(COOKIE pages)"
39
40 case ${PATH_INFO##*/} in
41   favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
42   common.css) FILE "$_EXEC/cgilite/common.css";;
43   webnote.css) FILE "$_EXEC/webnote.css";;
44   '') yield_page <<-EOF
45         [form .new action=new [button type=submit New Note]
46           $(if [ "$recent" ]; then
47             printf '[h2 Recent Pages][ul .recent'
48             for page in $recent; do
49               [ -f "$_DATA/$(printf %s "$page" |checkid)" ] \
50               && printf '[li [a href="./%s" . %s]]' "$page" "$(pagename "$page" |HTML)"
51             done
52             printf ']'
53           fi)
54         ]
55         EOF
56     return 0
57     ;;
58   new)
59     newid="$(timeid)"
60     touch "$_DATA/$newid"
61     git -C "$_DATA" add "$newid" >/dev/null
62     git -C "$_DATA" commit -m "new note \"${newid}\"" "$newid" >/dev/null
63     REDIRECT "./$newid"
64     ;;
65 esac
66
67 doc="$_DATA/$(printf %s ${PATH_INFO##*/} |checkid)"
68
69 if [ ! -f "$doc" ]; then
70   REDIRECT "${PATH_INFO%/*}/"
71   return 0
72 fi
73
74 [ $REQUEST_METHOD = POST ] && case $(POST action) in
75   edit)
76     if temp=$(SLOCK "$doc"); then
77       yield_page "$(pagename "$doc" |HTML)" <<-EOF
78         [form method=POST
79           [input type=hidden name=session_key value="$SESSION_KEY"]
80           [button type=submit name=action value=cancel Cancel]
81           [button type=submit name=action value=update Update]
82           [textarea name=document . $(HTML <"$doc")]
83         ]
84         EOF
85     else
86       yield_page "$(pagename "$doc" |HTML) - Error" <<-EOF
87         [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.]]
88         EOF
89     fi
90     return 0
91     ;;
92   cancel)
93     RELEASE_SLOCK "$doc"
94     REDIRECT "$PATH_INFO"
95     ;;
96   update)
97     if temp=$(CHECK_SLOCK "$doc"); then
98       RELEASE_SLOCK "$doc"
99       POST document >"${doc}"
100       git -C "$_DATA" commit -m "update note ${doc##*/} \"$(pagename "$doc")\"" "${doc##*/}" >/dev/null
101       REDIRECT "$PATH_INFO"
102     else
103       yield_page "$(pagename "$doc" |HTML) - Error" <<-EOF
104         [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!]]
105         [p Copy your Content for reference:]
106         [div .text . $(POST document |HTML)]
107         EOF
108     fi
109     return 0
110     ;;
111 esac
112
113 printf %s "$recent" |grep -qwF "${doc##*/}" \
114 || SET_COOKIE +$((90 * 86400)) pages="${recent}${recent+ }${doc##*/}" Path="${PATH_INFO%/*}/"
115
116 yield_page "$(pagename "$doc" |HTML)" <<-EOF
117         [form method=POST
118           [input type=hidden name=session_key value="$SESSION_KEY"]
119           [button type=submit name=action value=edit Edit]
120         ]
121         [div .text . $(markdown <"$doc")]
122         EOF