Comments
[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['bookId'], $_REQUEST['server']);
40 } catch (Exception $e) {
41     BRfatal($e->getMessage);
42 }
43
44 $knownPages = array('title','cover','preview');
45 $page = $_REQUEST['page'];
46 if (! in_array($page, $knownPages) ) {
47     BRfatal("Bad or no page specified");
48 }
49
50 // Index of image to return
51 $imageIndex = null;
52
53 switch ($page) {
54     case 'title':
55         if (! array_key_exists('titleIndex', $metadata)) {
56             BRfatal("No title page asserted in book");
57         }
58         $imageIndex = $metadata['titleIndex'];
59         break;
60         
61     case 'cover':
62         if (! array_key_exists('coverIndices', $metadata)) {
63             BRfatal("No cover asserted in book");
64         }
65         $imageIndex = $metadata['coverIndices'][0]; // $$$ TODO add support for other covers
66         break;
67         
68     case 'preview':
69         // Preference is:
70         //   Cover page if book was published >= 1950
71         //   Title page
72         //   Cover page
73         //   Page 0
74         
75         /*
76         header('Content-type: text/plain');
77         print 'Date ' . $metadata['date'];
78         print 'Year ' . $brm->parseYear($metadata['date']);
79         */
80  
81         if ( array_key_exists('date', $metadata) && array_key_exists('coverIndices', $metadata) ) {
82             if ($brm->parseYear($metadata['date']) >= 1950) {
83                 $imageIndex = $metadata['coverIndices'][0];                
84                 break;
85             }
86         }
87         if (array_key_exists('titleIndex', $metadata)) {
88             $imageIndex = $metadata['titleIndex'];
89             break;
90         }
91         if (array_key_exists('coverIndices', $metadata)) {
92             $imageIndex = $metadata['coverIndices'][0];
93             break;
94         }
95         
96         // First page
97         $imageIndex = 0;
98         break;
99         
100     default:
101         // Shouldn't be possible
102         BRfatal("Couldn't find page");
103         break;
104         
105 }
106
107 $leaf = $brm->leafForIndex($imageIndex, $metadata['leafNums']);
108
109 $requestEnv = array(
110     'zip' => $metadata['zip'],
111     'file' => $brm->imageFilePath($leaf, $metadata['bookId'], $metadata['imageFormat']),
112     'ext' => 'jpg',
113 );
114
115 // Return image data - will check privs
116 $bri = new BookReaderImages();
117 try {
118     $bri->serveRequest($requestEnv);
119 } catch (Exception $e) {
120     header("HTTP/1.0 404 Not Found");
121     header("Content-type: text/plain");
122     print "Error serving request:";
123     print "  " . $e->getMessage();
124     print "Debugging information:";
125     echo $e->getTraceAsString();
126     die(-1);
127 }
128
129 ?>