]> git.plutz.net Git - webpoll/blob - comments.sh
universal comment function
[webpoll] / comments.sh
1 #!/bin/sh
2
3 . "${_EXEC}/cgilite/session.sh"
4 . "${_EXEC}/cgilite/storage.sh"
5
6 comments_file="${_DATA}/comments/${PATH_INFO}.db"
7
8 comments_postcomment() {
9   local cuid="$1" username="$2" text="$3"
10   local db="$comments_file"
11
12   mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
13   if LOCK "$db"; then
14     if grep -qE "^${cuid}       " "$db"; then
15       RELEASE "$db"
16       REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_EXISTS"
17     else
18       printf "%s        %s      %s      %s      %s\n" \
19              "$cuid" "$(STRING "$username")" "$SESSION_ID" "$_DATE" "$(STRING "$text")" \
20              >>"$db"
21       RELEASE "$db"
22       REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
23     fi
24   else
25     REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
26   fi
27 }
28 comments_updatecomment() {
29   local cuid="$1" updatekey="$2" username="$3" text="$4"
30   local db="$comments_file"
31   local ousername sid time otext
32
33   mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
34   if LOCK "$db"; then
35     read -r cuid ousername sid time otext <<-EOF
36         $(grep -E "^${cuid}     " "$db")
37         EOF
38     if [ "$sid" = "$SESSION_ID" -a "$(session_mac "${ousername}|${time}|${otext}")" = "$updatekey" ]; then
39       sed -Ei "/^${cuid}        /d" "$db"
40       printf "%s        %s      %s      %s      %s\n" \
41              "$cuid" "$(STRING "$username")" "$SESSION_ID" "${time%,*},$_DATE" "$(STRING "$text")" \
42              >>"$db"
43       RELEASE "$db"
44       REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
45     else
46       RELEASE "$db"
47       REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_DIVERGE"
48     fi
49   else
50     REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
51   fi
52   
53 }
54
55 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
56   postcomment)   comments_postcomment "$(POST cuid)" "$(POST username)" "$(POST text)";;
57   updatecomment) comments_updatecomment "$(POST cuid)" "$(POST updatekey)" "$(POST username)" "$(POST text)";;
58   cancelcommentpost) REDIRECT "${_BASE}${PATH_INFO}#comments";;
59   cancelcommentedit) REDIRECT "${_BASE}${PATH_INFO}#comment_$(POST cuid)";;
60 esac
61
62 w_comments() {
63   local db="$comments_file"
64   local edit="$(GET editcomment |checkid)"
65   local cuid username sid time text
66
67   printf '[section #comments'
68   [ -f "$db" ] && grep -qE "^${edit}    [^      ]+      ${SESSION_ID}" "$db" \
69   || cat <<-EOF
70           [h2 Comments]
71           [input type=checkbox #comments_toggle_new][label for="comments_toggle_new" Write a Comment]
72           [form method=POST
73             [hidden "cuid" "$(timeid)"]
74             [input name=username placeholder="Your Name" autocomplete=off]
75             [textarea name=text placeholder="Your Text"]
76             [submit "action" "cancelcommentpost" Cancel][submit "action" "postcomment" . Post Comment]
77           ]
78         EOF
79
80   [ -f "$db" ] && sort -r "$db" \
81   | while read -r cuid username sid time text; do
82     if [ "$edit" = "$cuid" -a "$sid" = "$SESSION_ID" ]; then
83       printf '
84         [form .comment .edit #comment_%s method=POST
85           [hidden "cuid" "%s"][hidden "updatekey" "%s"]
86           [input type=text name=username placeholder="Your Name" value="%s" autocomplete=off]
87           [textarea name=text placeholder="Your Text" . %s]
88           [submit "action" "cancelcommentedit" Cancel][submit "action" "updatecomment" . Update Comment]
89         ]' "$cuid" "$cuid" "$(session_mac "${username}|${time}|${text}")" \
90            "$(UNSTRING "$username" |HTML)" "$(UNSTRING "$text" |HTML)"
91     elif [ "$username" -a "$edit" = "$cuid" ]; then
92       printf '[div .comment #comment_%s [h3 . %s, %s:][span .error You cannot edit this comment][div . %s]]' \
93              "$cuid" "$(UNSTRING "$username" |HTML)" "$(date -d "@${time%%,*}")" \
94              "$(UNSTRING "$text" |markdown)"
95     elif [ "$username" -a "$sid" = "$SESSION_ID" ]; then
96       printf '[div .comment #comment_%s [h3 . %s, %s:][a href="?editcomment=%s#comment_%s" edit][div . %s]]' \
97              "$cuid" "$(UNSTRING "$username" |HTML)" "$(date -d "@${time%%,*}")" \
98              "$cuid" "$cuid" "$(UNSTRING "$text" |markdown)"
99     elif [ "$username" -a "$text" ]; then
100       printf '[div .comment #comment_%s [h3 . %s, %s:][div . %s]]' \
101              "$cuid" "$(UNSTRING "$username" |HTML)" "$(date -d "@${time%%,*}")" \
102              "$(UNSTRING "$text" |markdown)"
103     else
104       printf '[div .comment .deleted #comment_%s [h3 (deleted)]]' "$cuid"
105     fi
106   done
107   printf ']'
108 }