3 . "${_EXEC}/cgilite/session.sh"
4 . "${_EXEC}/cgilite/storage.sh"
6 comments_file="${_DATA}/comments/${PATH_INFO}.db"
8 comments_postcomment() {
9 local cuid="$1" username="$2" text="$3"
10 local db="$comments_file"
12 mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
14 if grep -qE "^${cuid} " "$db"; then
16 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_EXISTS"
18 printf "%s %s %s %s %s\n" \
19 "$cuid" "$(STRING "$username")" "$SESSION_ID" "$_DATE" "$(STRING "$text")" \
22 REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
25 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
28 comments_updatecomment() {
29 local cuid="$1" updatekey="$2" username="$3" text="$4"
30 local db="$comments_file"
31 local ousername sid time otext
33 mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
35 read -r cuid ousername sid time otext <<-EOF
36 $(grep -E "^${cuid} " "$db")
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")" \
44 REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
47 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_DIVERGE"
50 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
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)";;
63 local db="$comments_file"
64 local edit="$(GET editcomment |checkid)"
65 local cuid username sid time text
67 printf '[section #comments'
68 [ -f "$db" ] && grep -qE "^${edit} [^ ]+ ${SESSION_ID}" "$db" \
71 [input type=checkbox #comments_toggle_new][label for="comments_toggle_new" Write a Comment]
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]
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
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)"
104 printf '[div .comment .deleted #comment_%s [h3 (deleted)]]' "$cuid"