4192fb84ab43eeb32c3b4ef88fe013de08b201f7
[bookreader.git] / BookReaderIA / datanode / BookReaderPreview.php
1 <?
2 /*
3
4 Provides access to preview images of book.  It is run with privileges and provides a reduced
5 access wrapper around BookReaderImages.
6
7 Michael Ang <http://github.com/mangtronix>
8
9 Copyright (c) 2010 Internet Archive. Software license AGPL version 3.
10
11 This file is part of BookReader.
12
13     BookReader is free software: you can redistribute it and/or modify
14     it under the terms of the GNU Affero General Public License as published by
15     the Free Software Foundation, either version 3 of the License, or
16     (at your option) any later version.
17
18     BookReader is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU Affero General Public License for more details.
22
23     You should have received a copy of the GNU Affero General Public License
24     along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 require_once('BookReaderMeta.inc.php');
28 require_once('BookReaderImages.inc.php');
29
30 function BRfatal($message) {
31     header("HTTP/1.0 404 Not Found");
32     header("Content-type: text/plain");
33     print $message;
34     die(-1);
35 }
36
37 $brm = new BookReaderMeta();
38 try {
39     $metadata = $brm->buildMetadata($_REQUEST['id'], $_REQUEST['itemPath'], $_REQUEST['subPrefix'], $_REQUEST['server']);
40 } catch (Exception $e) {
41     BRfatal($e->getMessage);
42 }
43
44 // $$$ allow size information
45 $knownPages = array('title','cover','preview');
46 $page = $_REQUEST['page'];
47 if (! in_array($page, $knownPages) ) {
48     BRfatal("Bad or no page specified");
49 }
50
51 // Index of image to return
52 $imageIndex = null;
53
54 switch ($page) {
55     case 'title':
56         if (! array_key_exists('titleIndex', $metadata)) {
57             BRfatal("No title page asserted in book");
58         }
59         $imageIndex = $metadata['titleIndex'];
60         break;
61         
62     case 'cover':
63         if (! array_key_exists('coverIndices', $metadata)) {
64             BRfatal("No cover asserted in book");
65         }
66         $imageIndex = $metadata['coverIndices'][0]; // $$$ TODO add support for other covers
67         break;
68         
69     case 'preview':
70         // Preference is:
71         //   Cover page if book was published >= 1950
72         //   Title page
73         //   Cover page
74         //   Page 0
75         
76         /*
77         header('Content-type: text/plain');
78         print 'Date ' . $metadata['date'];
79         print 'Year ' . $brm->parseYear($metadata['date']);
80         */
81  
82         if ( array_key_exists('date', $metadata) && array_key_exists('coverIndices', $metadata) ) {
83             if ($brm->parseYear($metadata['date']) >= 1950) {
84                 $imageIndex = $metadata['coverIndices'][0];                
85                 break;
86             }
87         }
88         if (array_key_exists('titleIndex', $metadata)) {
89             $imageIndex = $metadata['titleIndex'];
90             break;
91         }
92         if (array_key_exists('coverIndices', $metadata)) {
93             $imageIndex = $metadata['coverIndices'][0];
94             break;
95         }
96         
97         // First page
98         $imageIndex = 0;
99         break;
100         
101     default:
102         // Shouldn't be possible
103         BRfatal("Couldn't find page");
104         break;
105         
106 }
107
108 $leaf = $brm->leafForIndex($imageIndex, $metadata['leafNums']);
109
110 $requestEnv = array(
111     'zip' => $metadata['zip'],
112     'file' => $brm->imageFilePath($leaf, $metadata['subPrefix'], $metadata['imageFormat']),
113     'ext' => 'jpg',
114 );
115
116 // Return image data - will check privs
117 $bri = new BookReaderImages();
118 try {
119     $bri->serveRequest($requestEnv);
120 } catch (Exception $e) {
121     header("HTTP/1.0 404 Not Found");
122     header("Content-type: text/plain");
123     print "Error serving request:";
124     print "  " . $e->getMessage();
125     print "Debugging information:";
126     echo $e->getTraceAsString();
127     die(-1);
128 }
129
130 ?>