Move Archive-specific files into different directories.
[bookreader.git] / GnuBookIA / inc / GnuBook.inc
1 <?
2
3 class GnuBook
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)\.zip$@';
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   public static function draw($server, $mainDir, $identifier, $title,
51                               $coverleaf=null, $titlestart='Internet Archive')
52   {
53     // Set title to default if not set
54     if (!$title) {
55         $title = 'Bookreader';
56     }
57     
58     $id = $identifier;
59     
60     // manually update with Launchpad version number at each checkin so that browsers
61     // do not use old cached version
62     // see https://bugs.launchpad.net/gnubook/+bug/330748
63     $version = "0.9.7";
64     
65     if ("" == $id) {
66         echo "No identifier specified!";
67         die(-1);
68     }
69     
70     $metaURL = GnuBook::jsMetadataURL($server, $identifier, $mainDir);
71 ?>
72 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
73 <html>
74 <head>
75     <title><? echo $title; ?></title>
76 <!--[if lte IE 6]>
77     <meta http-equiv="refresh" content="2; URL=/bookreader/browserunsupported.php?id=<? echo($id); ?>">
78 <![endif]-->
79     <link rel="stylesheet" type="text/css" href="/bookreader/GnuBook.css?v=<? echo($version); ?>">    
80     <script src="http://www.archive.org/includes/jquery-1.2.6.min.js" type="text/javascript"></script>
81         <script type="text/javascript" src="/bookreader/GnuBook.js?v=<? echo($version); ?>"></script>
82     <script type="text/javascript" src="/bookreader/jquery.easing.1.3.js"></script>
83 </head>
84 <body style="background-color: #FFFFFF;">
85
86 <div id="GnuBook" style="left:10px; right:200px; top:10px; bottom:2em;">x</div>
87
88 <script type="text/javascript">
89   // Set some config variables -- $$$ NB: Config object format has not been finalized
90   var gbConfig = {};
91   gbConfig.mode = 2;
92 </script>
93 <script type="text/javascript" src="<? echo($metaURL); ?>"></script>
94
95 <div id="GnuBookSearch" style="width:190px; right:0px; top:10px; bottom:2em;">
96     <form action='javascript:' onsubmit="gb.search($('#GnuBookSearchBox').val());">
97         <p style="display: inline">
98             <input id="GnuBookSearchBox" type="text" size="20" value="search..." onfocus="if('search...'==this.value)this.value='';" /><input type="submit" value="go" />
99         </p>
100     </form>
101     <div id="GnuBookSearchResults">
102         Search results
103     </div>
104 </div>
105
106
107 <div id="GBfooter">
108     <div class="GBlogotype">
109         <a href="http://archive.org/" class="GBblack">Internet Archive</a>
110     </div>
111     <div class="GBnavlinks">
112         <!-- <a class="GBblack" href="http://openlibrary.org/dev/docs/bookreader">About the Bookreader</a> | -->
113         <a class="GBblack" href="https://bugs.launchpad.net/bookserver/+filebug-advanced">Report Errors</a> |
114         <a class="GBblack" href="http://www.archive.org/details/texts">Texts Collection</a> |
115         <a class="GBblack" href="http://www.archive.org/about/contact.php">Contact Us</a>
116     </div>
117 </div>
118
119 <script type="text/javascript">
120     // $$$ hack to workaround sizing bug when starting in two-up mode
121     $(document).ready(function() {
122         $(window).trigger('resize');
123     });
124 </script>
125   <?
126     exit;
127   }
128   
129   public static function jsMetadataURL($server, $identifier, $mainDir)
130   {
131     $serverBaseURL = $server;
132     
133     // Check if we're on a dev vhost and point to JSIA in the user's public_html on the datanode
134     if (preg_match("/^www-(\w+)/", $_SERVER["SERVER_NAME"], $match)) {
135       // $$$ the remapping isn't totally automatic yet and requires user to
136       //     ln -s ~/petabox/www/datanode/GnuBook ~/public_html/GnuBook
137       //     so we enable it only for known hosts
138       $devhosts = array('mang', 'testflip');
139       if (in_array($match[1], $devhosts)) {
140         $serverBaseURL = $serverBaseURL . ":81/~" . $match[1];
141       }
142     }
143
144     $url = "http://{$serverBaseURL}/GnuBook/GnuBookJSIA.php?id={$identifier}&itemPath={$mainDir}&server={$serverBaseURL}";
145     return $url;
146   }
147   
148 }
149   ?>