]> git.plutz.net Git - busy/blob - actions/image_upload.cgi
upgraded to fit new shcgi revision, slightly better input validation, switched to...
[busy] / actions / image_upload.cgi
1 #!/bin/zsh
2 # Copyright 2011 Paul Haensch
3 # This file is part of Kinoserver
4 #
5 # Kinoserver is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Kinoserver is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public License
16 # along with Kinoserver.  If not, see <http://www.gnu.org/licenses/>.
17
18 page="$(egrep -o '(^|&)p=[a-zA-Z0-9]+(&|$)' <<<"${QUERY_STRING}" |sed 's:.*p=::;s:&::g')"
19 info="$(egrep -o '(^|&)i=[0-9]{10}_[0-9]{3}(&|$)' <<<"${QUERY_STRING}" |tr -d 'i=&')"
20 [ -z "$page" ] && page="Locations"
21 [ -z "$info" ] && info="$(date +%s)_$(apg -M N -a 1 -n 1 -m 3 -x 3)"
22
23 if [ -z "${CONTENT_LENGTH}" -o "${CONTENT_LENGTH}" -le 0 ]; then
24   echo -n "Location: http://${HTTP_HOST}/?p=Error&r=tooSmall\n\n"
25   exit 0
26 elif [ "${CONTENT_LENGTH}" -gt 33554432 ]; then
27   echo -n "Location: http://${HTTP_HOST}/?p=Error&r=tooBig\n\n"
28   exit 0
29 fi
30
31 case "$page" in
32   Photographs) imgpath="../Photographs/${info}";;
33   Locations) imgpath="../Locations";;
34 esac
35 basename="$(date +%s)_$(apg -M N -a 1 -n 1 -m 3 -x 3)"
36 filename="$imgpath/${basename}_meta.upload"
37 mkdir -p "$imgpath"
38
39 n=1
40 (head -c "${CONTENT_LENGTH}") |while line="$(line)"; do
41   if [ -z "${delim}" ]; then
42     delim="${line}"
43     end="$(echo "${delim}" |sed 's;\r;--\r;')"
44   fi
45   if [ "${line}" = "${delim}" ]; then
46     field=$(egrep -o 'name="[a-zA-Z0-9]+"' <<<$(line) |sed 's,name=",,;s,"$,,')
47     line="$(line)"                              #consume a newline
48     [ "$field" = "picture" ] && line="$(line)"  #consume additional newline for file upload
49     if [ -n "$filename" ]; then                 #finish last uploaded file by removing trailing CR|LF
50       size=$(stat -c %s "$filename")
51       truncate -s $(($size - 2)) "$filename"
52     fi
53     if [ "$field" = "picture" ]; then
54       if [ "$page" = "Locations" -a "$filename" = "${imgpath}/${basename}_meta.upload" ]; then
55         echo -e "${lat}\t${lon}\t${title}" > "$filename"
56         echo -e "${tags}" |tr '\n' '|' >> "$filename"
57         echo -e "\n${desc}" >> "$filename"
58       elif [ "$page" = "Photographs" -a "$filename" = "${imgpath}/${basename}_meta.upload" ]; then
59         echo -e "name=$name" > "$filename"
60       fi
61       filename="${imgpath}/${basename}_$n.upload"
62       touch "$filename"
63     fi
64     n=$((n+1))
65   elif [ "${line}" = "${end}" ]; then
66     size=$(stat -c %s $filename)
67     truncate -s $(($size - 2)) $filename
68   else
69     case "$field" in
70       picture)  echo -E "$line" >>$filename
71         ;;
72       title)    title="$(sed 's/\t/ /g;s/\r|\n//g;s/\\/\\\\/g' <<<"$line")"
73         ;;
74       name)     name="$(sed 's/\t/ /g;s/\r|\n//g;s/\\/\\\\/g' <<<"$line")"
75         ;;
76       lat)      lat="$(egrep '[.0-9]{1,8}' <<<"$line" |tr -d '\r')"
77         ;;
78       lon)      lon="$(egrep '[.0-9]{1,8}' <<<"$line" |tr -d '\r')"
79         ;;
80       tags)     tags="${tags}$(sed 's/\t/ /g;s/\r//g;s/\\/\\\\/g' <<<"$line")\n"
81         ;;
82       desc)     desc="${desc}$(sed 's/\t/ /g;s/\r//g;s/\\/\\\\/g' <<<"$line")\n"
83         ;;
84     esac
85   fi
86 done
87
88 n=1
89 for each in ${imgpath}/${basename}_<0-9999>.upload; do
90   convert "$each" "${imgpath}/${basename}_$n.jpg"
91   rm "$each"
92   if [ "$page" = "Locations" -a -f "${imgpath}/${basename}_$n.jpg" ]; then
93     info="${basename}_$n"
94     cp "${imgpath}/${basename}_meta.upload" "${imgpath}/${basename}_${n}.meta"
95   fi
96   n=$((n+1))
97 done
98 [ "$page" = "Photographs" -a ! -f "${imgpath}.meta" ] && cp "${imgpath}/${basename}_meta.upload" "${imgpath}.meta"
99 rm "${imgpath}/${basename}_meta.upload"
100
101 if [ -z "$info" ]; then
102   echo -n  "Location: http://${HTTP_HOST}/?p=Error&r=noPic\n\n"
103 else
104   echo -n "Location: http://${HTTP_HOST}/?p=${page}&i=$info#$info\n\n"
105 fi