X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=BookReaderIA%2Finc%2FBookReader.inc;h=52496433132fdd381422fd7548dbd55de3f644bd;hb=d5667579b9c7591b7ccd1b6c94283c39c5476183;hp=41c05956f5952660e9665e2e3c2178bbe056f6b3;hpb=708e15116dc22edc06fc81a5591e433e912d24ed;p=bookreader.git diff --git a/BookReaderIA/inc/BookReader.inc b/BookReaderIA/inc/BookReader.inc index 41c0595..5249643 100644 --- a/BookReaderIA/inc/BookReader.inc +++ b/BookReaderIA/inc/BookReader.inc @@ -1,8 +1,21 @@ +/* + * + +/* + * Note: Edits to this file must pass through github. To submit a patch to this + * file please contact mang at archive dot org or http://github.com/mangtronix + * Direct changes to this file may get clobbered when the code is synchronized + * from github. + */ + class BookReader { + // Operators recognized in BookReader download URLs + public static $downloadOperators = array('page'); + // Returns true if can display the book in item with a given prefix (typically the item identifier) public static function canDisplay($item, $prefix, $checkOldScandata = false) { @@ -51,15 +64,18 @@ class BookReader public static function findPrefix($urlPortion) { if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) { + // URL portion was empty or started with /, &, or ? -- no item identifier return false; } - $prefix = $matches[0]; // identifier + $prefix = $matches[0]; // item identifier // $$$ Currently swallows the rest of the URL. // If we want to support e.g. /stream/itemid/subdir/prefix/page/23 will need to adjust. if (preg_match('#[^/&?]+/([^&?]+)#', $urlPortion, $matches)) { - $prefix = $matches[1]; // sub prefix + // Match is everything after item identifier and slash, up to end or ? or & + // e.g. itemid/{match/these/parts}?foo=bar + $prefix = $matches[1]; // sub prefix -- } return $prefix; @@ -81,7 +97,7 @@ class BookReader // manually update with Launchpad version number at each checkin so that browsers // do not use old cached version // see https://bugs.launchpad.net/gnubook/+bug/330748 - $version = "r25"; + $version = "imageurls"; if (BookReader::getDevHost($server)) { // on dev host - add time to force reload @@ -112,9 +128,15 @@ class BookReader } /* uiMode */ ?> - + + +
@@ -229,43 +251,130 @@ class BookReader // $path should look like {itemId}/{operator}/{filename} // Other operators may be added - $parts = preg_split('#/#', $path, 3); - if (count($parts) != 3) { - return null; + $urlParts = BookReader::parsePath($path); + + // Check for non-handled cases + $required = array('identifier', 'operator', 'operand'); + foreach ($required as $key) { + if (!array_key_exists($key, $urlParts)) { + return null; + } } - $identifier = $parts[0]; - $operator = $parts[1]; - $filename = $parts[2]; + + $identifier = $urlParts['identifier']; + $operator = $urlParts['operator']; + $filename = $urlParts['operand']; + $subPrefix = $urlParts['subPrefix']; $serverBaseURL = BookReader::serverBaseURL($item->getServer()); - + + // Baseline query params + $query = array( + 'id' => $identifier, + 'itemPath' => $item->getMainDir(), + 'server' => $serverBaseURL + ); + if ($subPrefix) { + $query['subPrefix'] = $subPrefix; + } + switch ($operator) { case 'page': - // Find bookId and which page was requested - $pathParts = pathinfo($filename); - // Look for preview request - if (preg_match('/^(.*)_(cover|title|preview)$/', $pathParts['filename'], $matches) === 0) { - return null; + // Look for old-style preview request - e.g. {identifier}_cover.jpg + if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) { + // Serve preview image + $page = $matches[2]; + $query['page'] = $page; + return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&'); } - $bookId = $matches[1]; - $page = $matches[2]; - $query = array( - 'id' => $identifier, - 'bookId' => $bookId, - 'itemPath' => $item->getMainDir(), - 'server' => $serverBaseURL, - 'page' => $page, - ); - return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&'); + + // New-style preview request - e.g. cover_thumb.jpg + if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) { + $query['page'] = $filename; + return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&'); + } + + // Asking for a non-preview page + $query['page'] = $filename; + return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&'); default: + // Unknown operator return null; } return null; // was not handled } + public static function browserFromUserAgent($userAgent) { + $browserPatterns = array( + 'ipad' => '/iPad/', + 'iphone' => '/iPhone/', // Also cover iPod Touch + 'android' => '/Android/', + ); + + foreach ($browserPatterns as $browser => $pattern) { + if (preg_match($pattern, $userAgent)) { + return $browser; + } + } + return null; + } + + + // $$$ Ideally we will not rely on user agent, but for the moment we do + public static function paramsFromUserAgent($userAgent) { + // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries + $browserParams = array( + 'ipad' => array( 'ui' => 'touch' ), + 'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ), + 'android' => array( 'ui' => 'embed', 'mode' => '1up' ), + ); + + $browser = BookReader::browserFromUserAgent($userAgent); + if ($browser) { + return $browserParams[$browser]; + } + return array(); + } + + public static function parsePath($path) { + // Parse the BookReader path and return the parts + // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir', + // 'operator' => 'page', 'filename' => 'cover.jpg') + + $parts = array(); + + // Pull off query, e.g. ?foo=bar + if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) { + $parts['query'] = $matches[2]; + $path = $matches[1]; + } + + // Pull off identifier + if (preg_match('#[^/&?]+#', $path, $matches) === 0) { + // no match + return $parts; + } + $parts['identifier'] = $matches[0]; + $path = substr($path, strlen($matches[0])); + + // Look for operators + // The sub-prefix can be arbitrary, so we match up until the first operator + $operators = '(' . join('|', self::$downloadOperators) . ')'; + $pattern = '#(?P