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