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