Change to class static variables
[bookreader.git] / BookReaderIA / datanode / BookReaderImages.inc.php
index b553f8f..9d2f45e 100644 (file)
@@ -24,9 +24,11 @@ the MIME type is "image/jpeg".
     along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+require_once("BookReaderMeta.inc.php");
+
 class BookReaderImages
 {
-    public $MIMES = array('gif' => 'image/gif',
+    public static $MIMES = array('gif' => 'image/gif',
                    'jp2' => 'image/jp2',
                    'jpg' => 'image/jpeg',
                    'jpeg' => 'image/jpeg',
@@ -34,7 +36,7 @@ class BookReaderImages
                    'tif' => 'image/tiff',
                    'tiff' => 'image/tiff');
                    
-    public $EXTENSIONS = array('gif' => 'gif',
+    public static $EXTENSIONS = array('gif' => 'gif',
                         'jp2' => 'jp2',
                         'jpeg' => 'jpeg',
                         'jpg' => 'jpeg',
@@ -43,13 +45,24 @@ class BookReaderImages
                         'tiff' => 'tiff');
     
     // Width when generating thumbnails
-    public $imageSizes = array(
+    public static $imageSizes = array(
         'thumb' => 100,
-        'small' => 240,
-        'medium' => 500,
-        'large' => 1024,
+        'small' => 256,
+        'medium' => 512,
+        'large' => 2048,
+    );
+
+    // Keys in the image permalink urls, e.g. http://www.archive.org/download/itemid/page/cover_{keyval}_{keyval}.jpg
+    public static $imageUrlKeys = array(
+        'r' => 'reduce',
+        's' => 'scale',
+        'region' => 'region',
+        'tile' => 'tile',
+        'w' => 'width',
+        'h' => 'height'
     );
     
+    
     // Paths to command-line tools
     var $exiftool = '/petabox/sw/books/exiftool/exiftool';
     var $kduExpand = '/petabox/sw/bin/kdu_expand';
@@ -79,15 +92,17 @@ class BookReaderImages
         // Index of image to return
         $imageIndex = null;
 
-        // XXX deal with subPrefix
-        $pageInfo = $this->parsePageRequest($page);
-
-        // Parse requested page for page type, size and format options
-        if (preg_match('#^([^_]+)#', $page, $matches) === 0) {
-            // Unrecognized page specifier
-            $this->BRfatal('Unrecognized page specifier');
+        // deal with subPrefix
+        if ($_REQUEST['subPrefix']) {
+            $parts = split('/', $_REQUEST['subPrefix']);
+            $bookId = $parts[count($parts) - 1 ];
+        } else {
+            $bookId = $_REQUEST['id'];
         }
-        $basePage = $matches[1];
+        
+        $pageInfo = $this->parsePageRequest($page, $bookId);
+
+        $basePage = $pageInfo['type'];
         
         switch ($basePage) {
             case 'title':
@@ -130,9 +145,26 @@ class BookReaderImages
                 $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;
+                
             default:
                 // Shouldn't be possible
-                $this->BRfatal("Couldn't find page");
+                $this->BRfatal("Unrecognized page type requested");
                 break;
                 
         }
@@ -144,6 +176,11 @@ class BookReaderImages
             'file' => $brm->imageFilePath($leaf, $metadata['subPrefix'], $metadata['imageFormat']),
             'ext' => 'jpg',
         );
+        
+        if ($pageInfo['reduce']) {
+            $requestEnv['reduce'] = $pageInfo['reduce'];
+        }
+        // $$$ handle scale, other sizes, rotation, etc
 
         // Return image data - will check privs        
         $this->serveRequest($requestEnv);
@@ -315,7 +352,7 @@ class BookReaderImages
         
         $filenameForClient = $this->filenameForClient($file, $ext);
         
-        $headers = array('Content-type: '. $MIMES[$ext], // XXX is nginx swallowing this?
+        $headers = array('Content-type: '. self::$MIMES[$ext],
                          'Cache-Control: max-age=15552000',
                          'Content-disposition: inline; filename=' . $filenameForClient);
                           
@@ -387,8 +424,8 @@ class BookReaderImages
     function imageExtensionToType($extension)
     {
         
-        if (array_key_exists($extension, $this->EXTENSIONS)) {
-            return $this->EXTENSIONS[$extension];
+        if (array_key_exists($extension, self::$EXTENSIONS)) {
+            return self::$EXTENSIONS[$extension];
         } else {
             $this->BRfatal('Unknown image extension');
         }            
@@ -692,8 +729,12 @@ class BookReaderImages
      */
     function parsePageRequest($pageRequest, $bookPrefix) {
     
+        // Will hold parsed results
         $pageInfo = array();
         
+        // Normalize
+        $pageRequest = strtolower($pageRequest);
+        
         // Pull off extension
         if (preg_match('#(.*)\.([^.]+)$#', $pageRequest, $matches) === 1) {
             $pageRequest = $matches[1];
@@ -750,19 +791,27 @@ class BookReaderImages
         
         // Look for other known parts
         foreach ($parts as $part) {
-            $start = substr($part, 0, 1);
+            if ( in_array($part, $imageSizes) ) {
+                $pageInfo['size'] = $part;
+                continue;
+            }
+        
+            // Key must be alpha, value must start with digit and contain digits, alpha, ',' or '.'
+            // Should prevent injection of strange values into the redirect to datanode
+            if ( preg_match('#^([a-z]+)(\d[a-z0-9,.]*)#', $part, $matches) === 0) {
+                // Not recognized
+                continue;
+            }
             
-            switch ($start) {
-                case 't':
-                    $pageInfo['size'] = $start;
-                    break;
-                case 'r':
-                    $pageInfo['reduce'] = substr($part, 0);
-                    break;
-                default:
-                    // Unrecognized... just let it pass
-                    break;
+            $key = $matches[1];
+            $value = $matches[2];
+            
+            if ( array_key_exists($key, self::$imageUrlKeys) ) {
+                $pageInfo[self::$imageUrlKeys[$key]] = $value;
+                continue;
             }
+            
+            // If we hit here, was unrecognized (no action)
         }
         
         return $pageInfo;