]> git.plutz.net Git - webtxt/blob - index.cgi
fix in output syntax
[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   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             for page in $(COOKIE pages); do
40               [ -f "$_DATA/$(printf %s "$page" |checkid)" ] && printf '[li [a href="./%s" . %s]]' "$page" "$page"
41             done
42             printf ']'
43           fi)
44         ]
45         EOF
46     return 0
47     ;;
48   new)
49     newid="$(timeid)"
50     touch "$_DATA/$newid"
51     git -C "$_DATA" add "$newid" >/dev/null
52     git -C "$_DATA" commit -m "new note \"${newid}\"" "$newid" >/dev/null
53     REDIRECT "./$newid"
54     ;;
55 esac
56
57 doc="$_DATA/$(printf %s ${PATH_INFO##*/} |checkid)"
58
59 if [ ! -f "$doc" ]; then
60   REDIRECT "${PATH_INFO%/*}/"
61   return 0
62 fi
63
64 [ $REQUEST_METHOD = POST ] && case $(POST action) in
65   edit)
66     if temp=$(SLOCK "$doc"); then
67       yield_page <<-EOF
68         [form method=POST
69           [input type=hidden name=session_key value="$SESSION_KEY"]
70           [button type=submit name=action value=cancel Cancel]
71           [button type=submit name=action value=update Update]
72           [textarea name=document . $(HTML <"$doc")]
73         ]
74         EOF
75     else
76       yield_page <<-EOF
77         [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.]]
78         EOF
79     fi
80     return 0
81     ;;
82   cancel)
83     RELEASE_SLOCK "$doc"
84     REDIRECT "$PATH_INFO"
85     ;;
86   update)
87     if temp=$(CHECK_SLOCK "$doc"); then
88       RELEASE_SLOCK "$doc"
89       POST document >"${doc}"
90       git -C "$_DATA" commit -m "update note ${doc##*/} \"$(sed '1{s;\r;;; q;}' "$doc")\"" "${doc##*/}" >/dev/null
91       REDIRECT "$PATH_INFO"
92     else
93       yield_page <<-EOF
94         [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!]]
95         [p Copy your Content for reference:]
96         [div .text . $(POST document |HTML)]
97         EOF
98     fi
99     return 0
100     ;;
101 esac
102
103 COOKIE pages |tr \  \\n |grep -qF "${doc##*/}" \
104 || SET_COOKIE +$((90 * 86400)) pages="$(COOKIE pages && printf ' %s' "${doc##*/}" || printf '%s' "${doc##*/}")" Path="${PATH_INFO%/*}/"
105
106 yield_page <<-EOF
107         [form method=POST
108           [input type=hidden name=session_key value="$SESSION_KEY"]
109           [button type=submit name=action value=edit Edit]
110         ]
111         [div .text . $(HTML <"$doc")]
112         EOF