Share and info popovers
[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         // If debugging on IE, remove this line otherwise breakpoints will be invalid after reload
107         $version .= '_' . time();
108     }
109     
110     if ("" == $id) {
111         echo "No identifier specified!";
112         die(-1);
113     }
114     
115     $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
116     $locateURL = BookReader::jsLocateURL($identifier, $subPrefix);
117     
118 ?>
119 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
120 <html>
121 <head>
122     <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
123     <meta name="apple-mobile-web-app-capable" content="yes" />
124     <title><? echo $title; ?></title>
125 <!--[if lte IE 6]>
126     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
127 <![endif]-->
128     <link rel="stylesheet" type="text/css" href="/bookreader/BookReader.css?v=<? echo($version); ?>">
129 <? if ($uiMode == "embed") { ?>
130     <link rel="stylesheet" type="text/css" href="/bookreader/BookReaderEmbed.css?v=<? echo($version); ?>">
131 <? } elseif ($uiMode == "touch") { ?>
132     <link rel="stylesheet" type="text/css" href="/bookreader/touch/BookReaderTouch.css?v=<? echo($version); ?>">
133 <? } /* uiMode */ ?>
134     <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
135     <script type="text/javascript" src="/bookreader/jquery-ui-1.8.5.custom.min.js?v=<? echo($version); ?>"></script>
136     <script type="text/javascript" src="http://www.archive.org/includes/analytics.js?v=2"></script>
137     <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
138     <script type="text/javascript" src="/bookreader/jquery.colorbox-min.js"></script>
139     <script type="text/javascript" src="/bookreader/jquery.ui.ipad.js"></script>
140      <!-- THIS ALLOWS BEAUTYTIPS TO WORK ON IE -->
141         <!--[if lt IE 9]>
142         <script type="text/javascript" src="/includes/excanvas.compiled.js"></script>
143         <![endif]-->
144     <script type="text/javascript" src="/bookreader/jquery.bt.min.js"></script>
145     <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
146 <? if ( !preg_match("/mobile/i", $_SERVER['HTTP_USER_AGENT']) ) { ?>
147     <script type="text/javascript" src="/bookreader/soundmanager/soundmanager2-ia.js?v=<? echo($version); ?>"></script>
148     <script>
149         soundManager.debugMode = false;
150         soundManager.url = '/bookreader/soundmanager/swf/';       
151         soundManager.useHTML5Audio = true;
152         soundManager.flashVersion = 9; //flash 8 version of swf is buggy when calling play() on a sound that is still loading
153     </script>
154 <? } /* mobile user agent */ ?>
155 </head>
156 <body style="background-color: ##939598;">
157
158 <?
159 /*
160 // <? if ($uiMode == 'full') { ?>
161 // <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
162 // <? } else { ?>
163 // <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>
164 // <? } ?>
165 */
166 ?>
167
168 <div id="BookReader">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
169
170 <script type="text/javascript">
171   // Set some config variables -- $$$ NB: Config object format has not been finalized
172   var brConfig = {};
173 <? if ($uiMode == 'embed') { ?>
174   brConfig["mode"] = 1;
175   brConfig["reduce"] = 8;
176   brConfig["ui"] = "embed";
177 <? } else { ?>
178   brConfig["mode"] = 2;
179 <? } ?>
180 </script>
181 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions.
182      The ia{number}.us.archive.org server referenced below can and does change, so this URL should NOT be used for permanent access.  -->
183 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
184 <!-- This URL will find the item and redirect to the correct server.  Remove the line above and use the URL below for stable access. -->
185 <!-- <script type="text/javascript" src="<? echo($locateURL); ?>"></script> -->
186
187 <script type="text/javascript">
188     // Usage stats
189     if(window.archive_analytics) { window.archive_analytics.values['bookreader'] = 'open'};
190 </script>
191   <?
192     exit;
193   }
194   
195   // Returns the user part of dev host from URL, or null
196   public static function getDevHost($server)
197   {
198       if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
199         return $match[1];
200       }
201       
202       return null;
203   }
204
205   
206   public static function serverBaseURL($server)
207   {
208       // Check if we're on a dev vhost and point to JSIA in the user's public_html
209       // on the datanode
210       // $$$ the remapping isn't totally automatic yet and requires user to
211       //     ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
212       //     so we enable it only for known hosts
213       $devhost = BookReader::getDevHost($server);
214       $devhosts = array('mang', 'testflip', 'rkumar');
215       if (in_array($devhost, $devhosts)) {
216         $server = $server . "/~" . $devhost;
217       }
218       return $server;
219   }
220   
221   
222   public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
223   {
224     $serverBaseURL = BookReader::serverBaseURL($server);
225
226     $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
227     if ($subPrefix) {
228         $params['subPrefix'] = $subPrefix;
229     }
230     
231     $keys = array_keys($params);
232     $lastParam = end($keys);
233     $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
234     foreach($params as $param=>$value) {
235         $url .= $param . '=' . $value;
236         if ($param != $lastParam) {
237             $url .= '&';
238         }
239     }
240     
241     return $url;
242   }
243   
244   // This returns a URL that finds the item then returns a redirect to BookReaderJSIA.php
245   // on the item's server.
246   public static function jsLocateURL($identifier, $subPrefix = '')
247   {
248     $locateURL = 'http://www.archive.org/bookreader/BookReaderJSLocate.php?id=' . $identifier;
249     if ($subPrefix) {
250         $locateURL .= '&subPrefix=' . $subPrefix;
251     }
252     return $locateURL;
253   }
254   
255   // Return the URL for the requested /download/$path, or null
256   public static function getURL($path, $item) {
257     // $path should look like {itemId}/{operator}/{filename}
258     // Other operators may be added
259     
260     $urlParts = BookReader::parsePath($path);
261     
262     // Check for non-handled cases
263     $required = array('identifier', 'operator', 'operand');
264     foreach ($required as $key) {
265         if (!array_key_exists($key, $urlParts)) {
266             return null;
267         }
268     }
269     
270     $identifier = $urlParts['identifier'];
271     $operator = $urlParts['operator'];
272     $filename = $urlParts['operand'];
273     $subPrefix = $urlParts['subPrefix'];
274     
275     $serverBaseURL = BookReader::serverBaseURL($item->getServer());
276     
277     // Baseline query params
278     $query = array(
279         'id' => $identifier,
280         'itemPath' => $item->getMainDir(),
281         'server' => $serverBaseURL
282     );
283     if ($subPrefix) {
284         $query['subPrefix'] = $subPrefix;
285     }
286     
287     switch ($operator) {
288         case 'page':
289             
290             // Look for old-style preview request - e.g. {identifier}_cover.jpg
291             if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) {
292                 // Serve preview image
293                 $page = $matches[2];
294                 $query['page'] = $page;
295                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
296             }
297             
298             // New-style preview request - e.g. cover_thumb.jpg
299             if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) {
300                 $query['page'] = $filename;
301                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
302             }
303             
304             // Asking for a non-preview page
305             $query['page'] = $filename;
306             return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
307         
308         default:
309             // Unknown operator
310             return null;            
311     }
312       
313     return null; // was not handled
314   }
315   
316   public static function browserFromUserAgent($userAgent) {
317       $browserPatterns = array(
318           'ipad' => '/iPad/',
319           'iphone' => '/iPhone/', // Also cover iPod Touch
320           'android' => '/Android/',
321       );
322       
323       foreach ($browserPatterns as $browser => $pattern) {
324           if (preg_match($pattern, $userAgent)) {
325               return $browser;
326           }
327       }
328       return null;
329   }
330
331   
332   // $$$ Ideally we will not rely on user agent, but for the moment we do
333   public static function paramsFromUserAgent($userAgent) {
334       // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
335       $browserParams = array(
336           'ipad' => array( 'ui' => 'touch' ),
337           'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
338           'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
339       );
340   
341       $browser = BookReader::browserFromUserAgent($userAgent);
342       if ($browser) {
343           return $browserParams[$browser];
344       }
345       return array();
346   }
347   
348   public static function parsePath($path) {
349     // Parse the BookReader path and return the parts
350     // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
351     //            'operator' => 'page', 'filename' => 'cover.jpg')
352     
353     $parts = array();
354     
355     // Pull off query, e.g. ?foo=bar
356     if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
357         $parts['query'] = $matches[2];
358         $path = $matches[1];
359     }
360     
361     // Pull off identifier
362     if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
363         // no match
364         return $parts;
365     }
366     $parts['identifier'] = $matches[0];
367     $path = substr($path, strlen($matches[0]));
368     
369     // Look for operators
370     // The sub-prefix can be arbitrary, so we match up until the first operator
371     $operators = '(' . join('|', self::$downloadOperators) . ')';
372     $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
373     if (preg_match($pattern, $path, $matches) === 1) {
374         $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
375         $parts['operator'] = $matches['operator'];
376         $parts['operand'] = $matches['operand'];
377     } else {
378         $parts['subPrefix'] = $path;
379     }
380     
381     return $parts;
382   }
383     
384 }
385
386 ?>