+ * Serve an image request that requires looking up the book metadata
+ *
+ * Code path:
+ * - Get book metadata
+ * - Parse the requested page (e.g. cover_t.jpg, n5_r4.jpg) to determine which page type,
+ * size and format (etc) is being requested
+ * - Determine the leaf number corresponding to the page
+ * - Determine scaling values
+ * - Serve image request now that all information has been gathered
+ */
+
+ function serveLookupRequest($requestEnv) {
+ $brm = new BookReaderMeta();
+ try {
+ $metadata = $brm->buildMetadata($_REQUEST['id'], $_REQUEST['itemPath'], $_REQUEST['subPrefix'], $_REQUEST['server']);
+ } catch (Exception $e) {
+ $this->BRfatal($e->getMessage());
+ }
+
+ $page = $_REQUEST['page'];
+
+ // Index of image to return
+ $imageIndex = null;
+
+ // deal with subPrefix
+ if ($_REQUEST['subPrefix']) {
+ $parts = split('/', $_REQUEST['subPrefix']);
+ $bookId = $parts[count($parts) - 1 ];
+ } else {
+ $bookId = $_REQUEST['id'];
+ }
+
+ $pageInfo = $this->parsePageRequest($page, $bookId);
+
+ $basePage = $pageInfo['type'];
+
+ $leaf = null;
+ switch ($basePage) {
+
+ case 'title':
+ if (! array_key_exists('titleIndex', $metadata)) {
+ $this->BRfatal("No title page asserted in book");
+ }
+ $imageIndex = $metadata['titleIndex'];
+ break;
+
+ /* Old 'cover' behaviour where it would show cover 0 if it exists or return 404.
+ Could be re-added as cover0, cover1, etc
+ case 'cover':
+ if (! array_key_exists('coverIndices', $metadata)) {
+ $this->BRfatal("No cover asserted in book");
+ }
+ $imageIndex = $metadata['coverIndices'][0]; // $$$ TODO add support for other covers
+ break;
+ */
+
+ case 'preview':
+ case 'cover': // Show our best guess if cover is requested
+ // Preference is:
+ // Cover page if book was published >= 1950
+ // Title page
+ // Cover page
+ // Page 0
+
+ if ( array_key_exists('date', $metadata) && array_key_exists('coverIndices', $metadata) ) {
+ if ($brm->parseYear($metadata['date']) >= 1950) {
+ $imageIndex = $metadata['coverIndices'][0];
+ break;
+ }
+ }
+ if (array_key_exists('titleIndex', $metadata)) {
+ $imageIndex = $metadata['titleIndex'];
+ break;
+ }
+ if (array_key_exists('coverIndices', $metadata)) {
+ $imageIndex = $metadata['coverIndices'][0];
+ break;
+ }
+
+ // First page
+ $imageIndex = 0;
+ break;
+
+ case 'n':
+ // Accessible index page
+ $imageIndex = intval($pageInfo['value']);
+ break;
+
+ case 'page':
+ // Named page
+ $index = array_search($pageInfo['value'], $metadata['pageNums']);
+ if ($index === FALSE) {
+ // Not found
+ $this->BRfatal("Page not found");
+ break;
+ }
+
+ $imageIndex = $index;
+ break;
+
+ case 'leaf':
+ // Leaf explicitly specified
+ $leaf = $pageInfo['value'];
+ break;
+
+ default:
+ // Shouldn't be possible
+ $this->BRfatal("Unrecognized page type requested");
+ break;
+
+ }
+
+ if (is_null($leaf)) {
+ // Leaf was not explicitly set -- look it up
+ $leaf = $brm->leafForIndex($imageIndex, $metadata['leafNums']);
+ }
+
+ $requestEnv = array(
+ 'zip' => $metadata['zip'],
+ 'file' => $brm->imageFilePath($leaf, $metadata['subPrefix'], $metadata['imageFormat']),
+ 'ext' => 'jpg', // XXX should pass through ext
+ );
+
+ // remove non-passthrough keys from pageInfo
+ unset($pageInfo['type']);
+ unset($pageInfo['value']);
+
+ // add pageinfo to request
+ $requestEnv = array_merge($pageInfo, $requestEnv);
+
+ // Return image data - will check privs
+ $this->serveRequest($requestEnv);
+
+ }
+
+ /*
+ * Returns a page image when all parameters such as the image stack location are
+ * passed in.
+ *