X-Git-Url: http://git.plutz.net/?p=bookman;a=blobdiff_plain;f=index.cgi;h=4f24149bd8f53024b5903c7e4a1ed37196185b22;hp=8129b7e3434edcd0c26f70fcfa006f237e2bf49f;hb=104596e3d1aa06eb5cb6108b477e2b379b202537;hpb=6994fcbc662fe3b8cae29d12dd57b633886afa85;ds=sidebyside
diff --git a/index.cgi b/index.cgi
old mode 100644
new mode 100755
index 8129b7e..4f24149
--- a/index.cgi
+++ b/index.cgi
@@ -4,9 +4,8 @@ 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
+# 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 '' $@; }
@@ -58,7 +57,7 @@ getFavicon(){
'
)"
- printf 'Shortcut icon for %s is %s\n' "$url" "$favinfo" >>debug
+ # 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"
@@ -72,9 +71,60 @@ getFavicon(){
chmod a+r "$file"
}
-QRYID="$(GET id |checkid)"
+list_folders(){
+ head -qn1 "${BDB}"/????????????.bm \
+ | sort -nk3 \
+ | cut -f1,2
+}
+
+order_files(){
+ n=1000
+ list_folders \
+ | while read fid nan; do
+ file="${BDB}/${fid}.bm"
+ sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1${n}\3;;" "$file"
+ n=$((n + 1000))
+ done
+}
+
COKID="$(COOKIE id |checkid)"
-BDB="users/${QRYID}"
+QRYID="$(GET id |checkid)"
+BDB="users/${COKID}"
+
+if [ -n "$QRYID" ]; then
+ printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL"
+ printf 'Location: %s\r\n' "${SCRIPT_NAME}?${QUERY_STRING#id=*&}"
+ SET_COOKIE +8640000 "id=${QRYID}"
+ printf '\r\n'
+ exit 0
+elif [ -z "$COKID" -a -z "$QRYID" ]; 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 "${BDB}" ]; 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
case "$(GET action)" in
newid)
@@ -101,20 +151,50 @@ case "$(GET action)" in
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"
+ git -C "${BDB}" add "${fid}.bm" >&-
+ git -C "${BDB}" commit -m "New bookmark folder: ${name} (${fid})" -- "${fid}.bm" >&-
fi
- REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
+ REDIRECT "${SCRIPT_NAME}#${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"
+ sed -ri "1s'^(${fid}\t)[^\t]+(\t.+)$'\1${name}\2';" "$file"
+ git -C "${BDB}" add "${fid}.bm" >&-
+ git -C "${BDB}" commit -m "Renamed folder ${fid} to '${name}'" -- "${fid}.bm" >&-
+ fi
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ delfolder)
+ fid="$(POST fid | checkid)"
+ target="$(POST target | checkid)"
+ file="${BDB}/${fid}.bm"
+ tfile="${BDB}/${target}.bm"
+ if [ "$(POST control)" = confirm -a -f "$file" ]; then
+ if [ "$target" = "____________" ] || tail -n+2 "$file" >>"$tfile"; then
+ rm -f "$file" >&-
+ git -C "${BDB}" commit -m "Deleted folder ${fid}" -- "${fid}.bm" "${target}.bm" >&-
+ fi
fi
- REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
+ REDIRECT "${SCRIPT_NAME}#${target}"
+ ;;
+ movefolder)
+ fid="$(POST fid | checkid)"
+ target="$(POST target | checkid)"
+ file="${BDB}/${fid}.bm"
+ tfile="${BDB}/${target}.bm"
+ if [ "$target" = "____________" -a -f "$file" -a "$(POST control)" = confirm ]; then
+ read nan1 nan2 last nan3 <"${BDB}/$(list_folders |tail -n1 |cut -f1).bm"
+ sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${last:-0} + 1000))\3;;" "$file"
+ elif [ -f "$tfile" -a -f "$file" -a "$(POST control)" = confirm ]; then
+ read nan1 nan2 tid nan3 <"$tfile"
+ sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${tid:-1} -1))\2;;" "$file"
+ order_files
+ fi
+ git -C "${BDB}" commit -a -m "Modified folder order (moved ${fid})" >&-
+ REDIRECT "${SCRIPT_NAME}#${fid}"
;;
newbookmark)
fid="$(POST fid | checkid)"
@@ -123,156 +203,252 @@ case "$(GET action)" in
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}"
+ printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$url" >>"${file}"
+ git -C "${BDB}" commit -m "New Bookmark: ${name} in ${fid}" -- "${fid}.bm" >&-
fi
getFavicon "$url" "$bid"
- REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
+ REDIRECT "${SCRIPT_NAME}#${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"
+ file="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
+ if [ -n "$name" -a -n "$url" -a "$(POST control)" = confirm -a -w "$file" ]; then
+ sed -rni "/^${bid}\t/!p; /^${bid}\t/i${bid}\t$(HTML "$name")\t${url}" "$file"
+ git -C "${BDB}" commit -m "Modified Bookmark: ${name} (${bid}) in ${file##*/}" -- "${file##*/}" >&-
fi
getFavicon "$url" "$bid"
- REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
+ REDIRECT "${SCRIPT_NAME}#${fid}"
;;
-esac
+ movebookmark)
+ bid="$(POST bid | checkid)"
+ fid="$(POST target | checkid)"
+ sfile="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
+ tfile="${BDB}/${fid}.bm"
-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 [ "$(POST control)" = confirm -a -n "$bid" -a -w "$sfile" -a -w "$tfile" ]; then
+ grep -m1 "^${bid}" "$sfile" >>"$tfile" \
+ && sed -ri "0,/^${bid}/{/^${bid}/d;}" "$sfile"
+ git -C "${BDB}" commit -m "Moved Bookmark ${bid} from ${sfile##*/} to ${tfile##*/}" \
+ -- "${sfile##*/}" "${tfile##*/}" >&-
+ fi
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ bmup)
+ fid="$(GET fid |checkid)"
+ bid="$(GET bid |checkid)"
+ file="${BDB}/${fid}.bm"
-if [ -z "$QRYID" -a -z "$COKID" ]; then
- printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+ if [ -n "$bid" -a -n "$fid" ] && grep -q "^${bid}" "$file"; then
+ sed -ri ":X;\$bY;N;bX;:Y; s;(\n[^\n]+)(\n${bid}\t[^\n]+);\2\1;;" "$file"
+ git -C "${BDB}" commit -m "Modified bookmark order in ${fid} (raised ${bid})" -- "${fid}.bm" >&-
+ fi
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ bmdn)
+ fid="$(GET fid |checkid)"
+ bid="$(GET bid |checkid)"
+ file="${BDB}/${fid}.bm"
- 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'
+ if [ -n "$bid" -a -n "$fid" ] && grep -q "^${bid}" "$file"; then
+ sed -ri ":X;\$bY;N;bX;:Y; s;(\n${bid}\t[^\n]+)(\n[^\n]+);\2\1;;" "$file"
+ git -C "${BDB}" commit -m "Modified bookmark order in ${fid} (lowered ${bid})" -- "${fid}.bm" >&-
+ fi
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ query)
+ fid="$(POST fid |checkid)"
+ bid="$(POST bid |checkid)"
+ file="${BDB}/${fid}.bm"
+ query="$(URL "$(POST query)")"
- cat <<-EOF
-
-
- Bookman - 404
-
- The collection you requested does not exist on this server.
- Click here to start a new collection.
-
+
+ url="$(grep -m1 "^${bid}" "$file" |cut -f3-)"
+ urlpfx="${url%\{@\}*}"
+ urlsfx="${url#*\{@\}}"
+
+ REDIRECT "${urlpfx}${query}${urlsfx}"
+ ;;
+esac
+
+bookmarkmod(){
+ bmod="$(GET bmodify |checkid)"
+ bmove="$(GET bmove |checkid)"
+
+ if [ -n "$bmod" ]; then
+ file="$(grep -lm1 "^${bmod}" "${BDB}/"????????????.bm |head -n1)"
+ read bid name url <<-EOF
+ $(grep -m1 "^${bmod}" "$file")
EOF
- exit 0
-fi
+ cat <<-EOF
+
+ EOF
+ elif [ -n "$bmove" ]; then
+ file="$(grep -lm1 "^${bmove}" "${BDB}/"????????????.bm |head -n1)"
+ read bid name url <<-EOF
+ $(grep -m1 "^${bmove}" "$file")
+ EOF
+ cat <<-EOF
+
+ EOF
+ fi
+}
-list_bookmarks(){
+show_bookmarks(){
fid="$1"
bmodify="$(GET bmodify |checkid)"
tail -n+2 "${BDB}/${fid}.bm" \
| while read bid name url; do
- if [ "${bid}" = "$bmodify" ]; then
+ if [ "${url%\{@\}*}" = "${url}" ]; then
cat <<-EOF
-
+
EOF
else
cat <<-EOF
-
+
EOF
fi
done
}
-list_folders(){
+foldermod(){
fmodify="$(GET fmodify |checkid )"
fdelete="$(GET fdelete |checkid )"
fmove="$(GET fmove |checkid )"
- head -qn1 "${BDB}"/????????????.bm \
- | sort -nk3 \
- | while read fid fname order; do
+ if [ -n "$fmodify" ]; then
+ read fid fname order <"${BDB}/${fmodify}.bm"
cat <<-EOF
-
- ${fname}
- Modify
- $(list_bookmarks "$fid")
-
-
- EOF
- if [ "$fid" = "$fmodify" ]; then
- cat <<-EOF
-
EOF
- elif [ "$fid" = "$fdelete" ]; then
- cat <<-EOF
-
EOF
- elif [ "$fid" = "$fmove" ]; then
- cat <<-EOF
-
EOF
+ fi
+}
+
+show_folders(){
+ list_folders \
+ | while read fid fname order; do
+ file="${BDB}/${fid}.bm"
+ cache="${BDB}/${fid}.cache"
+ if [ "${cache}" -nt "${file}" ]; then
+ cat "$cache"
+ else
+ tee "$cache" <<-EOF
+
+ EOF
fi
done
}
+SET_COOKIE +8640000 "id=${COKID}" # Refresh Cookie
printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+
cat <
Bookman - Your Collection
- $(list_folders)
-