#!/bin/sh exec 2>>error.log . shcgi/cgilite.sh mkdir -p users #env >>debug printf 'POST: %s\n' "$cgilite_post" >>debug printf 'action: %s\n' "$(GET action)" >>debug wget="$(which wget)" wget(){ "$wget" -T 5 -t 1 -q -U '' $@; } checkid(){ grep -m 1 -xE '[0-9a-zA-Z:_]{12}'; } genid(){ # generate random ID head -c9 /dev/urandom \ | uuencode -m - \ | sed -n '2{y;+/;:_;;p}' } timeid(){ # generate time based ID d=$(date +%s) { printf $( while [ "$d" -gt 0 ]; do printf \\%o $((d % 256)) d=$((d / 256)) done ) | tac head -c5 /dev/urandom } \ | uuencode -m - \ | sed -n '2{y;+/;:_;;p}' } getFavicon(){ url="$1" bid="$2" prot=${url%%://*} domain="${url#*://}" domain="${domain%%/*}" ubase="${prot}://${domain}" file="${BDB}/favicons/${bid}.ico" mkdir -p "${BDB}/favicons/" && chmod a+rx "${BDB}/favicons/" favinfo="$( wget -O- "$url" \ | head -c4096 \ | sed -rn \ 's;^.*(<[Ll][Ii][Nn][Kk]( [^>]*)? [Rr][Re][Ll]='\''([Ss][Hh][Oo][Rr][Tt][Cc][Uu][Tt] )?[Ii][Cc][Oo][Nn]'\''[^>]*>).*$;\1;; s;^.*(<[Ll][Ii][Nn][Kk]( [^>]*)? [Rr][Re][Ll]="([Ss][Hh][Oo][Rr][Tt][Cc][Uu][Tt] )?[Ii][Cc][Oo][Nn]"[^>]*>).*$;\1;; tX; b; :X; s;^.*<([^>]+) [Hh][Rr][Ee][Ff]="([^"]+)".*$:\2;; s;^.*<([^>]+) [Hh][Rr][Ee][Ff]='\''([^'\'']+)'\''.*$:\2;; tY; b; :Y; p ' )" printf 'Shortcut icon for %s is %s\n' "$url" "$favinfo" >>debug [ -z "$favinfo" ] && favinfo="${ubase}/favicon.ico" case "$favinfo" in http://*|https://*|//*) wget -O "$file" "$favinfo" ;; /*) wget -O "$file" "${ubase}/${favinfo}" ;; *) wget -O "$file" "${url%/*}/${favinfo}" ;; esac [ -f "${file}.1" ] && mv "${file}.1" "$file" chmod a+r "$file" } QRYID="$(GET id |checkid)" COKID="$(COOKIE id |checkid)" BDB="users/${QRYID}" case "$(GET action)" in newid) NEWID="$(genid)" { git init "users/${NEWID}" || mkdir -p "users/${NEWID}"; } >&- printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL" printf 'Location: %s\r\n' "${SCRIPT_NAME}?id=${NEWID}" SET_COOKIE +8640000 "id=${NEWID}" printf '\r\n' exit 0 ;; newfolder) name="$(POST name |head -n1)" fid="$(timeid)" order="$( head -qn1 "${BDB}"/????????????.bm \ | cut -f3 \ | sort -n \ | tail -n1 \ || printf 1 )" order="$(((order + 1000) / 1000 * 1000))" if [ -n "$name" -a -d "${BDB}" ]; then printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${BDB}/${fid}.bm" fi REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}" ;; modfolder) name="$(POST name |head -n1)" fid="$(POST fid | checkid)" file="${BDB}/${fid}.bm" if [ "$(POST control)" = confirm -a -n "$name" -a -f "$file" ]; then order="$(head -n1 "$file" |cut -f3 || printf 1000)" printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${file%.bm}.tmp" tail -n+2 "$file" >>"${file%.bm}.tmp" mv "${file%.bm}.tmp" "$file" fi REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}" ;; newbookmark) fid="$(POST fid | checkid)" name="$(POST name |head -n1)" url="$(POST url |head -n1)" file="${BDB}/${fid}.bm" bid="$(timeid)" if [ -n "$name" -a -f "${file}" ]; then printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$(HTML "$url")" >>"${file}" fi getFavicon "$url" "$bid" REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}" ;; modbookmark) bid="$(POST bid | checkid)" name="$(POST name |head -n1)" url="$(POST url |head -n1)" file="$(grep -lE "^${bid}" "${BDB}"/????????????.bm)" if [ -w "$file" -a -n "$name" -a -n "$url" ]; then bm="$(printf '%s\t%s\t%s' "$bid" "$(HTML "$name")" "$(HTML "$url")" |sed -r 's;[\&\;];\\&;g;')" sed -ri "s;^${bid}\t.*$;${bm};" "$file" fi getFavicon "$url" "$bid" REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}" ;; esac if [ -z "$QRYID" -a -n "$COKID" ]; then REDIRECT "${SCRIPT_NAME}?id=${COKID}" elif [ -n "$QRYID" -a -z "$COKID" ]; then SET_COOKIE +8640000 "id=${QRYID}" fi if [ -z "$QRYID" -a -z "$COKID" ]; then printf 'Content-Type: text/html; charset=utf-8\r\n\r\n' cat <<-EOF