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
16 // Operators recognized in BookReader download URLs
17 public static $downloadOperators = array('page');
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)
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}
27 $foundScandata = false;
28 $foundImageStack = false;
30 $targetScandata = $prefix . "_scandata.xml";
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)$@';
37 $baseLength = strlen($item->metadataGrabber->mainDir . '/');
38 foreach ($item->getFiles() as $location => $fileInfo) {
39 $filename = substr($location, $baseLength);
41 if ($checkOldScandata) {
42 if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
43 $foundScandata = $filename;
47 if ($filename == $targetScandata) {
48 $foundScandata = $filename;
51 if (preg_match($imageFormatRegex, $filename)) {
52 $foundImageStack = $filename;
56 if ($foundScandata && $foundImageStack) {
63 // Finds the prefix to use for the book given the part of the URL trailing after /stream/
64 public static function findPrefix($urlPortion)
66 if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
67 // URL portion was empty or started with /, &, or ? -- no item identifier
71 $prefix = $matches[0]; // item identifier
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 --
84 // $$$ would be cleaner to use different templates instead of the uiMode param
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')
90 // Set title to default if not set
92 $title = 'BookReader';
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 = "imageurls";
102 if (BookReader::getDevHost($server)) {
103 // on dev host - add time to force reload
104 $version .= '_' . time();
108 echo "No identifier specified!";
112 $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
115 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
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>
122 <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
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); ?>">
130 <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
131 <script type="text/javascript" src="/bookreader/jquery-ui-1.8.1.custom.min.js?v=<? echo($version); ?>"></script>
132 <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
133 <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
134 <script type="text/javascript" src="/bookreader/soundmanager/soundmanager2.js?v=<? echo($version); ?>"></script>
136 soundManager.debugMode = false;
137 soundManager.url = '/bookreader/soundmanager/swf/';
138 soundManager.useHTML5Audio = true;
141 <body style="background-color: #FFFFFF;">
143 <? if ($uiMode == 'full') { ?>
144 <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
146 <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>
149 <script type="text/javascript">
150 // Set some config variables -- $$$ NB: Config object format has not been finalized
152 <? if ($uiMode == 'embed') { ?>
153 brConfig["mode"] = 1;
154 brConfig["reduce"] = 8;
155 brConfig["ui"] = "embed";
157 brConfig["mode"] = 2;
160 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
161 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
163 <? if ($uiMode == 'full') { ?>
164 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
165 <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
166 <p style="display: inline">
167 <input id="BookReaderSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
170 <div id="BookReaderSearchResults">
177 <div class="BRlogotype">
178 <a href="http://archive.org/" class="BRblack">Internet Archive</a>
180 <div class="BRnavlinks">
181 <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
182 <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
183 <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
184 <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
185 <a class="BRblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
190 <script type="text/javascript">
191 // $$$ hack to workaround sizing bug when starting in two-up mode
192 $(document).ready(function() {
193 $(window).trigger('resize');
200 // Returns the user part of dev host from URL, or null
201 public static function getDevHost($server)
203 if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
211 public static function serverBaseURL($server)
213 // Check if we're on a dev vhost and point to JSIA in the user's public_html
215 // $$$ the remapping isn't totally automatic yet and requires user to
216 // ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
217 // so we enable it only for known hosts
218 $devhost = BookReader::getDevHost($server);
219 $devhosts = array('mang', 'testflip', 'rkumar', 'mccabe');
220 if (in_array($devhost, $devhosts)) {
221 $server = $server . "/~" . $devhost;
227 public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
229 $serverBaseURL = BookReader::serverBaseURL($server);
231 $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
233 $params['subPrefix'] = $subPrefix;
236 $keys = array_keys($params);
237 $lastParam = end($keys);
238 $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
239 foreach($params as $param=>$value) {
240 $url .= $param . '=' . $value;
241 if ($param != $lastParam) {
249 // Return the URL for the requested /download/$path, or null
250 public static function getURL($path, $item) {
251 // $path should look like {itemId}/{operator}/{filename}
252 // Other operators may be added
254 $urlParts = BookReader::parsePath($path);
256 // Check for non-handled cases
257 $required = array('identifier', 'operator', 'operand');
258 foreach ($required as $key) {
259 if (!array_key_exists($key, $urlParts)) {
264 $identifier = $urlParts['identifier'];
265 $operator = $urlParts['operator'];
266 $filename = $urlParts['operand'];
267 $subPrefix = $urlParts['subPrefix'];
269 $serverBaseURL = BookReader::serverBaseURL($item->getServer());
271 // Baseline query params
274 'itemPath' => $item->getMainDir(),
275 'server' => $serverBaseURL
278 $query['subPrefix'] = $subPrefix;
284 // Look for old-style preview request - e.g. {identifier}_cover.jpg
285 if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) {
286 // Serve preview image
288 $query['page'] = $page;
289 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
292 // New-style preview request - e.g. cover_thumb.jpg
293 if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) {
294 $query['page'] = $filename;
295 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
298 // Asking for a non-preview page
299 $query['page'] = $filename;
300 return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
307 return null; // was not handled
310 public static function browserFromUserAgent($userAgent) {
311 $browserPatterns = array(
313 'iphone' => '/iPhone/', // Also cover iPod Touch
314 'android' => '/Android/',
317 foreach ($browserPatterns as $browser => $pattern) {
318 if (preg_match($pattern, $userAgent)) {
326 // $$$ Ideally we will not rely on user agent, but for the moment we do
327 public static function paramsFromUserAgent($userAgent) {
328 // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
329 $browserParams = array(
330 'ipad' => array( 'ui' => 'touch' ),
331 'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
332 'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
335 $browser = BookReader::browserFromUserAgent($userAgent);
337 return $browserParams[$browser];
342 public static function parsePath($path) {
343 // Parse the BookReader path and return the parts
344 // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
345 // 'operator' => 'page', 'filename' => 'cover.jpg')
349 // Pull off query, e.g. ?foo=bar
350 if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
351 $parts['query'] = $matches[2];
355 // Pull off identifier
356 if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
360 $parts['identifier'] = $matches[0];
361 $path = substr($path, strlen($matches[0]));
363 // Look for operators
364 // The sub-prefix can be arbitrary, so we match up until the first operator
365 $operators = '(' . join('|', self::$downloadOperators) . ')';
366 $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
367 if (preg_match($pattern, $path, $matches) === 1) {
368 $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
369 $parts['operator'] = $matches['operator'];
370 $parts['operand'] = $matches['operand'];
372 $parts['subPrefix'] = $path;