]> git.plutz.net Git - bookman/blob - index.cgi
98e6719e4f9edbe986d984716c9ac3a146906096
[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       sed -ri "1s'^(${fid}\t)[^\t]+(\t.+)$'\1${name}\2';" "$file"
164     fi
165     REDIRECT "${SCRIPT_NAME}#${fid}"
166     ;;
167   delfolder)
168     fid="$(POST fid | checkid)"
169     target="$(POST target | checkid)"
170     file="${BDB}/${fid}.bm"
171     tfile="${BDB}/${target}.bm"
172     if [ "$(POST control)" = confirm -a -f "$file" ]; then
173       if [ "$target" = "____________" ] || tail -n+2 "$file" >>"$tfile"; then
174         rm -f "$file"
175       fi
176     fi
177     REDIRECT "${SCRIPT_NAME}#${target}"
178     ;;
179   movefolder)
180     fid="$(POST fid | checkid)"
181     target="$(POST target | checkid)"
182     file="${BDB}/${fid}.bm"
183     tfile="${BDB}/${target}.bm"
184     if [ "$target" = "____________" -a -f "$file" -a "$(POST control)" = confirm ]; then
185       read nan1 nan2 last nan3 <"${BDB}/$(list_folders |tail -n1 |cut -f1).bm"
186       sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${last:-0} + 1000))\3;;" "$file"
187     elif [ -f "$tfile" -a -f "$file" -a "$(POST control)" = confirm ]; then
188       read nan1 nan2 tid nan3 <"$tfile"
189       sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${tid:-1} -1))\2;;" "$file"
190       order_files
191     fi
192     REDIRECT "${SCRIPT_NAME}#${fid}"
193     ;;
194   newbookmark)
195     fid="$(POST fid | checkid)"
196     name="$(POST name |head -n1)"
197     url="$(POST url |head -n1)"
198     file="${BDB}/${fid}.bm"
199     bid="$(timeid)"
200     if [ -n "$name" -a -f "${file}" ]; then
201       printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$url" >>"${file}"
202     fi
203     getFavicon "$url" "$bid"
204     REDIRECT "${SCRIPT_NAME}#${fid}"
205     ;;
206   modbookmark)
207     bid="$(POST bid | checkid)"
208     name="$(POST name |head -n1)"
209     url="$(POST url |head -n1)"
210     file="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
211     if [ -n "$name" -a -n "$url" -a "$(POST control)" = confirm -a -w "$file" ]; then
212       sed -rni "/^${bid}\t/!p; /^${bid}\t/i${bid}\t$(HTML "$name")\t${url}" "$file"
213     fi
214     getFavicon "$url" "$bid"
215     REDIRECT "${SCRIPT_NAME}#${fid}"
216     ;;
217   movebookmark)
218     bid="$(POST bid | checkid)"
219     fid="$(POST target | checkid)"
220     sfile="$(grep -lm1 "^${bid}" "${BDB}"/????????????.bm |head -n1)"
221     tfile="${BDB}/${fid}.bm"
222
223     if [ "$(POST control)" = confirm -a -n "$bid" -a -w "$sfile" -a -w "$tfile" ]; then
224       grep -m1 "^${bid}" "$sfile" >>"$tfile" \
225       && sed -ri "0,/^${bid}/{/^${bid}/d;}" "$sfile"
226     fi
227     REDIRECT "${SCRIPT_NAME}#${fid}"
228     ;;
229   bmup)
230     fid="$(GET fid |checkid)"
231     bid="$(GET bid |checkid)"
232     file="${BDB}/${fid}.bm"
233
234     if [ -n "$bid" -a -n "$fid" ] && grep -q "^${bid}" "$file"; then
235       sed -ri ":X;\$bY;N;bX;:Y; s;(\n[^\n]+)(\n${bid}\t[^\n]+);\2\1;;" "$file"
236     fi
237     REDIRECT "${SCRIPT_NAME}#${fid}"
238     ;;
239   bmdn)
240     fid="$(GET fid |checkid)"
241     bid="$(GET bid |checkid)"
242     file="${BDB}/${fid}.bm"
243
244     if [ -n "$bid" -a -n "$fid" ] && grep -q "^${bid}" "$file"; then
245       sed -ri ":X;\$bY;N;bX;:Y; s;(\n${bid}\t[^\n]+)(\n[^\n]+);\2\1;;" "$file"
246     fi
247     REDIRECT "${SCRIPT_NAME}#${fid}"
248     ;;
249   query)
250     fid="$(POST fid |checkid)"
251     bid="$(POST bid |checkid)"
252     file="${BDB}/${fid}.bm"
253     query="$(URL "$(POST query)")"
254
255
256     url="$(grep -m1 "^${bid}" "$file" |cut -f3-)"
257     urlpfx="${url%\{@\}*}"
258     urlsfx="${url#*\{@\}}"
259
260     REDIRECT "${urlpfx}${query}${urlsfx}"
261     ;;
262 esac
263
264 bookmarkmod(){
265   bmod="$(GET bmodify |checkid)"
266   bmove="$(GET bmove |checkid)"
267
268   if [ -n "$bmod" ]; then
269     file="$(grep -lm1 "^${bmod}" "${BDB}/"????????????.bm |head -n1)"
270     read bid name url <<-EOF
271         $(grep -m1 "^${bmod}" "$file")
272         EOF
273     cat <<-EOF
274         <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?action=modbookmark">
275           <input type="hidden" name="bid" value="${bid}" />
276           <h1>Modify: ${name}</h1>
277           <label class="tab">Modify</label>
278           <a class="tab" href="${SCRIPT_NAME}?bmove=${bid}">Move</a>
279           <label>Name:</label>
280           <input type="text" name="name" value="${name}")" placeholder="Name" />
281           <label>URL:</label>
282           <input type="text" name="url"  value="$(HTML "${url}")")" placeholder="URL" />
283           <button type="submit" name="control" value="confirm">OK</button>
284           <button type="submit" name="control" value="cancel">Cancel</button>
285         </form>
286         EOF
287   elif [ -n "$bmove" ]; then
288     file="$(grep -lm1 "^${bmove}" "${BDB}/"????????????.bm |head -n1)"
289     read bid name url <<-EOF
290         $(grep -m1 "^${bmove}" "$file")
291         EOF
292     cat <<-EOF
293         <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?action=movebookmark">
294           <input type="hidden" name="bid" value="${bid}" />
295           <h1>Move: ${name}</h1>
296           <a class="tab" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
297           <label class="tab">Move</label>
298           <label>Move to Folder:</label>
299           <select name="target">
300             $(printf '<option value="%s">%s</option>' $(list_folders))
301           </select>
302           <button type="submit" name="control" value="confirm">OK</button>
303           <button type="submit" name="control" value="cancel">Cancel</button>
304         </form>
305         EOF
306   fi
307 }
308
309 show_bookmarks(){
310   fid="$1"
311   bmodify="$(GET bmodify |checkid)"
312
313   tail -n+2 "${BDB}/${fid}.bm" \
314   | while read bid name url; do
315     if [ "${url%\{@\}*}" = "${url}" ]; then
316       cat <<-EOF
317         <div class="bookmark">
318           <a class="modify" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
319           <a class="link" target="_blank" href="$(HTML "${url}")")"><img src="${BDB}/favicons/${bid}.ico"/>${name}</a>
320           <a class="bmove" href="${SCRIPT_NAME}?action=bmup&fid=${fid}&bid=${bid}">move left</a>
321           <a class="bmove" href="${SCRIPT_NAME}?action=bmdn&fid=${fid}&bid=${bid}">move right</a>
322         </div>
323         EOF
324     else
325       cat <<-EOF
326         <form class="bookmark" target="_blank" method="POST" action="${SCRIPT_NAME}?action=query">
327           <a class="modify" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
328           <input type="hidden" name="fid" value="$fid" />
329           <input type="hidden" name="bid" value="$bid" />
330           <img src="${BDB}/favicons/${bid}.ico"/><input name="query" placeholder="$name"/>
331           <a class="bmove" href="${SCRIPT_NAME}?action=bmup&fid=${fid}&bid=${bid}">move left</a>
332           <a class="bmove" href="${SCRIPT_NAME}?action=bmdn&fid=${fid}&bid=${bid}">move right</a>
333         </form>
334         EOF
335     fi
336   done
337 }
338
339 foldermod(){
340   fmodify="$(GET fmodify |checkid )"
341   fdelete="$(GET fdelete |checkid )"
342   fmove="$(GET fmove |checkid )"
343
344   if [ -n "$fmodify" ]; then
345     read fid fname order <"${BDB}/${fmodify}.bm"
346     cat <<-EOF
347         <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?action=modfolder">
348           <h1>Rename Folder: ${fname}</h1>
349           <input type="hidden" name="fid" value="${fid}" />
350           <label class="tab">Rename</label>
351           <a class="tab"
352             href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
353           <a class="tab"
354             href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
355           <input type="text" name="name" value="${fname}" />
356           <button type="submit" name="control" value="confirm">OK</button>
357           <button type="submit" name="control" value="cancel">Cancel</button>
358         </form>
359         EOF
360   elif [ -n "$fdelete" ]; then
361     read fid fname order <"${BDB}/${fdelete}.bm"
362     cat <<-EOF
363         <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?action=delfolder">
364           <h1>Delete Folder: ${fname}</h1>
365           <input type="hidden" name="fid" value="${fid}" />
366           <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
367           <label class="tab">Delete</label>
368           <a class="tab"
369             href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
370           <label>Pass Bookmarks on to:</label>
371           <select name="target">
372             $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
373             <option value="____________">(discard)</option>
374           </select>
375           <button type="submit" name="control" value="confirm">OK</button>
376           <button type="submit" name="control" value="cancel">Cancel</button>
377         </form>
378         EOF
379   elif [ -n "$fmove" ]; then
380     read fid fname order <"${BDB}/${fmove}.bm"
381     cat <<-EOF
382         <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?action=movefolder">
383           <h1>Move Folder: ${fname}</h1>
384           <input type="hidden" name="fid" value="${fid}" />
385           <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
386           <a class="tab" href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
387           <label class="tab">Move</label>
388           <label>Move before folder:</label>
389           <select name="target">
390             $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
391             <option value="____________">(last)</option>
392           </select>
393           <button type="submit" name="control" value="confirm">OK</button>
394           <button type="submit" name="control" value="cancel">Cancel</button>
395         </form>
396         EOF
397   fi
398 }
399
400 show_folders(){
401   list_folders \
402   | while read fid fname order; do
403     file="${BDB}/${fid}.bm"
404     cache="${BDB}/${fid}.cache"
405     if [ "${cache}" -nt "${file}" ]; then
406       cat "$cache"
407     else
408       tee "$cache" <<-EOF
409         <section class="folder" id="${fid}">
410           <h1>${fname}</h1>
411           <a class="modify" href="${SCRIPT_NAME}?fmodify=${fid}">Modify</a>
412           $(show_bookmarks "$fid")
413           <form class="newbookmark" method="POST" action="${SCRIPT_NAME}?action=newbookmark">
414             <input type="hidden" name="fid" value="${fid}" />
415             <input type="text" name="name" value="" placeholder="Name" />
416             <input type="text" name="url"  value="" placeholder="URL" />
417             <button type="submit">New Bookmark</button>
418           </form>
419         </section>
420         EOF
421     fi
422   done
423 }
424
425 SET_COOKIE +8640000 "id=${COKID}"      # Refresh Cookie
426 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
427
428 cat <<EOF
429 <!DOCTYPE HTML>
430 <HTML><head>
431   <title>Bookman - Your Collection</title>
432   <link rel="stylesheet" type="text/css" href="bookmarks.css" />
433 </head><body id="collection">
434   $(foldermod)
435   $(bookmarkmod)
436   $(show_folders)
437   <form class="newfolder" method="POST" action="${SCRIPT_NAME}?action=newfolder">
438     <input type="text" name="name" value="" placeholder="New Folder" />
439     <button type="submit">New</button>
440   </form>
441 </body></HTML>
442 EOF
443
444 #set filetype=sh