]> git.plutz.net Git - bookman/blobdiff - index.cgi
bookmark modification functions
[bookman] / index.cgi
index 3cf7e2914c07ae7c8d444eed8137b6a751020e8b..00006da66746bf8dd3121a10175ed3c2f3021989 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -78,10 +78,54 @@ list_folders(){
   | 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
+}
 
-QRYID="$(GET    id |checkid)"
 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}"
+  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
+       <!DOCTYPE HTML>
+       <HTML><head>
+         <title>Bookman - New Collection</title>
+       </head><body id="newcollection">
+         <h1>You have not yet set up a collection on this server.</h1>
+         Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
+       </body></HTML>
+       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
+       <!DOCTYPE HTML>
+       <HTML><head>
+         <title>Bookman - 404</title>
+       </head><body id="missingcollection">
+         <h1>The collection you requested does not exist on this server.</h1>
+         Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
+       </body></HTML>
+       EOF
+  exit 0
+fi
 
 case "$(GET action)" in
   newid)
@@ -109,7 +153,7 @@ case "$(GET action)" in
     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}"
+    REDIRECT "${SCRIPT_NAME}#${fid}"
     ;;
   modfolder)
     name="$(POST name |head -n1)"
@@ -121,7 +165,34 @@ case "$(GET action)" in
       tail -n+2 "$file" >>"${file%.bm}.tmp"
       mv "${file%.bm}.tmp" "$file"
     fi
-    REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
+    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"
+      fi
+    fi
+    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
+    REDIRECT "${SCRIPT_NAME}#${fid}"
     ;;
   newbookmark)
     fid="$(POST fid | checkid)"
@@ -133,80 +204,113 @@ case "$(GET action)" in
       printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$(HTML "$url")" >>"${file}"
     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
+    file="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
+    if [ -n "$name" -a -n "$url" -a "$(POST control)" = confirm -a -w "$file" ]; 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}"
+    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"
+    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"
+    fi
+    REDIRECT "${SCRIPT_NAME}#${fid}"
+    ;;
+  bmdn)
+    fid="$(GET fid |checkid)"
+    bid="$(GET bid |checkid)"
+    file="${BDB}/${fid}.bm"
 
-  cat <<-EOF
-       <!DOCTYPE HTML>
-       <HTML><head>
-         <title>Bookman - New Collection</title>
-       </head><body id="newcollection">
-         <h1>You have not yet set up a collection on this server.</h1>
-         Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
-       </body></HTML>
-       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"
+    fi
+    REDIRECT "${SCRIPT_NAME}#${fid}"
+    ;;
+esac
 
-  cat <<-EOF
-       <!DOCTYPE HTML>
-       <HTML><head>
-         <title>Bookman - 404</title>
-       </head><body id="missingcollection">
-         <h1>The collection you requested does not exist on this server.</h1>
-         Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
-       </body></HTML>
+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
+       <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?action=modbookmark">
+         <input type="hidden" name="bid" value="${bid}" />
+         <h1>Modify: ${name}</h1>
+         <label class="tab">Modify</label>
+         <a class="tab" href="${SCRIPT_NAME}?bmove=${bid}">Move</a>
+         <label>Name:</label>
+         <input type="text" name="name" value="${name}")" placeholder="Name" />
+         <label>URL:</label>
+         <input type="text" name="url"  value="${url}")" placeholder="URL" />
+         <button type="submit" name="control" value="confirm">OK</button>
+         <button type="submit" name="control" value="cancel">Cancel</button>
+       </form>
+       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
+       <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?action=movebookmark">
+         <input type="hidden" name="bid" value="${bid}" />
+         <h1>Move: ${name}</h1>
+         <a class="tab" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
+         <label class="tab">Move</label>
+         <label>Move to Folder:</label>
+         <select name="target">
+           $(printf '<option value="%s">%s</option>' $(list_folders))
+         </select>
+         <button type="submit" name="control" value="confirm">OK</button>
+         <button type="submit" name="control" value="cancel">Cancel</button>
+       </form>
+       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
-      cat <<-EOF
-       <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modbookmark">
-         <input type="hidden" name="bid" value="${bid}" />
-         <input type="text" name="name" value="${name}")" placeholder="Name" />
-         <input type="text" name="url"  value="${url}")" placeholder="URL" />
-         <button type="submit">Modify</button>
-       </form>
-       EOF
-    else
-      cat <<-EOF
+    cat <<-EOF
        <div class="bookmark">
-         <a class="modify" href="${SCRIPT_NAME}?id=${QRYID}&bmodify=${bid}">Modify</a>
+         <a class="modify" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
          <a class="link" href="${url}")"><img src="${BDB}/favicons/${bid}.ico"/>${name}</a>
+         <a class="bmove" href="${SCRIPT_NAME}?action=bmup&fid=${fid}&bid=${bid}">move left</a>
+         <a class="bmove" href="${SCRIPT_NAME}?action=bmdn&fid=${fid}&bid=${bid}">move right</a>
        </div>
        EOF
-    fi
   done
 }
 
@@ -218,14 +322,14 @@ foldermod(){
   if [ -n "$fmodify" ]; then
     read fid fname order <"${BDB}/${fmodify}.bm"
     cat <<-EOF
-       <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
+       <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?action=modfolder">
           <h1>Rename Folder: ${fname}</h1>
          <input type="hidden" name="fid" value="${fid}" />
          <label class="tab">Rename</label>
          <a class="tab"
-           href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a>
+           href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
          <a class="tab"
-           href="${SCRIPT_NAME}?id=${QRYID}&fmove=${fid}">Move</a>
+           href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
          <input type="text" name="name" value="${fname}" />
          <button type="submit" name="control" value="confirm">OK</button>
          <button type="submit" name="control" value="cancel">Cancel</button>
@@ -234,17 +338,17 @@ foldermod(){
   elif [ -n "$fdelete" ]; then
     read fid fname order <"${BDB}/${fdelete}.bm"
     cat <<-EOF
-       <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=delfolder">
+       <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?action=delfolder">
           <h1>Delete Folder: ${fname}</h1>
          <input type="hidden" name="fid" value="${fid}" />
-         <a class="tab" href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a>
+         <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
          <label class="tab">Delete</label>
          <a class="tab"
-           href="${SCRIPT_NAME}?id=${QRYID}&fmove=${fid}">Move</a>
+           href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
          <label>Pass Bookmarks on to:</label>
          <select name="target">
            $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
-           <option value="____________">(none)</option>
+           <option value="____________">(discard)</option>
          </select>
          <button type="submit" name="control" value="confirm">OK</button>
          <button type="submit" name="control" value="cancel">Cancel</button>
@@ -253,11 +357,11 @@ foldermod(){
   elif [ -n "$fmove" ]; then
     read fid fname order <"${BDB}/${fmove}.bm"
     cat <<-EOF
-       <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
+       <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?action=movefolder">
           <h1>Move Folder: ${fname}</h1>
          <input type="hidden" name="fid" value="${fid}" />
-         <a class="tab" href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a>
-         <a class="tab" href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a>
+         <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
+         <a class="tab" href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
          <label class="tab">Move</label>
          <label>Move before folder:</label>
          <select name="target">
@@ -277,9 +381,9 @@ show_folders(){
     cat <<-EOF
        <section class="folder" id="${fid}">
          <h1>${fname}</h1>
-         <a class="modify" href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Modify</a>
-         $(list_bookmarks "$fid")
-          <form class="newbookmark" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=newbookmark">
+         <a class="modify" href="${SCRIPT_NAME}?fmodify=${fid}">Modify</a>
+         $(show_bookmarks "$fid")
+          <form class="newbookmark" method="POST" action="${SCRIPT_NAME}?action=newbookmark">
            <input type="hidden" name="fid" value="${fid}" />
            <input type="text" name="name" value="" placeholder="Name" />
            <input type="text" name="url"  value="" placeholder="URL" />
@@ -290,7 +394,9 @@ show_folders(){
   done
 }
 
+SET_COOKIE +8640000 "id=${COKID}"      # Refresh Cookie
 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+
 cat <<EOF
 <!DOCTYPE HTML>
 <HTML><head>
@@ -298,8 +404,9 @@ cat <<EOF
   <link rel="stylesheet" type="text/css" href="bookmarks.css" />
 </head><body id="collection">
   $(foldermod)
+  $(bookmarkmod)
   $(show_folders)
-  <form class="newfolder" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=newfolder">
+  <form class="newfolder" method="POST" action="${SCRIPT_NAME}?action=newfolder">
     <input type="text" name="name" value="" placeholder="New Folder" />
     <button type="submit">New</button>
   </form>