]> git.plutz.net Git - webpoll/blobdiff - index.cgi
Merge commit '752db311013e53c3c16f685f86b507e191b2239c'
[webpoll] / index.cgi
index 48c9a79e6fb0f5850afce0adff6109d41c5707d9..d1469cdbfcbb446e1bdf0ed25539986afc99f399 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -2,24 +2,18 @@
 
 _EXEC="${_EXEC:-${0%/*}/}"
 _DATA="${_DATA:-.}"
+_BASE="${_BASE%/}"
 
 . "$_EXEC"/cgilite/cgilite.sh
-#. "$_EXEC"/cgilite/session.sh
+. "$_EXEC"/cgilite/session.sh
 . "$_EXEC"/cgilite/file.sh
+. "$_EXEC"/cgilite/storage.sh
 #. "$_EXEC"/session_lock.sh
+. "$_EXEC"/widgets.sh
 
-#git init "$_DATA" >/dev/null &
+PATH_INFO="$(PATH "/${PATH_INFO#${_BASE}}")"
 
-checked(){
-  if [ "$1" = "$2" ] || [ "$1" -eq "$2" ]; then
-    printf 'checked="checked"'
-  fi 2>/dev/null
-}
-selected(){
-  if [ "$1" = "$2" ] || [ "$1" -eq "$2" ]; then
-    printf 'selected="selected"'
-  fi 2>/dev/null
-}
+#git init "$_DATA" >/dev/null &
 
 yield_page(){
   title="${1:-Webpoll}" page="$2"
@@ -29,70 +23,115 @@ yield_page(){
   { printf '[html
     [head
       [meta name="viewport" content="width=device-width"]
-      [link rel="stylesheet" type="text/css" href="common.css"]
+      [link rel="stylesheet" type="text/css" href="%s/common.css"]
+      [link rel="stylesheet" type="text/css" href="%s/widgets.css"]
+      [link rel="stylesheet" type="text/css" href="%s/webpoll.css"]
       [title %s]
     ] [body class="%s"
-  ' "$title" "$page"
+  ' "$_BASE" "$_BASE" "$_BASE" "$title" "$page"
   cat
   printf '] ]'
   } |"$_EXEC/cgilite/html-sh.sed" -u
 }
 
-w_month() {
-  local month="$1" selected="$2"
-  local dow dom days n=1 Y m d V w B
-  if [ $month ]; then
-    read Y m d V w B<<-EOF
-       $(date -d "${month}-01" +"%Y %m %d %V %w %B")
-       EOF
+pagename() {
+  local id="$1"
+  local file="$_DATA/$id"
+  if [ -f "$file" ]; then
+    DBM "$file" get title || printf 'Unnamed Page'
   else
-    read Y m d V w <<-EOF
-       $(date +"%Y %m %d %V %w")
-       EOF
-    month="$Y-$m"
-    V="$((V - d / 7))"
-    [ $V -lt 1 ] && V=$((V + 53))
+    return 1;
   fi
+}
 
-  case $m in
-    0[13578]|10|12)
-      days="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31";;
-    0[469]|11)
-      days="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30";;
-    02) if [ $(( Y / 400 )) = 0 ]; then
-        days="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29";
-      elif [ $(( Y / 100 )) = 0 ]; then
-        days="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28";
-      elif [ $(( Y /   4 )) = 0 ]; then
-        days="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29";
-      else
-        days="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28";
-      fi;;
-  esac
+[ "$REQUEST_METHOD" = POST ] && case ${PATH_INFO} in
+  /)
+    case $(POST start) in
+      date)
+        id="$(randomid)"
+        touch "$_DATA/$id"
+        REDIRECT "$_BASE/$id/newdate"
+        ;;
+      options)
+        id="$(randomid)"
+        touch "$_DATA/$id"
+        REDIRECT "$_BASE/$id/newoptions"
+        ;;
+      *) REDIRECT "$_BASE/";;
+      esac
+    ;;
+  /*/newdate)
+    id="${PATH_INFO%/newdate}"; id="${id#/}"
+    file="$_DATA/$id"
+    month="$(POST month |grep -m1 -xE '[0-9]{4}-(0[1-9]|1[012])')"
+    DBM "$file" set title "$(POST title)"
+    DBM "$file" set description "$(POST description)"
+    DBM "$file" set dates "$(
+      for date in $(seq 1 $(POST_COUNT date)); do
+        POST date "$date"
+        printf \\n
+      done |sort
+    )"
+    if [ "$(POST cancel)" = cancel ]; then
+      rm -- "$file"
+      REDIRECT "$_BASE/"
+    elif [ "$month" ]; then
+      REDIRECT "$_BASE$PATH_INFO?month=$month"
+    else
+      REDIRECT "$_BASE$PATH_INFO"
+    fi
+    ;;
+esac
 
-  printf '[table .calendar month=%s [thead
-            [tr .monthname [th colspan=8 . %s]]
-            [tr   .weekday [th][th . %s][th . %s][th . %s][th . %s][th . %s][th . %s][th . %s]]
-          ][tbody
-         ' "$month" "$B $Y" Mo Di Mi Do Fr Sa So
-  for dom in $days; do
-    dow=$(( (w - d + 35 + dom ) % 7))
-    [ $dow = 1 -o $dom = 1 ] && printf '[tr [th .weekno . %i]' $V
-    [ $dom = 1 ] && while [ $n -lt $(( ($dow + 6) % 7 + 1)) ]; do printf '[td ]'; n=$((n + 1)); done
-    printf '[td [label [input type=radio name=date value="%04i-%02i-%02i" %s] %i]]' $Y $m $dom "$(checked $dom $selected)" $dom
-    [ $dow = 0 ] && printf ']\n'
-    [ $dow = 0 ] && V=$(( V % 53 + 1))
-  done
-  printf ']]'
-}
 
-case ${PATH_INFO##*/} in
-  favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
-  common.css) FILE "$_EXEC/cgilite/common.css";;
-  '') yield_page <<-EOF
-       $(w_month 2019-12 24)
-       $(w_month 2020-01)
-       $(w_month 2020-02)
+case ${PATH_INFO} in
+  /favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';;
+  /common.css) FILE "$_EXEC/cgilite/common.css";;
+  /widgets.css|/webpoll.css) FILE "${_EXEC}/${PATH_INFO}";;
+  /)  recent="$(COOKIE pages)"
+      yield_page "Start a Poll" "home" <<-EOF
+       [form method=post
+         [submit "start" "date" Start a new poll]
+         $(if [ "$recent" ]; then
+           printf '[h2 Recent Polls][ul .recent'
+           for page in $recent; do
+             [ -f "$_DATA/$(printf %s "$page" |checkid)" ] \
+             && printf '[li [a href="./%s" . %s]]' "$page" "$(pagename "$page" |HTML)"
+           done
+           printf ']'
+         fi)
+       ]
+       EOF
+    ;;
+  /*/newdate)
+    id="${PATH_INFO%/newdate}"; id="${id#/}"
+    file="$_DATA/$id"
+    month="$(GET month |grep -m1 -xE '[0-9]{4}-(0[1-9]|1[012])' || date +%Y-%m)"
+    Y="${month%-*}"; m="${month#*-}"; Y=${Y#0}; m=${m#0};
+    [ "$m" = 1 ] && prev=$(printf '%04i-%02i' $((Y - 1)) 12) || prev=$(printf '%04i-%02i' $Y $((m - 1)))
+    [ "$m" = 12 ] && next=$(printf '%04i-%02i' $((Y + 1)) 01) || next=$(printf '%04i-%02i' $Y $((m + 1)))
+    dates="$(DBM "$file" get dates)"
+    days="$(printf %s "$dates" |sed -E "/^${month}-/!d; s;^.*-([0-9]{2})$;\1;g")"
+    additional="$(printf %s "$dates" |sed -E "/^${month}-/d;")"
+    yield_page "$(pagename "$id")" "newdate" <<-EOF
+       [form method=post
+         [input name=title value="$(DBM "$file" get title |HTML)" placeholder="Title"]
+         [textarea name=description placeholder="Description" . $(DBM "$file" get description |HTML)]
+         [submit "month" "$prev" Previous Month]
+         $(w_month multiple date "$month" $days)
+         [submit "month" "$next" Next Month]
+         $(printf '[hidden "date" "%s"]' $additional)
+         [submit "cancel" "cancel" Cancel]
+         [submit "post" "post" Post Event]
+       ]
+       EOF
+    ;;
+  /*/newoptions);;
+  *);;
+  /) yield_page <<-EOF
+       $(w_month none date 2019-12 24 25 26)
+       $(w_month select date 2020-01)
+       $(w_month multiple date 2020-02)
        EOF
     return 0
     ;;