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