]> git.plutz.net Git - bookman/blobdiff - index.cgi
start of complete rewrite
[bookman] / index.cgi
index a0a84906df8e5f443ed23c43b2836d8ce4b88cb2..8129b7e3434edcd0c26f70fcfa006f237e2bf49f 100644 (file)
--- a/index.cgi
+++ b/index.cgi
-#!/bin/zsh
-echo 'Content-type: text/html\n'
+#!/bin/sh
 
-cat <<NOTES
-<!DOCTYPE HTML>
-<html>
-  <head>
-    <title>Bookmarks!</title>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-    <link rel="stylesheet" type="text/css" href="bookmarks.css">
-    <script type="text/javascript">
-    <!--
-    function show(id){
-      document.getElementById(id).style.display = "block";
-    }
-    function hide(id){
-      document.getElementById(id).style.display = "none";
-    }
-    -->
-    </script>
-  </head>
-  <body>
-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
+       <!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'
+
+  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
 
-echo '</body></html>'
+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
+       <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
+       <div class="bookmark">
+         <a class="modify" href="${SCRIPT_NAME}?id=${QRYID}&bmodify=${bid}">Modify</a>
+         <a class="link" href="${url}")"><img src="${BDB}/favicons/${bid}.ico"/>${name}</a>
+       </div>
+       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
+       <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">
+           <input type="hidden" name="fid" value="${fid}" />
+           <input type="text" name="name" value="" placeholder="Name" />
+           <input type="text" name="url"  value="" placeholder="URL" />
+           <button type="submit">New Bookmark</button>
+          </form>
+       </section>
+       EOF
+    if [ "$fid" = "$fmodify" ]; then
+      cat <<-EOF
+       <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
+         <input type="hidden" name="fid" value="${fid}" />
+          <label>Rename</label><a
+           href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a><a
+           href="${SCRIPT_NAME}?id=${QRYID}&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>
+       </form>
+       EOF
+    elif [ "$fid" = "$fdelete" ]; then
+      cat <<-EOF
+       <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
+         <input type="hidden" name="fid" value="${fid}" />
+         <a href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a><label
+            >Delete</label><a
+           href="${SCRIPT_NAME}?id=${QRYID}&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>
+       </form>
+       EOF
+    elif [ "$fid" = "$fmove" ]; then
+      cat <<-EOF
+       <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
+         <input type="hidden" name="fid" value="${fid}" />
+         <a href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a><a
+           href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a><label
+           >Move</label>
+         <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>
+       </form>
+       EOF
+    fi
+  done
+}
+
+printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+cat <<EOF
+<!DOCTYPE HTML>
+<HTML><head>
+  <title>Bookman - Your Collection</title>
+  <link rel="stylesheet" type="text/css" href="bookmarks.css" />
+</head><body id="collection">
+  $(list_folders)
+  <form class="newfolder" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=newfolder">
+    <input type="text" name="name" value="" placeholder="New Folder" />
+    <button type="submit">New</button>
+  </form>
+</body></HTML>
+EOF
+
+#set filetype=sh