X-Git-Url: http://git.plutz.net/?p=bookman;a=blobdiff_plain;f=index.cgi;h=8129b7e3434edcd0c26f70fcfa006f237e2bf49f;hp=a0a84906df8e5f443ed23c43b2836d8ce4b88cb2;hb=6994fcbc662fe3b8cae29d12dd57b633886afa85;hpb=8c4dbf46bd14b002a8ff840babfc7e8385401701;ds=sidebyside diff --git a/index.cgi b/index.cgi index a0a8490..8129b7e 100644 --- a/index.cgi +++ b/index.cgi @@ -1,31 +1,282 @@ -#!/bin/zsh -echo 'Content-type: text/html\n' +#!/bin/sh -cat < - - - Bookmarks! - - - - - -NOTES - -if [ -z "${REMOTE_USER}" ]; then - . ./error.sh -else - . ./bookmarks.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 + + + Bookman - New Collection + +

You have not yet set up a collection on this server.

+ Click here to start a new collection. + + EOF + exit 0 +elif ! [ -d "users/${QRYID}" ]; then + printf '%s 404 Not Found\r\n' "$SERVER_PROTOCOL" + printf 'Content-Type: text/html; charset=utf-8\r\n\r\n' + + cat <<-EOF + + + Bookman - 404 + +

The collection you requested does not exist on this server.

+ Click here to start a new collection. + + EOF + exit 0 fi -echo '' +list_bookmarks(){ + fid="$1" + bmodify="$(GET bmodify |checkid)" + + tail -n+2 "${BDB}/${fid}.bm" \ + | while read bid name url; do + if [ "${bid}" = "$bmodify" ]; then + cat <<-EOF +
+ + + + +
+ EOF + else + cat <<-EOF +
+ Modify + ${name} +
+ EOF + fi + done +} + +list_folders(){ + fmodify="$(GET fmodify |checkid )" + fdelete="$(GET fdelete |checkid )" + fmove="$(GET fmove |checkid )" + + head -qn1 "${BDB}"/????????????.bm \ + | sort -nk3 \ + | while read fid fname order; do + cat <<-EOF +
+

${fname}

+ Modify + $(list_bookmarks "$fid") +
+ + + + +
+
+ EOF + if [ "$fid" = "$fmodify" ]; then + cat <<-EOF +
+ + DeleteMove + + + +
+ EOF + elif [ "$fid" = "$fdelete" ]; then + cat <<-EOF +
+ + RenameMove + + + +
+ EOF + elif [ "$fid" = "$fmove" ]; then + cat <<-EOF +
+ + RenameDelete + + + +
+ EOF + fi + done +} + +printf 'Content-Type: text/html; charset=utf-8\r\n\r\n' +cat < + + Bookman - Your Collection + + + $(list_folders) +
+ + +
+ +EOF + +#set filetype=sh