4 * Copyright(c) 2008-2010 Internet Archive. Software license AGPL version 3.
6 * This file is part of BookReader. The full source code can be found at GitHub:
7 * http://github.com/openlibrary/bookreader
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
18 // Operators recognized in BookReader download URLs
19 public static $downloadOperators = array('page');
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)
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}
29 $foundScandata = false;
30 $foundImageStack = false;
32 $targetScandata = $prefix . "_scandata.xml";
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)$@';
39 $baseLength = strlen($item->metadataGrabber->mainDir . '/');
40 foreach ($item->getFiles() as $location => $fileInfo) {
41 $filename = substr($location, $baseLength);
43 if ($checkOldScandata) {
44 if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
45 $foundScandata = $filename;
49 if ($filename == $targetScandata) {
50 $foundScandata = $filename;
53 if (preg_match($imageFormatRegex, $filename)) {
54 $foundImageStack = $filename;
58 if ($foundScandata && $foundImageStack) {
65 // Finds the prefix to use for the book given the part of the URL trailing after /stream/
66 public static function findPrefix($urlPortion)
68 if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
69 // URL portion was empty or started with /, &, or ? -- no item identifier
73 $prefix = $matches[0]; // item identifier
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 --
86 // $$$ would be cleaner to use different templates instead of the uiMode param
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')
92 // Set title to default if not set
94 $title = 'BookReader';
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
104 if (BookReader::getDevHost($server)) {
105 // on dev host - add time to force reload
106 $version .= '_' . time();
110 echo "No identifier specified!";
114 $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
117 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
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>
124 <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
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); ?>">
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 <script type="text/javascript" src="/bookreader/jquery.ui.ipad.js"></script>
138 <!-- THIS ALLOWS BEAUTYTIPS TO WORK ON IE -->
140 <script type="text/javascript" src="excanvas.compiled.js"></script>
142 <script type="text/javascript" src="/bookreader/jquery.bt.min.js"></script>
143 <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
144 <script type="text/javascript" src="/bookreader/soundmanager/soundmanager2.js?v=<? echo($version); ?>"></script>
146 soundManager.debugMode = false;
147 soundManager.url = '/bookreader/soundmanager/swf/';
148 soundManager.useHTML5Audio = true;
149 soundManager.flashVersion = 9; //flash 8 version of swf is buggy when calling play() on a sound that is still loading
152 <body style="background-color: ##939598;">
156 // <? if ($uiMode == 'full') { ?>
157 // <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
159 // <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 <div id="BookReader">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
166 <script type="text/javascript">
167 // Set some config variables -- $$$ NB: Config object format has not been finalized
169 <? if ($uiMode == 'embed') { ?>
170 brConfig["mode"] = 1;
171 brConfig["reduce"] = 8;
172 brConfig["ui"] = "embed";
174 brConfig["mode"] = 2;
177 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
178 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
180 <script type="text/javascript">
181 // $$$ hack to workaround sizing bug when starting in two-up mode
182 $(document).ready(function() {
183 $(window).trigger('resize');
187 if(window.archive_analytics) { window.archive_analytics.values['bookreader'] = 'open'};
193 // Returns the user part of dev host from URL, or null
194 public static function getDevHost($server)
196 if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
204 public static function serverBaseURL($server)
206 // Check if we're on a dev vhost and point to JSIA in the user's public_html
208 // $$$ the remapping isn't totally automatic yet and requires user to
209 // ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
210 // so we enable it only for known hosts
211 $devhost = BookReader::getDevHost($server);
212 $devhosts = array('mang', 'testflip', 'rkumar');
213 if (in_array($devhost, $devhosts)) {
214 $server = $server . "/~" . $devhost;
220 public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
222 $serverBaseURL = BookReader::serverBaseURL($server);
224 $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
226 $params['subPrefix'] = $subPrefix;
229 $keys = array_keys($params);
230 $lastParam = end($keys);
231 $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
232 foreach($params as $param=>$value) {
233 $url .= $param . '=' . $value;
234 if ($param != $lastParam) {
242 // Return the URL for the requested /download/$path, or null
243 public static function getURL($path, $item) {
244 // $path should look like {itemId}/{operator}/{filename}
245 // Other operators may be added
247 $urlParts = BookReader::parsePath($path);
249 // Check for non-handled cases
250 $required = array('identifier', 'operator', 'operand');
251 foreach ($required as $key) {
252 if (!array_key_exists($key, $urlParts)) {
257 $identifier = $urlParts['identifier'];
258 $operator = $urlParts['operator'];
259 $filename = $urlParts['operand'];
260 $subPrefix = $urlParts['subPrefix'];
262 $serverBaseURL = BookReader::serverBaseURL($item->getServer());
264 // Baseline query params
267 'itemPath' => $item->getMainDir(),
268 'server' => $serverBaseURL
271 $query['subPrefix'] = $subPrefix;
277 // Look for old-style preview request - e.g. {identifier}_cover.jpg
278 if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) {
279 // Serve preview image
281 $query['page'] = $page;
282 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
285 // New-style preview request - e.g. cover_thumb.jpg
286 if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) {
287 $query['page'] = $filename;
288 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
291 // Asking for a non-preview page
292 $query['page'] = $filename;
293 return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
300 return null; // was not handled
303 public static function browserFromUserAgent($userAgent) {
304 $browserPatterns = array(
306 'iphone' => '/iPhone/', // Also cover iPod Touch
307 'android' => '/Android/',
310 foreach ($browserPatterns as $browser => $pattern) {
311 if (preg_match($pattern, $userAgent)) {
319 // $$$ Ideally we will not rely on user agent, but for the moment we do
320 public static function paramsFromUserAgent($userAgent) {
321 // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
322 $browserParams = array(
323 'ipad' => array( 'ui' => 'touch' ),
324 'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
325 'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
328 $browser = BookReader::browserFromUserAgent($userAgent);
330 return $browserParams[$browser];
335 public static function parsePath($path) {
336 // Parse the BookReader path and return the parts
337 // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
338 // 'operator' => 'page', 'filename' => 'cover.jpg')
342 // Pull off query, e.g. ?foo=bar
343 if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
344 $parts['query'] = $matches[2];
348 // Pull off identifier
349 if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
353 $parts['identifier'] = $matches[0];
354 $path = substr($path, strlen($matches[0]));
356 // Look for operators
357 // The sub-prefix can be arbitrary, so we match up until the first operator
358 $operators = '(' . join('|', self::$downloadOperators) . ')';
359 $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
360 if (preg_match($pattern, $path, $matches) === 1) {
361 $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
362 $parts['operator'] = $matches['operator'];
363 $parts['operand'] = $matches['operand'];
365 $parts['subPrefix'] = $path;