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