6 // Operators recognized in BookReader download URLs
7 public static $downloadOperators = array('page');
9 // Returns true if can display the book in item with a given prefix (typically the item identifier)
10 public static function canDisplay($item, $prefix, $checkOldScandata = false)
13 // A "book" is an image stack and scandata.
14 // 1. Old items may have scandata.xml or scandata.zip and itemid_{imageformat}.{zip,tar}
15 // 2. Newer items may have multiple {arbitraryname}_scandata.xml and {arbitraryname}_{imageformat}.{zip,tar}
17 $foundScandata = false;
18 $foundImageStack = false;
20 $targetScandata = $prefix . "_scandata.xml";
22 // $$$ TODO add support for jpg and tar stacks
23 // https://bugs.edge.launchpad.net/gnubook/+bug/323003
24 // https://bugs.edge.launchpad.net/gnubook/+bug/385397
25 $imageFormatRegex = '@' . preg_quote($prefix, '@') . '_(jp2|tif|jpg)\.(zip|tar)$@';
27 $baseLength = strlen($item->metadataGrabber->mainDir . '/');
28 foreach ($item->getFiles() as $location => $fileInfo) {
29 $filename = substr($location, $baseLength);
31 if ($checkOldScandata) {
32 if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
33 $foundScandata = $filename;
37 if ($filename == $targetScandata) {
38 $foundScandata = $filename;
41 if (preg_match($imageFormatRegex, $filename)) {
42 $foundImageStack = $filename;
46 if ($foundScandata && $foundImageStack) {
53 // Finds the prefix to use for the book given the part of the URL trailing after /stream/
54 public static function findPrefix($urlPortion)
56 if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
57 // URL portion was empty or started with /, &, or ? -- no item identifier
61 $prefix = $matches[0]; // item identifier
63 // $$$ Currently swallows the rest of the URL.
64 // If we want to support e.g. /stream/itemid/subdir/prefix/page/23 will need to adjust.
65 if (preg_match('#[^/&?]+/([^&?]+)#', $urlPortion, $matches)) {
66 // Match is everything after item identifier and slash, up to end or ? or &
67 // e.g. itemid/{match/these/parts}?foo=bar
68 $prefix = $matches[1]; // sub prefix --
74 // $$$ would be cleaner to use different templates instead of the uiMode param
76 // @param subprefix Optional prefix to display a book inside an item (e.g. if does not match identifier)
77 public static function draw($server, $mainDir, $identifier, $subPrefix, $title,
78 $coverLeaf=null, $titleStart='Internet Archive', $uiMode='full')
80 // Set title to default if not set
82 $title = 'BookReader';
87 // manually update with Launchpad version number at each checkin so that browsers
88 // do not use old cached version
89 // see https://bugs.launchpad.net/gnubook/+bug/330748
92 if (BookReader::getDevHost($server)) {
93 // on dev host - add time to force reload
94 $version .= '_' . time();
98 echo "No identifier specified!";
102 $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
105 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
108 <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
109 <meta name="apple-mobile-web-app-capable" content="yes" />
110 <title><? echo $title; ?></title>
112 <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
114 <link rel="stylesheet" type="text/css" href="/bookreader/BookReader.css?v=<? echo($version); ?>">
115 <? if ($uiMode == "embed") { ?>
116 <link rel="stylesheet" type="text/css" href="/bookreader/BookReaderEmbed.css?v=<? echo($version); ?>">
117 <? } elseif ($uiMode == "touch") { ?>
118 <link rel="stylesheet" type="text/css" href="/bookreader/touch/BookReaderTouch.css?v=<? echo($version); ?>">
120 <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
121 <script type="text/javascript" src="/bookreader/jquery-ui-1.8.1.custom.min.js?v=<? echo($version); ?>"></script>
122 <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
123 <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
125 <body style="background-color: #FFFFFF;">
127 <? if ($uiMode == 'full') { ?>
128 <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
130 <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>
133 <script type="text/javascript">
134 // Set some config variables -- $$$ NB: Config object format has not been finalized
136 <? if ($uiMode == 'embed') { ?>
137 brConfig["mode"] = 1;
138 brConfig["reduce"] = 8;
139 brConfig["ui"] = "embed";
141 brConfig["mode"] = 2;
144 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
145 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
147 <? if ($uiMode == 'full') { ?>
148 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
149 <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
150 <p style="display: inline">
151 <input id="BookReaderSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
154 <div id="BookReaderSearchResults">
161 <div class="BRlogotype">
162 <a href="http://archive.org/" class="BRblack">Internet Archive</a>
164 <div class="BRnavlinks">
165 <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
166 <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
167 <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
168 <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
169 <a class="BRblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
174 <script type="text/javascript">
175 // $$$ hack to workaround sizing bug when starting in two-up mode
176 $(document).ready(function() {
177 $(window).trigger('resize');
184 // Returns the user part of dev host from URL, or null
185 public static function getDevHost($server)
187 if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
195 public static function serverBaseURL($server)
197 // Check if we're on a dev vhost and point to JSIA in the user's public_html
199 // $$$ the remapping isn't totally automatic yet and requires user to
200 // ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
201 // so we enable it only for known hosts
202 $devhost = BookReader::getDevHost($server);
203 $devhosts = array('mang', 'testflip', 'rkumar');
204 if (in_array($devhost, $devhosts)) {
205 $server = $server . "/~" . $devhost;
211 public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
213 $serverBaseURL = BookReader::serverBaseURL($server);
215 $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
217 $params['subPrefix'] = $subPrefix;
220 $keys = array_keys($params);
221 $lastParam = end($keys);
222 $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
223 foreach($params as $param=>$value) {
224 $url .= $param . '=' . $value;
225 if ($param != $lastParam) {
233 // Return the URL for the requested /download/$path, or null
234 public static function getURL($path, $item) {
235 // $path should look like {itemId}/{operator}/{filename}
236 // Other operators may be added
238 $urlParts = BookReader::parsePath($path);
240 // Check for non-handled cases
241 $required = array('identifier', 'operator', 'operand');
242 foreach ($required as $key) {
243 if (!array_key_exists($key, $urlParts)) {
248 $identifier = $urlParts['identifier'];
249 $operator = $urlParts['operator'];
250 $filename = $urlParts['operand'];
251 $subPrefix = $urlParts['subPrefix'];
253 $serverBaseURL = BookReader::serverBaseURL($item->getServer());
255 // Baseline query params
258 'itemPath' => $item->getMainDir(),
259 'server' => $serverBaseURL
262 $query['subPrefix'] = $subPrefix;
268 // Look for old-style preview request
269 // $$$ currently ignoring file extension
270 if (preg_match('/^(.*)_(cover|title|preview)/', $filename, $matches) === 1) {
271 // Serve preview image
273 $query['page'] = $page;
274 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
277 // Asking for a non-preview page
278 $query['page'] = $filename;
279 return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
286 return null; // was not handled
289 public static function browserFromUserAgent($userAgent) {
290 $browserPatterns = array(
292 'iphone' => '/iPhone/', // Also cover iPod Touch
293 'android' => '/Android/',
296 foreach ($browserPatterns as $browser => $pattern) {
297 if (preg_match($pattern, $userAgent)) {
305 // $$$ Ideally we will not rely on user agent, but for the moment we do
306 public static function paramsFromUserAgent($userAgent) {
307 // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
308 $browserParams = array(
309 'ipad' => array( 'ui' => 'touch' ),
310 'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
311 'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
314 $browser = BookReader::browserFromUserAgent($userAgent);
316 return $browserParams[$browser];
321 public static function parsePath($path) {
322 // Parse the BookReader path and return the parts
323 // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
324 // 'operator' => 'page', 'filename' => 'cover.jpg')
328 // Pull off query, e.g. ?foo=bar
329 if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
330 $parts['query'] = $matches[2];
334 // Pull off identifier
335 if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
339 $parts['identifier'] = $matches[0];
340 $path = substr($path, strlen($matches[0]));
342 // Look for operators
343 // The sub-prefix can be arbitrary, so we match up until the first operator
344 $operators = '(' . join('|', self::$downloadOperators) . ')';
345 $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
346 if (preg_match($pattern, $path, $matches) === 1) {
347 $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
348 $parts['operator'] = $matches['operator'];
349 $parts['operand'] = $matches['operand'];
351 $parts['subPrefix'] = $path;