Fix for images being returned overly large
[bookreader.git] / BookReaderIA / inc / BookReader.inc
index 426b1ce..44d4057 100644 (file)
@@ -1,8 +1,24 @@
 <?
 
+/*
+ * Copyright(c) 2008-2010 Internet Archive. Software license AGPL version 3.
+ *
+ * This file is part of BookReader.  The full source code can be found at GitHub:
+ * http://github.com/openlibrary/bookreader
+ *
+ * Note: Edits to this file must pass through github.  To submit a patch to this
+ *       file please contact mang via http://github.com/mangtronix or mang at archive dot org
+ *       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)
   {
@@ -51,15 +67,18 @@ class BookReader
   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;
@@ -69,7 +88,7 @@ class BookReader
   // 
   // @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')
+                              $coverLeaf=null, $titleStart='Internet Archive', $uiMode='full', $protected = false, $isAdmin=false)
   {
     // Set title to default if not set
     if (!$title) {
@@ -81,10 +100,11 @@ class BookReader
     // 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 = "r26";
+    $version = "3.0.6";
     
     if (BookReader::getDevHost($server)) {
-        // on dev host - add time to force reload
+        // On dev host - add time to force reload
+        // If debugging on IE, remove this line otherwise breakpoints will be invalid after reload
         $version .= '_' . time();
     }
     
@@ -94,6 +114,11 @@ class BookReader
     }
     
     $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
+    $metaURL .= "&version=" . $version;
+    $locateURL = BookReader::jsLocateURL($identifier, $subPrefix);
+    $coverThumb = 'http://www.archive.org/download/' . $identifier . '/'. $subPrefix . '/page/cover_w114.jpg';
+    // startup-up-image must be exactly 320x460
+    //$startupImage = 'http://www.archive.org/download/' . $identifier . '/'. $subPrefix . '/page/cover_w512.jpg';
     
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
@@ -101,20 +126,144 @@ class BookReader
 <head>
     <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
     <meta name="apple-mobile-web-app-capable" content="yes" />
+    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
+    <link rel="apple-touch-icon" href="<? echo($coverThumb); ?>" />
     <title><? echo $title; ?></title>
-<!--[if lte IE 6]>
+    
+    <!--[if lte IE 6]>
     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
-<![endif]-->
+    <![endif]-->
+
+    <!-- THIS ALLOWS BEAUTYTIPS TO WORK ON IE -->
+    <!--[if lt IE 9]>
+    <script type="text/javascript" src="/includes/excanvas.compiled.js"></script>
+    <![endif]-->
+
+<!--[if !IE 7]><![IGNORE[--><![IGNORE[]]>
     <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 */ ?>
+<? if ($protected) { ?>
+    <link rel="stylesheet" type="text/css" href="/bookreader/BookReaderLending.css?v=<? echo($version); ?>">
+<? } ?>
     <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/jquery-ui-1.8.5.custom.min.js?v=<? echo($version); ?>"></script>
+    <script type="text/javascript" src="http://www.archive.org/includes/analytics.js?v=2"></script>
     <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
+    <script type="text/javascript" src="/bookreader/jquery.colorbox-min.js"></script>
+    <script type="text/javascript" src="/bookreader/jquery.ui.ipad.js"></script>
+
+    <script type="text/javascript" src="/bookreader/jquery.bt.min.js"></script>
     <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
+<? if ( !preg_match("/mobile/i", $_SERVER['HTTP_USER_AGENT']) ) { ?>
+    <script type="text/javascript" src="/bookreader/soundmanager/soundmanager2-ia.js?v=<? echo($version); ?>"></script>
+    <script>
+        soundManager.debugMode = false;
+        soundManager.url = '/bookreader/soundmanager/swf/';       
+        soundManager.useHTML5Audio = true;
+        soundManager.flashVersion = 9; //flash 8 version of swf is buggy when calling play() on a sound that is still loading
+    </script>
+<? } /* mobile user agent */ ?>
+</head>
+<body style="background-color: ##939598;">
+
+<div id="BookReader">
+    Internet Archive BookReader - <? echo $title; ?>
+    <br/>
+    
+    <noscript>
+    <p>
+        The BookReader requires JavaScript to be enabled. Please check that your browser supports JavaScript and that it is enabled in the browser settings.  You can also try one of the <a href="http://www.archive.org/details/<? echo $identifier; ?>"> other formats of the book</a>.
+    </p>
+    </noscript>
+</div>
+
+<script type="text/javascript">
+  // Set some config variables -- $$$ NB: Config object format has not been finalized
+  var brConfig = {};
+<? if ($uiMode == 'embed') { ?>
+  brConfig["mode"] = 1;
+  brConfig["ui"] = "embed";
+<? } else { ?>
+  brConfig["mode"] = 2;
+<? } ?>
+<? if ($isAdmin == true) {
+     echo '  brConfig["isAdmin"] = true;';
+   } ?>   
+</script>
+<script type="text/javascript">
+// The URL in the script tag below is dynamically generated JavaScript that includes the book metadata and page image access functions.
+// The ia{number}.us.archive.org server for the book can and does change, so this URL should NOT be used for permanent access.
+// Use the JSLocate URL below instead for stable access - it will find the item and redirect to the correct server
+// <? echo($locateURL); ?>
+
+</script>
+<script type="text/javascript" src="<? echo($metaURL); ?>"></script>
+
+<script type="text/javascript">
+    // Usage stats
+    if(window.archive_analytics) { window.archive_analytics.values['bookreader'] = 'open'};
+</script>
+<!--<![endif]-->
+
+<!--[if IE 7]>
+<? BookReader::emitForIE7($server, $mainDir, $identifier, $subPrefix, $title, $coverLeaf, $titleStart, $uiMode); ?>
+<![endif]-->
+
+
+</body>
+</html>
+  <?
+    exit;
+  }
+  
+  
+
+  
+  // Emit the HTML for the version of the BookReader for IE7
+  public static function emitForIE7($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 = "ie7";
+    
+    if (BookReader::getDevHost($server)) {
+        // on dev host - add time to force reload
+        $version .= '_' . time();
+    }
+    
+    if ("" == $id) {
+        echo "No identifier specified!";
+        die(-1);
+    }
+    
+    $metaURL = BookReader::jsMetadataURLForIE7($server, $identifier, $mainDir, $subPrefix);
+    
+    
+?>
+    <link rel="stylesheet" type="text/css" href="/bookreader/ie7/BookReader.css?v=<? echo($version); ?>">
+<? if ($uiMode == "embed") { ?>
+    <link rel="stylesheet" type="text/css" href="/bookreader/ie7/BookReaderEmbed.css?v=<? echo($version); ?>">
+<? } elseif ($uiMode == "touch") { ?>
+    <link rel="stylesheet" type="text/css" href="/bookreader/ie7/touch/BookReaderTouch.css?v=<? echo($version); ?>">
+<? } /* uiMode */ ?>
+    <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
+    <script type="text/javascript" src="http://www.archive.org/includes/analytics.js?v=2"></script>
+    <script type="text/javascript" src="/bookreader/ie7/jquery-ui-1.8.1.custom.min.js?v=<? echo($version); ?>"></script>
+    <script type="text/javascript" src="/bookreader/ie7/dragscrollable.js?v=<? echo($version); ?>"></script>
+    <script type="text/javascript" src="/bookreader/ie7/BookReader.js?v=<? echo($version); ?>"></script>
 </head>
 <body style="background-color: #FFFFFF;">
 
@@ -125,7 +274,7 @@ class BookReader
 <? } /* uiMode*/ ?>
 
 <script type="text/javascript">
-  // Set some config variables -- $$$ NB: Config object format has not been finalized
+  // Set some config variables - $$$ NB: Config object format has not been finalized
   var brConfig = {};
 <? if ($uiMode == 'embed') { ?>
   brConfig["mode"] = 1;
@@ -135,7 +284,6 @@ class BookReader
   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') { ?>
@@ -156,7 +304,6 @@ class BookReader
         <a href="http://archive.org/" class="BRblack">Internet Archive</a>
     </div>
     <div class="BRnavlinks">
-        <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
         <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
         <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
         <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
@@ -170,11 +317,14 @@ class BookReader
     $(document).ready(function() {
         $(window).trigger('resize');
     });
+    
+    // Usage stats
+    if(window.archive_analytics) { window.archive_analytics.values['bookreader'] = 'open'};
 </script>
   <?
-    exit;
   }
   
+  
   // Returns the user part of dev host from URL, or null
   public static function getDevHost($server)
   {
@@ -224,42 +374,95 @@ class BookReader
     return $url;
   }
   
+    public static function jsMetadataURLForIE7($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/ie7/BookReaderJSIA.php?";
+    foreach($params as $param=>$value) {
+        $url .= $param . '=' . $value;
+        if ($param != $lastParam) {
+            $url .= '&';
+        }
+    }
+    
+    return $url;
+  }
+
+  
+  // This returns a URL that finds the item then returns a redirect to BookReaderJSIA.php
+  // on the item's server.
+  public static function jsLocateURL($identifier, $subPrefix = '')
+  {
+    $locateURL = 'http://www.archive.org/bookreader/BookReaderJSLocate.php?id=' . $identifier;
+    if ($subPrefix) {
+        $locateURL .= '&subPrefix=' . $subPrefix;
+    }
+    return $locateURL;
+  }
+  
   // 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;
+    $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 = $parts[0];
-    $operator = $parts[1];
-    $filename = $parts[2];
+    
+    $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':
-            // 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;
+            // 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, '', '&');
             }
-            $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, '', '&');
+            
+            // Asking for a non-preview page
+            $query['page'] = $filename;
+            return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
         
         default:
+            // Unknown operator
             return null;            
     }
       
@@ -297,8 +500,43 @@ class BookReader
       }
       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;
+  }
     
 }
 
 ?>
-