X-Git-Url: http://git.plutz.net/?p=bookman;a=blobdiff_plain;f=index.cgi;h=e96c9f01e750c7c323a8b7d031177918f6c96e7f;hp=0c563eae531e062cb42b899b95bb61caf83c7d7a;hb=HEAD;hpb=3ecee15fc19ec1a2d4b44d965e63412860ea01b6;ds=sidebyside
diff --git a/index.cgi b/index.cgi
index 0c563ea..e96c9f0 100755
--- 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"
@@ -93,8 +92,8 @@ 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}"
+ printf 'Status: 303 See Other\r\n'
+ printf 'Location: %s\r\n' "${SCRIPT_NAME}?${QUERY_STRING#id=????????????}"
SET_COOKIE +8640000 "id=${QRYID}"
printf '\r\n'
exit 0
@@ -104,6 +103,7 @@ elif [ -z "$COKID" -a -z "$QRYID" ]; then
cat <<-EOF
+
Bookman - New Collection
You have not yet set up a collection on this server.
@@ -112,12 +112,13 @@ elif [ -z "$COKID" -a -z "$QRYID" ]; then
EOF
exit 0
elif ! [ -d "${BDB}" ]; then
- printf '%s 404 Not Found\r\n' "$SERVER_PROTOCOL"
+ printf 'Status: 404 Not Found\r\n'
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.
@@ -133,7 +134,7 @@ case "$(GET action)" in
{ git init "users/${NEWID}" || mkdir -p "users/${NEWID}"; } >&-
- printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL"
+ printf 'Status: 303 See Other\r\n'
printf 'Location: %s\r\n' "${SCRIPT_NAME}?id=${NEWID}"
SET_COOKIE +8640000 "id=${NEWID}"
printf '\r\n'
@@ -152,6 +153,8 @@ 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}#${fid}"
;;
@@ -160,10 +163,9 @@ case "$(GET action)" in
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}"
;;
@@ -173,8 +175,12 @@ case "$(GET action)" in
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"
+ if [ "$target" = "____________" ]; then
+ rm -f "$file" "${file%.bm}.cache" >&-
+ git -C "${BDB}" commit -m "Deleted folder ${fid}" -- "${fid}.bm" >&-
+ elif tail -n+2 "$file" >>"$tfile"; then
+ rm -f "$file" "${file%.bm}.cache" >&-
+ git -C "${BDB}" commit -m "Deleted folder ${fid}" -- "${fid}.bm" "${target}.bm" >&-
fi
fi
REDIRECT "${SCRIPT_NAME}#${target}"
@@ -192,6 +198,7 @@ case "$(GET action)" in
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)
@@ -200,47 +207,194 @@ case "$(GET action)" in
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}"
+ ctl="$(POST control)"
+ [ "$url" = "${url#*://}" ] && url="http://$url"
+
+ if [ -n "$name" -a -n "$url" -a -f "${file}" -a "$ctl" = "confirm" ]; then
+ printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$url" >>"${file}"
+ git -C "${BDB}" commit -m "New Bookmark: ${name} in ${fid}" -- "${fid}.bm" >&-
+ getFavicon "$url" "$bid"
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ elif [ "$ctl" = "confirm" ]; then
+ REDIRECT "${SCRIPT_NAME}?newbm=${fid}&nbmurl=${url}&nbmname=${name}"
+ else
+ REDIRECT "${SCRIPT_NAME}#${fid}"
fi
- getFavicon "$url" "$bid"
- 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}#${fid}"
;;
+ movebookmark)
+ bid="$(POST bid | checkid)"
+ fid="$(POST target | checkid)"
+ sfile="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
+ tfile="${BDB}/${fid}.bm"
+
+ 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 [ -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"
+
+ 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)")"
+
+
+ url="$(grep -m1 "^${bid}" "$file" |cut -f3-)"
+ urlpfx="${url%\{@\}*}"
+ urlsfx="${url#*\{@\}}"
+
+ REDIRECT "${urlpfx}${query}${urlsfx}"
+ ;;
esac
-list_bookmarks(){
+bookmarkgen(){
+ fid="$(GET newbm |checkid)"
+ name="$(GET nbmname)"
+ url="$(GET nbmurl)"
+ file="${BDB}/${fid}.bm"
+
+ if [ -z "$name" -a -n "$url" ]; then
+ name="$(wget -O- "$url" \
+ | head -c4096 \
+ | sed -rn ':X;$bY;N;bX;:Y; s;^.*]*>([^<]+)<.*$;\1;p;'
+ )"
+ fi
+
+ if [ -n "$fid" -o -n "$name" -o -n "$url" ]; then
+ [ "$url" = "${url#*://}" ] && url="http://$url"
+ cat <<-EOF
+
+ EOF
+ fi
+}
+
+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
+ 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
+}
+
+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
-
+
+
+
+
+ Move up
+ Move down
+
EOF
fi
done
@@ -254,7 +408,7 @@ foldermod(){
if [ -n "$fmodify" ]; then
read fid fname order <"${BDB}/${fmodify}.bm"
cat <<-EOF
-