Fix potential security hole
[bpgallery.git] / bpgallery.sh
1 #!/usr/bin/env bash
2
3 # bpgallery.sh - builds a simple 'gallery' from a collection of images
4 # Copyright (C) 2004 Brett Parker <iDunno@sommitrealweird.co.uk>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 set -f
21
22 VERSION="1.0.3"
23
24 function bpgallery_default_head() {
25 cat <<END
26 <?xml version="1.0"?>
27 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
28 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
29 <head>
30         <title>${TITLE}</title>
31         <link rel="stylesheet" href="style.css" type="text/css" />
32 </head>
33 <body>
34 END
35 }
36
37 declare -rf bpgallery_default_head
38
39 function bpgallery_default_description() {
40 cat <<END
41         <h1>${TITLE}</h1>
42         ${DESCRIPTION}
43 END
44 }
45
46 declare -rf bpgallery_default_description
47
48 function bpgallery_default_thumbsline() {
49 if [[ -z $caption ]]; then
50         caption_alt=Unknown
51 else
52         caption_alt=$caption
53 fi
54 cat << END
55 <div class="thumbnail"><a href="$link"><img src="icons/$filename" alt="$caption_alt" /></a><div class="caption">$caption</div></div>
56 END
57 }
58
59 declare -rf bpgallery_default_thumbsline
60
61 function bpgallery_default_tail() {
62 cat << END
63 </body>
64 </html>
65 END
66 }
67
68 declare -rf bpgallery_default_tail
69
70 function bpgallery_default_stylesheet() {
71 cat <<END
72 body {
73         background: white;
74         color: black;
75         font-family: sans-serif;
76         font-size: 10pt;
77 }
78
79 div.thumbnail {
80         float: left;
81         padding: 20px;
82         border: 0px;
83         width: ${WIDTH}px;
84         height: ${MAXHEIGHT}px;
85 }
86
87 div.caption {
88         width: 100%;
89         text-align: center;
90         font-weight: bold;
91 }
92
93 div.navigation {
94         width: 100%;
95         margin: 2em;
96         text-align: center;
97 }
98
99 div.navigation ul {
100         width: 100%;
101         display: inline;
102         text-align: center;
103 }
104
105 div.navigation ul li {
106         display: inline;
107         margin: 2em;
108 }
109
110 a {
111         border: 0px;
112 }
113
114 img {
115         border: 0px;
116 }
117 END
118 }
119
120 declare -rf bpgallery_default_stylesheet
121
122 function bpgallery_default_page() {
123         $BPGALLERY_HEAD_FUNCTION
124         cat <<END
125 <h1>${caption}</h1>
126 <div class="navigation">
127         <ul>
128                 <li><a href='index.html'>Thumbnails</a></li>
129 END
130         if [[ ! -z $previouspage ]]; then
131                 echo "<li><a href='${previouspage}'>Previous</a></li>"
132         else
133                 echo "<li>Previous</li>"
134         fi
135
136         if [[ ! -z $nextpage ]]; then
137                 echo "<li><a href='${nextpage}'>Next</a></li>"
138         else
139                 echo "<li>Next</li>"
140         fi
141 cat <<END
142         </ul>
143 </div>
144 <div class="mainimage">
145         <img src="${filename}" alt="${caption}" />
146 </div>
147 END
148         $BPGALLERY_TAIL_FUNCTION
149 }
150
151 declare -rf bpgallery_default_page
152
153 function bpgallery_escape_url() {
154         temp=$1
155         temp=${temp//\%/%25}
156         temp=${temp//:/%3A}
157         temp=${temp//;/%3B}
158         temp=${temp// /%20}
159         temp=${temp//$/%24}
160         temp=${temp//&/%26}
161         temp=${temp//+/%2B}
162         temp=${temp//,/%2C}
163         temp=${temp//\//%2F}
164         temp=${temp//=/%3D}
165         temp=${temp//\?/%3F}
166         temp=${temp//@/%40}
167         temp=${temp//\"/%22}
168         $ECHOCOMMAND $temp
169 }
170
171 declare -rf bpgallery_escape_url
172
173 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
174         declare -r BPGALLERY_THEME
175 fi
176
177 if [[ ! -z ${TITLE} ]] ; then
178         declare -r TITLE
179 fi
180
181 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
182         . /usr/local/etc/bpgallery/config 2>/dev/null
183 fi
184
185 if [[ -e /etc/bpgallery/config ]] ; then
186         . /etc/bpgallery/config 2>/dev/null
187 fi
188
189 if [[ -e $HOME/.bpgallery.rc ]]; then
190         . $HOME/.bpgallery.rc 2>/dev/null
191 fi
192
193 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
194         . $1/.bpgallery.rc 2>/dev/null
195 fi
196
197 if [[ -z ${BPGALLERY_THEME} ]]; then
198         BPGALLERY_THEME=default
199 fi
200
201 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
202         [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
203         . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
204 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
205         . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
206 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
207         . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
208 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
209         . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
210 fi
211
212 if [[ -z $TITLE ]]; then 
213         TITLE="Photo Gallery"
214 fi
215
216 if [[ -z $CONVERTTOOL ]]; then
217         CONVERTTOOL=convert
218 fi
219
220 if [[ -z $FINDCOMMAND ]]; then
221         FINDCOMMAND=find
222 fi
223
224 if [[ -z $XARGSCOMMAND ]]; then
225         XARGSCOMMAND=xargs
226 fi
227
228 if [[ -z $ECHOCOMMAND ]]; then
229         ECHOCOMMAND=echo
230 fi
231
232 if [[ -z $SORTCOMMAND ]]; then
233         SORTCOMMAND=sort
234 fi
235
236 if [[ -z $IDENTIFYCOMMAND ]]; then
237         IDENTIFYCOMMAND=identify
238 fi
239
240 if [[ -z $HEADCOMMAND ]]; then
241         HEADCOMMAND=head
242 fi
243
244 if [[ -z $GREPCOMMAND ]]; then
245         GREPCOMMAND=grep
246 fi
247
248 if [[ -z $SEDCOMMAND ]]; then
249         SEDCOMMAND=sed
250 fi
251
252 if [[ -z $WIDTH ]]; then
253         WIDTH=100
254 fi
255
256 if [[ -z $IMAGEEXTENSIONS ]]; then
257         IMAGEEXTENSIONS="jpeg jpg gif png";
258 fi
259
260 if [[ -z $WCCOMMAND ]]; then
261         WCCOMMAND="wc -l"
262 fi
263
264 if [[ -z $CAPTIONHEIGHT ]]; then
265         CAPTIONHEIGHT=75
266 fi
267
268 if [[ -z $OUTPUTHTML ]]; then
269         OUTPUTHTML=0
270 fi
271
272 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
273         GENERATEPAGESFORFULLSIZE=0
274 fi
275
276 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
277         if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
278                 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
279         else
280                 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
281         fi
282 fi
283
284 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
285         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
286 else
287         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
288 fi
289
290 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
291         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
292 else
293         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
294 fi
295
296 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
297         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
298 else
299         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
300 fi
301
302 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
303         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
304 else
305         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
306 fi
307
308 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
309         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
310 else
311         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
312 fi
313
314 FINDIMAGESOPTIONS=""
315
316 for imageext in $IMAGEEXTENSIONS; do
317         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
318 done
319
320 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
321 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
322
323 function usage() {
324 cat <<END
325 Usage:
326   $0 [--help|--version|<path to images>]
327
328     --help
329         displays this help screen
330     --version
331         displays the version and exits
332         
333 END
334 }
335
336 function version() {
337 cat <<END
338 Version: $VERSION
339 END
340 }
341
342 function generate_thumbs() {
343         $ECHOCOMMAND "Generating Thumbnails"
344         currentimage=0
345         totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | $WCCOMMAND);
346         imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do filename=${filename//\"/\\\"}; if [ ! -r "icons/$filename" ] || [ "$filename" -nt "icons/$filename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND $filename; fi; done | $WCCOMMAND)
347         
348         if [ $totalimages -eq $imagestoupdate ]; then
349                 $ECHOCOMMAND "Regenerating all thumbnails"
350         elif [ $imagestoupdate -eq 0 ]; then
351                 $ECHOCOMMAND "No Updated Thumbs, not regenerating"
352                 return
353         else
354                 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages thumbnails"
355         fi
356         
357         $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do tempfilename=${filename//\"/\\\"/}; if [ ! -r "icons/$tempfilename" ] || [ "$tempfilename" -nt "icons/$tempfilename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND -n $filename; $ECHOCOMMAND -n -e "\000"; fi; done | $XARGSCOMMAND -0 --verbose --max-procs=4 --replace $CONVERTTOOL -resize $WIDTH '{}' 'icons/{}' 2>&1 | while read throwout; do $ECHOCOMMAND done: $currentimage/$imagestoupdate images; currentimage=$((currentimage+1)); done
358         $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
359         $ECHOCOMMAND "Completed generating thumbs for $totalimages images"
360 }
361
362 function generate_pages() {
363         $ECHOCOMMAND "Generating Pages"
364         previouspage=""
365         currentpage=""
366         nextpage=""
367         previousimage=""
368         currentimage=""
369         nextimage=""
370
371         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
372         $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | \
373         $SORTCOMMAND -g | \
374         while read imagefilename; do
375                 previousimage=$currentimage
376                 currentimage=$nextimage
377                 nextimage=$imagefilename
378                 previouspage=$currentpage
379                 currentpage=$nextpage
380                 nextpage=${imagefilename}.html
381                 filename=${currentimage}
382                 generate_general_page "$previouspage" "$currentpage" "$nextpage"
383                 echo $nextimage
384         done | tail -n 2 | (
385                 read previouspage
386                 read currentpage
387                 filename=${currentpage}
388                 currentpage=${currentpage}.html
389                 previouspage=${previouspage}.html
390                 nextpage=""
391                 generate_general_page "$previouspage" "$currentpage" ""
392         )
393 }
394
395
396
397 function generate_general_page() {
398
399         if [[ -z $1 ]]; then
400                 previouspage=""
401         fi
402
403         if [[ -z $2 ]]; then
404                 currentpage=""
405                 return
406         fi
407
408         if [[ -z $3 ]]; then
409                 nextpage=""
410         fi
411
412         if [ -r captions.txt ]; then
413                 caption=$($GREPCOMMAND -E "^${filename}   " captions.txt); caption=${caption#*    }
414         else
415                 caption=""
416         fi
417
418         $BPGALLERY_PAGE_FUNCTION > "$currentpage"
419 }
420
421 if [[ $OUTPUTHTML != 0 ]]; then
422         $ECHOCOMMAND "<pre>"
423 fi
424
425 if [[ -z $1 ]]; then
426         $ECHOCOMMAND "No path given"
427         usage
428         exit 1
429 fi
430
431 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
432         usage
433         exit 0
434 fi
435
436 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
437         version
438         exit 0
439 fi
440
441 if [[ ! -d $1 ]]; then
442         $ECHOCOMMAND "$1 is not a directory"
443         usage
444         exit 2
445 fi
446
447 cd "$1"
448
449 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
450         $ECHOCOMMAND "$1 does not contain any images. Quitting."
451         exit 4
452 fi
453
454 if [ ! -w . ]; then
455         $ECHOCOMMAND "Can't write to images directory, exiting"
456         exit 8
457 fi
458
459 if [ -e index.html ] && [ ! -w index.html ]; then
460         $ECHOCOMMAND "Can't write index.html, exiting"
461         exit 8
462 fi
463
464 if [ -e style.css ] && [ ! -w style.css ]; then
465         $ECHOCOMMAND "Can't write style.css, exiting"
466         exit 8
467 fi
468
469 if [ ! -d icons ]; then
470         mkdir icons
471 fi
472
473 if [ ! -w icons ]; then
474         $ECHOCOMMAND "Can't write to icons directory, exiting"
475         exit 16
476 fi
477
478 generate_thumbs
479
480 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
481         generate_pages
482 fi
483
484 if [ -r description.txt ] ; then
485         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
486 else
487         DESCRIPTION=""
488 fi
489
490
491 $ECHOCOMMAND "Starting to generate page"
492
493 $BPGALLERY_HEAD_FUNCTION > index.html
494 $BPGALLERY_DESCRIPTION_FUNCTION >> index.html
495
496 $ECHOCOMMAND "Adding Captions"
497
498 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} |$SORTCOMMAND -g | while read filename; do filename=${filename#./}; if [ -r captions.txt ]; then caption=$($GREPCOMMAND -E "^$filename   " captions.txt); caption=${caption#*    }; else caption=""; fi; if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then link=$(bpgallery_escape_url "${filename}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> index.html
499
500 $BPGALLERY_TAIL_FUNCTION >> index.html
501
502 $ECHOCOMMAND "Finished generating the page"
503 $ECHOCOMMAND "Generating stylesheet"
504 cd icons
505
506 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
507 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
508
509 cd ..
510
511 # add a bit to the maxheight for the size of the caption
512 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
513
514 $BPGALLERY_STYLESHEET_FUNCTION > style.css
515
516 $ECHOCOMMAND "All done"
517
518 if [[ $OUTPUTHTML != 0 ]]; then
519         $ECHOCOMMAND "</pre>"
520 fi