#!/bin/sh _EXEC="${_EXEC:-${0%/*}/}" _DATA="${_DATA:-.}" . "$_EXEC"/cgilite/cgilite.sh . "$_EXEC"/cgilite/session.sh . "$_EXEC"/cgilite/file.sh . "$_EXEC"/session_lock.sh git init "$_DATA" >/dev/null & export MD_HTML="false" markdown() { local awkbin=`which awk` if [ "$awkbin" ]; then "$awkbin" -f "$_EXEC"/cgilite/markdown.awk else busybox awk -f "$_EXEC"/cgilite/markdown.awk fi } yield_page(){ title="${1:-WebTXT}" page="$2" printf '%s\r\n' 'Content-Type: text/html; charset=utf-8' \ "Content-Security-Policy: script-src 'none'" \ '' { printf '[html [head [meta name="viewport" content="width=device-width"] [link rel="stylesheet" type="text/css" href="common.css"] [link rel="stylesheet" type="text/css" href="webnote.css"] [title %s] ] [body class="%s" ' "$title" "$page" cat printf '] ]' } |"$_EXEC/cgilite/html-sh.sed" -u } pagename() { local name page="${1##*/}" name="$(sed -nE "/[a-zA-Z0-9].[^\r ]/{s;\r$;;; p; q;};" "$_DATA/$page")" [ "$name" ] && printf %s "$name" \ || printf %s "$page" } recent="$(COOKIE pages)" case ${PATH_INFO##*/} in favicon.ico) printf '%s\r\n' 'Content-Length: 0' '';; common.css) FILE "$_EXEC/cgilite/common.css";; webnote.css) FILE "$_EXEC/webnote.css";; '') yield_page <<-EOF [form .new action=new [button type=submit New Note] $(if [ "$recent" ]; then printf '[h2 Recent Pages][ul .recent' for page in $recent; do [ -f "$_DATA/$(printf %s "$page" |checkid)" ] \ && printf '[li [a href="./%s" . %s]]' "$page" "$(pagename "$page" |HTML)" done printf ']' fi) ] EOF return 0 ;; new) newid="$(timeid)" touch "$_DATA/$newid" git -C "$_DATA" add "$newid" >/dev/null git -C "$_DATA" commit -m "new note \"${newid}\"" "$newid" >/dev/null REDIRECT "./$newid" ;; esac doc="$_DATA/$(printf %s ${PATH_INFO##*/} |checkid)" if [ ! -f "$doc" ]; then REDIRECT "${PATH_INFO%/*}/" return 0 fi [ $REQUEST_METHOD = POST ] && case $(POST action) in edit) if temp=$(SLOCK "$doc"); then yield_page "$(pagename "$doc" |HTML)" <<-EOF [form method=POST [input type=hidden name=session_key value="$SESSION_KEY"] [button type=submit name=action value=cancel Cancel] [button type=submit name=action value=update Update] [textarea name=document . $(HTML <"$doc")] ] EOF else yield_page "$(pagename "$doc" |HTML) - Error" <<-EOF [p .error .locked Someone else is already editing this Dokument. Wait a few minutes and try again. [a href="$PATH_INFO" Nothing else I can do.]] EOF fi return 0 ;; cancel) RELEASE_SLOCK "$doc" REDIRECT "$PATH_INFO" ;; update) if temp=$(CHECK_SLOCK "$doc"); then RELEASE_SLOCK "$doc" POST document >"${doc}" git -C "$_DATA" commit -m "update note ${doc##*/} \"$(pagename "$doc")\"" "${doc##*/}" >/dev/null REDIRECT "$PATH_INFO" else yield_page "$(pagename "$doc" |HTML) - Error" <<-EOF [p .error .stolen Your edit took too long and someone else is now editing this file. [a href="$PATH_INFO" Dang, I must be quicker next time!]] [p Copy your Content for reference:] [div .text . $(POST document |HTML)] EOF fi return 0 ;; esac printf %s "$recent" |grep -qwF "${doc##*/}" \ || SET_COOKIE +$((90 * 86400)) pages="${recent}${recent+ }${doc##*/}" Path="${PATH_INFO%/*}/" yield_page "$(pagename "$doc" |HTML)" <<-EOF [form method=POST [input type=hidden name=session_key value="$SESSION_KEY"] [button type=submit name=action value=edit Edit] ] [div .text . $(markdown <"$doc")] EOF