]> git.plutz.net Git - webpoll/blob - index.cgi
ignore serverkey and poll files
[webpoll] / index.cgi
1 #!/bin/sh
2
3 _EXEC="${_EXEC:-${0%/*}/}"
4 _DATA="${_DATA:-.}"
5 _BASE="${_BASE%/}"
6
7 . "$_EXEC"/cgilite/cgilite.sh
8 . "$_EXEC"/cgilite/session.sh
9 . "$_EXEC"/cgilite/file.sh
10 . "$_EXEC"/cgilite/storage.sh
11 #. "$_EXEC"/session_lock.sh
12 . "$_EXEC"/widgets.sh
13
14 PATH_INFO="$(PATH "/${PATH_INFO#${_BASE}}")"
15
16 #git init "$_DATA" >/dev/null &
17
18 yield_page(){
19   title="${1:-Webpoll}" page="$2"
20   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
21                   "Content-Security-Policy: script-src 'none'" \
22                   ''
23   { printf '[html
24     [head
25       [meta name="viewport" content="width=device-width"]
26       [link rel="stylesheet" type="text/css" href="%s/common.css"]
27       [link rel="stylesheet" type="text/css" href="%s/widgets.css"]
28       [link rel="stylesheet" type="text/css" href="%s/webpoll.css"]
29       [title %s]
30     ] [body class="%s"
31   ' "$_BASE" "$_BASE" "$_BASE" "$title" "$page"
32   cat
33   printf '] ]'
34   } |"$_EXEC/cgilite/html-sh.sed" -u
35 }
36
37 pagename() {
38   local id="$1"
39   local file="$_DATA/$id"
40   if [ -f "$file" ]; then
41     DBM "$file" get title || printf 'Unnamed Page'
42   else
43     return 1;
44   fi
45 }
46
47 [ "$REQUEST_METHOD" = POST ] && case ${PATH_INFO} in
48   /)
49     case $(POST start) in
50       date)
51         id="$(randomid)"
52         touch "$_DATA/$id"
53         REDIRECT "$_BASE/$id/newdate"
54         ;;
55       options)
56         id="$(randomid)"
57         touch "$_DATA/$id"
58         REDIRECT "$_BASE/$id/newoptions"
59         ;;
60       *) REDIRECT "$_BASE/";;
61       esac
62     ;;
63   /*/newdate)
64     id="${PATH_INFO%/newdate}"; id="${id#/}"
65     file="$_DATA/$id"
66     month="$(POST month |grep -m 1 -xE '[0-9]{4}-(0[1-9]|1[012])')"
67     todremove="$(POST todremove |grep -m 1 -xE '[0-9]+')"
68     DBM "$file" set title "$(POST title)"
69     DBM "$file" set description "$(POST description)"
70     DBM "$file" set dates "$(
71       for date in $(seq 1 $(POST_COUNT date)); do
72         POST date "$date"
73         printf \\n
74       done \
75       | grep -xE '^[0-9]{4}-((01|03|05|07|08|10|12)-([012][0-9]|3[01])|(04|06|09|11)-([012][0-9]|30)|02-[012][0-9])$' \
76       | sort -u
77     )"
78     DBM "$file" set todall "$(
79       for todcount in $(seq 1 $(POST_COUNT todstart)); do
80         [ "$todremove" -eq "$todcount" ] 2>&- && continue;
81         todstart="$(POST todstart "$todcount")"
82         todend="$(POST todend "$todcount")"
83         [ "${todstart%:??}" -lt "${todend%:??}" -o "${todstart%:??}" -eq "${todend%:??}" -a "${todstart#*:}" -lt "${todend#*:}" ] \
84         2>&- \
85         && { printf '%02i:%02i-%02i:%02i\n' "${todstart%:??}" "${todstart#*:}" "${todend%:??}" "${todend#*:}"; }\
86         || { [ "${todstart%:??}" -ge 0 -a  "${todstart#*:}" -ge 0 ] 2>&- && printf '%02i:%02i-\n' "${todstart%:??}" "${todstart#*:}"; }
87       done |grep -xE '^([01][0-9]|2[0-3]):([0-5][0-9])-(([01][0-9]|2[0-3]):([0-5][0-9]))?$' |sort -u
88     )"
89     if [ "$(POST cancel)" = cancel ]; then
90       rm -- "$file"
91       REDIRECT "$_BASE/"
92     elif [ "$(POST addtime)" = global ]; then
93       DBM "$file" append todall "${BR}-"
94       REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}"
95     elif [ "$month" ]; then
96       REDIRECT "$_BASE$PATH_INFO?month=$month"
97     else
98       REDIRECT "$_BASE$PATH_INFO"
99     fi
100     ;;
101 esac
102
103
104 case ${PATH_INFO} in
105   /favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
106   /common.css) FILE "$_EXEC/cgilite/common.css";;
107   /widgets.css|/webpoll.css) FILE "${_EXEC}/${PATH_INFO}";;
108   /)  recent="$(COOKIE pages)"
109       yield_page "Start a Poll" "home" <<-EOF
110         [form method=post
111           [submit "start" "date" Start a new poll]
112           $(if [ "$recent" ]; then
113             printf '[h2 Recent Polls][ul .recent'
114             for page in $recent; do
115               [ -f "$_DATA/$(checkid "$page")" ] \
116               && printf '[li [a href="./%s" . %s]]' "$page" "$(pagename "$page" |HTML)"
117             done
118             printf ']'
119           fi)
120         ]
121         EOF
122     ;;
123   /*/newdate)
124     id="${PATH_INFO%/newdate}"; id="${id#/}"
125     file="$_DATA/$id"
126     month="$(GET month |grep -m1 -xE '[0-9]{4}-(0[1-9]|1[012])' || date +%Y-%m)"
127     Y="${month%-*}"; m="${month#*-}"; Y=${Y#0}; m=${m#0};
128     [ "$m" = 1 ] && prev=$(printf '%04i-%02i' $((Y - 1)) 12) || prev=$(printf '%04i-%02i' $Y $((m - 1)))
129     [ "$m" = 12 ] && next=$(printf '%04i-%02i' $((Y + 1)) 01) || next=$(printf '%04i-%02i' $Y $((m + 1)))
130     dates="$(DBM "$file" get dates)"
131     days="$(printf %s "$dates" |sed -E "/^${month}-/!d; s;^.*-([0-9]{2})$;\1;g")"
132     additional="$(printf %s "$dates" |sed -E "/^${month}-/d;")"
133     todall="$(DBM "$file" get todall)"
134
135     yield_page "$(pagename "$id")" "newdate" <<-EOF
136         $(dlist_timeofday)
137         [form method=post
138           [input name=title value="$(DBM "$file" get title |HTML)" placeholder="Title"]
139           [textarea name=description placeholder="Description" . $(DBM "$file" get description |HTML)]
140           [fieldset .date
141             $(printf '[hidden "date" "%s"]' $additional)
142             [submit "month" "$prev" Previous Month]
143             $(w_month multiple date "$month" $days)
144             [submit "month" "$next" Next Month]
145             [hidden "month" "$month"]
146           ]
147           [fieldset .timeofday
148             [label .todstart Start Time (optional):
149             ]
150             [label .todend   End Time (optional):
151             ]
152             $(c=0; for time in ${todall:--}; do
153               c=$((c + 1))
154               printf '
155                 <input name="todstart" value="%s" placeholder="HH:MM" list="dlist_timeofday"
156                        pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
157                 <input name="todend"   value="%s" placeholder="HH:MM"   list="dlist_timeofday"
158                        pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
159                 [submit "todremove" "%i" -]
160               ' "${time%-*}" "${time#*-}" "${c}"
161             done)
162             [submit "addtime" "global" + Add time option]
163           ]
164           [submit "cancel" "cancel" Cancel]
165           [submit "post" "post" Post Event]
166         ]
167         EOF
168     ;;
169   /*/newoptions);;
170   *);;
171 esac
172