-
Notifications
You must be signed in to change notification settings - Fork 16
/
BookGluttonZipEpub.php
executable file
·276 lines (197 loc) · 5.99 KB
/
BookGluttonZipEpub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
require_once('./BookGluttonEpub.php');
class BookGluttonZipEpub extends BookGluttonEpub
{
public function __construct()
{
$this->apache_user = 'apache';
$this->logecho = false;
parent::__construct();
}
public function loadZip($file)
{
$this->zipfile = $file;
$this->_za = null;
$this->filelist = array();
$this->log("reading zip file ".$file);
$this->_openZip();
if(!is_resource($this->ziphandle)) {
$this->log('error '.$this->ziphandle);
throw new Exception('could not open zip data');
} else {
while($e = zip_read($this->ziphandle)) {
$this->filelist[] = array(
'name'=>zip_entry_name($e),
'size'=>zip_entry_filesize($e)
);
if(zip_entry_name($e)=='META-INF/container.xml') {
// stub TODO
}
}
}
$this->_closeZip();
$xml = $this->getFile('META-INF/container.xml');
$this->container = simplexml_load_string($xml);
if(!$this->container) {
throw new Exception('cannot find or parse container doc');
} else {
if(!is_object($this->container->rootfiles->rootfile)) {
throw new Exception('could not get rootfile element from container doc');
}
$atts = $this->container->rootfiles->rootfile->attributes();
$this->opfpath = $atts['full-path'];
$this->opfroot = pathinfo($this->opfpath, PATHINFO_DIRNAME);
error_log('opfroot is '.$this->opfroot);
$this->opfXML = $this->getFile($this->opfpath);
$this->opf = parent::makeOpfDoc($this->opfXML);
foreach($this->opf_manifestNode->getElementsByTagName('item') as $item) {
$type = $item->getAttribute("media-type");
if($type=="application/x-dtbncx+xml") {
$this->ncxpath = $this->getNcxPath($item->getAttribute("href"));
$this->ncxXML = $this->getFile($this->opfroot . '/' . $this->ncxpath);
if($this->ncxXML) {
$ncxfound = true;
break;
} else {
$this->log('ncx '.$path . '/' . $this->ncxpath.' not located');
}
}
}
$this->ncx = parent::makeNcxDoc($this->ncxXML);
return true;
}
}
public function getOpfXML()
{
return $this->opfXML;
}
public function ingestZipData($data, $opspath=null)
{
// used when pulling an epub from a zipped archive
// first writes it to disk, then ingests it whole
if($opspath) {
if(!is_dir($opspath)) {
throw new Exception('ops path passed is not a directory!');
}
}
$tmpfile = Util::getTempDir().'/epubimport'.time().'.epub';
if(file_put_contents($tmpfile, $data)) {
if($this->loadZip($tmpfile)) {
if($opspath) {
// TODO eliminate need for ZipArchive memory bloat. For now, it's a shortcut
// idea here is to dump to a path if needing OPS, if not,
// we should one day offer the option to store structure and/or zipdata
// in memory or on disk
if($puts = $this->writeFiles($opspath)) {
parent::loadOPS($opspath);
} else {
$this->log("no files were written.");
}
/*
$this->_za = new ZipArchive();
// TODO we dont have to store OPS in disk, could use memory
if ($this->_za->open($tmpfile) === TRUE) {
echo "opened\n";
if($this->_za->extractTo($opspath)) {
echo "extracted\n";
$this->_za->close();
echo "closed\n";
if(!file_exists($opspath.'/META-INF/container.xml')) {
echo "throwing up\n";
throw new Exception('no container found in package!');
} else {
echo file_get_contents($opspath.'/META-INF/container.xml');
}
echo "calling parent\n";
parent::loadOPS($opspath);
echo "loaded OPS\n";
} else {
echo "could not extract this bitch\n";
}
} else {
echo "throwing up again\n";
throw new Exception('could not open zip archive!');
}
*/
}
// TODO we dont have to get rid of the zip data
$data = null;
// TODO we dont have to get rid of the stored zip data either
unlink($tmpfile);
}
} else {
throw new Exception('EPUB file could not be stored locally for import');
}
}
public function writeFiles($path)
{
$this->_openZip();
$puts = 0;
while($e = zip_read($this->ziphandle)) {
if(zip_entry_open($this->ziphandle, $e)) {
//$this->log('opened entry');
$size = zip_entry_filesize($e);
while($data = zip_entry_read($e,$size)) {
$fullpath = $path .'/'. zip_entry_name($e);
$dirpath = $path .'/'. pathinfo(zip_entry_name($e), PATHINFO_DIRNAME);
mkdir($dirpath .'/', 03775, true);
//chmod($dirpath . '/', 03775);
//chown($dirpath, $this->apache_user);
//chgrp($dirpath, get_current_user());
if(file_put_contents($fullpath, $data)) {
//echo $path .'/'. zip_entry_name($e)." written\n";
$puts++;
} else {
$this->log($path .'/'. zip_entry_name($e) . "failed to write as user ".getmyuid().':'.get_current_user());
}
}
}
}
return $puts;
}
public function getFile($name)
{
$this->_openZip();
$contents = '';
while($e = zip_read($this->ziphandle)) {
if(zip_entry_name($e)==$name) {
if(zip_entry_open($this->ziphandle, $e)) {
$size = zip_entry_filesize($e);
while($data = zip_entry_read($e,$size)) {
$contents .= $data;
}
}
}
}
return $contents;
}
public function log($msg, $level=0)
{
if($this->logecho) {
echo $msg."\n";
} else {
error_log($msg);
}
}
private function _closeZip()
{
zip_close($this->ziphandle);
}
private function _openZip()
{
$this->ziphandle = zip_open($this->zipfile);
if(!$this->ziphandle) {
$this->log('could not open zipfile');
}
}
public function enableLogging()
{
$this->logecho = true;
$this->log('logging enabled');
}
public function __destruct()
{
$this->_closeZip();
}
}
?>