class BookReader
{
// 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)
{
// A "book" is an image stack and scandata.
// 1. Old items may have scandata.xml or scandata.zip and itemid_{imageformat}.{zip,tar}
// 2. Newer items may have multiple {arbitraryname}_scandata.xml and {arbitraryname}_{imageformat}.{zip,tar}
$foundScandata = false;
$foundImageStack = false;
$targetScandata = $prefix . "_scandata.xml";
// $$$ 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|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;
}
}
if ($filename == $targetScandata) {
$foundScandata = $filename;
}
if (preg_match($imageFormatRegex, $filename)) {
$foundImageStack = $filename;
}
}
if ($foundScandata && $foundImageStack) {
return true;
}
return false;
}
// Finds the prefix to use for the book given the part of the URL trailing after /stream/
public static function findPrefix($urlPortion)
{
if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
return false;
}
$prefix = $matches[0]; // 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
}
return $prefix;
}
// $$$ would be cleaner to use different templates instead of the uiMode param
//
// @param subprefix Optional prefix to display a book inside an item (e.g. if does not match identifier)
public static function draw($server, $mainDir, $identifier, $subPrefix, $title,
$coverLeaf=null, $titleStart='Internet Archive', $uiMode='full')
{
// Set title to default if not set
if (!$title) {
$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 = "r25";
if (BookReader::getDevHost($server)) {
// on dev host - add time to force reload
$version .= '_' . time();
}
if ("" == $id) {
echo "No identifier specified!";
die(-1);
}
$metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
?>
} /* uiMode */ ?>
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 serverBaseURL($server)
{
// 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
$devhost = BookReader::getDevHost($server);
$devhosts = array('mang', 'testflip', 'rkumar');
if (in_array($devhost, $devhosts)) {
$server = $server . "/~" . $devhost;
}
return $server;
}
public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
{
$serverBaseURL = BookReader::serverBaseURL($server);
$params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
if ($subPrefix) {
$params['subPrefix'] = $subPrefix;
}
$keys = array_keys($params);
$lastParam = end($keys);
$url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
foreach($params as $param=>$value) {
$url .= $param . '=' . $value;
if ($param != $lastParam) {
$url .= '&';
}
}
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
$parts = preg_split('#/#', $path, 3);
if (count($parts) != 3) {
return null;
}
$identifier = $parts[0];
$operator = $parts[1];
$filename = $parts[2];
$serverBaseURL = BookReader::serverBaseURL($item->getServer());
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;
}
$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, '', '&');
default:
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();
}
}
?>