]> git.plutz.net Git - bookman/blob - index.cgi
8129b7e3434edcd0c26f70fcfa006f237e2bf49f
[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 QRYID="$(GET    id |checkid)"
76 COKID="$(COOKIE id |checkid)"
77 BDB="users/${QRYID}"
78
79 case "$(GET action)" in
80   newid)
81     NEWID="$(genid)"
82
83     { git init "users/${NEWID}" || mkdir -p "users/${NEWID}"; } >&-
84
85     printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL"
86     printf 'Location: %s\r\n' "${SCRIPT_NAME}?id=${NEWID}"
87     SET_COOKIE +8640000 "id=${NEWID}"
88     printf '\r\n'
89     exit 0
90     ;;
91   newfolder)
92     name="$(POST name |head -n1)"
93     fid="$(timeid)"
94     order="$(
95       head -qn1 "${BDB}"/????????????.bm \
96       | cut -f3 \
97       | sort -n \
98       | tail -n1 \
99       || printf 1
100     )"
101     order="$(((order + 1000) / 1000 * 1000))"
102     if [ -n "$name" -a -d "${BDB}" ]; then
103       printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${BDB}/${fid}.bm"
104     fi
105     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
106     ;;
107   modfolder)
108     name="$(POST name |head -n1)"
109     fid="$(POST fid | checkid)"
110     file="${BDB}/${fid}.bm"
111     if [ "$(POST control)" = confirm -a -n "$name" -a -f "$file" ]; then
112       order="$(head -n1 "$file" |cut -f3 || printf 1000)"
113       printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${file%.bm}.tmp"
114       tail -n+2 "$file" >>"${file%.bm}.tmp"
115       mv "${file%.bm}.tmp" "$file"
116     fi
117     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
118     ;;
119   newbookmark)
120     fid="$(POST fid | checkid)"
121     name="$(POST name |head -n1)"
122     url="$(POST url |head -n1)"
123     file="${BDB}/${fid}.bm"
124     bid="$(timeid)"
125     if [ -n "$name" -a -f "${file}" ]; then
126       printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$(HTML "$url")" >>"${file}"
127     fi
128     getFavicon "$url" "$bid"
129     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
130     ;;
131   modbookmark)
132     bid="$(POST bid | checkid)"
133     name="$(POST name |head -n1)"
134     url="$(POST url |head -n1)"
135     file="$(grep -lE "^${bid}" "${BDB}"/????????????.bm)"
136     if [ -w "$file" -a -n "$name" -a -n "$url" ]; then
137       bm="$(printf '%s\t%s\t%s' "$bid" "$(HTML "$name")" "$(HTML "$url")" |sed -r 's;[\&\;];\\&;g;')"
138       sed -ri "s;^${bid}\t.*$;${bm};" "$file"
139     fi
140     getFavicon "$url" "$bid"
141     REDIRECT "${SCRIPT_NAME}?id=${QRYID}#${fid}"
142     ;;
143 esac
144
145 if [ -z "$QRYID" -a -n "$COKID" ]; then
146   REDIRECT "${SCRIPT_NAME}?id=${COKID}"
147 elif [ -n "$QRYID" -a -z "$COKID" ]; then
148   SET_COOKIE +8640000 "id=${QRYID}"
149 fi
150
151 if [ -z "$QRYID" -a -z "$COKID" ]; then
152   printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
153
154   cat <<-EOF
155         <!DOCTYPE HTML>
156         <HTML><head>
157           <title>Bookman - New Collection</title>
158         </head><body id="newcollection">
159           <h1>You have not yet set up a collection on this server.</h1>
160           Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
161         </body></HTML>
162         EOF
163   exit 0
164 elif ! [ -d "users/${QRYID}" ]; then
165   printf '%s 404 Not Found\r\n' "$SERVER_PROTOCOL"
166   printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
167
168   cat <<-EOF
169         <!DOCTYPE HTML>
170         <HTML><head>
171           <title>Bookman - 404</title>
172         </head><body id="missingcollection">
173           <h1>The collection you requested does not exist on this server.</h1>
174           Click <a href="${SCRIPT_NAME}?action=newid">here</a> to start a new collection.
175         </body></HTML>
176         EOF
177   exit 0
178 fi
179
180 list_bookmarks(){
181   fid="$1"
182   bmodify="$(GET bmodify |checkid)"
183
184   tail -n+2 "${BDB}/${fid}.bm" \
185   | while read bid name url; do
186     if [ "${bid}" = "$bmodify" ]; then
187       cat <<-EOF
188         <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modbookmark">
189           <input type="hidden" name="bid" value="${bid}" />
190           <input type="text" name="name" value="${name}")" placeholder="Name" />
191           <input type="text" name="url"  value="${url}")" placeholder="URL" />
192           <button type="submit">Modify</button>
193         </form>
194         EOF
195     else
196       cat <<-EOF
197         <div class="bookmark">
198           <a class="modify" href="${SCRIPT_NAME}?id=${QRYID}&bmodify=${bid}">Modify</a>
199           <a class="link" href="${url}")"><img src="${BDB}/favicons/${bid}.ico"/>${name}</a>
200         </div>
201         EOF
202     fi
203   done
204 }
205
206 list_folders(){
207   fmodify="$(GET fmodify |checkid )"
208   fdelete="$(GET fdelete |checkid )"
209   fmove="$(GET fmove |checkid )"
210
211   head -qn1 "${BDB}"/????????????.bm \
212   | sort -nk3 \
213   | while read fid fname order; do
214     cat <<-EOF
215         <section class="folder" id="${fid}">
216           <h1>${fname}</h1>
217           <a class="modify" href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Modify</a>
218           $(list_bookmarks "$fid")
219           <form class="newbookmark" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=newbookmark">
220             <input type="hidden" name="fid" value="${fid}" />
221             <input type="text" name="name" value="" placeholder="Name" />
222             <input type="text" name="url"  value="" placeholder="URL" />
223             <button type="submit">New Bookmark</button>
224           </form>
225         </section>
226         EOF
227     if [ "$fid" = "$fmodify" ]; then
228       cat <<-EOF
229         <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
230           <input type="hidden" name="fid" value="${fid}" />
231           <label>Rename</label><a
232             href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a><a
233             href="${SCRIPT_NAME}?id=${QRYID}&fmove=${fid}">Move</a>
234           <input type="text" name="name" value="${fname}" />
235           <button type="submit" name="control" value="confirm">OK</button>
236           <button type="submit" name="control" value="cancel">Cancel</button>
237         </form>
238         EOF
239     elif [ "$fid" = "$fdelete" ]; then
240       cat <<-EOF
241         <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
242           <input type="hidden" name="fid" value="${fid}" />
243           <a href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a><label
244             >Delete</label><a
245             href="${SCRIPT_NAME}?id=${QRYID}&fmove=${fid}">Move</a>
246           <input type="text" name="name" value="${fname}" />
247           <button type="submit" name="control" value="confirm">OK</button>
248           <button type="submit" name="control" value="cancel">Cancel</button>
249         </form>
250         EOF
251     elif [ "$fid" = "$fmove" ]; then
252       cat <<-EOF
253         <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=modfolder">
254           <input type="hidden" name="fid" value="${fid}" />
255           <a href="${SCRIPT_NAME}?id=${QRYID}&fmodify=${fid}">Rename</a><a
256             href="${SCRIPT_NAME}?id=${QRYID}&fdelete=${fid}">Delete</a><label
257             >Move</label>
258           <input type="text" name="name" value="${fname}" />
259           <button type="submit" name="control" value="confirm">OK</button>
260           <button type="submit" name="control" value="cancel">Cancel</button>
261         </form>
262         EOF
263     fi
264   done
265 }
266
267 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
268 cat <<EOF
269 <!DOCTYPE HTML>
270 <HTML><head>
271   <title>Bookman - Your Collection</title>
272   <link rel="stylesheet" type="text/css" href="bookmarks.css" />
273 </head><body id="collection">
274   $(list_folders)
275   <form class="newfolder" method="POST" action="${SCRIPT_NAME}?id=${QRYID}&action=newfolder">
276     <input type="text" name="name" value="" placeholder="New Folder" />
277     <button type="submit">New</button>
278   </form>
279 </body></HTML>
280 EOF
281
282 #set filetype=sh