]> git.plutz.net Git - bookman/blob - index.cgi
bookmark modification functions
[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 order_files(){
82   n=1000
83   list_folders \
84   | while read fid nan; do
85     file="${BDB}/${fid}.bm"
86     sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1${n}\3;;" "$file"
87     n=$((n + 1000))
88   done
89 }
90
91 COKID="$(COOKIE id |checkid)"
92 QRYID="$(GET    id |checkid)"
93 BDB="users/${COKID}"
94
95 if [ -n "$QRYID" ]; then
96   printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL"
97   printf 'Location: %s\r\n' "${SCRIPT_NAME}"
98   SET_COOKIE +8640000 "id=${QRYID}"
99   printf '\r\n'
100   exit 0
101 elif [ -z "$COKID" -a -z "$QRYID" ]; then
102   printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
103
104   cat <<-EOF
105         <!DOCTYPE HTML>
106         <HTML><head>
107           <title>Bookman - New Collection</title>
108         </head><body id="newcollection">
109           <h1>You have not yet set up a collection on this server.</h1>
110           Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
111         </body></HTML>
112         EOF
113   exit 0
114 elif ! [ -d "${BDB}" ]; then
115   printf '%s 404 Not Found\r\n' "$SERVER_PROTOCOL"
116   printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
117
118   cat <<-EOF
119         <!DOCTYPE HTML>
120         <HTML><head>
121           <title>Bookman - 404</title>
122         </head><body id="missingcollection">
123           <h1>The collection you requested does not exist on this server.</h1>
124           Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
125         </body></HTML>
126         EOF
127   exit 0
128 fi
129
130 case "$(GET action)" in
131   newid)
132     NEWID="$(genid)"
133
134     { git init "users/${NEWID}" || mkdir -p "users/${NEWID}"; } >&-
135
136     printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL"
137     printf 'Location: %s\r\n' "${SCRIPT_NAME}?id=${NEWID}"
138     SET_COOKIE +8640000 "id=${NEWID}"
139     printf '\r\n'
140     exit 0
141     ;;
142   newfolder)
143     name="$(POST name |head -n1)"
144     fid="$(timeid)"
145     order="$(
146       head -qn1 "${BDB}"/????????????.bm \
147       | cut -f3 \
148       | sort -n \
149       | tail -n1 \
150       || printf 1
151     )"
152     order="$(((order + 1000) / 1000 * 1000))"
153     if [ -n "$name" -a -d "${BDB}" ]; then
154       printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${BDB}/${fid}.bm"
155     fi
156     REDIRECT "${SCRIPT_NAME}#${fid}"
157     ;;
158   modfolder)
159     name="$(POST name |head -n1)"
160     fid="$(POST fid | checkid)"
161     file="${BDB}/${fid}.bm"
162     if [ "$(POST control)" = confirm -a -n "$name" -a -f "$file" ]; then
163       order="$(head -n1 "$file" |cut -f3 || printf 1000)"
164       printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${file%.bm}.tmp"
165       tail -n+2 "$file" >>"${file%.bm}.tmp"
166       mv "${file%.bm}.tmp" "$file"
167     fi
168     REDIRECT "${SCRIPT_NAME}#${fid}"
169     ;;
170   delfolder)
171     fid="$(POST fid | checkid)"
172     target="$(POST target | checkid)"
173     file="${BDB}/${fid}.bm"
174     tfile="${BDB}/${target}.bm"
175     if [ "$(POST control)" = confirm -a -f "$file" ]; then
176       if [ "$target" = "____________" ] || tail -n+2 "$file" >>"$tfile"; then
177         rm -f "$file"
178       fi
179     fi
180     REDIRECT "${SCRIPT_NAME}#${target}"
181     ;;
182   movefolder)
183     fid="$(POST fid | checkid)"
184     target="$(POST target | checkid)"
185     file="${BDB}/${fid}.bm"
186     tfile="${BDB}/${target}.bm"
187     if [ "$target" = "____________" -a -f "$file" -a "$(POST control)" = confirm ]; then
188       read nan1 nan2 last nan3 <"${BDB}/$(list_folders |tail -n1 |cut -f1).bm"
189       sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${last:-0} + 1000))\3;;" "$file"
190     elif [ -f "$tfile" -a -f "$file" -a "$(POST control)" = confirm ]; then
191       read nan1 nan2 tid nan3 <"$tfile"
192       sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${tid:-1} -1))\2;;" "$file"
193       order_files
194     fi
195     REDIRECT "${SCRIPT_NAME}#${fid}"
196     ;;
197   newbookmark)
198     fid="$(POST fid | checkid)"
199     name="$(POST name |head -n1)"
200     url="$(POST url |head -n1)"
201     file="${BDB}/${fid}.bm"
202     bid="$(timeid)"
203     if [ -n "$name" -a -f "${file}" ]; then
204       printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$(HTML "$url")" >>"${file}"
205     fi
206     getFavicon "$url" "$bid"
207     REDIRECT "${SCRIPT_NAME}#${fid}"
208     ;;
209   modbookmark)
210     bid="$(POST bid | checkid)"
211     name="$(POST name |head -n1)"
212     url="$(POST url |head -n1)"
213     file="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
214     if [ -n "$name" -a -n "$url" -a "$(POST control)" = confirm -a -w "$file" ]; then
215       bm="$(printf '%s\t%s\t%s' "$bid" "$(HTML "$name")" "$(HTML "$url")" |sed -r 's;[\&\;];\\&;g;')"
216       sed -ri "s;^${bid}\t.*$;${bm};" "$file"
217     fi
218     getFavicon "$url" "$bid"
219     REDIRECT "${SCRIPT_NAME}#${fid}"
220     ;;
221   movebookmark)
222     bid="$(POST bid | checkid)"
223     fid="$(POST target | checkid)"
224     sfile="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
225     tfile="${BDB}/${fid}.bm"
226
227     if [ "$(POST control)" = confirm -a -n "$bid" -a -w "$sfile" -a -w "$tfile" ]; then
228       grep -m1 "^${bid}" "$sfile" >>"$tfile" \
229       && sed -ri "0,/^${bid}/{/^${bid}/d;}" "$sfile"
230     fi
231     REDIRECT "${SCRIPT_NAME}#${fid}"
232     ;;
233   bmup)
234     fid="$(GET fid |checkid)"
235     bid="$(GET bid |checkid)"
236     file="${BDB}/${fid}.bm"
237
238     if [ -n "$bid" -a -n "$fid" ] && grep -q "^${bid}" "$file"; then
239       sed -ri ":X;\$bY;N;bX;:Y; s;(\n[^\n]+)(\n${bid}\t[^\n]+);\2\1;;" "$file"
240     fi
241     REDIRECT "${SCRIPT_NAME}#${fid}"
242     ;;
243   bmdn)
244     fid="$(GET fid |checkid)"
245     bid="$(GET bid |checkid)"
246     file="${BDB}/${fid}.bm"
247
248     if [ -n "$bid" -a -n "$fid" ] && grep -q "^${bid}" "$file"; then
249       sed -ri ":X;\$bY;N;bX;:Y; s;(\n${bid}\t[^\n]+)(\n[^\n]+);\2\1;;" "$file"
250     fi
251     REDIRECT "${SCRIPT_NAME}#${fid}"
252     ;;
253 esac
254
255 bookmarkmod(){
256   bmod="$(GET bmodify |checkid)"
257   bmove="$(GET bmove |checkid)"
258
259   if [ -n "$bmod" ]; then
260     file="$(grep -lm1 "^${bmod}" "${BDB}/"????????????.bm |head -n1)"
261     read bid name url <<-EOF
262         $(grep -m1 "^${bmod}" "$file")
263         EOF
264     cat <<-EOF
265         <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?action=modbookmark">
266           <input type="hidden" name="bid" value="${bid}" />
267           <h1>Modify: ${name}</h1>
268           <label class="tab">Modify</label>
269           <a class="tab" href="${SCRIPT_NAME}?bmove=${bid}">Move</a>
270           <label>Name:</label>
271           <input type="text" name="name" value="${name}")" placeholder="Name" />
272           <label>URL:</label>
273           <input type="text" name="url"  value="${url}")" placeholder="URL" />
274           <button type="submit" name="control" value="confirm">OK</button>
275           <button type="submit" name="control" value="cancel">Cancel</button>
276         </form>
277         EOF
278   elif [ -n "$bmove" ]; then
279     file="$(grep -lm1 "^${bmove}" "${BDB}/"????????????.bm |head -n1)"
280     read bid name url <<-EOF
281         $(grep -m1 "^${bmove}" "$file")
282         EOF
283     cat <<-EOF
284         <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?action=movebookmark">
285           <input type="hidden" name="bid" value="${bid}" />
286           <h1>Move: ${name}</h1>
287           <a class="tab" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
288           <label class="tab">Move</label>
289           <label>Move to Folder:</label>
290           <select name="target">
291             $(printf '<option value="%s">%s</option>' $(list_folders))
292           </select>
293           <button type="submit" name="control" value="confirm">OK</button>
294           <button type="submit" name="control" value="cancel">Cancel</button>
295         </form>
296         EOF
297   fi
298 }
299
300 show_bookmarks(){
301   fid="$1"
302   bmodify="$(GET bmodify |checkid)"
303
304   tail -n+2 "${BDB}/${fid}.bm" \
305   | while read bid name url; do
306     cat <<-EOF
307         <div class="bookmark">
308           <a class="modify" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
309           <a class="link" href="${url}")"><img src="${BDB}/favicons/${bid}.ico"/>${name}</a>
310           <a class="bmove" href="${SCRIPT_NAME}?action=bmup&fid=${fid}&bid=${bid}">move left</a>
311           <a class="bmove" href="${SCRIPT_NAME}?action=bmdn&fid=${fid}&bid=${bid}">move right</a>
312         </div>
313         EOF
314   done
315 }
316
317 foldermod(){
318   fmodify="$(GET fmodify |checkid )"
319   fdelete="$(GET fdelete |checkid )"
320   fmove="$(GET fmove |checkid )"
321
322   if [ -n "$fmodify" ]; then
323     read fid fname order <"${BDB}/${fmodify}.bm"
324     cat <<-EOF
325         <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?action=modfolder">
326           <h1>Rename Folder: ${fname}</h1>
327           <input type="hidden" name="fid" value="${fid}" />
328           <label class="tab">Rename</label>
329           <a class="tab"
330             href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
331           <a class="tab"
332             href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
333           <input type="text" name="name" value="${fname}" />
334           <button type="submit" name="control" value="confirm">OK</button>
335           <button type="submit" name="control" value="cancel">Cancel</button>
336         </form>
337         EOF
338   elif [ -n "$fdelete" ]; then
339     read fid fname order <"${BDB}/${fdelete}.bm"
340     cat <<-EOF
341         <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?action=delfolder">
342           <h1>Delete Folder: ${fname}</h1>
343           <input type="hidden" name="fid" value="${fid}" />
344           <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
345           <label class="tab">Delete</label>
346           <a class="tab"
347             href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
348           <label>Pass Bookmarks on to:</label>
349           <select name="target">
350             $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
351             <option value="____________">(discard)</option>
352           </select>
353           <button type="submit" name="control" value="confirm">OK</button>
354           <button type="submit" name="control" value="cancel">Cancel</button>
355         </form>
356         EOF
357   elif [ -n "$fmove" ]; then
358     read fid fname order <"${BDB}/${fmove}.bm"
359     cat <<-EOF
360         <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?action=movefolder">
361           <h1>Move Folder: ${fname}</h1>
362           <input type="hidden" name="fid" value="${fid}" />
363           <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
364           <a class="tab" href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
365           <label class="tab">Move</label>
366           <label>Move before folder:</label>
367           <select name="target">
368             $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
369             <option value="____________">(last)</option>
370           </select>
371           <button type="submit" name="control" value="confirm">OK</button>
372           <button type="submit" name="control" value="cancel">Cancel</button>
373         </form>
374         EOF
375   fi
376 }
377
378 show_folders(){
379   list_folders \
380   | while read fid fname order; do
381     cat <<-EOF
382         <section class="folder" id="${fid}">
383           <h1>${fname}</h1>
384           <a class="modify" href="${SCRIPT_NAME}?fmodify=${fid}">Modify</a>
385           $(show_bookmarks "$fid")
386           <form class="newbookmark" method="POST" action="${SCRIPT_NAME}?action=newbookmark">
387             <input type="hidden" name="fid" value="${fid}" />
388             <input type="text" name="name" value="" placeholder="Name" />
389             <input type="text" name="url"  value="" placeholder="URL" />
390             <button type="submit">New Bookmark</button>
391           </form>
392         </section>
393         EOF
394   done
395 }
396
397 SET_COOKIE +8640000 "id=${COKID}"      # Refresh Cookie
398 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
399
400 cat <<EOF
401 <!DOCTYPE HTML>
402 <HTML><head>
403   <title>Bookman - Your Collection</title>
404   <link rel="stylesheet" type="text/css" href="bookmarks.css" />
405 </head><body id="collection">
406   $(foldermod)
407   $(bookmarkmod)
408   $(show_folders)
409   <form class="newfolder" method="POST" action="${SCRIPT_NAME}?action=newfolder">
410     <input type="text" name="name" value="" placeholder="New Folder" />
411     <button type="submit">New</button>
412   </form>
413 </body></HTML>
414 EOF
415
416 #set filetype=sh