sanitize inputs
[bookreader.git] / BookReaderIA / www / print.php
1 <?
2 /*
3 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
4
5 This file is part of BookReader.
6
7     BookReader is free software: you can redistribute it and/or modify
8     it under the terms of the GNU Affero General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     BookReader is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU Affero General Public License for more details.
16
17     You should have received a copy of the GNU Affero General Public License
18     along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
19
20 BookReaderPrint.php exists to get around the same-origin policy that prevents
21 us from calling print() on an iframe that comes from a cluster datanode.
22 */
23
24 $id     = $_REQUEST['id'];
25 $server = $_REQUEST['server'];
26 $zip    = $_REQUEST['zip'];
27 $file  = $_REQUEST['file'];
28 $format = $_REQUEST['format'];
29 //$imgAspect = $_REQUEST['aspect'];
30 $width = floatval($_REQUEST['width']);
31 $height = floatval($_REQUEST['height']);
32 $title = $_REQUEST['title'];
33
34 /* We assume that the print aspect ratio is somewhat close to US Letter in portrait orientation */
35 $paperAspect = 8.5/11;
36
37 // $$$ may want to adjust this if two page with foldout looks strange
38 $allowRotate = true;
39
40 // Returns (url, attrs)
41 function imageURL($paperAspect, $file, $format, $width, $height, $allowRotate) {
42     global $server, $id, $zip;
43     
44     $rotate = "0";
45     $imgAspect = $width / $height;
46     
47     if ('jp2' == $format && $allowRotate) {
48         // Rotation is possible
49         if ($imgAspect > $paperAspect && $imgAspect > 1) {
50             $rotate = "90";
51             $imgAspect = 1 / $imgAspect;
52         }
53     }
54     
55     if ($imgAspect > $paperAspect) {
56         // wider than paper, fit width
57         $htmlAttrs = "width='95%'";
58     } else {
59         // taller than paper, fit height
60         $htmlAttrs = "height='95%'";
61     }
62     
63     $queryParams = array(
64         'id' => $id, // global
65         'format' => $format,
66         'index' => $index,
67         'file' => $file,
68         'zip' => $zip, // global
69         'rotate' => $rotate,
70         'scale' => 1
71     );
72
73     $_server = htmlspecialchars($server);
74     return "<img src='http://{$_server}/BookReader/BookReaderImages.php?" . http_build_query($queryParams) . "' " . $htmlAttrs . " />";
75 }
76
77 echo "<html><head>";
78 echo '<link rel="stylesheet" type="text/css" href="BookReader.css" />';
79 echo "<style type='text/css'>";
80 echo "  @media print { .noprint { font-size: 40pt; display: none; } }";
81 echo "</style>";
82 echo "<script type='text/javascript'>";
83 echo "  function conditionalPrint() {";
84 echo "    var doPrint = true; var agent = navigator.userAgent.toLowerCase();";
85 echo "    if (agent.indexOf('safari') != -1) { doPrint = false; }";
86 echo "    if (doPrint) { print(); }";
87 echo "  }";
88 echo "</script>";
89 echo "<title>" . htmlspecialchars($title) . "</title><body onload='conditionalPrint(); return false;'>";
90 echo   "<p class='noprint' style='text-align: right'>";
91 echo     "<button class='BRicon rollover print' title='Print' onclick='print(); return false;'></button> <a href='#' onclick='print(); return false;'>Print</a></p>";
92 echo   "<p style='text-align:center;'>";
93 echo     imageURL($paperAspect, $file, $format, $width, $height, $allowRotate);
94 echo   "</p>";
95
96 if (isset($_REQUEST['file2'])) {    
97     $file2 = $_REQUEST['file2'];
98     $width2 = floatval($_REQUEST['width2']);
99     $height2 = floatval($_REQUEST['height2']);
100     
101     
102     echo "<p style='text-align: center;'>";
103     echo imageURL($paperAspect, $file2, $format, $width2, $height2, $allowRotate);
104     echo "</p>";
105 }
106 echo  "</body></html>";
107
108 ?>