Update background colour
[bookreader.git] / BookReaderIA / inc / BookReader.inc
index ad519e0..68eff3c 100644 (file)
@@ -1,8 +1,23 @@
 <?
 
+/*
+ * 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 +66,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;
@@ -73,7 +91,7 @@ class BookReader
   {
     // Set title to default if not set
     if (!$title) {
-        $title = 'Bookreader';
+        $title = 'BookReader';
     }
     
     $id = $identifier;
@@ -81,7 +99,12 @@ 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 = "r25";
+    $version = "r28";
+    
+    if (BookReader::getDevHost($server)) {
+        // on dev host - add time to force reload
+        $version .= '_' . time();
+    }
     
     if ("" == $id) {
         echo "No identifier specified!";
@@ -94,6 +117,8 @@ class BookReader
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
+    <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
+    <meta name="apple-mobile-web-app-capable" content="yes" />
     <title><? echo $title; ?></title>
 <!--[if lte IE 6]>
     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
@@ -101,18 +126,216 @@ class BookReader
     <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 */ ?>
-    <script src="/includes/jquery-1.3.2.min.js" type="text/javascript"></script>
-    <script type="text/javascript" src="/bookreader/jquery.easing.1.3.js"></script>
+    <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
+    <script type="text/javascript" src="/bookreader/jquery-ui-1.8.5.custom.min.js?v=<? echo($version); ?>"></script>
+    <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
+    <script type="text/javascript" src="/bookreader/jquery.colorbox-min.js"></script>
+     <!-- THIS ALLOWS BEAUTYTIPS TO WORK ON IE -->
+        <!--[if lt IE 9]>
+        <script type="text/javascript" src="excanvas.compiled.js"></script>
+        <![endif]-->
+    <script type="text/javascript" src="/bookreader/jquery.bt.min.js"></script>
     <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
 </head>
-<body style="background-color: #FFFFFF;">
+<body style="background-color: ##939598;">
 
-<? if ($uiMode == 'full') { ?>
-<div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
-<? } else { ?>
-<div id="BookReader" style="left:0; right:0; top:0; bottom:0; border:0">Internet Archive Bookreader <noscript>requires JavaScript to be enabled.</noscript></div>
-<? } /* uiMode*/ ?>
+<?
+/*
+// <? if ($uiMode == 'full') { ?>
+// <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
+// <? } else { ?>
+// <div id="BookReader" style="left:0; right:0; top:0; bottom:0; border:0">Internet Archive Bookreader <noscript>requires JavaScript to be enabled.</noscript></div>
+// <? } ?>
+*/
+?>
+
+<div id="BookReader">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
+
+<script type="text/javascript">
+// XXXmang
+$().ready(function(){
+    $('.chapter').bt({
+        contentSelector: '$(this).find(".title")',
+        trigger: 'hover',
+        closeWhenOthersOpen: true,
+        cssStyles: {
+            backgroundColor: '#000',
+            border: '2px solid #e2dcc5',
+            borderBottom: 'none',
+            padding: '5px 10px',
+            fontFamily: '"Arial", sans-serif',
+            fontSize: '11px',
+            fontWeight: '700',
+            color: '#fff',
+            whiteSpace: 'nowrap'
+        },
+        shrinkToFit: true,
+        width: '200px',
+        padding: 0,
+        spikeGirth: 0,
+        spikeLength: 0,
+        overlap: '16px',
+        overlay: false,
+        killTitle: true, 
+        textzIndex: 9999,
+        boxzIndex: 9998,
+        wrapperzIndex: 9997,
+        offsetParent: null,
+        positions: ['top'],
+        fill: 'black',
+        windowMargin: 10,
+        strokeWidth: 0,
+        cornerRadius: 0,
+        centerPointX: 0,
+        centerPointY: 0,
+        shadow: false
+    });
+    $('.search').bt({
+        contentSelector: '$(this).find(".query")',
+        trigger: 'click',
+        closeWhenOthersOpen: true,
+        cssStyles: {
+            padding: '10px 10px 15px',
+            backgroundColor: '#fff',
+            border: '3px solid #e2dcc5',
+            borderBottom: 'none',
+            fontFamily: '"Lucida Grande","Arial",sans-serif',
+            fontSize: '12px',
+            lineHeight: '18px',
+            color: '#615132'
+        },
+        shrinkToFit: false,
+        width: '230px',
+        padding: 0,
+        spikeGirth: 0,
+        spikeLength: 0,
+        overlap: '10px',
+        overlay: false,
+        killTitle: false, 
+        textzIndex: 9999,
+        boxzIndex: 9998,
+        wrapperzIndex: 9997,
+        offsetParent: null,
+        positions: ['top'],
+        fill: 'white',
+        windowMargin: 10,
+        strokeWidth: 3,
+        strokeStyle: '#e2dcc5',
+        cornerRadius: 0,
+        centerPointX: 0,
+        centerPointY: 0,
+        shadow: false
+    });
+    $('.searchChap').bt({
+        contentSelector: '$(this).find(".query")',
+        trigger: 'click',
+        closeWhenOthersOpen: true,
+        cssStyles: {
+            width: '250px',
+            padding: '10px 10px 15px',
+            backgroundColor: '#fff',
+            border: '3px solid #e2dcc5',
+            borderBottom: 'none',
+            fontFamily: '"Lucida Grande","Arial",sans-serif',
+            fontSize: '12px',
+            lineHeight: '18px',
+            color: '#615132'
+        },
+        shrinkToFit: false,
+        width: '230px',
+        padding: 0,
+        spikeGirth: 0,
+        spikeLength: 0,
+        overlap: '10px',
+        overlay: false,
+        killTitle: true, 
+        textzIndex: 9999,
+        boxzIndex: 9998,
+        wrapperzIndex: 9997,
+        offsetParent: null,
+        positions: ['top'],
+        fill: 'white',
+        windowMargin: 10,
+        strokeWidth: 3,
+        strokeStyle: '#e2dcc5',
+        cornerRadius: 0,
+        centerPointX: 0,
+        centerPointY: 0,
+        shadow: false
+    });
+    $('.chapter').each(function(){
+        $(this).hover(function(){
+            $(this).addClass('front');
+        },function(){
+            $(this).removeClass('front');
+        });
+    });
+    $('.search').each(function(){
+        $(this).hover(function(){
+            $(this).addClass('front');
+        },function(){
+            $(this).removeClass('front');
+        });
+    });
+    $('.searchChap').each(function(){
+        $(this).hover(function(){
+            $(this).addClass('front');
+        },function(){
+            $(this).removeClass('front');
+        });
+    });
+    $("#pager").draggable({axis:'x',containment:'parent'});
+});
+</script>
+<div id="BRnav">
+    <div id="BRnavpos">
+    <div id="pager"></div>
+        <div id="BRnavline"></div>
+<!-- LOAD SEARCH RESULTS FIRST SO CHAPTER INDICATORS CAN APPEAR IN FRONT -->
+        <div class="search" style="left:80%;" title="Search result">
+            <div class="query">The Kingdom of the Future, for instance, though interesting in a Caley Robinson way, with its cold, mystical colour relieved by touches of warm reddish browns, and its big draped figures, was a composition in the past, and did not stimulate the <strong><a href="">emotional</a></strong> powers of the observer with a suggestion of coming ages or a prophecy of progress. <span>Page 26</span></div>
+        </div>
+        
+        <div class="search" style="left:22%;" title="Search result">
+            <div class="query">A related distinction is between the emotion and the results of the emotion, principally behaviors and <strong><a href="">emotional</a></strong> expressions. People often behave in certain ways as a direct result of their <strong><a href="">emotional</a></strong> state, such as crying, fighting or fleeing. <span>Page 27</span></div>
+        </div>
+        
+        <div class="search" style="left:75%;" title="Search result">
+            <div class="query">A related distinction is between the emotion and the results of the emotion, principally behaviors and <strong><a href="">emotional</a></strong> expressions. People often behave in certain ways as a direct result of their <strong><a href="">emotional</a></strong> state, such as crying, fighting or fleeing. <span>Page 27</span></div>
+        </div>
+        
+        <div class="chapter" style="left:1%;">
+            <div class="title">I. The Minotaur <span>|</span> Page 1</div>
+        </div>
+        
+        <div class="chapter" style="left:17%;">
+            <div class="title">II. The Griffon <span>|</span> Page 44</div>
+        </div>
+        
+        <div class="chapter" style="left:30%;">
+            <div class="title">III. The Firedrake <span>|</span> Page 129</div>
+        </div>
+        
+        <div class="chapter" style="left:67.5%;">
+            <div class="title">V. The Pegasus <span>|</span> Page 201</div>
+        </div>
+        
+        <div class="chapter" style="left:90%;">
+            <div class="title">VI. The Goblin <span>|</span> Page 255</div>
+        </div>
+        
+        <div class="searchChap" style="left:49%;" title="Search result">
+            <div class="query">
+            A related distinction is between the emotion and the results of the emotion, principally behaviors and <strong><a href="">emotional</a></strong> expressions. People often behave in certain ways as a direct result of their <strong><a href="">emotional</a></strong> state, such as crying, fighting or fleeing. <span>Page 163</span>
+                <div class="queryChap">IV. The Witch <span>|</span> Page 163</div>
+            </div>
+        </div>
+        
+    </div>
+</div>
 
 <script type="text/javascript">
   // Set some config variables -- $$$ NB: Config object format has not been finalized
@@ -128,7 +351,7 @@ class BookReader
 <!-- 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') { ?>
+<? if ($uiMode == 'XXXmang was full') { ?>
 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
     <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
         <p style="display: inline">
@@ -160,26 +383,58 @@ class BookReader
     $(document).ready(function() {
         $(window).trigger('resize');
     });
+    
+    //XXXmang
+   function hideFace() {
+        $('#BookReader').die('mousemove').live('mousemove',function(event) {
+            var toolpos = $('#BRtoolbar').offset();
+            var tooltop = toolpos.top;
+            var navkey = $(document).height() - 75;
+            if ((event.pageY < 76) || (event.pageY > navkey)) {
+                if (tooltop == -60) {
+                    $('#BRtoolbar').animate({top:'0'});
+                    $('#BRnav').animate({bottom:'0'});
+                };
+            } else if ($('.bt-wrapper').size() == 0) {
+                if (tooltop == 0) {
+                    $('#BRtoolbar').animate({top:'-60'});
+                    $('#BRnav').animate({bottom:'-60'});
+                }
+            };
+        });
+    };
+    window.onload = function() {
+        window.setTimeout(hideFace, 3000);
+    };
 </script>
   <?
     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
-    if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
+      // 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($match[1], $devhosts)) {
-        $server = $server . "/~" . $match[1];
+      if (in_array($devhost, $devhosts)) {
+        $server = $server . "/~" . $devhost;
       }
-    }
-    return $server;
+      return $server;
   }
   
   
@@ -210,43 +465,130 @@ class BookReader
     // $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;            
     }
       
     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();
+  }
+  
+  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;
+  }
+    
 }
 
-?>
\ No newline at end of file
+?>