conditionalize TTS debugging on soundManager.debugMode == true
[bookreader.git] / BookReaderIA / inc / BookReader.inc
1 <?
2
3 /*
4  * 
5
6 /*
7  * Note: Edits to this file must pass through github.  To submit a patch to this
8  *       file please contact mang at archive dot org or http://github.com/mangtronix
9  *       Direct changes to this file may get clobbered when the code is synchronized
10  *       from github.
11  */
12
13 class BookReader
14 {
15
16   // Operators recognized in BookReader download URLs
17   public static $downloadOperators = array('page');
18
19   // Returns true if can display the book in item with a given prefix (typically the item identifier)
20   public static function canDisplay($item, $prefix, $checkOldScandata = false)
21   {
22     
23     // A "book" is an image stack and scandata.
24     // 1. Old items may have scandata.xml or scandata.zip and itemid_{imageformat}.{zip,tar}
25     // 2. Newer items may have multiple {arbitraryname}_scandata.xml and {arbitraryname}_{imageformat}.{zip,tar}
26         
27     $foundScandata = false;
28     $foundImageStack = false;
29     
30     $targetScandata = $prefix . "_scandata.xml";
31         
32     // $$$ TODO add support for jpg and tar stacks
33     // https://bugs.edge.launchpad.net/gnubook/+bug/323003
34     // https://bugs.edge.launchpad.net/gnubook/+bug/385397
35     $imageFormatRegex = '@' . preg_quote($prefix, '@') . '_(jp2|tif|jpg)\.(zip|tar)$@';
36     
37     $baseLength = strlen($item->metadataGrabber->mainDir . '/');
38     foreach ($item->getFiles() as $location => $fileInfo) {
39         $filename = substr($location, $baseLength);
40         
41         if ($checkOldScandata) {
42             if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
43                 $foundScandata = $filename;
44             }
45         }
46         
47         if ($filename == $targetScandata) {
48             $foundScandata = $filename;
49         }
50         
51         if (preg_match($imageFormatRegex, $filename)) {
52             $foundImageStack = $filename;
53         }
54     }
55     
56     if ($foundScandata && $foundImageStack) {
57         return true;
58     }
59     
60     return false;
61   }
62   
63   // Finds the prefix to use for the book given the part of the URL trailing after /stream/
64   public static function findPrefix($urlPortion)
65   {
66     if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
67         // URL portion was empty or started with /, &, or ? -- no item identifier
68         return false;
69     }
70     
71     $prefix = $matches[0]; // item identifier
72     
73     // $$$ Currently swallows the rest of the URL.
74     //     If we want to support e.g. /stream/itemid/subdir/prefix/page/23 will need to adjust.
75     if (preg_match('#[^/&?]+/([^&?]+)#', $urlPortion, $matches)) {
76         // Match is everything after item identifier and slash, up to end or ? or &
77         // e.g. itemid/{match/these/parts}?foo=bar
78         $prefix = $matches[1]; // sub prefix -- 
79     }
80     
81     return $prefix;
82   }
83
84   // $$$ would be cleaner to use different templates instead of the uiMode param
85   // 
86   // @param subprefix Optional prefix to display a book inside an item (e.g. if does not match identifier)
87   public static function draw($server, $mainDir, $identifier, $subPrefix, $title,
88                               $coverLeaf=null, $titleStart='Internet Archive', $uiMode='full')
89   {
90     // Set title to default if not set
91     if (!$title) {
92         $title = 'BookReader';
93     }
94     
95     $id = $identifier;
96     
97     // manually update with Launchpad version number at each checkin so that browsers
98     // do not use old cached version
99     // see https://bugs.launchpad.net/gnubook/+bug/330748
100     $version = "imageurls";
101     
102     if (BookReader::getDevHost($server)) {
103         // on dev host - add time to force reload
104         $version .= '_' . time();
105     }
106     
107     if ("" == $id) {
108         echo "No identifier specified!";
109         die(-1);
110     }
111     
112     $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
113     
114 ?>
115 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
116 <html>
117 <head>
118     <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
119     <meta name="apple-mobile-web-app-capable" content="yes" />
120     <title><? echo $title; ?></title>
121 <!--[if lte IE 6]>
122     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
123 <![endif]-->
124     <link rel="stylesheet" type="text/css" href="/bookreader/BookReader.css?v=<? echo($version); ?>">
125 <? if ($uiMode == "embed") { ?>
126     <link rel="stylesheet" type="text/css" href="/bookreader/BookReaderEmbed.css?v=<? echo($version); ?>">
127 <? } elseif ($uiMode == "touch") { ?>
128     <link rel="stylesheet" type="text/css" href="/bookreader/touch/BookReaderTouch.css?v=<? echo($version); ?>">
129 <? } /* uiMode */ ?>
130     <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
131     <script type="text/javascript" src="/bookreader/jquery-ui-1.8.1.custom.min.js?v=<? echo($version); ?>"></script>
132     <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
133     <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
134     <script type="text/javascript" src="/bookreader/soundmanager/soundmanager2.js?v=<? echo($version); ?>"></script>
135     <script>
136         soundManager.debugMode = false;
137         soundManager.url = '/bookreader/soundmanager/swf/';       
138         soundManager.useHTML5Audio = true;
139         soundManager.flashVersion = 9; //flash 8 version of swf is buggy when calling play() on a sound that is still loading
140     </script>
141 </head>
142 <body style="background-color: #FFFFFF;">
143
144 <? if ($uiMode == 'full') { ?>
145 <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
146 <? } else { ?>
147 <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>
148 <? } /* uiMode*/ ?>
149
150 <script type="text/javascript">
151   // Set some config variables -- $$$ NB: Config object format has not been finalized
152   var brConfig = {};
153 <? if ($uiMode == 'embed') { ?>
154   brConfig["mode"] = 1;
155   brConfig["reduce"] = 8;
156   brConfig["ui"] = "embed";
157 <? } else { ?>
158   brConfig["mode"] = 2;
159 <? } ?>
160 </script>
161 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
162 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
163
164 <? if ($uiMode == 'full') { ?>
165 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
166     <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
167         <p style="display: inline">
168             <input id="BookReaderSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
169         </p>
170     </form>
171     <div id="BookReaderSearchResults">
172         Search results
173     </div>
174 </div>
175
176
177 <div id="BRfooter">
178     <div class="BRlogotype">
179         <a href="http://archive.org/" class="BRblack">Internet Archive</a>
180     </div>
181     <div class="BRnavlinks">
182         <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
183         <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
184         <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
185         <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
186         <a class="BRblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
187     </div>
188 </div>
189 <? } /* uiMode */ ?>
190
191 <script type="text/javascript">
192     // $$$ hack to workaround sizing bug when starting in two-up mode
193     $(document).ready(function() {
194         $(window).trigger('resize');
195     });
196 </script>
197   <?
198     exit;
199   }
200   
201   // Returns the user part of dev host from URL, or null
202   public static function getDevHost($server)
203   {
204       if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
205         return $match[1];
206       }
207       
208       return null;
209   }
210
211   
212   public static function serverBaseURL($server)
213   {
214       // Check if we're on a dev vhost and point to JSIA in the user's public_html
215       // on the datanode
216       // $$$ the remapping isn't totally automatic yet and requires user to
217       //     ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
218       //     so we enable it only for known hosts
219       $devhost = BookReader::getDevHost($server);
220       $devhosts = array('mang', 'testflip', 'rkumar', 'mccabe');
221       if (in_array($devhost, $devhosts)) {
222         $server = $server . "/~" . $devhost;
223       }
224       return $server;
225   }
226   
227   
228   public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
229   {
230     $serverBaseURL = BookReader::serverBaseURL($server);
231
232     $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
233     if ($subPrefix) {
234         $params['subPrefix'] = $subPrefix;
235     }
236     
237     $keys = array_keys($params);
238     $lastParam = end($keys);
239     $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
240     foreach($params as $param=>$value) {
241         $url .= $param . '=' . $value;
242         if ($param != $lastParam) {
243             $url .= '&';
244         }
245     }
246     
247     return $url;
248   }
249   
250   // Return the URL for the requested /download/$path, or null
251   public static function getURL($path, $item) {
252     // $path should look like {itemId}/{operator}/{filename}
253     // Other operators may be added
254     
255     $urlParts = BookReader::parsePath($path);
256     
257     // Check for non-handled cases
258     $required = array('identifier', 'operator', 'operand');
259     foreach ($required as $key) {
260         if (!array_key_exists($key, $urlParts)) {
261             return null;
262         }
263     }
264     
265     $identifier = $urlParts['identifier'];
266     $operator = $urlParts['operator'];
267     $filename = $urlParts['operand'];
268     $subPrefix = $urlParts['subPrefix'];
269     
270     $serverBaseURL = BookReader::serverBaseURL($item->getServer());
271     
272     // Baseline query params
273     $query = array(
274         'id' => $identifier,
275         'itemPath' => $item->getMainDir(),
276         'server' => $serverBaseURL
277     );
278     if ($subPrefix) {
279         $query['subPrefix'] = $subPrefix;
280     }
281     
282     switch ($operator) {
283         case 'page':
284             
285             // Look for old-style preview request - e.g. {identifier}_cover.jpg
286             if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) {
287                 // Serve preview image
288                 $page = $matches[2];
289                 $query['page'] = $page;
290                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
291             }
292             
293             // New-style preview request - e.g. cover_thumb.jpg
294             if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) {
295                 $query['page'] = $filename;
296                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
297             }
298             
299             // Asking for a non-preview page
300             $query['page'] = $filename;
301             return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
302         
303         default:
304             // Unknown operator
305             return null;            
306     }
307       
308     return null; // was not handled
309   }
310   
311   public static function browserFromUserAgent($userAgent) {
312       $browserPatterns = array(
313           'ipad' => '/iPad/',
314           'iphone' => '/iPhone/', // Also cover iPod Touch
315           'android' => '/Android/',
316       );
317       
318       foreach ($browserPatterns as $browser => $pattern) {
319           if (preg_match($pattern, $userAgent)) {
320               return $browser;
321           }
322       }
323       return null;
324   }
325
326   
327   // $$$ Ideally we will not rely on user agent, but for the moment we do
328   public static function paramsFromUserAgent($userAgent) {
329       // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
330       $browserParams = array(
331           'ipad' => array( 'ui' => 'touch' ),
332           'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
333           'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
334       );
335   
336       $browser = BookReader::browserFromUserAgent($userAgent);
337       if ($browser) {
338           return $browserParams[$browser];
339       }
340       return array();
341   }
342   
343   public static function parsePath($path) {
344     // Parse the BookReader path and return the parts
345     // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
346     //            'operator' => 'page', 'filename' => 'cover.jpg')
347     
348     $parts = array();
349     
350     // Pull off query, e.g. ?foo=bar
351     if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
352         $parts['query'] = $matches[2];
353         $path = $matches[1];
354     }
355     
356     // Pull off identifier
357     if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
358         // no match
359         return $parts;
360     }
361     $parts['identifier'] = $matches[0];
362     $path = substr($path, strlen($matches[0]));
363     
364     // Look for operators
365     // The sub-prefix can be arbitrary, so we match up until the first operator
366     $operators = '(' . join('|', self::$downloadOperators) . ')';
367     $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
368     if (preg_match($pattern, $path, $matches) === 1) {
369         $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
370         $parts['operator'] = $matches['operator'];
371         $parts['operand'] = $matches['operand'];
372     } else {
373         $parts['subPrefix'] = $path;
374     }
375     
376     return $parts;
377   }
378     
379 }
380
381 ?>