Add missing ,
[bookreader.git] / BookReaderIA / inc / BookReader.inc
1 <?
2
3 /*
4  * Copyright(c) 2008-2010 Internet Archive. Software license AGPL version 3.
5  *
6  * This file is part of BookReader.  The full source code can be found at GitHub:
7  * http://github.com/openlibrary/bookreader
8  *
9  * Note: Edits to this file must pass through github.  To submit a patch to this
10  *       file please contact mang via http://github.com/mangtronix or mang at archive dot org
11  *       Direct changes to this file may get clobbered when the code is synchronized
12  *       from github.
13  */
14
15 class BookReader
16 {
17
18   // Operators recognized in BookReader download URLs
19   public static $downloadOperators = array('page');
20
21   // Returns true if can display the book in item with a given prefix (typically the item identifier)
22   public static function canDisplay($item, $prefix, $checkOldScandata = false)
23   {
24     
25     // A "book" is an image stack and scandata.
26     // 1. Old items may have scandata.xml or scandata.zip and itemid_{imageformat}.{zip,tar}
27     // 2. Newer items may have multiple {arbitraryname}_scandata.xml and {arbitraryname}_{imageformat}.{zip,tar}
28         
29     $foundScandata = false;
30     $foundImageStack = false;
31     
32     $targetScandata = $prefix . "_scandata.xml";
33         
34     // $$$ TODO add support for jpg and tar stacks
35     // https://bugs.edge.launchpad.net/gnubook/+bug/323003
36     // https://bugs.edge.launchpad.net/gnubook/+bug/385397
37     $imageFormatRegex = '@' . preg_quote($prefix, '@') . '_(jp2|tif|jpg)\.(zip|tar)$@';
38     
39     $baseLength = strlen($item->metadataGrabber->mainDir . '/');
40     foreach ($item->getFiles() as $location => $fileInfo) {
41         $filename = substr($location, $baseLength);
42         
43         if ($checkOldScandata) {
44             if ($filename == 'scandata.xml' || $filename == 'scandata.zip') {
45                 $foundScandata = $filename;
46             }
47         }
48         
49         if ($filename == $targetScandata) {
50             $foundScandata = $filename;
51         }
52         
53         if (preg_match($imageFormatRegex, $filename)) {
54             $foundImageStack = $filename;
55         }
56     }
57     
58     if ($foundScandata && $foundImageStack) {
59         return true;
60     }
61     
62     return false;
63   }
64   
65   // Finds the prefix to use for the book given the part of the URL trailing after /stream/
66   public static function findPrefix($urlPortion)
67   {
68     if (!preg_match('#[^/&?]+#', $urlPortion, $matches)) {
69         // URL portion was empty or started with /, &, or ? -- no item identifier
70         return false;
71     }
72     
73     $prefix = $matches[0]; // item identifier
74     
75     // $$$ Currently swallows the rest of the URL.
76     //     If we want to support e.g. /stream/itemid/subdir/prefix/page/23 will need to adjust.
77     if (preg_match('#[^/&?]+/([^&?]+)#', $urlPortion, $matches)) {
78         // Match is everything after item identifier and slash, up to end or ? or &
79         // e.g. itemid/{match/these/parts}?foo=bar
80         $prefix = $matches[1]; // sub prefix -- 
81     }
82     
83     return $prefix;
84   }
85
86   // $$$ would be cleaner to use different templates instead of the uiMode param
87   // 
88   // @param subprefix Optional prefix to display a book inside an item (e.g. if does not match identifier)
89   public static function draw($server, $mainDir, $identifier, $subPrefix, $title,
90                               $coverLeaf=null, $titleStart='Internet Archive', $uiMode='full')
91   {
92     // Set title to default if not set
93     if (!$title) {
94         $title = 'BookReader';
95     }
96     
97     $id = $identifier;
98     
99     // manually update with Launchpad version number at each checkin so that browsers
100     // do not use old cached version
101     // see https://bugs.launchpad.net/gnubook/+bug/330748
102     $version = "r28";
103     
104     if (BookReader::getDevHost($server)) {
105         // on dev host - add time to force reload
106         $version .= '_' . time();
107     }
108     
109     if ("" == $id) {
110         echo "No identifier specified!";
111         die(-1);
112     }
113     
114     $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
115     
116 ?>
117 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
118 <html>
119 <head>
120     <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
121     <meta name="apple-mobile-web-app-capable" content="yes" />
122     <title><? echo $title; ?></title>
123 <!--[if lte IE 6]>
124     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
125 <![endif]-->
126     <link rel="stylesheet" type="text/css" href="/bookreader/BookReader.css?v=<? echo($version); ?>">
127 <? if ($uiMode == "embed") { ?>
128     <link rel="stylesheet" type="text/css" href="/bookreader/BookReaderEmbed.css?v=<? echo($version); ?>">
129 <? } elseif ($uiMode == "touch") { ?>
130     <link rel="stylesheet" type="text/css" href="/bookreader/touch/BookReaderTouch.css?v=<? echo($version); ?>">
131 <? } /* uiMode */ ?>
132     <script src="/includes/jquery-1.4.2.min.js" type="text/javascript"></script>
133     <script type="text/javascript" src="/bookreader/jquery-ui-1.8.5.custom.min.js?v=<? echo($version); ?>"></script>
134     <script type="text/javascript" src="/bookreader/dragscrollable.js?v=<? echo($version); ?>"></script>
135     <script type="text/javascript" src="/bookreader/jquery.colorbox-min.js"></script>
136      <!-- THIS ALLOWS BEAUTYTIPS TO WORK ON IE -->
137         <!--[if lt IE 9]>
138         <script type="text/javascript" src="excanvas.compiled.js"></script>
139         <![endif]-->
140     <script type="text/javascript" src="/bookreader/jquery.bt.min.js"></script>
141     <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
142 </head>
143 <body style="background-color: #FFFFFF;">
144
145 <?
146 /*
147 // <? if ($uiMode == 'full') { ?>
148 // <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
149 // <? } else { ?>
150 // <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>
151 // <? } ?>
152 */
153 ?>
154
155 <div id="BookReader">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
156
157 <script type="text/javascript">
158 // XXXmang
159 $().ready(function(){
160     $('.chapter').bt({
161         contentSelector: '$(this).find(".title")',
162         trigger: 'hover',
163         closeWhenOthersOpen: true,
164         cssStyles: {
165             backgroundColor: '#000',
166             border: '2px solid #e2dcc5',
167             borderBottom: 'none',
168             padding: '5px 10px',
169             fontFamily: '"Arial", sans-serif',
170             fontSize: '11px',
171             fontWeight: '700',
172             color: '#fff',
173             whiteSpace: 'nowrap'
174         },
175         shrinkToFit: true,
176         width: '200px',
177         padding: 0,
178         spikeGirth: 0,
179         spikeLength: 0,
180         overlap: '16px',
181         overlay: false,
182         killTitle: true, 
183         textzIndex: 9999,
184         boxzIndex: 9998,
185         wrapperzIndex: 9997,
186         offsetParent: null,
187         positions: ['top'],
188         fill: 'black',
189         windowMargin: 10,
190         strokeWidth: 0,
191         cornerRadius: 0,
192         centerPointX: 0,
193         centerPointY: 0,
194         shadow: false
195     });
196     $('.search').bt({
197         contentSelector: '$(this).find(".query")',
198         trigger: 'click',
199         closeWhenOthersOpen: true,
200         cssStyles: {
201             padding: '10px 10px 15px',
202             backgroundColor: '#fff',
203             border: '3px solid #e2dcc5',
204             borderBottom: 'none',
205             fontFamily: '"Lucida Grande","Arial",sans-serif',
206             fontSize: '12px',
207             lineHeight: '18px',
208             color: '#615132'
209         },
210         shrinkToFit: false,
211         width: '230px',
212         padding: 0,
213         spikeGirth: 0,
214         spikeLength: 0,
215         overlap: '10px',
216         overlay: false,
217         killTitle: false, 
218         textzIndex: 9999,
219         boxzIndex: 9998,
220         wrapperzIndex: 9997,
221         offsetParent: null,
222         positions: ['top'],
223         fill: 'white',
224         windowMargin: 10,
225         strokeWidth: 3,
226         strokeStyle: '#e2dcc5',
227         cornerRadius: 0,
228         centerPointX: 0,
229         centerPointY: 0,
230         shadow: false
231     });
232     $('.searchChap').bt({
233         contentSelector: '$(this).find(".query")',
234         trigger: 'click',
235         closeWhenOthersOpen: true,
236         cssStyles: {
237             width: '250px',
238             padding: '10px 10px 15px',
239             backgroundColor: '#fff',
240             border: '3px solid #e2dcc5',
241             borderBottom: 'none',
242             fontFamily: '"Lucida Grande","Arial",sans-serif',
243             fontSize: '12px',
244             lineHeight: '18px',
245             color: '#615132'
246         },
247         shrinkToFit: false,
248         width: '230px',
249         padding: 0,
250         spikeGirth: 0,
251         spikeLength: 0,
252         overlap: '10px',
253         overlay: false,
254         killTitle: true, 
255         textzIndex: 9999,
256         boxzIndex: 9998,
257         wrapperzIndex: 9997,
258         offsetParent: null,
259         positions: ['top'],
260         fill: 'white',
261         windowMargin: 10,
262         strokeWidth: 3,
263         strokeStyle: '#e2dcc5',
264         cornerRadius: 0,
265         centerPointX: 0,
266         centerPointY: 0,
267         shadow: false
268     });
269     $('.chapter').each(function(){
270         $(this).hover(function(){
271             $(this).addClass('front');
272         },function(){
273             $(this).removeClass('front');
274         });
275     });
276     $('.search').each(function(){
277         $(this).hover(function(){
278             $(this).addClass('front');
279         },function(){
280             $(this).removeClass('front');
281         });
282     });
283     $('.searchChap').each(function(){
284         $(this).hover(function(){
285             $(this).addClass('front');
286         },function(){
287             $(this).removeClass('front');
288         });
289     });
290     $("#pager").draggable({axis:'x',containment:'parent'});
291 });
292 </script>
293 <div id="BRnav">
294     <div id="BRnavpos">
295     <div id="pager"></div>
296         <div id="BRnavline"></div>
297 <!-- LOAD SEARCH RESULTS FIRST SO CHAPTER INDICATORS CAN APPEAR IN FRONT -->
298         <div class="search" style="left:80%;" title="Search result">
299             <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>
300         </div>
301         
302         <div class="search" style="left:22%;" title="Search result">
303             <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>
304         </div>
305         
306         <div class="search" style="left:75%;" title="Search result">
307             <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>
308         </div>
309         
310         <div class="chapter" style="left:1%;">
311             <div class="title">I. The Minotaur <span>|</span> Page 1</div>
312         </div>
313         
314         <div class="chapter" style="left:17%;">
315             <div class="title">II. The Griffon <span>|</span> Page 44</div>
316         </div>
317         
318         <div class="chapter" style="left:30%;">
319             <div class="title">III. The Firedrake <span>|</span> Page 129</div>
320         </div>
321         
322         <div class="chapter" style="left:67.5%;">
323             <div class="title">V. The Pegasus <span>|</span> Page 201</div>
324         </div>
325         
326         <div class="chapter" style="left:90%;">
327             <div class="title">VI. The Goblin <span>|</span> Page 255</div>
328         </div>
329         
330         <div class="searchChap" style="left:49%;" title="Search result">
331             <div class="query">
332             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>
333                 <div class="queryChap">IV. The Witch <span>|</span> Page 163</div>
334             </div>
335         </div>
336         
337     </div>
338 </div>
339
340 <script type="text/javascript">
341   // Set some config variables -- $$$ NB: Config object format has not been finalized
342   var brConfig = {};
343 <? if ($uiMode == 'embed') { ?>
344   brConfig["mode"] = 1;
345   brConfig["reduce"] = 8;
346   brConfig["ui"] = "embed";
347 <? } else { ?>
348   brConfig["mode"] = 2;
349 <? } ?>
350 </script>
351 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
352 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
353
354 <? if ($uiMode == 'XXXmang was full') { ?>
355 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
356     <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
357         <p style="display: inline">
358             <input id="BookReaderSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
359         </p>
360     </form>
361     <div id="BookReaderSearchResults">
362         Search results
363     </div>
364 </div>
365
366
367 <div id="BRfooter">
368     <div class="BRlogotype">
369         <a href="http://archive.org/" class="BRblack">Internet Archive</a>
370     </div>
371     <div class="BRnavlinks">
372         <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
373         <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
374         <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
375         <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
376         <a class="BRblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
377     </div>
378 </div>
379 <? } /* uiMode */ ?>
380
381 <script type="text/javascript">
382     // $$$ hack to workaround sizing bug when starting in two-up mode
383     $(document).ready(function() {
384         $(window).trigger('resize');
385     });
386     
387     //XXXmang
388    function hideFace() {
389         $('#BookReader').die('mousemove').live('mousemove',function(event) {
390             var toolpos = $('#BRtoolbar').offset();
391             var tooltop = toolpos.top;
392             var navkey = $(document).height() - 75;
393             if ((event.pageY < 76) || (event.pageY > navkey)) {
394                 if (tooltop == -60) {
395                     $('#BRtoolbar').animate({top:'0'});
396                     $('#BRnav').animate({bottom:'0'});
397                 };
398             } else if ($('.bt-wrapper').size() == 0) {
399                 if (tooltop == 0) {
400                     $('#BRtoolbar').animate({top:'-60'});
401                     $('#BRnav').animate({bottom:'-60'});
402                 }
403             };
404         });
405     };
406     window.onload = function() {
407         window.setTimeout(hideFace, 3000);
408     };
409 </script>
410   <?
411     exit;
412   }
413   
414   // Returns the user part of dev host from URL, or null
415   public static function getDevHost($server)
416   {
417       if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
418         return $match[1];
419       }
420       
421       return null;
422   }
423
424   
425   public static function serverBaseURL($server)
426   {
427       // Check if we're on a dev vhost and point to JSIA in the user's public_html
428       // on the datanode
429       // $$$ the remapping isn't totally automatic yet and requires user to
430       //     ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
431       //     so we enable it only for known hosts
432       $devhost = BookReader::getDevHost($server);
433       $devhosts = array('mang', 'testflip', 'rkumar');
434       if (in_array($devhost, $devhosts)) {
435         $server = $server . "/~" . $devhost;
436       }
437       return $server;
438   }
439   
440   
441   public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
442   {
443     $serverBaseURL = BookReader::serverBaseURL($server);
444
445     $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
446     if ($subPrefix) {
447         $params['subPrefix'] = $subPrefix;
448     }
449     
450     $keys = array_keys($params);
451     $lastParam = end($keys);
452     $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
453     foreach($params as $param=>$value) {
454         $url .= $param . '=' . $value;
455         if ($param != $lastParam) {
456             $url .= '&';
457         }
458     }
459     
460     return $url;
461   }
462   
463   // Return the URL for the requested /download/$path, or null
464   public static function getURL($path, $item) {
465     // $path should look like {itemId}/{operator}/{filename}
466     // Other operators may be added
467     
468     $urlParts = BookReader::parsePath($path);
469     
470     // Check for non-handled cases
471     $required = array('identifier', 'operator', 'operand');
472     foreach ($required as $key) {
473         if (!array_key_exists($key, $urlParts)) {
474             return null;
475         }
476     }
477     
478     $identifier = $urlParts['identifier'];
479     $operator = $urlParts['operator'];
480     $filename = $urlParts['operand'];
481     $subPrefix = $urlParts['subPrefix'];
482     
483     $serverBaseURL = BookReader::serverBaseURL($item->getServer());
484     
485     // Baseline query params
486     $query = array(
487         'id' => $identifier,
488         'itemPath' => $item->getMainDir(),
489         'server' => $serverBaseURL
490     );
491     if ($subPrefix) {
492         $query['subPrefix'] = $subPrefix;
493     }
494     
495     switch ($operator) {
496         case 'page':
497             
498             // Look for old-style preview request - e.g. {identifier}_cover.jpg
499             if (preg_match('/^(.*)_((cover|title|preview).*)/', $filename, $matches) === 1) {
500                 // Serve preview image
501                 $page = $matches[2];
502                 $query['page'] = $page;
503                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
504             }
505             
506             // New-style preview request - e.g. cover_thumb.jpg
507             if (preg_match('/^(cover|title|preview)/', $filename, $matches) === 1) {
508                 $query['page'] = $filename;
509                 return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
510             }
511             
512             // Asking for a non-preview page
513             $query['page'] = $filename;
514             return 'http://' . $serverBaseURL . '/BookReader/BookReaderImages.php?' . http_build_query($query, '', '&');
515         
516         default:
517             // Unknown operator
518             return null;            
519     }
520       
521     return null; // was not handled
522   }
523   
524   public static function browserFromUserAgent($userAgent) {
525       $browserPatterns = array(
526           'ipad' => '/iPad/',
527           'iphone' => '/iPhone/', // Also cover iPod Touch
528           'android' => '/Android/',
529       );
530       
531       foreach ($browserPatterns as $browser => $pattern) {
532           if (preg_match($pattern, $userAgent)) {
533               return $browser;
534           }
535       }
536       return null;
537   }
538
539   
540   // $$$ Ideally we will not rely on user agent, but for the moment we do
541   public static function paramsFromUserAgent($userAgent) {
542       // $$$ using 'embed' here for devices with assumed small screens -- really should just use CSS3 media queries
543       $browserParams = array(
544           'ipad' => array( 'ui' => 'touch' ),
545           'iphone' => array( 'ui' => 'embed', 'mode' => '1up' ),
546           'android' => array( 'ui' => 'embed', 'mode' => '1up' ),
547       );
548   
549       $browser = BookReader::browserFromUserAgent($userAgent);
550       if ($browser) {
551           return $browserParams[$browser];
552       }
553       return array();
554   }
555   
556   public static function parsePath($path) {
557     // Parse the BookReader path and return the parts
558     // e.g. itemid/some/sub/dir/page/cover.jpg -> array( 'identifier' => 'itemid', 'subPrefix' => 'some/sub/dir',
559     //            'operator' => 'page', 'filename' => 'cover.jpg')
560     
561     $parts = array();
562     
563     // Pull off query, e.g. ?foo=bar
564     if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
565         $parts['query'] = $matches[2];
566         $path = $matches[1];
567     }
568     
569     // Pull off identifier
570     if (preg_match('#[^/&?]+#', $path, $matches) === 0) {
571         // no match
572         return $parts;
573     }
574     $parts['identifier'] = $matches[0];
575     $path = substr($path, strlen($matches[0]));
576     
577     // Look for operators
578     // The sub-prefix can be arbitrary, so we match up until the first operator
579     $operators = '(' . join('|', self::$downloadOperators) . ')';
580     $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
581     if (preg_match($pattern, $path, $matches) === 1) {
582         $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
583         $parts['operator'] = $matches['operator'];
584         $parts['operand'] = $matches['operand'];
585     } else {
586         $parts['subPrefix'] = $path;
587     }
588     
589     return $parts;
590   }
591     
592 }
593
594 ?>