7c4b95153f9d6a01893fc26d5cd3940fc8f6b76e
[bookreader.git] / BookReaderIA / inc / BookReader.inc
1 <?
2
3 /*
4  * 
5
6 /*
7  * Note: Edits to this file must pass through github.  To submit a patch to this
8  *       file please contact mang at archive dot org or http://github.com/mangtronix
9  *       Direct changes to this file may get clobbered when the code is synchronized
10  *       from github.
11  */
12
13 class BookReader
14 {
15
16   // Operators recognized in BookReader download URLs
17   public static $downloadOperators = array('page');
18
19   // Returns true if can display the book in item with a given prefix (typically the item identifier)
20   public static function canDisplay($item, $prefix, $checkOldScandata = false)
21   {
22     
23     // A "book" is an image stack and scandata.
24     // 1. Old items may have scandata.xml or scandata.zip and itemid_{imageformat}.{zip,tar}
25     // 2. Newer items may have multiple {arbitraryname}_scandata.xml and {arbitraryname}_{imageformat}.{zip,tar}
26         
27     $foundScandata = false;
28     $foundImageStack = false;
29     
30     $targetScandata = $prefix . "_scandata.xml";
31         
32     // $$$ TODO add support for jpg and tar stacks
33     // https://bugs.edge.launchpad.net/gnubook/+bug/323003
34     // https://bugs.edge.launchpad.net/gnubook/+bug/385397
35     $imageFormatRegex = '@' . preg_quote($prefix, '@') . '_(jp2|tif|jpg)\.(zip|tar)$@';
36     
37     $baseLength = strlen($item->metadataGrabber->mainDir . '/');
38     foreach ($item->getFiles() as $location => $fileInfo) {
39         $filename = substr($location, $baseLength);
40         
41         if ($checkOldScandata) {
42             if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
43                 $foundScandata = $filename;
44             }
45         }
46         
47         if ($filename == $targetScandata) {
48             $foundScandata = $filename;
49         }
50         
51         if (preg_match($imageFormatRegex, $filename)) {
52             $foundImageStack = $filename;
53         }
54     }
55     
56     if ($foundScandata && $foundImageStack) {
57         return true;
58     }
59     
60     return false;
61   }
62   
63   // Finds the prefix to use for the book given the part of the URL trailing after /stream/
64   public static function findPrefix($urlPortion)
65   {
66     if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
67         // URL portion was empty or started with /, &, or ? -- no item identifier
68         return false;
69     }
70     
71     $prefix = $matches[0]; // item identifier
72     
73     // $$$ Currently swallows the rest of the URL.
74     //     If we want to support e.g. /stream/itemid/subdir/prefix/page/23 will need to adjust.
75     if (preg_match('#[^/&?]+/([^&?]+)#', $urlPortion, $matches)) {
76         // Match is everything after item identifier and slash, up to end or ? or &
77         // e.g. itemid/{match/these/parts}?foo=bar
78         $prefix = $matches[1]; // sub prefix -- 
79     }
80     
81     return $prefix;
82   }
83
84   // $$$ would be cleaner to use different templates instead of the uiMode param
85   // 
86   // @param subprefix Optional prefix to display a book inside an item (e.g. if does not match identifier)
87   public static function draw($server, $mainDir, $identifier, $subPrefix, $title,
88                               $coverLeaf=null, $titleStart='Internet Archive', $uiMode='full')
89   {
90     // Set title to default if not set
91     if (!$title) {
92         $title = 'BookReader';
93     }
94     
95     $id = $identifier;
96     
97     // manually update with Launchpad version number at each checkin so that browsers
98     // do not use old cached version
99     // see https://bugs.launchpad.net/gnubook/+bug/330748
100     $version = "r28";
101     
102     if (BookReader::getDevHost($server)) {
103         // on dev host - add time to force reload
104         $version .= '_' . time();
105     }
106     
107     if ("" == $id) {
108         echo "No identifier specified!";
109         die(-1);
110     }
111     
112     $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
113     
114 ?>
115 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
116 <html>
117 <head>
118     <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
119     <meta name="apple-mobile-web-app-capable" content="yes" />
120     <title><? echo $title; ?></title>
121 <!--[if lte IE 6]>
122     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
123 <![endif]-->
124     <link rel="stylesheet" type="text/css" href="/bookreader/BookReader.css?v=<? echo($version); ?>">
125 <? if ($uiMode == "embed") { ?>
126     <link rel="stylesheet" type="text/css" href="/bookreader/BookReaderEmbed.css?v=<? echo($version); ?>">
127 <? } elseif ($uiMode == "touch") { ?>
128     <link rel="stylesheet" type="text/css" href="/bookreader/touch/BookReaderTouch.css?v=<? echo($version); ?>">
129 <? } /* uiMode */ ?>
130     <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
131     <script type="text/javascript" src="http://www.archive.org/includes/analytics.js?v=2"></script>
132     <script type="text/javascript" src="/bookreader/jquery-ui-1.8.1.custom.min.js?v=<? echo($version); ?>"></script>
133     <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
134     <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
135 </head>
136 <body style="background-color: #FFFFFF;">
137
138 <? if ($uiMode == 'full') { ?>
139 <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
140 <? } else { ?>
141 <div id="BookReader" style="left:0; right:0; top:0; bottom:0; border:0">Internet Archive Bookreader <noscript>requires JavaScript to be enabled.</noscript></div>
142 <? } /* uiMode*/ ?>
143
144 <script type="text/javascript">
145   // Set some config variables -- $$$ NB: Config object format has not been finalized
146   var brConfig = {};
147 <? if ($uiMode == 'embed') { ?>
148   brConfig["mode"] = 1;
149   brConfig["reduce"] = 8;
150   brConfig["ui"] = "embed";
151 <? } else { ?>
152   brConfig["mode"] = 2;
153 <? } ?>
154 </script>
155 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
156 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
157
158 <? if ($uiMode == 'full') { ?>
159 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
160     <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
161         <p style="display: inline">
162             <input id="BookReaderSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
163         </p>
164     </form>
165     <div id="BookReaderSearchResults">
166         Search results
167     </div>
168 </div>
169
170
171 <div id="BRfooter">
172     <div class="BRlogotype">
173         <a href="http://archive.org/" class="BRblack">Internet Archive</a>
174     </div>
175     <div class="BRnavlinks">
176         <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
177         <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
178         <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
179         <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
180         <a class="BRblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
181     </div>
182 </div>
183 <? } /* uiMode */ ?>
184
185 <script type="text/javascript">
186     // $$$ hack to workaround sizing bug when starting in two-up mode
187     $(document).ready(function() {
188         $(window).trigger('resize');
189     });
190     
191     // Usage stats
192     if(window.archive_analytics) { window.archive_analytics.values['bookreader'] = 'open'}; </script>
193 </script>
194   <?
195     exit;
196   }
197   
198   // Returns the user part of dev host from URL, or null
199   public static function getDevHost($server)
200   {
201       if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
202         return $match[1];
203       }
204       
205       return null;
206   }
207
208   
209   public static function serverBaseURL($server)
210   {
211       // Check if we're on a dev vhost and point to JSIA in the user's public_html
212       // on the datanode
213       // $$$ the remapping isn't totally automatic yet and requires user to
214       //     ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
215       //     so we enable it only for known hosts
216       $devhost = BookReader::getDevHost($server);
217       $devhosts = array('mang', 'testflip', 'rkumar');
218       if (in_array($devhost, $devhosts)) {
219         $server = $server . "/~" . $devhost;
220       }
221       return $server;
222   }
223   
224   
225   public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
226   {
227     $serverBaseURL = BookReader::serverBaseURL($server);
228
229     $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
230     if ($subPrefix) {
231         $params['subPrefix'] = $subPrefix;
232     }
233     
234     $keys = array_keys($params);
235     $lastParam = end($keys);
236     $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
237     foreach($params as $param=>$value) {
238         $url .= $param . '=' . $value;
239         if ($param != $lastParam) {
240             $url .= '&';
241         }
242     }
243     
244     return $url;
245   }
246   
247   // Return the URL for the requested /download/$path, or null
248   public static function getURL($path, $item) {
249     // $path should look like {itemId}/{operator}/{filename}
250     // Other operators may be added
251     
252     $urlParts = BookReader::parsePath($path);
253     
254     // Check for non-handled cases
255     $required = array('identifier', 'operator', 'operand');
256     foreach ($required as $key) {
257         if (!array_key_exists($key, $urlParts)) {
258             return null;
259         }
260     }
261     
262     $identifier = $urlParts['identifier'];
263     $operator = $urlParts['operator'];
264     $filename = $urlParts['operand'];
265     $subPrefix = $urlParts['subPrefix'];
266     
267     $serverBaseURL = BookReader::serverBaseURL($item->getServer());
268     
269     // Baseline query params
270     $query = array(
271         'id' => $identifier,
272         'itemPath' => $item->getMainDir(),
273         'server' => $serverBaseURL
274     );
275     if ($subPrefix) {
276         $query['subPrefix'] = $subPrefix;
277     }
278     
279     switch ($operator) {
280         case 'page':
281             
282             // Look for old-style preview request - e.g. {identifier}_cover.jpg
283             if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) {
284                 // Serve preview image
285                 $page = $matches[2];
286                 $query['page'] = $page;
287                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
288             }
289             
290             // New-style preview request - e.g. cover_thumb.jpg
291             if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) {
292                 $query['page'] = $filename;
293                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
294             }
295             
296             // Asking for a non-preview page
297             $query['page'] = $filename;
298             return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
299         
300         default:
301             // Unknown operator
302             return null;            
303     }
304       
305     return null; // was not handled
306   }
307   
308   public static function browserFromUserAgent($userAgent) {
309       $browserPatterns = array(
310           'ipad' => '/iPad/',
311           'iphone' => '/iPhone/', // Also cover iPod Touch
312           'android' => '/Android/',
313       );
314       
315       foreach ($browserPatterns as $browser => $pattern) {
316           if (preg_match($pattern, $userAgent)) {
317               return $browser;
318           }
319       }
320       return null;
321   }
322
323   
324   // $$$ Ideally we will not rely on user agent, but for the moment we do
325   public static function paramsFromUserAgent($userAgent) {
326       // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
327       $browserParams = array(
328           'ipad' => array( 'ui' => 'touch' ),
329           'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
330           'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
331       );
332   
333       $browser = BookReader::browserFromUserAgent($userAgent);
334       if ($browser) {
335           return $browserParams[$browser];
336       }
337       return array();
338   }
339   
340   public static function parsePath($path) {
341     // Parse the BookReader path and return the parts
342     // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
343     //            'operator' => 'page', 'filename' => 'cover.jpg')
344     
345     $parts = array();
346     
347     // Pull off query, e.g. ?foo=bar
348     if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
349         $parts['query'] = $matches[2];
350         $path = $matches[1];
351     }
352     
353     // Pull off identifier
354     if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
355         // no match
356         return $parts;
357     }
358     $parts['identifier'] = $matches[0];
359     $path = substr($path, strlen($matches[0]));
360     
361     // Look for operators
362     // The sub-prefix can be arbitrary, so we match up until the first operator
363     $operators = '(' . join('|', self::$downloadOperators) . ')';
364     $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
365     if (preg_match($pattern, $path, $matches) === 1) {
366         $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
367         $parts['operator'] = $matches['operator'];
368         $parts['operand'] = $matches['operand'];
369     } else {
370         $parts['subPrefix'] = $path;
371     }
372     
373     return $parts;
374   }
375     
376 }
377
378 ?>