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 [ ! "$cuid" -o ! "$username" -o ! "$text" ] \
13 && REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_INVALID"
15 mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
17 if grep -qE "^${cuid} " "$db"; then
19 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_EXISTS"
21 printf "%s %s %s %s %s\n" \
22 "$cuid" "$(STRING "$username")" "$SESSION_ID" "$_DATE" "$(STRING "$text")" \
25 REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
28 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
31 comments_updatecomment() {
32 local cuid="$1" updatekey="$2" username="$3" text="$4"
33 local db="$comments_file"
34 local ousername sid time otext
36 [ ! "$cuid" -o ! "$username" -o ! "$text" ] \
37 && REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_INVALID"
39 mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
41 read -r cuid ousername sid time otext <<-EOF
42 $(grep -E "^${cuid} " "$db")
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")" \
50 REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
53 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_DIVERGE"
56 REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
61 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
62 postcomment) comments_postcomment \
63 "$(POST cuid |checkid)" \
64 "$(POST username |grep -m1 -oE '[^[:space:]].*[^[:space:]]')" \
66 updatecomment) comments_updatecomment \
67 "$(POST cuid |checkid)" "$(POST updatekey)" \
68 "$(POST username |grep -m1 -oE '[^[:space:]].*[^[:space:]]')" \
70 cancelcommentpost) REDIRECT "${_BASE}${PATH_INFO}#comments";;
71 cancelcommentedit) REDIRECT "${_BASE}${PATH_INFO}#comment_$(POST cuid |checkid)";;
75 local db="$comments_file"
76 local edit="$(GET editcomment |checkid)"
77 local cuid username sid time text
79 printf '[section #comments'
80 [ -f "$db" ] && grep -qE "^${edit} [^ ]+ ${SESSION_ID}" "$db" \
83 [input type=checkbox #comments_toggle_new][label for="comments_toggle_new" Write a Comment]
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]
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
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)"
115 printf '[div .comment .deleted #comment_%s [h3 (deleted)]]' "$cuid"