Release 25
[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 ("" == $id) {
87         echo "No identifier specified!";
88         die(-1);
89     }
90     
91     $metaURL = BookReader::jsMetadataURL($server, $identifier, $mainDir, $subPrefix);
92     
93 ?>
94 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
95 <html>
96 <head>
97     <title><? echo $title; ?></title>
98 <!--[if lte IE 6]>
99     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
100 <![endif]-->
101     <link rel="stylesheet" type="text/css" href="/bookreader/BookReader.css?v=<? echo($version); ?>">
102 <? if ($uiMode == "embed") { ?>
103     <link rel="stylesheet" type="text/css" href="/bookreader/BookReaderEmbed.css?v=<? echo($version); ?>">
104 <? } /* uiMode */ ?>
105     <script src="/includes/jquery-1.3.2.min.js" type="text/javascript"></script>
106     <script type="text/javascript" src="/bookreader/jquery.easing.1.3.js"></script>
107     <script type="text/javascript" src="/bookreader/BookReader.js?v=<? echo($version); ?>"></script>
108 </head>
109 <body style="background-color: #FFFFFF;">
110
111 <? if ($uiMode == 'full') { ?>
112 <div id="BookReader" style="left:10px; right:200px; top:10px; bottom:2em;">Internet Archive BookReader <noscript>requires JavaScript to be enabled.</noscript></div>
113 <? } else { ?>
114 <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>
115 <? } /* uiMode*/ ?>
116
117 <script type="text/javascript">
118   // Set some config variables -- $$$ NB: Config object format has not been finalized
119   var brConfig = {};
120 <? if ($uiMode == 'embed') { ?>
121   brConfig["mode"] = 1;
122   brConfig["reduce"] = 8;
123   brConfig["ui"] = "embed";
124 <? } else { ?>
125   brConfig["mode"] = 2;
126 <? } ?>
127 </script>
128 <!-- The script included below is dynamically generated JavaScript that includes the book metadata and page image access functions -->
129 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
130
131 <? if ($uiMode == 'full') { ?>
132 <div id="BookReaderSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
133     <form action='javascript:' onsubmit="br.search($('#BookReaderSearchBox').val());">
134         <p style="display: inline">
135             <input id="BookReaderSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
136         </p>
137     </form>
138     <div id="BookReaderSearchResults">
139         Search results
140     </div>
141 </div>
142
143
144 <div id="BRfooter">
145     <div class="BRlogotype">
146         <a href="http://archive.org/" class="BRblack">Internet Archive</a>
147     </div>
148     <div class="BRnavlinks">
149         <!-- <a class="BRblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
150         <a class="BRblack" href="http://www.archive.org/about/faqs.php#Report_Item">Content Problems</a> |
151         <a class="BRblack" href="https://bugs.launchpad.net/bookreader/+filebug">Report Bugs</a> |
152         <a class="BRblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
153         <a class="BRblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
154     </div>
155 </div>
156 <? } /* uiMode */ ?>
157
158 <script type="text/javascript">
159     // $$$ hack to workaround sizing bug when starting in two-up mode
160     $(document).ready(function() {
161         $(window).trigger('resize');
162     });
163 </script>
164   <?
165     exit;
166   }
167
168   
169   public static function serverBaseURL($server)
170   {
171     // Check if we're on a dev vhost and point to JSIA in the user's public_html
172     // on the datanode
173     if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
174       // $$$ the remapping isn't totally automatic yet and requires user to
175       //     ln -s ~/petabox/www/datanode/BookReader ~/public_html/BookReader
176       //     so we enable it only for known hosts
177       $devhosts = array('mang', 'testflip', 'rkumar');
178       if (in_array($match[1], $devhosts)) {
179         $server = $server . "/~" . $match[1];
180       }
181     }
182     return $server;
183   }
184   
185   
186   public static function jsMetadataURL($server, $identifier, $mainDir, $subPrefix = '')
187   {
188     $serverBaseURL = BookReader::serverBaseURL($server);
189
190     $params = array( 'id' => $identifier, 'itemPath' => $mainDir, 'server' => $server );
191     if ($subPrefix) {
192         $params['subPrefix'] = $subPrefix;
193     }
194     
195     $keys = array_keys($params);
196     $lastParam = end($keys);
197     $url = "http://{$serverBaseURL}/BookReader/BookReaderJSIA.php?";
198     foreach($params as $param=>$value) {
199         $url .= $param . '=' . $value;
200         if ($param != $lastParam) {
201             $url .= '&';
202         }
203     }
204     
205     return $url;
206   }
207   
208   // Return the URL for the requested /download/$path, or null
209   public static function getURL($path, $item) {
210     // $path should look like {itemId}/{operator}/{filename}
211     // Other operators may be added
212     
213     $parts = preg_split('#/#', $path, 3);
214     if (count($parts) != 3) {
215         return null;
216     }
217     $identifier = $parts[0];
218     $operator = $parts[1];
219     $filename = $parts[2];
220     
221     $serverBaseURL = BookReader::serverBaseURL($item->getServer());
222             
223     switch ($operator) {
224         case 'page':
225             // Find bookId and which page was requested
226             $pathParts = pathinfo($filename);
227             
228             // Look for preview request
229             if (preg_match('/^(.*)_(cover|title|preview)$/', $pathParts['filename'], $matches) === 0) {
230                 return null;
231             }
232             $bookId = $matches[1];
233             $page = $matches[2];
234             $query = array(
235                 'id' => $identifier,
236                 'bookId' => $bookId,
237                 'itemPath' => $item->getMainDir(),
238                 'server' => $serverBaseURL,
239                 'page' => $page,
240             );
241             return 'http://' . $serverBaseURL . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
242         
243         default:
244             return null;            
245     }
246       
247     return null; // was not handled
248   }
249   
250 }
251
252 ?>