3 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
5 This file is part of GnuBook.
7 GnuBook 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 GnuBook 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 GnuBook. If not, see <http://www.gnu.org/licenses/>.
21 $id = $_REQUEST['id'];
22 $itemPath = $_REQUEST['itemPath'];
23 $server = $_REQUEST['server'];
26 GBFatal("No identifier specified!");
29 if ("" == $itemPath) {
30 GBFatal("No itemPath specified!");
34 GBFatal("No server specified!");
38 if (!preg_match("|^/[0-3]/items/{$id}$|", $itemPath)) {
42 $imageFormat = 'unknown';
43 $zipFile = "$itemPath/{$id}_jp2.zip";
44 if (file_exists($zipFile)) {
47 $zipFile = "$itemPath/${id}_tif.zip";
48 if (file_exists($zipFile)) {
53 if ("unknown" == $imageFormat) {
54 GBfatal("Unknown image format");
57 $scanDataFile = "$itemPath/{$id}_scandata.xml";
58 $scanDataZip = "$itemPath/scandata.zip";
59 if (file_exists($scanDataFile)) {
60 $scanData = simplexml_load_file($scanDataFile);
61 } else if (file_exists($scanDataZip)) {
62 $cmd = 'unzip -p ' . escapeshellarg($scanDataZip) . ' scandata.xml';
63 exec($cmd, $output, $retval);
64 if ($retval != 0) GBFatal("Could not unzip ScanData!");
66 $dump = join("\n", $output);
67 $scanData = simplexml_load_string($dump);
68 } else if (file_exists("$itemPath/scandata.xml")) {
69 // For e.g. Scribe v.0 books!
70 $scanData = simplexml_load_file("$itemPath/scandata.xml");
72 GBFatal("ScanData file not found!");
75 $metaDataFile = "$itemPath/{$id}_meta.xml";
76 if (!file_exists($metaDataFile)) {
77 GBFatal("MetaData file not found!");
81 $metaData = simplexml_load_file($metaDataFile);
83 //$firstLeaf = $scanData->pageData->page[0]['leafNum'];
88 gb.getPageWidth = function(index) {
89 //return parseInt(this.pageW[index]/this.reduce);
90 return this.pageW[index];
93 gb.getPageHeight = function(index) {
94 //return parseInt(this.pageH[index]/this.reduce);
95 return this.pageH[index];
98 gb.getPageURI = function(index) {
100 var imgStr = this.leafMap[index].toString();
101 var re = new RegExp("0{"+imgStr.length+"}$");
104 var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+this.bookId+'_'+this.imageFormat+'/'+this.bookId+'_'+leafStr.replace(re, imgStr) + '.'+this.imageFormat+'&scale='+this.reduce;
106 var ratio = this.getPageHeight(index) / this.twoPageH;
108 // $$$ we make an assumption here that the scales are available pow2 (like kakadu)
111 } else if (ratio <= 4) {
117 //var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+this.bookId+'_jp2/'+this.bookId+'_'+leafStr.replace(re, imgStr) + '.jp2&height='+this.twoPageH+'&origHeight='+this.getPageHeight(index);
118 var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+this.bookId+'_'+this.imageFormat+'/'+this.bookId+'_'+leafStr.replace(re, imgStr) + '.'+this.imageFormat+'&scale='+scale;
124 gb.getPageSide = function(index) {
125 //assume the book starts with a cover (right-hand leaf)
126 //we should really get handside from scandata.xml
128 // $$$ we should get this from scandata instead of assuming the accessible
129 // leafs are contiguous
130 if ('rl' != this.pageProgression) {
131 // If pageProgression is not set RTL we assume it is LTR
132 if (0 == (index & 0x1)) {
133 // Even-numbered page
141 if (0 == (index & 0x1)) {
149 gb.getPageNum = function(index) {
150 return this.pageNums[index];
153 gb.leafNumToIndex = function(leafNum) {
154 var index = jQuery.inArray(leafNum, this.leafMap);
162 // This function returns the left and right indices for the user-visible
163 // spread that contains the given index. The return values may be
164 // null if there is no facing page or the index is invalid.
165 gb.getSpreadIndices = function(pindex) {
166 // $$$ we could make a separate function for the RTL case and
167 // only bind it if necessary instead of always checking
168 // $$$ we currently assume there are no gaps
170 var spreadIndices = [null, null];
171 if ('rl' == this.pageProgression) {
173 if (this.getPageSide(pindex) == 'R') {
174 spreadIndices[1] = pindex;
175 spreadIndices[0] = pindex + 1;
177 // Given index was LHS
178 spreadIndices[0] = pindex;
179 spreadIndices[1] = pindex - 1;
183 if (this.getPageSide(pindex) == 'L') {
184 spreadIndices[0] = pindex;
185 spreadIndices[1] = pindex + 1;
187 // Given index was RHS
188 spreadIndices[1] = pindex;
189 spreadIndices[0] = pindex - 1;
193 //console.log(" index %d mapped to spread %d,%d", pindex, spreadIndices[0], spreadIndices[1]);
195 return spreadIndices;
201 foreach ($scanData->pageData->page as $page) {
202 if (shouldAddPage($page)) {
203 if(0 != $i) echo ","; //stupid IE
204 echo "{$page->cropBox->w}";
215 foreach ($scanData->pageData->page as $page) {
216 if (shouldAddPage($page)) {
217 if(0 != $i) echo ","; //stupid IE
218 echo "{$page->cropBox->h}";
219 $totalHeight += intval($page->cropBox->h/4) + 10;
228 foreach ($scanData->pageData->page as $page) {
229 if (shouldAddPage($page)) {
230 if(0 != $i) echo ","; //stupid IE
231 echo "{$page['leafNum']}";
241 foreach ($scanData->pageData->page as $page) {
242 if (shouldAddPage($page)) {
243 if(0 != $i) echo ","; //stupid IE
244 if (array_key_exists('pageNumber', $page) && ('' != $page->pageNumber)) {
245 echo "'{$page->pageNumber}'";
256 /* Output title leaf if marked */
258 foreach ($scanData->pageData->page as $page) {
259 if (("Title Page" == $page->pageType) || ("Title" == $page->pageType)) {
260 $titleLeaf = "{$page['leafNum']}";
265 if ('' != $titleLeaf) {
266 printf("gb.titleLeaf = %d;\n", $titleLeaf);
270 gb.numLeafs = gb.pageW.length;
272 gb.bookId = '<?echo $id;?>';
273 gb.zip = '<?echo $zipFile;?>';
274 gb.server = '<?echo $server;?>';
275 gb.bookTitle= '<?echo preg_replace("/\'/", "\\'", $metaData->title);?>';
276 gb.bookPath = '<?echo $itemPath;?>';
277 gb.bookUrl = '<?echo "http://www.archive.org/details/$id";?>';
278 gb.imageFormat = '<?echo $imageFormat;?>';
281 # Load some values from meta.xml
282 if ('' != $metaData->{'page-progression'}) {
283 echo "gb.pageProgression = '" . $metaData->{"page-progression"} . "';";
285 // Assume page progression is Left To Right
286 echo "gb.pageProgression = 'lr';";
290 if ('bandersnatchhsye00scarrich' == $id) {
291 echo "gb.mode = 2;\n";
292 echo "gb.auto = true;\n";
298 // Check for config object
299 if (typeof(gbConfig) != 'undefined') {
300 if (gbConfig['mode'] == 1) {
302 } else if (gbConfig['mode'] == 2) {
305 //$$$mang hack to override request for 2up for RTL until we have full RTL support
306 // we need a better way to determine the mode and pass config options
307 //if ((typeof(gb.pageProgression) != 'undefined') && (gb.pageProgression == 'rl')) {
319 function GBFatal($string) {
320 echo "alert('$string')\n";
324 // Returns true if a page should be added based on it's information in
326 function shouldAddPage($page) {
327 // Return false only if the page is marked addToAccessFormats false.
328 // If there is no assertion we assume it should be added.
329 if (isset($page->addToAccessFormats)) {
330 if ("false" == strtolower(trim($page->addToAccessFormats))) {