wok view sane-backends/stuff/tazpanel/sane.cgi @ rev 21768

sane-backends/sane.cgi: do not limit parameters
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Jun 30 11:35:08 2019 +0200 (2019-06-30)
parents 9a1f50677074
children 5b7852dbf3f4
line source
1 #!/bin/sh
2 #
3 # Scanner CGI interface - Scan documents via a browser
4 #
5 # (C) 2015 SliTaz GNU/Linux - BSD License
6 #
8 # Common functions from libtazpanel
9 . lib/libtazpanel
10 get_config
12 #------
13 # menu
14 #------
16 case "$1" in
17 menu)
18 TEXTDOMAIN_original=$TEXTDOMAIN
19 export TEXTDOMAIN='sane'
21 cat <<EOT
22 <li><a data-icon="text" href="sane.cgi"$(groups | grep -q scanner ||
23 echo ' data-root')>$(_ 'Scanner')</a></li>
24 EOT
25 export TEXTDOMAIN=$TEXTDOMAIN_original
26 exit
27 esac
29 TITLE="$(_ 'TazPanel - Hardware') - $(_ 'Scanner')"
31 inrange() {
32 local n=$1
33 [ $1 -lt $2 ] && n=$2
34 [ $1 -gt $3 ] && n=$3
35 echo $n
36 }
38 getgeometry() {
39 CMD=""
40 ARGS=""
41 for i in x y l t ; do
42 j=$(inrange $(xPOST geometry_$i) $(xPOST ${i}_min) $(xPOST ${i}_max))
43 eval "geometry_$i=$j"
44 CMD="$CMD -$i $j"
45 ARGS="$ARGS $j"
46 done
47 for i in $(POST) ; do
48 case $i in
49 preview|format|resolution|res_min|res_max|l_min|l_max);;
50 geometry_l|t_min|t_max|geometry_t|x_min|x_max|geometry_x);;
51 y_min|y_max|geometry_y|tmpreview|device|params);;
52 *) v="$(xPOST $i)"; i="--${i//_/-}"
53 case "$v" in
54 '') ;;
55 yes|no) CMD="$CMD $i=$v" ;;
56 *) CMD="$CMD $i '$v'" ;;
57 esac
58 esac
59 done
60 resolution=${1:-0}
61 if [ $resolution -eq 0 ]; then
62 resolution=$(xPOST res_min)
63 width=$(GET width)
64 [ ${geometry_x:-0} -le 0 ] && geometry_x=$(xPOST x_max)
65 while [ $((${resolution:=150} * ${geometry_x%.*})) -lt ${width:-8192} ]; do
66 resolution=$(($resolution * 2))
67 done
68 fi
69 case "$(xPOST mode)" in
70 *lack*|*ineart*) mode="-monochrome" ;;
71 *ray*) mode="-colorspace gray" ;;
72 *) mode="" ;;
73 esac
74 [ -d tmp ] || ln -s /tmp tmp
75 case "$device" in
76 fake*) f=$(ls fake-sane/*.png | sed q)
77 [ -s "$f" ] || f=/usr/share/images/slitaz-banner.png
78 c="$(echo $ARGS $(identify $f | sed \
79 's/.* \([0-9]*\)x.*/\1/') $(GET width) $(POST x_max) | awk '
80 function a(x) { return int(($x * $5)/ $7); }
81 { printf "%dx%d+%d+%d -resize %dx%d",a(1),a(2),a(3),a(4),$6,int(($2*$6)/$1)}
82 ')"
83 suf="png"; [ "$1" ] && suf="pnm"
84 cat <<EOT
85 if convert -crop $c $mode $f /tmp/sane$$.$suf ; then
86 cat /tmp/sane$$.$suf
87 rm -f /tmp/sane$$.$suf
88 else
89 cat $f
90 fi 2> /dev/null
91 EOT
92 ;;
93 *) echo -n "scanimage -d '$(echo $device | sed 's/,.*//')' --resolution '$(inrange $resolution $(xPOST res_min) $(xPOST res_max))dpi'$CMD"
94 if [ -z "$1" -a "$(which convert)" ]; then
95 echo -n "> /tmp/sane$$.pnm ; convert -resize "
96 echo $ARGS $(GET width) | awk '{ printf "%dx%d",$5,int(($2*$5)/$1)}'
97 echo -n " /tmp/sane$$.pnm /tmp/sane$$.png ;"
98 echo -n "cat /tmp/sane$$.png ; rm -f /tmp/sane$$.pn?"
99 fi
100 esac
101 }
103 imgformat() {
104 tmp=$(mktemp -u -t tazsane.XXXXXX)
105 while read key name type exe pkg cmd ; do
106 case "$key" in
107 \#*) continue
108 esac
109 case "$1" in
110 list)
111 echo -n "<option value=\"$key\""
112 [ "$(which $exe 2> /dev/null)" ] ||
113 echo -n " disabled title=\"$exe not found: install $pkg\""
114 [ "$key" == "pnm" ] &&
115 echo -n " title=\"not supported by most browsers\""
116 echo ">$key" ;;
117 *)
118 case "$key" in
119 $(xPOST format)|'*')
120 case "$HTTP_USER_AGENT" in
122 # Tazweb has no download support
123 TazWe*) rm -f /tmp/$name
124 eval "$(getgeometry $(xPOST resolution)) $cmd >/tmp/$name" 2> $tmp.err
125 if [ -s /tmp/$name ]; then
126 info="Stored in /tmp/$name ($(stat -c %s /tmp/$name) bytes)."
127 else
128 error="$(sed 's|$|<br />|' $tmp.err)"
129 [ "$error" ] || error="I/O error"
130 fi
131 rm -f $tmp.* ;;
133 # Others should work
134 *) header "Content-Type: $type" \
135 "Content-Disposition: attachment; filename=$name" \
137 eval "$(getgeometry $(xPOST resolution)) $cmd"
138 rm -f $tmp.*
139 exit ;;
140 esac ;;
141 esac ;;
142 esac
143 done <<EOT
144 png tazsane.png image/png convert imagemagick > $tmp.pnm; convert $tmp.pnm png:-
145 jpeg tazsane.jpg image/jpeg convert imagemagick > $tmp.pnm; convert $tmp.pnm jpg:-
146 jpeg2000 tazsane.jp2 image/jpeg2000-image convert imagemagick > $tmp.pnm; convert $tmp.pnm jp2:-
147 tiff tazsane.tiff image/tiff convert imagemagick > $tmp.pnm; convert $tmp.pnm tiff:-
148 ps tazsane.ps application/postscript convert imagemagick > $tmp.pnm; convert -page A4+0+0 $tmp.pnm ps:-
149 pdf tazsane.pdf image/pdf convert imagemagick > $tmp.pnm; convert $tmp.pnm pdf:-
150 ocr1 tazsane-OCR1.txt text/plain gocr gocr | gocr -
151 ocr2 tazsane-OCR2.txt text/plain tesseract tesseract-ocr | tesseract stdin stdout
152 pnm tazsane.pnm image/pnm true slitaz
153 EOT
154 }
156 xPOST() {
157 [ "$preview" == "reset" ] || POST $@
158 }
160 tmpreview="$(POST tmpreview)"
161 find tmp/ -name 'tazsane*' -mmin +60 -prune -exec rm -f {} \;
163 device="$(POST device)"
164 preview="$(POST preview)"
165 info=""
166 error=""
168 case " $(POST) " in
169 *\ reset\ *)
170 unset device tmpreview
171 preview="reset" ;;
172 *\ preview\ *)
173 [ "$tmpreview" ] || tmpreview=$(mktemp -u -t tazsane.XXXXXX).png
174 tmp=$(mktemp -u -t tazsane.XXXXXX)
175 eval "$(getgeometry)" > $tmp.pnm 2> $tmp.err
176 if [ -s "$tmp.pnm" ]; then
177 convert $tmp.pnm $tmpreview > /dev/null 2>&1 ||
178 pnm2png < $tmp.pnm > $tmpreview ||
179 cp $tmp.pnm $tmpreview
180 else
181 error="$(sed 's|$|<br />|' $tmp.err)"
182 rm -f $tmpreview
183 fi
184 rm -f $tmp.pnm $tmp.err ;;
185 *\ scan\ *)
186 imgformat download ;;
187 esac
189 header
190 xhtml_header
191 [ -n "$error" ] && msg warn "$error"
192 [ -n "$info" ] && msg tip "$info"
193 if [ -z "$device" ]; then
194 suggested=""
195 while read exe pkg msg; do
196 [ "$(which $exe 2> /dev/null)" ] && continue
197 suggested="$suggested
198 <li><a href=\"/user/pkgs.cgi?info=$pkg\">$pkg</a>&nbsp;$msg</li>"
199 done <<EOT
200 convert imagemagick $(_ "to preview images and support more image formats (recommended)")
201 gocr gocr $(_ "a basic optical character recognition")
202 tesseract tesseract-ocr $(_ "a better optical character recognition")
203 EOT
204 [ "$suggested" ] &&
205 msg tip "$(_ "You may need to install:") <ol>$suggested</ol>"
206 all="$(scanimage -f '%d,%v %m|'|\
207 cat - sane-fake.log fake-sane/sane-fake.log |sed 's/|/\n/g')"
208 case "$(echo "$all" | wc -l)" in
209 1) if [ -z "$all" ]; then
210 msg warn "$(_ "No scanner found")"
211 msg tip "$(_ "You can test this GUI with ")\
212 <a href=\"/user/pkgs.cgi?info=fake-sane\">fake-sane</a>"
213 xhtml_footer
214 exit 0
215 fi
216 if [ "$suggested" ]; then
217 cat <<EOT
218 <section>
219 <header>
220 <form name="scanner" method="post">
221 <input type="hidden" name="device" value="$all">
222 Scanner ${all#*,}
223 <button data-icon="start">Continue</button>
224 </form>
225 </header>
226 </section>
227 EOT
228 xhtml_footer
229 exit 0
230 fi
231 device="${all%|}" ;;
232 *)
233 cat <<EOT
234 <section>
235 <header>
236 <form name="scanner" method="post">
237 Scanner
238 <select name="device" size=1>
239 EOT
240 echo "$all" | awk -F, '{ if (NF > 0) print "<option value=\"" $0 "\">" 1+i++ " - " $2 }'
241 cat <<EOT
242 </select>
243 <button data-icon="start">$(_ "Continue")</button>
244 </form>
245 </header>
246 </section>
247 EOT
248 xhtml_footer
249 exit 0 ;;
250 esac
251 fi
253 cat <<EOT
254 <section>
255 <form name="parameters" method="post" style="width:100%">
257 <header>
258 $(echo $device | sed 's/.*,//')
259 <div class="float-right">
260 <button name="scan" data-icon="start">$(_ "Scan")</button>
261 <button name="reset" data-icon="refresh">$(_ "Reset")</button>
262 <button name="preview" data-icon="view">$(_ "Preview")</button>
263 </div>
264 </header>
266 <table style="width:100%">
267 <tr>
268 <td title="Sets the file format for the scanned image">
269 <fieldset><legend>$(_ 'Format')</legend>
270 <select name="format" size=1>
271 $(imgformat list)
272 </select>
273 </fieldset>
274 </td>
275 EOT
277 if [ "$(xPOST params)" ]; then
278 params="$(xPOST params | uudecode)"
279 else
280 params="$({
281 cat "$(echo $device | sed 's/,.*//').log" 2> /dev/null ||
282 scanimage --help -d "$(echo $device | sed 's/,.*//')"
283 } | dos2unix | sed 's|\[=| [|;s/||/|/g' | awk '
284 function minmax()
285 {
286 inactive=1
287 if (match($2,"[0-9]")) {
288 i=$2; sub(/\.\..*/,"",i)
289 j=$2; sub(/.*\.\./,"",j)
290 sub(/\..*/,"",j); sub(/[dm%].*/,"",j)
291 k=$0; sub(/.* \[/,"",k); sub(/\].*/,"",k)
292 printf("\n%s",$1 " " int(k) " " int(i) " " int(j))
293 inactive=0
294 }
295 }
297 function enum()
298 {
299 i=$0
300 inactive=1
301 if (index(i,"|")) {
302 sub(/^ *--*[a-z-]* */,"",i)
303 sub(/\[\(/,"",i); sub(/\)\]/,"",i)
304 sub(/dpi .*/,"",i); gsub(/ \[.*\].*/,"",i)
305 k=$0; sub(/.* \[/,"",k); sub(/\].*/,"",k)
306 gsub(/ /,"=",k)
307 gsub(/ /,"=",i)
308 printf("\n%s",$1 " " k " enum " i)
309 inactive=0
310 }
311 else minmax()
312 }
314 {
315 if (/scanimage --help/) end=1
316 if (end != 0) next
317 if (/:$/) parse=0
318 if (/Scan mode/ || /Mode/ || /Advanced/ || /Geometry/) parse=1
319 if (parse != 1) next
320 if (/\[inactive\]/) { inactive=1; next }
321 if (match("-l-t-x-y", $1)) minmax()
322 else if (/^ --/) enum()
323 else if (!/:$/ && inactive == 0) printf(" %s",$0)
324 } END { print "" }
325 ' | sed 1d)"
326 fi
327 echo "<!-- $params -->"
328 output="$(n=$(echo "$params" | wc -l); echo "$params" | \
329 while read name def min max help; do
330 name="${name#-}"
331 name="${name#-}"
332 name="${name//-/_}"
333 help="$(echo $help | sed 's| | |g;s|"|\&#34|g')"
334 def="${def//=/ }"
335 max="${max//=/ }"
336 if [ "$min" == "enum" ]; then
337 res_min=1000000
338 res_max=0
339 echo "<td title=\"$help\"><fieldset><legend>$name</legend>"
340 echo -n "<select name=\"$name\" size=1"
341 [ "$name" == "resolution" ] && echo -n " onchange=showGeometry()"
342 echo ">"
343 IFS="|"; set -- $max ; unset IFS
344 while [ "$1" ]; do
345 echo -n "<option value=\"$1\""
346 [ "$(xPOST $name)" == "$1" ] && echo -n " selected"
347 [ -z "$(xPOST $name)" -a "$def" == "$1" ] && echo -n " selected"
348 echo ">$(_ "$1")"
349 if [ "$name" == "resolution" ]; then
350 [ $res_max -lt $1 ] && res_max=$1
351 [ $res_min -gt $1 ] && res_min=$1
352 fi
353 shift
354 done
355 echo "</select>"
356 else
357 [ "$(xPOST $name)" ] && def=$(xPOST $name)
358 [ $def -lt $min ] && def=$min
359 [ $def -gt $max ] && def=$max
360 f="<fieldset><legend>$(_ "$name")</legend><input name=\"$name\" value=\"$def\""
361 u=""
362 case "$name" in
363 x|y|l|t) cat <<EOT
364 :${name}_max=$max
365 <input type="hidden" name="${name}_min" value="$min">
366 <input type="hidden" name="${name}_max" value="$max">
367 EOT
368 while read name2 n2 id val; do
369 [ "$name" == "$name2" ] || continue
370 [ "$(xPOST geometry_$name)" ] &&
371 val="$(xPOST geometry_$name)"
372 f="<fieldset><legend>$(_ "$n2")</legend><input name=\"geometry_$name\" id=\"$id\" value=\"$val\""
373 u="&nbsp;mm"
374 break
375 done <<EOT
376 l X-Offset x 0
377 t Y-Offset y 0
378 x Width width $max
379 y Height height $max
380 EOT
381 esac
382 [ "$name" == "resolution" ] && f="$f onchange=showGeometry()"
383 echo "<td>$f type=\"text\" title=\"$min .. $max. $help\" size=4 maxlength=4>$u"
384 res_min=$min
385 res_max=$max
386 fi
387 case "$name" in
388 resolution) cat <<EOT
389 <input type="hidden" name="res_min" value="$res_min">
390 <input type="hidden" name="res_max" value="$res_max">
391 &nbsp;dpi
392 EOT
393 esac
394 echo "</filedset></td>"
395 n=$(($n - 2))
396 case "$n" in
397 1|2) echo "</tr><tr>"
398 esac
399 done)"
400 echo "$output" | sed '/^:/d'
402 org_x=$(xPOST geometry_x); [ "$org_x" ] || org_x=$(echo "$output" | sed '/^:x_max=/!d;s/.*=//')
403 org_y=$(xPOST geometry_y); [ "$org_y" ] || org_y=$(echo "$output" | sed '/^:y_max=/!d;s/.*=//')
405 cat <<EOT
406 </tr>
407 </table>
408 <input type="hidden" name="tmpreview" value="$tmpreview">
409 <input type="hidden" name="device" value="$device">
410 <input type="hidden" name="params" value="$(echo "$params" | uuencode -m -)">
411 <script language="JavaScript" type="text/javascript">
412 <!--
413 function setGeometry(x,y) {
414 document.parameters.geometry_x.value = x;
415 document.parameters.geometry_y.value = y;
416 cropSetFrameByInput();
417 }
419 function showGeometry() {
420 var resolution = document.parameters.resolution.value;
421 if (resolution) {
422 resolution /= 25.4;
423 var x = Math.floor(document.parameters.geometry_x.value * resolution);
424 var y = Math.floor(document.parameters.geometry_y.value * resolution);
425 alert((Math.round(x * y / 100000)/10) + ' Mpixels\n' + x + 'x' + y);
426 }
427 }
428 -->
429 </script>
431 <footer align="center">
432 EOT
433 awk -vox=$org_x -voy=$org_y 'END {
434 x=210*4; y=297*4; n=0; cnt=0;
435 while (cnt < 9) {
436 if (ox +1 >= x && oy +1 >= y) {
437 print "&nbsp;<a href=\"javascript:setGeometry(" x "," y ")\">DIN-A" n "</a>"
438 cnt++
439 }
440 if (ox +1 >= x && oy +1 >= y) {
441 print "&nbsp;<a href=\"javascript:setGeometry(" y "," x ")\">DIN-A" n "L</a>"
442 cnt++
443 }
444 tmp=x; x=y/2; y=tmp
445 n++
446 }
447 }' < /dev/null
449 cat <<EOT
450 </footer>
451 </form>
452 </section>
453 EOT
455 [ -s "$tmpreview" ] && cat <<EOT
456 <div margin="15px" style="overflow-x: auto">
457 <script type="text/javascript" src="lib/crop.js"></script>
458 <img src="$tmpreview" onload=cropInit(this,'x','y','width','height')>
459 </div>
460 EOT
461 xhtml_footer