set link target=_blank to open links in new window/tab
[bookman] / bookmarks.sh
1 #!/bin/zsh
2 bdir="bookmarks/${REMOTE_USER}"
3 icodir=icodir
4 alias wget="/usr/bin/wget -T 5 -t 1 -q -U ''"
5
6 if [ -n "${CONTENT_LENGTH}" -a "${CONTENT_LENGTH}" -gt 0 ]; then
7   (head -c "${CONTENT_LENGTH}" | sed 's:&:\n:g'; echo) >$bdir/$(date +%s)
8 fi
9
10 moveName="$(echo -en "$(sed -rn '/mn=.+&mc=.+/{s:(^|.+&)mn=(.+)(&.+|$):\2:;s:\+: :g;s:%:\\x:g;p}' <<<"$QUERY_STRING")")"
11 moveCont="$(echo -en "$(sed -rn '/mn=.+&mc=.+/{s:(^|.+&)mc=(.+)(&.+|$):\2:;s:\+: :g;s:%:\\x:g;p}' <<<"$QUERY_STRING")")"
12 [ -n "$moveName" -a -n "$moveCont" ] && move=true || move=false
13
14 [ -d "$bdir" ] || mkdir -p "$bdir"
15 names=$(sed -rn 's:^name=(.*)$:\1:pg' $bdir/$(ls $bdir |tail -n1))
16 hidecmd=$(for each in $(echo $names); do echo "try{hide('e_$each');}catch(e){};"; done)
17 showcmd=$(for each in $(echo $names); do echo "try{show('d_$each');}catch(e){};"; done)
18
19 delFolder=false
20 delBookmark=false
21
22 echo "
23 <form action='index.cgi' method='post' accept-charset='utf-8'>
24   <div style='display:none;'>
25   qs $QUERY_STRING
26   mn $moveName
27   mc $moveCont
28 "
29
30 cat $bdir/$(ls $bdir |tail -n1) |while read line; do
31   case "$line" in
32     deleteFolder=*) delFolder=true
33         ;;
34     deleteBookmark=*) delBookmark=true
35         ;;
36     folder=*) echo '<!-- FOLDER -->'
37         fId=$(sed 's:^folder=::' <<<"$line")
38         [ -z "$fId" ] && fId='###'
39         fName="$(echo -ne "$(sed 's:+: :g;s:%:\\x:g' <<<"$fId")")"
40         $delFolder || echo "
41           <input type='hidden' name='title' value='New Bookmark'>
42           <input type='submit' name='content' value='New Bookmark'>
43         </div><div class='folder'>
44           <div class='fName' id='fName_$fId'>
45             <a href='#' onclick='javascript:hide(\"fName_$fId\");show(\"efName_$fId\")'>&rarr;</a>$fName
46           </div><div class='efName' id='efName_$fId'>
47             <a href='#' onclick='javascript:hide(\"efName_$fId\");show(\"fName_$fId\")'>&larr;</a>
48             <input type='submit' value='Rename'><input type='submit' name='deleteFolder' value='Remove'>
49             <input type='text' name='folder' value='$fName'>
50           </div>
51         "
52         delFolder=false
53         $move && echo "
54           <div class='moveButton'>
55             <input type='hidden' name='title' value='$moveName'>
56             <button type='submit' name='content' value='$moveCont'>Move Here</button>
57           </div>
58         "
59         ;;
60     title=*) 
61         bmId=$(sed 's:^title=::' <<<"$line")
62         [ -z "$bmId" ] && bmId='###'
63         bmName="$(echo -ne "$(sed 's:+: :g;s:%:\\x:g' <<<"$bmId")")"
64         ;;
65     content=*)
66         bmLink="$(echo -ne "$(sed 's:^content=::;s:+: :g;s:%:\\x:g' <<<"$line")")"
67         bmBase="$(sed -r 's:^(https?\://[^/]+)/?.*$:\1:' <<<"$bmLink")"
68         bmFav="$(sed -r 's:^(https?\://[^/]+)/?.*$:\1/favicon.ico:' <<<"$bmLink")"
69         bmFavFile="$icodir/$(sed -r 's:^https?\://([^/]+)/?.*$:\1.ico:' <<<"$bmLink")"
70         [ -f "$bmFavFile" ] || (wget -O - "$bmLink" |head -c 4096 |\
71            sed -rn "/<link.*rel=[\"'](shortcut )?icon[\"'].*>/Is:^.*<link.*href=[\"']([^\"']*)[\"'].*$:\1:ip" |\
72            read p && [ -n "$p" ] && (wget -O "$bmFavFile" "$p" || wget -O "$bmFavFile" "$bmBase/$p") ||\
73            wget -O "$bmFavFile" "$bmFav"
74         ) &
75         [ "$bmName" = "$moveName" -a "$bmLink" = "$moveCont" ] && delBookmark=true
76         $delBookmark || echo "
77           <div class='bmDisplay' id='d_$bmId'>
78             <a class='edButton' href='#' onclick='javascript:${hidecmd}${showcmd}show(\"e_$bmId\");hide(\"d_$bmId\");'>&rarr;</a>
79             <img width=16 height=16 src='$bmFavFile'>
80             <a target='_blank' href='$bmLink'>$bmName</a>
81           </div><div class='bmEdit' id='e_$bmId'>
82             <a class='edButton' href='#' onclick='javascript:show(\"d_$bmId\");hide(\"e_$bmId\");'>&larr;</a>
83             <input type='submit' value='Update'><input type='submit' name='deleteBookmark' value='Remove'><br>
84             <a href='?mn=$bmName&mc=$bmLink'>Move Bookmark</a><br>
85             Name: <input type='text' name='title' value='$bmName'><br>
86             Link: <input type='text' name='content' value='$bmLink'>
87           </div>
88         "
89         $delBookmark || ($move && echo "
90           <div class='moveButton'>
91             <input type='hidden' name='title' value='$moveName'>
92             <button type='submit' name='content' value='$moveCont'>Move Here</button>
93           </div>
94         ")
95         delBookmark=false
96         ;;
97     *) echo "<!-- $line -->"
98         ;;
99   esac
100 done
101
102 echo "
103     <input type='hidden' name='title' value='New Bookmark'>
104     <input type='submit' name='content' value='New Bookmark'>
105   </div>
106   <input type='submit' value='Submit Changes'>
107   <input type='submit' name='folder' value='New Folder'>
108 </form>
109 "