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