]> git.plutz.net Git - webpoll/blob - index.cgi
0694c6e4d9b5f8c9c9ea8fa3c744149d8a2ece86
[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 export MD_HTML="false"
15 if [ "$(which awk)" ]; then
16   markdown() { awk -f "$_EXEC/cgilite/markdown.awk"; }
17 else
18   markdown() { busybox awk -f "$_EXEC/cgilite/markdown.awk"; }
19 fi
20
21 PATH_INFO="$(PATH "/${PATH_INFO#${_BASE}}")"
22
23 #git init "$_DATA" >/dev/null &
24
25 yield_page(){
26   title="${1:-Webpoll}" page="$2"
27   printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \
28                   "Content-Security-Policy: script-src 'none'" \
29                   ''
30   { printf '[html
31     [head
32       [meta name="viewport" content="width=device-width"]
33       [link rel="stylesheet" type="text/css" href="%s/common.css"]
34       [link rel="stylesheet" type="text/css" href="%s/widgets.css"]
35       [link rel="stylesheet" type="text/css" href="%s/webpoll.css"]
36       [title %s]
37     ] [body class="%s"
38   ' "$_BASE" "$_BASE" "$_BASE" "$(HTML "$title")" "$page"
39   cat
40   printf '] ]'
41   } |"$_EXEC/cgilite/html-sh.sed" -u
42 }
43
44 pagename() {
45   local id="$1"
46   local file="$_DATA/$id"
47   if [ -f "$file" ]; then
48     DBM "$file" get title || printf 'Unnamed Page'
49   else
50     return 1;
51   fi
52 }
53
54 page_home() {
55   if [ "$REQUEST_METHOD" = POST ]; then
56     case $(POST start) in
57       date)
58         id="$(randomid)"
59         touch "$_DATA/$id"
60         REDIRECT "$_BASE/$id/newdate"
61         ;;
62       options)
63         id="$(randomid)"
64         touch "$_DATA/$id"
65         REDIRECT "$_BASE/$id/newoptions"
66         ;;
67       *) REDIRECT "$_BASE/";;
68     esac
69   else
70     recent="$(COOKIE pages)"
71     yield_page "Start a Poll" "home" <<-EOF
72         [form method=post
73           [submit "start" "date" Start a new poll]
74           $(if [ "$recent" ]; then
75             printf '[h2 Recent Polls][ul .recent'
76             for page in $recent; do
77               [ -f "$_DATA/$(checkid "$page")" ] \
78               && printf '[li [a href="./%s" . %s]]' "$page" "$(pagename "$page" |HTML)"
79             done
80             printf ']'
81           fi)
82         ]
83         EOF
84   fi
85 }
86 page_newdate() { . "$_EXEC"/newdate.sh; }
87 page_poll() { . "$_EXEC/poll.sh"; }
88
89 case ${PATH_INFO} in
90   /favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
91   /common.css) FILE "$_EXEC/cgilite/common.css";;
92   /widgets.css|/webpoll.css) FILE "${_EXEC}/${PATH_INFO}";;
93   /) page_home;;
94   /*/newdate) page_newdate;;
95   /*/newoptions);;
96   /[0-9a-zA-Z:=]???????????????) page_poll;;
97 esac
98
99 exit 0