--- /dev/null
+#!/bin/sh
+
+. "${_EXEC}/cgilite/session.sh"
+. "${_EXEC}/cgilite/storage.sh"
+
+comments_file="${_DATA}/comments/${PATH_INFO}.db"
+
+comments_postcomment() {
+ local cuid="$1" username="$2" text="$3"
+ local db="$comments_file"
+
+ mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
+ if LOCK "$db"; then
+ if grep -qE "^${cuid} " "$db"; then
+ RELEASE "$db"
+ REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_EXISTS"
+ else
+ printf "%s %s %s %s %s\n" \
+ "$cuid" "$(STRING "$username")" "$SESSION_ID" "$_DATE" "$(STRING "$text")" \
+ >>"$db"
+ RELEASE "$db"
+ REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
+ fi
+ else
+ REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
+ fi
+}
+comments_updatecomment() {
+ local cuid="$1" updatekey="$2" username="$3" text="$4"
+ local db="$comments_file"
+ local ousername sid time otext
+
+ mkdir -p "${comments_file%/*}" || REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOCREAT"
+ if LOCK "$db"; then
+ read -r cuid ousername sid time otext <<-EOF
+ $(grep -E "^${cuid} " "$db")
+ EOF
+ if [ "$sid" = "$SESSION_ID" -a "$(session_mac "${ousername}|${time}|${otext}")" = "$updatekey" ]; then
+ sed -Ei "/^${cuid} /d" "$db"
+ printf "%s %s %s %s %s\n" \
+ "$cuid" "$(STRING "$username")" "$SESSION_ID" "${time%,*},$_DATE" "$(STRING "$text")" \
+ >>"$db"
+ RELEASE "$db"
+ REDIRECT "${_BASE}${PATH_INFO}#comment_${cuid}"
+ else
+ RELEASE "$db"
+ REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_DIVERGE"
+ fi
+ else
+ REDIRECT "${_BASE}${PATH_INFO}#ERROR_COMMENT_NOLOCK"
+ fi
+
+}
+
+[ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
+ postcomment) comments_postcomment "$(POST cuid)" "$(POST username)" "$(POST text)";;
+ updatecomment) comments_updatecomment "$(POST cuid)" "$(POST updatekey)" "$(POST username)" "$(POST text)";;
+ cancelcommentpost) REDIRECT "${_BASE}${PATH_INFO}#comments";;
+ cancelcommentedit) REDIRECT "${_BASE}${PATH_INFO}#comment_$(POST cuid)";;
+esac
+
+w_comments() {
+ local db="$comments_file"
+ local edit="$(GET editcomment |checkid)"
+ local cuid username sid time text
+
+ printf '[section #comments'
+ [ -f "$db" ] && grep -qE "^${edit} [^ ]+ ${SESSION_ID}" "$db" \
+ || cat <<-EOF
+ [h2 Comments]
+ [input type=checkbox #comments_toggle_new][label for="comments_toggle_new" Write a Comment]
+ [form method=POST
+ [hidden "cuid" "$(timeid)"]
+ [input name=username placeholder="Your Name" autocomplete=off]
+ [textarea name=text placeholder="Your Text"]
+ [submit "action" "cancelcommentpost" Cancel][submit "action" "postcomment" . Post Comment]
+ ]
+ EOF
+
+ [ -f "$db" ] && sort -r "$db" \
+ | while read -r cuid username sid time text; do
+ if [ "$edit" = "$cuid" -a "$sid" = "$SESSION_ID" ]; then
+ printf '
+ [form .comment .edit #comment_%s method=POST
+ [hidden "cuid" "%s"][hidden "updatekey" "%s"]
+ [input type=text name=username placeholder="Your Name" value="%s" autocomplete=off]
+ [textarea name=text placeholder="Your Text" . %s]
+ [submit "action" "cancelcommentedit" Cancel][submit "action" "updatecomment" . Update Comment]
+ ]' "$cuid" "$cuid" "$(session_mac "${username}|${time}|${text}")" \
+ "$(UNSTRING "$username" |HTML)" "$(UNSTRING "$text" |HTML)"
+ elif [ "$username" -a "$edit" = "$cuid" ]; then
+ printf '[div .comment #comment_%s [h3 . %s, %s:][span .error You cannot edit this comment][div . %s]]' \
+ "$cuid" "$(UNSTRING "$username" |HTML)" "$(date -d "@${time%%,*}")" \
+ "$(UNSTRING "$text" |markdown)"
+ elif [ "$username" -a "$sid" = "$SESSION_ID" ]; then
+ printf '[div .comment #comment_%s [h3 . %s, %s:][a href="?editcomment=%s#comment_%s" edit][div . %s]]' \
+ "$cuid" "$(UNSTRING "$username" |HTML)" "$(date -d "@${time%%,*}")" \
+ "$cuid" "$cuid" "$(UNSTRING "$text" |markdown)"
+ elif [ "$username" -a "$text" ]; then
+ printf '[div .comment #comment_%s [h3 . %s, %s:][div . %s]]' \
+ "$cuid" "$(UNSTRING "$username" |HTML)" "$(date -d "@${time%%,*}")" \
+ "$(UNSTRING "$text" |markdown)"
+ else
+ printf '[div .comment .deleted #comment_%s [h3 (deleted)]]' "$cuid"
+ fi
+ done
+ printf ']'
+}
line-height: .75em;
border: .125em solid;
}
+
+section#comments input#comments_toggle_new,
+section#comments input#comments_toggle_new + label + form {
+ display: none;
+ text-align: left;
+}
+section#comments input#comments_toggle_new:checked + label + form {
+ display: block;
+}
+section#comments input#comments_toggle_new + label {
+ display: block;
+ width: 100%; margin: 0;
+ padding: .25em .75em;
+ background-color: #FFF;
+ text-align: left;
+ border: .5pt solid;
+ border-radius: 2pt;
+}
+section#comments input#comments_toggle_new:checked + label {
+ display: none;
+}
+
+section#comments input[name=username],
+section#comments textarea {
+ width: 100%;
+}
+section#comments textarea {
+ min-height: 7em;
+ margin: .5em 0;
+}
+
+section#comments .comment {
+ text-align: left;
+ padding-left: .5em;
+ border-left: 3pt solid #CCC;
+}
+
+section#comments div.comment > h3 {
+ background-color: #CCC;
+ margin-left: -.5em;
+ padding-left: .5em;
+}