<?
+/*
+ *
+
+/*
+ * 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)
{
// $$$ TODO add support for jpg and tar stacks
// https://bugs.edge.launchpad.net/gnubook/+bug/323003
// https://bugs.edge.launchpad.net/gnubook/+bug/385397
- $imageFormatRegex = '@' . preg_quote($prefix, '@') . '_(jp2|tif)\.zip$@';
+ $imageFormatRegex = '@' . preg_quote($prefix, '@') . '_(jp2|tif|jpg)\.(zip|tar)$@';
$baseLength = strlen($item->metadataGrabber->mainDir . '/');
foreach ($item->getFiles() as $location => $fileInfo) {
$filename = substr($location, $baseLength);
-
+
if ($checkOldScandata) {
if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
$foundScandata = $filename;
$foundImageStack = $filename;
}
}
-
+
if ($foundScandata && $foundImageStack) {
return true;
}
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;
{
// Set title to default if not set
if (!$title) {
- $title = 'Bookreader';
+ $title = 'BookReader';
}
$id = $identifier;
// 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 = "0.9.18";
+ $version = "imageurls";
+
+ if (BookReader::getDevHost($server)) {
+ // on dev host - add time to force reload
+ $version .= '_' . time();
+ }
if ("" == $id) {
echo "No identifier specified!";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
+ <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
+ <meta name="apple-mobile-web-app-capable" content="yes" />
<title><? echo $title; ?></title>
<!--[if lte IE 6]>
<meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
<link rel="stylesheet" type="text/css" href="/bookreader/BookReader.css?v=<? echo($version); ?>">
<? if ($uiMode == "embed") { ?>
<link rel="stylesheet" type="text/css" href="/bookreader/BookReaderEmbed.css?v=<? echo($version); ?>">
+<? } elseif ($uiMode == "touch") { ?>
+ <link rel="stylesheet" type="text/css" href="/bookreader/touch/BookReaderTouch.css?v=<? echo($version); ?>">
<? } /* uiMode */ ?>
- <script src="/includes/jquery-1.3.2.min.js" type="text/javascript"></script>
- <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
- <script type="text/javascript" src="/bookreader/jquery.easing.1.3.js"></script>
+ <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
+ <script type="text/javascript" src="/bookreader/jquery-ui-1.8.1.custom.min.js?v=<? echo($version); ?>"></script>
+ <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
+ <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
+ <script type="text/javascript" src="/bookreader/soundmanager/soundmanager2.js?v=<? echo($version); ?>"></script>
+ <script>
+ soundManager.debugMode = false;
+ soundManager.url = '/bookreader/soundmanager/swf/';
+ soundManager.useHTML5Audio = true;
+ </script>
</head>
<body style="background-color: #FFFFFF;">
brConfig["mode"] = 2;
<? } ?>
</script>
+<!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
<script type="text/javascript" src="<? echo($metaURL); ?>"></script>
<? if ($uiMode == 'full') { ?>
<?
exit;
}
+
+ // Returns the user part of dev host from URL, or null
+ public static function getDevHost($server)
+ {
+ if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
+ return $match[1];
+ }
+
+ return null;
+ }
- public static function adjustToHome($server)
+ public static function serverBaseURL($server)
{
- // Check if we're on a dev vhost and point to JSIA in the user's public_html
- // on the datanode
- if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
+ // Check if we're on a dev vhost and point to JSIA in the user's public_html
+ // on the datanode
// $$$ the remapping isn't totally automatic yet and requires user to
// ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
// so we enable it only for known hosts
- $devhosts = array('mang', 'testflip', 'rkumar');
- if (in_array($match[1], $devhosts)) {
- return $server . ":81/~" . $match[1]; //xxxx
+ $devhost = BookReader::getDevHost($server);
+ $devhosts = array('mang', 'testflip', 'rkumar', 'mccabe');
+ if (in_array($devhost, $devhosts)) {
+ $server = $server . "/~" . $devhost;
}
- }
- return $server;
+ return $server;
}
public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
{
- $serverBaseURL = BookReader::adjustToHome($server);
+ $serverBaseURL = BookReader::serverBaseURL($server);
$params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
if ($subPrefix) {
return $url;
}
+ // Return the URL for the requested /download/$path, or null
+ public static function getURL($path, $item) {
+ // $path should look like {itemId}/{operator}/{filename}
+ // Other operators may be added
+
+ $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 = $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':
+
+ // 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, '', '&');
+ }
+
+ // 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<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
+ if (preg_match($pattern, $path, $matches) === 1) {
+ $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
+ $parts['operator'] = $matches['operator'];
+ $parts['operand'] = $matches['operand'];
+ } else {
+ $parts['subPrefix'] = $path;
+ }
+
+ return $parts;
+ }
+
}
- ?>
+
+?>