+ // 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
+ }
+
+ // $$$ Ideally we will not rely on user agent, but for the moment we do
+ public static function paramsFromUserAgent($userAgent) {
+ $browserParams = array(
+ 'ipad' => array( 'ui' => 'touch' ),
+ 'iphone' => array( 'ui' => 'embed', 'mode' => '1up'),
+ );
+
+ $browser = BookReader::browserFromUserAgent($userAgent);
+ if ($browser) {
+ return $browserParams[$browser];
+ }
+ return array();
+ }
+
+ public static function browserFromUserAgent($userAgent) {
+ $browserPatterns = array(
+ 'ipad' => '/iPad/',
+ 'iphone' => '/iPhone/', // Also cover iPod Touch
+ );
+
+ foreach ($browserPatterns as $browser => $pattern) {
+ if (preg_match($pattern, $userAgent)) {
+ return $browser;
+ }
+ }
+ return null;
+ }
+