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