]> git.plutz.net Git - webtxt/blob - index.cgi
da1814251b310825d1db2205c400ac63ccc63de6
[webtxt] / index.cgi
1 #!/bin/sh
2
3 _EXEC="${_EXEC:-.}"
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 yield_page(){
12   page="$1"
13   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
14                   "Content-Security-Policy: script-src 'none'" \
15                   ''
16   { printf '[html
17     [head
18       [meta name="viewport" content="width=device-width"]
19       [link rel="stylesheet" type="text/css" href="common.css"]
20       [link rel="stylesheet" type="text/css" href="webnote.css"]
21       [title Webnote]
22     ] [body class="%s"
23   ' "$page"
24   cat
25   printf '] ]'
26   } |"$_EXEC/cgilite/html-sh.sed" -u
27 }
28
29 case ${PATH_INFO##*/} in
30   favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
31   common.css) FILE "$_EXEC/cgilite/common.css";;
32   webnote.css) FILE "$_EXEC/webnote.css";;
33   '') yield_page <<-EOF
34         [form .new action=new [button type=submit New Note]
35           [ul .recent
36           $({ COOKIE pages; echo; } |tr \  \\n |while read page; do
37             [ "$(printf %s "$page" |checkid)" ] && printf '[li [a href="./%s" . %s]]' "$page" "$page"
38           done)]
39         ]
40         EOF
41     return 0
42     ;;
43   new)
44     newid="$(timeid)"
45     touch "$_DATA/$newid"
46     REDIRECT "./$newid"
47     ;;
48 esac
49
50 doc="$_DATA/$(printf %s ${PATH_INFO##*/} |checkid)"
51
52 if [ ! -f "$doc" ]; then
53   REDIRECT "${PATH_INFO%%/*}/"
54   return 0
55 fi
56
57 [ $REQUEST_METHOD = POST ] && case $(POST action) in
58   edit)
59     if temp=$(SLOCK "$doc"); then
60       yield_page <<-EOF
61         [form method=POST
62           [input type=hidden name=session_key value="$SESSION_KEY"]
63           [button type=submit name=action value=cancel Cancel]
64           [button type=submit name=action value=update Update]
65           [textarea name=document . $(HTML <"$doc")]
66         ]
67         EOF
68     else
69       yield_page <<-EOF
70         [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.]]
71         EOF
72     fi
73     return 0
74     ;;
75   cancel)
76     RELEASE_SLOCK "$doc"
77     REDIRECT "$PATH_INFO"
78     ;;
79   update)
80     if temp=$(CHECK_SLOCK "$doc"); then
81       RELEASE_SLOCK "$doc"
82       POST document >"${doc}"
83       REDIRECT "$PATH_INFO"
84     else
85       yield_page <<-EOF
86         [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!]]
87         [p Copy your Content for reference:]
88         [div .text . $(POST document |HTML)]
89         EOF
90     fi
91     return 0
92     ;;
93 esac
94
95 COOKIE pages |tr \  \\n |grep -qF "${doc##*/}" \
96 || SET_COOKIE +$((90 * 86400)) pages="$(COOKIE pages && printf ' %s' "${doc##*/}" || printf '%s' "${doc##*/}")"
97
98 yield_page <<-EOF
99         [form method=POST
100           [input type=hidden name=session_key value="$SESSION_KEY"]
101           [button type=submit name=action value=edit Edit]
102         ]
103         [div .text . $(HTML <"$doc")]
104         EOF