3 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
5 This file is part of BookReader.
7 BookReader is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Affero General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 BookReader is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Affero General Public License for more details.
17 You should have received a copy of the GNU Affero General Public License
18 along with BookReader. If not, see <http://www.gnu.org/licenses/>.
21 $id = $_REQUEST['id'];
22 $itemPath = $_REQUEST['itemPath'];
23 $subPrefix = $_REQUEST['subPrefix'];
24 $server = $_REQUEST['server'];
26 // Check if we're on a dev vhost and point to JSIA in the user's public_html on the datanode
27 // $$$ TODO consolidate this logic
28 if (strpos($_SERVER["REQUEST_URI"], "/~mang") === 0) { // Serving out of home dir
29 $server .= ':80/~mang';
30 } else if (strpos($_SERVER["REQUEST_URI"], "/~testflip") === 0) { // Serving out of home dir
31 $server .= ':80/~testflip';
35 $subItemPath = $itemPath . '/' . $subPrefix;
37 $subItemPath = $itemPath . '/' . $id;
41 BRFatal("No identifier specified!");
44 if ("" == $itemPath) {
45 BRFatal("No itemPath specified!");
49 BRFatal("No server specified!");
52 if (!preg_match("|^/[0-3]/items/{$id}$|", $itemPath)) {
56 // XXX check here that subitem is okay
58 $imageFormat = 'unknown';
59 $zipFile = "${subItemPath}_jp2.zip";
61 if (file_exists($zipFile)) {
64 $zipFile = "${subItemPath}_tif.zip";
65 if (file_exists($zipFile)) {
70 if ("unknown" == $imageFormat) {
71 BRfatal("Unknown image format");
74 $scanDataFile = "${subItemPath}_scandata.xml";
75 $scanDataZip = "$itemPath/scandata.zip";
76 if (file_exists($scanDataFile)) {
77 $scanData = simplexml_load_file($scanDataFile);
78 } else if (file_exists($scanDataZip)) {
79 $cmd = 'unzip -p ' . escapeshellarg($scanDataZip) . ' scandata.xml';
80 exec($cmd, $output, $retval);
81 if ($retval != 0) BRFatal("Could not unzip ScanData!");
83 $dump = join("\n", $output);
84 $scanData = simplexml_load_string($dump);
85 } else if (file_exists("$itemPath/scandata.xml")) {
86 // For e.g. Scribe v.0 books!
87 $scanData = simplexml_load_file("$itemPath/scandata.xml");
89 BRFatal("ScanData file not found!");
92 $metaDataFile = "$itemPath/{$id}_meta.xml";
93 if (!file_exists($metaDataFile)) {
94 BRFatal("MetaData file not found!");
98 $metaData = simplexml_load_file($metaDataFile);
100 //$firstLeaf = $scanData->pageData->page[0]['leafNum'];
103 br = new BookReader();
106 /* Output title leaf if marked */
108 foreach ($scanData->pageData->page as $page) {
109 if (("Title Page" == $page->pageType) || ("Title" == $page->pageType)) {
110 $titleLeaf = "{$page['leafNum']}";
115 if ('' != $titleLeaf) {
116 printf("br.titleLeaf = %d;\n", $titleLeaf);
120 br.getPageWidth = function(index) {
121 return this.pageW[index];
124 br.getPageHeight = function(index) {
125 return this.pageH[index];
128 // Returns true if page image is available rotated
129 br.canRotatePage = function(index) {
130 return 'jp2' == this.imageFormat; // Assume single format for now
133 // reduce defaults to 1 (no reduction)
134 // rotate defaults to 0 (no rotation)
135 br.getPageURI = function(index, reduce, rotate) {
139 if ('undefined' == typeof(reduce)) {
144 if ('undefined' == typeof(rotate)) {
150 var file = this._getPageFile(index);
152 // $$$ add more image stack formats here
154 var url = 'http://'+this.server+'/BookReader/BookReaderImages.php?zip='+this.zip+'&file='+file+'&scale='+_reduce+'&rotate='+_rotate;
156 if ('undefined' == typeof(reduce)) {
157 // reduce not passed in
158 var ratio = this.getPageHeight(index) / this.twoPage.height;
160 // $$$ we make an assumption here that the scales are available pow2 (like kakadu)
163 } else if (ratio < 4) {
165 } else if (ratio < 8) {
167 } else if (ratio < 16) {
169 } else if (ratio < 32) {
177 var url = 'http://'+this.server+'/BookReader/BookReaderImages.php?zip='+this.zip+'&file='+file+'&scale='+_reduce+'&rotate='+_rotate;
183 br._getPageFile = function(index) {
184 var leafStr = '0000';
185 var imgStr = this.leafMap[index].toString();
186 var re = new RegExp("0{"+imgStr.length+"}$");
188 var insideZipPrefix = this.subPrefix.match('[^/]+$');
189 var file = insideZipPrefix + '_' + this.imageFormat + '/' + insideZipPrefix + '_' + leafStr.replace(re, imgStr) + '.' + this.imageFormat;
194 br.getPageSide = function(index) {
195 //assume the book starts with a cover (right-hand leaf)
196 //we should really get handside from scandata.xml
198 <? // Use special function if we should infer the page sides based off the title page index
199 if (preg_match('/goog$/', $id) && ('' != $titleLeaf)) {
201 // assume page side based on title pagex
202 var titleIndex = br.leafNumToIndex(br.titleLeaf);
203 // assume title page is RHS
204 var delta = titleIndex - index;
205 if (0 == (delta & 0x1)) {
215 // $$$ we should get this from scandata instead of assuming the accessible
216 // leafs are contiguous
217 if ('rl' != this.pageProgression) {
218 // If pageProgression is not set RTL we assume it is LTR
219 if (0 == (index & 0x1)) {
220 // Even-numbered page
228 if (0 == (index & 0x1)) {
236 br.getPageNum = function(index) {
237 var pageNum = this.pageNums[index];
245 br.leafNumToIndex = function(leafNum) {
246 var index = jQuery.inArray(leafNum, this.leafMap);
254 // This function returns the left and right indices for the user-visible
255 // spread that contains the given index. The return values may be
256 // null if there is no facing page or the index is invalid.
257 br.getSpreadIndices = function(pindex) {
258 // $$$ we could make a separate function for the RTL case and
259 // only bind it if necessary instead of always checking
260 // $$$ we currently assume there are no gaps
262 var spreadIndices = [null, null];
263 if ('rl' == this.pageProgression) {
265 if (this.getPageSide(pindex) == 'R') {
266 spreadIndices[1] = pindex;
267 spreadIndices[0] = pindex + 1;
269 // Given index was LHS
270 spreadIndices[0] = pindex;
271 spreadIndices[1] = pindex - 1;
275 if (this.getPageSide(pindex) == 'L') {
276 spreadIndices[0] = pindex;
277 spreadIndices[1] = pindex + 1;
279 // Given index was RHS
280 spreadIndices[1] = pindex;
281 spreadIndices[0] = pindex - 1;
285 //console.log(" index %d mapped to spread %d,%d", pindex, spreadIndices[0], spreadIndices[1]);
287 return spreadIndices;
290 // Remove the page number assertions for all but the highest index page with
291 // a given assertion. Ensures there is only a single page "{pagenum}"
292 // e.g. the last page asserted as page 5 retains that assertion.
293 br.uniquifyPageNums = function() {
296 for (var i = br.pageNums.length - 1; i--; i >= 0) {
297 var pageNum = br.pageNums[i];
298 if ( !seen[pageNum] ) {
299 seen[pageNum] = true;
301 br.pageNums[i] = null;
307 br.cleanupMetadata = function() {
308 br.uniquifyPageNums();
313 // Returns a URL for an embedded version of the current book
314 br.getEmbedURL = function() {
315 // We could generate a URL hash fragment here but for now we just leave at defaults
316 var url = 'http://' + window.location.host + '/stream/'+this.bookId;
317 if (this.subPrefix != this.bookId) { // Only include if needed
318 url += '/' + this.subPrefix;
326 // Returns the embed code HTML fragment suitable for copy and paste
327 br.getEmbedCode = function() {
328 return "<iframe src='" + this.getEmbedURL() + "' width='480px' height='430px'></iframe>";
334 foreach ($scanData->pageData->page as $page) {
335 if (shouldAddPage($page)) {
336 if(0 != $i) echo ","; //stupid IE
337 echo "{$page->cropBox->w}";
348 foreach ($scanData->pageData->page as $page) {
349 if (shouldAddPage($page)) {
350 if(0 != $i) echo ","; //stupid IE
351 echo "{$page->cropBox->h}";
352 $totalHeight += intval($page->cropBox->h/4) + 10;
361 foreach ($scanData->pageData->page as $page) {
362 if (shouldAddPage($page)) {
363 if(0 != $i) echo ","; //stupid IE
364 echo "{$page['leafNum']}";
374 foreach ($scanData->pageData->page as $page) {
375 if (shouldAddPage($page)) {
376 if(0 != $i) echo ","; //stupid IE
377 if (array_key_exists('pageNumber', $page) && ('' != $page->pageNumber)) {
378 echo "'{$page->pageNumber}'";
389 br.numLeafs = br.pageW.length;
391 br.bookId = '<?echo $id;?>';
392 br.zip = '<?echo $zipFile;?>';
393 br.subPrefix = '<?echo $subPrefix;?>';
394 br.server = '<?echo $server;?>';
395 br.bookTitle= '<?echo preg_replace("/\'/", "\\'", $metaData->title);?>';
396 br.bookPath = '<?echo $subItemPath;?>';
397 br.bookUrl = '<?echo "http://www.archive.org/details/$id";?>';
398 br.imageFormat = '<?echo $imageFormat;?>';
402 # Load some values from meta.xml
403 if ('' != $metaData->{'page-progression'}) {
404 echo "br.pageProgression = '" . $metaData->{"page-progression"} . "';";
406 // Assume page progression is Left To Right
407 echo "br.pageProgression = 'lr';";
411 if ('bandersnatchhsye00scarrich' == $id) {
412 echo "br.mode = 2;\n";
413 echo "br.auto = true;\n";
418 // Check for config object
419 // $$$ change this to use the newer params object
420 if (typeof(brConfig) != 'undefined') {
421 if (typeof(brConfig["ui"]) != 'undefined') {
422 br.ui = brConfig["ui"];
425 if (brConfig['mode'] == 1) {
427 if (typeof(brConfig['reduce'] != 'undefined')) {
428 br.reduce = brConfig['reduce'];
430 } else if (brConfig['mode'] == 2) {
434 //$$$mang hack to override request for 2up for books with attribution page
435 // as first page until we can display that page in 2up
437 if (strrpos($id, $needle) === strlen($id)-strlen($needle)) {
438 print "// override for books with attribution page\n";
439 print "br.mode = 1;\n";
445 br.cleanupMetadata();
451 function BRFatal($string) {
452 echo "alert('$string')\n";
456 // Returns true if a page should be added based on it's information in
458 function shouldAddPage($page) {
459 // Return false only if the page is marked addToAccessFormats false.
460 // If there is no assertion we assume it should be added.
461 if (isset($page->addToAccessFormats)) {
462 if ("false" == strtolower(trim($page->addToAccessFormats))) {