-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.php
More file actions
396 lines (365 loc) · 13.8 KB
/
handler.php
File metadata and controls
396 lines (365 loc) · 13.8 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php include_once("includes/connection.php"); ?>
<?php
// The following function is an error handler which is used
// to output an HTML error page if the file upload fails
function error($error, $seconds = 3)
{
echo $error;
} // end error handler
$max_file_size = $_POST['MAX_FILE_SIZE'];
$filename = 'file';
$basedir = dirname ( realpath ( __FILE__ )) ;
//$project_id
$dir_to_upload = $basedir."/ePub/";
if (!file_exists($dir_to_upload)) {
$result = mkdir($dir_to_upload);
}
$uploadsDirectory = $dir_to_upload;
// extract file type
$file_name = $_FILES[$filename]['name'];
$file_type = "";
$dot_pos = strripos($file_name, ".");
if ($dot_pos) {
$file_type = substr($file_name, $dot_pos);
}
$newFilePath = $uploadsDirectory.$file_name;
// Now let's deal with the upload
// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded',
2 => 'html form max file size exceeded',
3 => 'file upload was only partial',
4 => 'no file was attached');
// check the upload form was actually submitted else print the form
isset($_POST['submit'])
or error('the upload form is needed');
// check for PHP's built-in uploading errors
($_FILES[$filename]['error'] == 0)
or error($errors[$_FILES[$filename]['error']]);
// check that the file we are working on really was the subject of an HTTP upload
is_uploaded_file($_FILES[$filename]['tmp_name'])
or error('not an HTTP upload');
// check that the file size is under limit
($_FILES[$filename]['size']<=$max_file_size)
or error('file limit exceeded');
// now let's move the file to its final location and allocate the new filename to it
move_uploaded_file($_FILES[$filename]['tmp_name'], $newFilePath)
or error('receiving directory insuffiecient permission');
$bookFileName=$newFilePath;
// If you got this far, everything has worked and the file has been successfully saved.
// Do appropriate actions
// Process the uploaded file with the path in: $_FILES[$filename]['tmp_name']
// Setup variables
$basedir = dirname(realpath(__FILE__));
$basedir = $basedir."/epubs";
if(!file_exists($basedir)) mkdir($basedir);
$OPF = "";
$STATE = "";
$BOOK = array();
$BOOK['CHAPTERCOUNT']=0;
$bookID=0;
function createFile($base,$fileName){
$tmp = explode("/",$fileName);
$result = "";
if(count($tmp)>1){
for($i=1; $i<count($tmp); $i++){
$result.="/".$tmp[$i];
if($i!=count($tmp)-1 && !file_exists($base.$result)) mkdir($base.$result);
}
} else {
$result = "/".$fileName;
}
if($result!=""){
touch($base.$result);
}
return $result;
}
function containerStartElement($parser,$name,$attr){
global $OPF,$BOOK;
if(strcasecmp($name,"ROOTFILE")==0){
$OPF = $attr["FULL-PATH"];
if(strpos($OPF,"/")===FALSE) $BOOK['OPF'] = $OPF;
else $BOOK['OPF'] = substr($OPF,strpos($OPF,"/")+1);
}
}
function contentMetadataStart($parser,$name,$attr){
global $STATE,$BOOK;
//echo $name."<br />";
$lowerName = strtolower($name);
if($lowerName=="dc:title"){
$STATE = "TITLE";
} else if($lowerName=="dc:creator"){
if(strtolower($attr['OPF:ROLE'])=="aut"){
$STATE = "AUTHOR";
}
} else if($lowerName=="dc:identifier"){
$STATE = "IDENTIFIER";
} else if($lowerName=="dc:date"){
$STATE = "DATE";
} else if($lowerName=="dc:publisher"){
$STATE = "PUBLISHER";
} else if($lowerName=="dc:rights"){
$STATE = "RIGHTS";
} else if($lowerName=="dc:subject"){
$STATE = "SUBJECT";
} else if($lowerName=="dc:language"){
$STATE = "LANGUAGE";
}else {
}
}
/**
* Function to read the manifest and spine of OPF file
* The metadata is assumed to be read by contentMetadataStart
*/
function contentStart($parser,$name,$attr){
global $BOOK,$connection,$bookID;
$lowerName = strtolower($name);
if($lowerName=="item"){
$itemID = $attr['ID'];
$href = $attr['HREF'];
if((strtolower($itemID)=="cover")||(strtolower($itemID)=="cover-image")){
$BOOK['COVER'] = $href;
} else {
$BOOK['ITEM'][$itemID]['HREF'] = $href;
}
} else if($lowerName=="itemref") {
$itemID = $attr['IDREF'];
if(!isset($attr['LINEAR']) || strcasecmp($attr['LINEAR'],"yes")==0){
$BOOK['CHAPTER'][$BOOK['CHAPTERCOUNT']] = $BOOK['ITEM'][$itemID]['HREF'];
$query = "INSERT INTO Chapter (b_id,c_id,title,filename) VALUES ({$bookID},".count($BOOK['CHAPTER']).",'{$itemID}','{$BOOK['CHAPTER'][$BOOK['CHAPTERCOUNT']]}')";
$result = mysql_query($query,$connection);
if(!$result){
die("Query: {$query}<br />Error inserting chapter: ".mysql_error());
}
$BOOK['CHAPTERCOUNT']++;
}
} else {
}
}
/**
* Function to read the data of tags inside OPF file
* Including metadata, manifest, and spine
*/
function contentData($parser,$data){
global $STATE,$BOOK,$connection;
if($STATE=="TITLE"){
$BOOK['TITLE'] = trim($data);
} else if($STATE=="AUTHOR"){
$BOOK['AUTHOR'] = trim($data);
} else if($STATE=="IDENTIFIER"){
// Get the UUID with format ********-****-****-****-************ where * means any alphanumeric characters
preg_match('/[A-Za-z0-9]{8}(-[A-Za-z0-9]{4}){3}-[A-Za-z0-9]{12}/',$data,$matches);
if(count($matches)==0){
// Get the ISBN number with format 13 digits
preg_match('/[0-9]{13}/',$data,$matches);
if(count($matches)==0){
// No identification, use the encoded title as the identification
$BOOK['IDENTIFIER'] = "BS64:".base64_encode($BOOK['TITLE']);
} else {
$BOOK['IDENTIFIER'] = "ISBN:".$matches[0];
}
} else {
$BOOK['IDENTIFIER'] = "UUID:".$matches[0];
}
} else if($STATE=="DATE"){
$BOOK['DATE'] = trim($data);
} else if($STATE=="PUBLISHER"){
$BOOK['PUBLISHER'] = trim($data);
} else if($STATE=="LANGUAGE"){
$BOOK['LANGUAGE'] = trim($data);
if (($BOOK['LANGUAGE']=='zh-CN')||($BOOK['LANGUAGE']=='简体中文'))
$BOOK['LANGUAGE']='zh';
}else if($STATE=="RIGHTS"){
$BOOK['RIGHTS'] = trim($data);
} else if($STATE=="SUBJECT"){
$tagName = trim($data);
$BOOK['TAGS'][] = $tagName;
$tagName = mysql_prep($tagName);
$query = "SELECT * FROM Tag WHERE t_name='{$tagName}'";
$result = mysql_query($query,$connection);
if(!$result){
die("Query: {$query}<br />Error checking tag name: ".mysql_error());
}
if(mysql_num_rows($result)==0){
$query = "INSERT INTO Tag(t_name) VALUES ('{$tagName}')";
$result = mysql_query($query,$connection);
if(!$result){
die("Query: {$query}<br />Error inserting tag name: ".mysql_error());
}
}
} else {
}
}
function contentEnd($parser,$name){
global $STATE,$BOOK;
if($STATE=="IDENTIFIER" && (!isset($BOOK['IDENTIFIER']) || strlen($BOOK['IDENTIFIER'])==0)){
if(isset($BOOK['TITLE'])){
$BOOK['IDENTIFIER'] = "BS64:".base64_encode($BOOK['TITLE']);
} else {
$BOOK['IDENTIFIER'] = "BS64:";
}
}
$STATE = "";
}
function isImage($path){
if(preg_match("/(jpg|png|gif)$/",$path)){
return true;
} else return false;
}
// Function that does nothing
function nop(){}
?>
<html>
<head>
<title>ePub-Reader</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!--<script type="text/javascript" src="js/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>-->
</head>
<body>
<?php
////////////////////////////////////////////////////////////////////////
// Open and extract the epub file //
// Read the content file to determine whether the book already exists //
// If it doesn't exist, add it to the database //
////////////////////////////////////////////////////////////////////////
//$bookFileName = "ePub/poe-purloined-letter.epub";
//$bookFileName = "ePub/carroll-alice-in-wonderland-illustrations.epub";
//$bookFileName = "ePub/cooper-deerslayer.epub";
//$bookFileName = "ePub/ming.epub"; // Doesn't work for Chinese as for now
$zip = zip_open($bookFileName);
while($BOOK['TITLE']==""){ // The OPF file might be extracted first before the container.xml
while($zip_entry = zip_read($zip)){
////////////////////////////////
// Process the container file //
////////////////////////////////
$zip_entry_name = zip_entry_name($zip_entry);
if($zip_entry_name=="META-INF/container.xml"){
if(!zip_entry_open($zip,$zip_entry)) continue;
$content = "";
while($zip_content = zip_entry_read($zip_entry)){
$content.=$zip_content;
}
zip_entry_close($zip_entry);
$parser = xml_parser_create();
xml_set_element_handler($parser,containerStartElement,nop);
if(!xml_parse($parser,$content)){
die("Error on line " . xml_get_current_line_number($parser));
}
xml_parser_free($parser);
} else if($zip_entry_name==$OPF){
if(!zip_entry_open($zip,$zip_entry)) continue;
$content = "";
while($zip_content = zip_entry_read($zip_entry)){
$content.=$zip_content;
}
zip_entry_close($zip_entry);
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "utf-8");
xml_set_element_handler($parser,contentMetadataStart,contentEnd);
xml_set_character_data_handler($parser,contentData);
if(!xml_parse($parser,$content)){
die("Error on line " . xml_get_current_line_number($parser));
}
xml_parser_free($parser);
}
}
if($OPF==""){
// Invalid epub file
die("Invalid epub file, please check your file again<br />");
}
}
zip_close($zip);
$query = "SELECT title FROM Book WHERE UUID='".mysql_prep($BOOK['IDENTIFIER'])."'";
$result = mysql_query($query,$connection);
if(!$result){
die("Error checking book existance: ".mysql_error());
}
echo "The title of the book is: ".$BOOK['TITLE'].". Written by: ".$BOOK['AUTHOR']."<br />";
echo "Book identification number: ".$BOOK['IDENTIFIER']."<br />";
if(mysql_num_rows($result)==0){
echo "Inserting the book into the database<br />";
$query = "INSERT INTO Book(UUID,title,author,publisher,publish_date,content_path,rights,language,create_date) VALUES
('".mysql_prep($BOOK['IDENTIFIER'])."','".mysql_prep($BOOK['TITLE'])."','".mysql_prep($BOOK['AUTHOR'])."','".mysql_prep($BOOK['PUBLISHER'])."','".mysql_prep($BOOK['DATE'])."','".mysql_prep($BOOK['OPF'])."','".mysql_prep($BOOK['RIGHTS'])."','".mysql_prep($BOOK['LANGUAGE'])."','".date("Y-m-d H:i:s",time())."')";
$result = mysql_query($query,$connection);
if(!$result){
die("Error inserting new book: ".mysql_error());
}
$bookID = mysql_insert_id();
// Inserting book tags
if(count($BOOK['TAGS'])>0){
$query = "INSERT INTO Booktag (b_id,t_id) (SELECT '{$bookID}',t_id FROM Tag WHERE ";
for($i=0; $i<count($BOOK['TAGS']); $i++){
if($i>0) $query.=" OR";
$query.=" t_name='".mysql_prep($BOOK['TAGS'][$i])."'";
}
$query.=")";
$result = mysql_query($query,$connection);
if(!$result){
die("Query: {$query}<br />Error applying tags: ".mysql_error());
}
}
$upload_dir = $basedir."/".$bookID;
if(!file_exists($upload_dir)) mkdir($upload_dir);
$zip = zip_open($bookFileName);
while($zip_entry = zip_read($zip)){
/////////////////////////////////
// Printing file name and size //
/////////////////////////////////
echo ($zip_entry_name=zip_entry_name($zip_entry)).", Size: ";
echo zip_entry_filesize($zip_entry)."<br />";
//////////////////////////////////////////////////
// Open the file, put the content in a variable //
//////////////////////////////////////////////////
if(!zip_entry_open($zip,$zip_entry)) continue;
$content = "";
while($zip_content = zip_entry_read($zip_entry)){
$content.=$zip_content;
}
zip_entry_close($zip_entry);
//////////////////////////////////////
// Create the file or directory //
// Discards the top level directory //
//////////////////////////////////////
$fname = createFile($upload_dir,$zip_entry_name);
// If it's not empty and not a directory, put the contents
if($fname!="" && $fname[strlen($fname)-1]!="/"){
file_put_contents($upload_dir.$fname,$content);
}
////////////////////////////////
// Read the chapter directive //
////////////////////////////////
if($zip_entry_name==$OPF){
$parser = xml_parser_create();
xml_set_element_handler($parser,contentStart,nop);
if(!xml_parse($parser,$content)){
die("Error on line " . xml_get_current_line_number($parser));
}
xml_parser_free($parser);
// Update the total chapters
$query = "UPDATE Book SET totalChapter=".count($BOOK['CHAPTER'])." WHERE UUID='{$BOOK['IDENTIFIER']}'";
$result = mysql_query($query,$connection);
if(!$result){
die("Error updating total chapters: ".mysql_error());
}
}
echo "<hr /><br />";
}
zip_close($zip);
if(isset($BOOK['COVER']) && $BOOK['COVER']!="" && isImage($BOOK['COVER'])){
require_once("SimpleImage.php");
$img = new SimpleImage();
$img->load($upload_dir."/".$BOOK['COVER']);
$img->resizeToHeight(200);
$img->save($upload_dir."/".$BOOK['COVER']);
$res = mysql_query("UPDATE Book SET cover_path='".mysql_prep($BOOK['COVER'])."' WHERE UUID='".$BOOK['IDENTIFIER']."'");
if(!$res){
die("Error adding cover: ".mysql_error());
}
}
} else {
echo "The book was already inside the database<br />";
}
?>
<br />
</body>
</html>