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="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
135 <script type="text/javascript" src="/bookreader/jquery.colorbox-min.js"></script>
136 <!-- THIS ALLOWS BEAUTYTIPS TO WORK ON IE -->
138 <script type="text/javascript" src="excanvas.compiled.js"></script>
140 <script type="text/javascript" src="/bookreader/jquery.bt.min.js"></script>
141 <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
143 <body style="background-color: ##939598;">
147 // <? if ($uiMode == 'full') { ?>
148 // <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
150 // <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>
155 <div id="BookReader">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
159 <div id="pager"></div>
160 <div id="BRnavline"></div>
161 <!-- LOAD SEARCH RESULTS FIRST SO CHAPTER INDICATORS CAN APPEAR IN FRONT -->
162 <div class="search" style="left:80%;" title="Search result">
163 <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>
166 <div class="search" style="left:22%;" title="Search result">
167 <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>
170 <div class="search" style="left:75%;" title="Search result">
171 <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>
174 <div class="chapter" style="left:1%;">
175 <div class="title">I. The Minotaur <span>|</span> Page 1</div>
178 <div class="chapter" style="left:17%;">
179 <div class="title">II. The Griffon <span>|</span> Page 44</div>
182 <div class="chapter" style="left:30%;">
183 <div class="title">III. The Firedrake <span>|</span> Page 129</div>
186 <div class="chapter" style="left:67.5%;">
187 <div class="title">V. The Pegasus <span>|</span> Page 201</div>
190 <div class="chapter" style="left:90%;">
191 <div class="title">VI. The Goblin <span>|</span> Page 255</div>
194 <div class="searchChap" style="left:49%;" title="Search result">
196 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>
197 <div class="queryChap">IV. The Witch <span>|</span> Page 163</div>
204 <script type="text/javascript">
205 // Set some config variables -- $$$ NB: Config object format has not been finalized
207 <? if ($uiMode == 'embed') { ?>
208 brConfig["mode"] = 1;
209 brConfig["reduce"] = 8;
210 brConfig["ui"] = "embed";
212 brConfig["mode"] = 2;
215 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
216 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
218 <? if ($uiMode == 'XXXmang was full') { ?>
219 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
220 <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
221 <p style="display: inline">
222 <input id="BookReaderSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
225 <div id="BookReaderSearchResults">
232 <div class="BRlogotype">
233 <a href="http://archive.org/" class="BRblack">Internet Archive</a>
235 <div class="BRnavlinks">
236 <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
237 <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
238 <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
239 <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
240 <a class="BRblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
245 <script type="text/javascript">
246 // $$$ hack to workaround sizing bug when starting in two-up mode
247 $(document).ready(function() {
248 $(window).trigger('resize');
252 function hideFace() {
253 $('#BookReader').die('mousemove').live('mousemove',function(event) {
254 var toolpos = $('#BRtoolbar').offset();
255 var tooltop = toolpos.top;
256 var navkey = $(document).height() - 75;
257 if ((event.pageY < 76) || (event.pageY > navkey)) {
258 if (tooltop == -60) {
259 $('#BRtoolbar').animate({top:'0'});
260 $('#BRnav').animate({bottom:'0'});
262 } else if ($('.bt-wrapper').size() == 0) {
264 $('#BRtoolbar').animate({top:'-60'});
265 $('#BRnav').animate({bottom:'-60'});
270 window.onload = function() {
271 window.setTimeout(hideFace, 3000);
278 // Returns the user part of dev host from URL, or null
279 public static function getDevHost($server)
281 if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
289 public static function serverBaseURL($server)
291 // Check if we're on a dev vhost and point to JSIA in the user's public_html
293 // $$$ the remapping isn't totally automatic yet and requires user to
294 // ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
295 // so we enable it only for known hosts
296 $devhost = BookReader::getDevHost($server);
297 $devhosts = array('mang', 'testflip', 'rkumar');
298 if (in_array($devhost, $devhosts)) {
299 $server = $server . "/~" . $devhost;
305 public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
307 $serverBaseURL = BookReader::serverBaseURL($server);
309 $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
311 $params['subPrefix'] = $subPrefix;
314 $keys = array_keys($params);
315 $lastParam = end($keys);
316 $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
317 foreach($params as $param=>$value) {
318 $url .= $param . '=' . $value;
319 if ($param != $lastParam) {
327 // Return the URL for the requested /download/$path, or null
328 public static function getURL($path, $item) {
329 // $path should look like {itemId}/{operator}/{filename}
330 // Other operators may be added
332 $urlParts = BookReader::parsePath($path);
334 // Check for non-handled cases
335 $required = array('identifier', 'operator', 'operand');
336 foreach ($required as $key) {
337 if (!array_key_exists($key, $urlParts)) {
342 $identifier = $urlParts['identifier'];
343 $operator = $urlParts['operator'];
344 $filename = $urlParts['operand'];
345 $subPrefix = $urlParts['subPrefix'];
347 $serverBaseURL = BookReader::serverBaseURL($item->getServer());
349 // Baseline query params
352 'itemPath' => $item->getMainDir(),
353 'server' => $serverBaseURL
356 $query['subPrefix'] = $subPrefix;
362 // Look for old-style preview request - e.g. {identifier}_cover.jpg
363 if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) {
364 // Serve preview image
366 $query['page'] = $page;
367 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
370 // New-style preview request - e.g. cover_thumb.jpg
371 if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) {
372 $query['page'] = $filename;
373 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
376 // Asking for a non-preview page
377 $query['page'] = $filename;
378 return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
385 return null; // was not handled
388 public static function browserFromUserAgent($userAgent) {
389 $browserPatterns = array(
391 'iphone' => '/iPhone/', // Also cover iPod Touch
392 'android' => '/Android/',
395 foreach ($browserPatterns as $browser => $pattern) {
396 if (preg_match($pattern, $userAgent)) {
404 // $$$ Ideally we will not rely on user agent, but for the moment we do
405 public static function paramsFromUserAgent($userAgent) {
406 // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
407 $browserParams = array(
408 'ipad' => array( 'ui' => 'touch' ),
409 'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
410 'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
413 $browser = BookReader::browserFromUserAgent($userAgent);
415 return $browserParams[$browser];
420 public static function parsePath($path) {
421 // Parse the BookReader path and return the parts
422 // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
423 // 'operator' => 'page', 'filename' => 'cover.jpg')
427 // Pull off query, e.g. ?foo=bar
428 if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
429 $parts['query'] = $matches[2];
433 // Pull off identifier
434 if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
438 $parts['identifier'] = $matches[0];
439 $path = substr($path, strlen($matches[0]));
441 // Look for operators
442 // The sub-prefix can be arbitrary, so we match up until the first operator
443 $operators = '(' . join('|', self::$downloadOperators) . ')';
444 $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
445 if (preg_match($pattern, $path, $matches) === 1) {
446 $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
447 $parts['operator'] = $matches['operator'];
448 $parts['operand'] = $matches['operand'];
450 $parts['subPrefix'] = $path;