UI for folder modification
[bookman] / index.cgi
1 #!/bin/sh
2
3 exec 2>>error.log
4
5 . shcgi/cgilite.sh
6 mkdir -p users
7 #env >>debug
8 printf 'POST: %s\n' "$cgilite_post" >>debug
9 printf 'action: %s\n' "$(GET action)" >>debug
10
11 wget="$(which wget)"
12 wget(){ "$wget" -T 5 -t 1 -q -U '' $@; }
13 checkid(){ grep -m 1 -xE '[0-9a-zA-Z:_]{12}'; }
14
15 genid(){
16   # generate random ID
17   head -c9 /dev/urandom \
18   | uuencode -m - \
19   | sed -n '2{y;+/;:_;;p}'
20 }
21
22 timeid(){
23   # generate time based ID
24   d=$(date +%s)
25   { printf $(
26       while [ "$d" -gt 0 ]; do
27         printf \\%o $((d % 256))
28         d=$((d / 256))
29       done
30     ) | tac
31     head -c5 /dev/urandom
32   } \
33   | uuencode -m - \
34   | sed -n '2{y;+/;:_;;p}'
35 }
36
37 getFavicon(){
38   url="$1"
39   bid="$2"
40   prot=${url%%://*}
41   domain="${url#*://}"
42   domain="${domain%%/*}"
43   ubase="${prot}://${domain}"
44   file="${BDB}/favicons/${bid}.ico"
45
46   mkdir -p "${BDB}/favicons/" && chmod a+rx "${BDB}/favicons/"
47
48   favinfo="$(
49     wget -O- "$url" \
50     | head -c4096 \
51     | sed -rn \
52       's;^.*(<[Ll][Ii][Nn][Kk]( [^>]*)? [Rr][Re][Ll]='\''([Ss][Hh][Oo][Rr][Tt][Cc][Uu][Tt] )?[Ii][Cc][Oo][Nn]'\''[^>]*>).*$;\1;;
53        s;^.*(<[Ll][Ii][Nn][Kk]( [^>]*)? [Rr][Re][Ll]="([Ss][Hh][Oo][Rr][Tt][Cc][Uu][Tt] )?[Ii][Cc][Oo][Nn]"[^>]*>).*$;\1;;
54        tX; b; :X;
55        s;^.*<([^>]+) [Hh][Rr][Ee][Ff]="([^"]+)".*$:\2;;
56        s;^.*<([^>]+) [Hh][Rr][Ee][Ff]='\''([^'\'']+)'\''.*$:\2;;
57        tY; b; :Y; p
58       '
59   )"
60
61   printf 'Shortcut icon for %s is %s\n' "$url" "$favinfo" >>debug
62   [ -z "$favinfo" ] && favinfo="${ubase}/favicon.ico"
63   case "$favinfo" in
64     http://*|https://*|//*) wget -O "$file" "$favinfo"
65     ;;
66     /*) wget -O "$file" "${ubase}/${favinfo}"
67     ;;
68     *) wget -O "$file" "${url%/*}/${favinfo}"
69     ;;
70   esac
71   [ -f "${file}.1" ] && mv "${file}.1" "$file"
72   chmod a+r "$file"
73 }
74
75 list_folders(){
76   head -qn1 "${BDB}"/????????????.bm \
77   | sort -nk3 \
78   | cut -f1,2
79 }
80
81
82 QRYID="$(GET    id |checkid)"
83 COKID="$(COOKIE id |checkid)"
84 BDB="users/${QRYID}"
85
86 case "$(GET action)" in
87   newid)
88     NEWID="$(genid)"
89
90     { git init "users/${NEWID}" || mkdir -p "users/${NEWID}"; } >&-
91
92     printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL"
93     printf 'Location: %s\r\n' "${SCRIPT_NAME}?id=${NEWID}"
94     SET_COOKIE +8640000 "id=${NEWID}"
95     printf '\r\n'
96     exit 0
97     ;;
98   newfolder)
99     name="$(POST name |head -n1)"
100     fid="$(timeid)"
101     order="$(
102       head -qn1 "${BDB}"/????????????.bm \
103       | cut -f3 \
104       | sort -n \
105       | tail -n1 \
106       || printf 1
107     )"
108     order="$(((order + 1000) / 1000 * 1000))"
109     if [ -n "$name" -a -d "${BDB}" ]; then
110       printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${BDB}/${fid}.bm"
111     fi
112     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
113     ;;
114   modfolder)
115     name="$(POST name |head -n1)"
116     fid="$(POST fid | checkid)"
117     file="${BDB}/${fid}.bm"
118     if [ "$(POST control)" = confirm -a -n "$name" -a -f "$file" ]; then
119       order="$(head -n1 "$file" |cut -f3 || printf 1000)"
120       printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${file%.bm}.tmp"
121       tail -n+2 "$file" >>"${file%.bm}.tmp"
122       mv "${file%.bm}.tmp" "$file"
123     fi
124     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
125     ;;
126   newbookmark)
127     fid="$(POST fid | checkid)"
128     name="$(POST name |head -n1)"
129     url="$(POST url |head -n1)"
130     file="${BDB}/${fid}.bm"
131     bid="$(timeid)"
132     if [ -n "$name" -a -f "${file}" ]; then
133       printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$(HTML "$url")" >>"${file}"
134     fi
135     getFavicon "$url" "$bid"
136     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
137     ;;
138   modbookmark)
139     bid="$(POST bid | checkid)"
140     name="$(POST name |head -n1)"
141     url="$(POST url |head -n1)"
142     file="$(grep -lE "^${bid}" "${BDB}"/????????????.bm)"
143     if [ -w "$file" -a -n "$name" -a -n "$url" ]; then
144       bm="$(printf '%s\t%s\t%s' "$bid" "$(HTML "$name")" "$(HTML "$url")" |sed -r 's;[\&\;];\\&;g;')"
145       sed -ri "s;^${bid}\t.*$;${bm};" "$file"
146     fi
147     getFavicon "$url" "$bid"
148     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
149     ;;
150 esac
151
152 if [ -z "$QRYID" -a -n "$COKID" ]; then
153   REDIRECT "${SCRIPT_NAME}?id=${COKID}"
154 elif [ -n "$QRYID" -a -z "$COKID" ]; then
155   SET_COOKIE +8640000 "id=${QRYID}"
156 fi
157
158 if [ -z "$QRYID" -a -z "$COKID" ]; then
159   printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
160
161   cat <<-EOF
162         <!DOCTYPE HTML>
163         <HTML><head>
164           <title>Bookman - New Collection</title>
165         </head><body id="newcollection">
166           <h1>You have not yet set up a collection on this server.</h1>
167           Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
168         </body></HTML>
169         EOF
170   exit 0
171 elif ! [ -d "users/${QRYID}" ]; then
172   printf '%s 404 Not Found\r\n' "$SERVER_PROTOCOL"
173   printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
174
175   cat <<-EOF
176         <!DOCTYPE HTML>
177         <HTML><head>
178           <title>Bookman - 404</title>
179         </head><body id="missingcollection">
180           <h1>The collection you requested does not exist on this server.</h1>
181           Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
182         </body></HTML>
183         EOF
184   exit 0
185 fi
186
187 list_bookmarks(){
188   fid="$1"
189   bmodify="$(GET bmodify |checkid)"
190
191   tail -n+2 "${BDB}/${fid}.bm" \
192   | while read bid name url; do
193     if [ "${bid}" = "$bmodify" ]; then
194       cat <<-EOF
195         <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modbookmark">
196           <input type="hidden" name="bid" value="${bid}" />
197           <input type="text" name="name" value="${name}")" placeholder="Name" />
198           <input type="text" name="url"  value="${url}")" placeholder="URL" />
199           <button type="submit">Modify</button>
200         </form>
201         EOF
202     else
203       cat <<-EOF
204         <div class="bookmark">
205           <a class="modify" href="${SCRIPT_NAME}?id=${QRYID}&bmodify=${bid}">Modify</a>
206           <a class="link" href="${url}")"><img src="${BDB}/favicons/${bid}.ico"/>${name}</a>
207         </div>
208         EOF
209     fi
210   done
211 }
212
213 foldermod(){
214   fmodify="$(GET fmodify |checkid )"
215   fdelete="$(GET fdelete |checkid )"
216   fmove="$(GET fmove |checkid )"
217
218   if [ -n "$fmodify" ]; then
219     read fid fname order <"${BDB}/${fmodify}.bm"
220     cat <<-EOF
221         <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
222           <h1>Rename Folder: ${fname}</h1>
223           <input type="hidden" name="fid" value="${fid}" />
224           <label class="tab">Rename</label>
225           <a class="tab"
226             href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a>
227           <a class="tab"
228             href="${SCRIPT_NAME}?id=${QRYID}&fmove=${fid}">Move</a>
229           <input type="text" name="name" value="${fname}" />
230           <button type="submit" name="control" value="confirm">OK</button>
231           <button type="submit" name="control" value="cancel">Cancel</button>
232         </form>
233         EOF
234   elif [ -n "$fdelete" ]; then
235     read fid fname order <"${BDB}/${fdelete}.bm"
236     cat <<-EOF
237         <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=delfolder">
238           <h1>Delete Folder: ${fname}</h1>
239           <input type="hidden" name="fid" value="${fid}" />
240           <a class="tab" href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a>
241           <label class="tab">Delete</label>
242           <a class="tab"
243             href="${SCRIPT_NAME}?id=${QRYID}&fmove=${fid}">Move</a>
244           <label>Pass Bookmarks on to:</label>
245           <select name="target">
246             $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
247             <option value="____________">(none)</option>
248           </select>
249           <button type="submit" name="control" value="confirm">OK</button>
250           <button type="submit" name="control" value="cancel">Cancel</button>
251         </form>
252         EOF
253   elif [ -n "$fmove" ]; then
254     read fid fname order <"${BDB}/${fmove}.bm"
255     cat <<-EOF
256         <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
257           <h1>Move Folder: ${fname}</h1>
258           <input type="hidden" name="fid" value="${fid}" />
259           <a class="tab" href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a>
260           <a class="tab" href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a>
261           <label class="tab">Move</label>
262           <label>Move before folder:</label>
263           <select name="target">
264             $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
265             <option value="____________">(last)</option>
266           </select>
267           <button type="submit" name="control" value="confirm">OK</button>
268           <button type="submit" name="control" value="cancel">Cancel</button>
269         </form>
270         EOF
271   fi
272 }
273
274 show_folders(){
275   list_folders \
276   | while read fid fname order; do
277     cat <<-EOF
278         <section class="folder" id="${fid}">
279           <h1>${fname}</h1>
280           <a class="modify" href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Modify</a>
281           $(list_bookmarks "$fid")
282           <form class="newbookmark" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=newbookmark">
283             <input type="hidden" name="fid" value="${fid}" />
284             <input type="text" name="name" value="" placeholder="Name" />
285             <input type="text" name="url"  value="" placeholder="URL" />
286             <button type="submit">New Bookmark</button>
287           </form>
288         </section>
289         EOF
290   done
291 }
292
293 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
294 cat <<EOF
295 <!DOCTYPE HTML>
296 <HTML><head>
297   <title>Bookman - Your Collection</title>
298   <link rel="stylesheet" type="text/css" href="bookmarks.css" />
299 </head><body id="collection">
300   $(foldermod)
301   $(show_folders)
302   <form class="newfolder" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=newfolder">
303     <input type="text" name="name" value="" placeholder="New Folder" />
304     <button type="submit">New</button>
305   </form>
306 </body></HTML>
307 EOF
308
309 #set filetype=sh