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