forked from Suika/BTE-GEN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epub.php
842 lines (697 loc) · 38.3 KB
/
epub.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of epub
*
* @author Simon
*/
class epub {
private $zip;
private $title = "";
private $fileName = "";
private $language = "en";
private $isbn = "";
private $uuid = "";
private $description = "";
private $author = "";
private $publisher = "";
private $date = null;
private $rights = "All materials' copyrights reserved by their respective authors and the associated publishers. Please respect their rights. Works will be deleted upon request by copyright holders.";
private $depth = 1;
private $BID = 0;
private $SID = 0;
private $illustrator = "";
private $editor = "";
private $translator = "";
private $ncx = "";
private $manifest = "";
private $opf = "";
private $opf_ncx = "";
private $body = "";
private $illustartions = "";
private $cover = "";
private $ncx_navmap = "";
private $contentIllustartions = "";
private $content = "";
private $contentImgUrls = array();
private $device = array('sgs2' => array('h' => 800, 'w' => 480),
'iphone4s' => array('h' => 960, 'w' => 640),
'ipad2' => array('h' => 2048, 'w' => 1536),
'ipad' => array('h' => 1024, 'w' => 768),
'acer' => array('h' => 1280, 'w' => 800),
'kindlefire' => array('h' => 1024, 'w' => 600),
'iphone3' => array('h' => 480, 'w' => 320),
'iphone5' => array('h' => 1136, 'w' => 640),
't9800' => array('h' => 480, 'w' => 360),
't9810' => array('h' => 640, 'w' => 480),
'htc' => array('h' => 960, 'w' => 540),
'lg' => array('h' => 800, 'w' => 400),
'sb' => array('h' => 320, 'w' => 240),
'kobotouch' => array('h' => 800, 'w' => 600));
private $resolution = "";
private $epub = NULL;
private $replaceImg = array();
private $noImages = FALSE;
private $first = TRUE;
private $iter = 0;
function __construct() {
$this->epub = tempnam("tmp", "EPUB");
if (!copy('epexampleub.epub', $this->epub)) {
echo "There was an error, please contact the administrator.";
die();
}
}
function __destruct() {
unlink($this->epub);
}
protected function getContent($urlTitle) {
header('Content-Type: text/html; charset=utf-8');
$this->content = file_get_contents('http://www.baka-tsuki.org/project/index.php?action=render&title=' . $urlTitle);
return TRUE;
}
protected function cleanContent($content) {
$content = preg_replace('/<span class="mw-editsection">.*>]<\/span><\/span>/m', "", $content);
$content = preg_replace('/<div\sid="toc".+?<div\sid="toctitle".+?<\/div>.+?<\/div>/s', '', $content);
//$content = preg_replace('%<table\b\sid="toc"[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i', '', $content);
$this->content = $content;
return TRUE;
}
protected function trimHeaders($content) {
if (preg_match('/<h2>/', $content) & !preg_match('/<h1>/', $content)) {
$content = str_replace("h2", "h1", $content);
$content = str_replace("h3", "h2", $content);
$content = str_replace("h4", "h3", $content);
$content = str_replace("h5", "h4", $content);
$content = str_replace("h6", "h5", $content);
$content = str_replace("h7", "h6", $content);
} elseif (!preg_match('/<h1>/', $content) & !preg_match('/<h2>/', $content) & preg_match('/<h3>/', $content)) {
$content = str_replace("h3", "h1", $content);
$content = str_replace("h4", "h2", $content);
$content = str_replace("h5", "h3", $content);
$content = str_replace("h6", "h4", $content);
$content = str_replace("h7", "h5", $content);
$content = str_replace("h8", "h6", $content);
}
$this->content = $content;
return TRUE;
}
protected function getImagesURL($content) {
$urls = array();
$doc = new DOMDocument();
@$doc->loadHTML($content);
$tags = $doc->getElementsByTagName('a');
foreach ($tags as $tag) {
if (preg_match('/\/project\/index\.php\?title=File:/', $tag->getAttribute('href'))) {
$tag = $tag->getAttribute('href');
$tag = explode(":", $tag);
$tag = end($tag);
array_push($urls, $tag);
}
}
$this->contentImgUrls = array_unique($urls);
return TRUE;
}
private function min_by_key($arr, $key){
$min = array();
foreach ($arr as $val) {
if (!isset($val[$key]) and is_array($val)) {
$min2 = $this->min_by_key($val, $key);
$min[$min2] = 1;
} elseif (!isset($val[$key]) and !is_array($val)) {
return false;
} elseif (isset($val[$key])) {
$min[$val[$key]] = 1;
}
}
return min( array_keys($min) );
}
private function navPoint($array) {
if (empty($array))
return;
$high_header = $this->min_by_key($array, 'h');
$hhk = array();
foreach ($array as $key => $value) {
if($value['h'] == $high_header){
array_push($hhk, $key);
}
}
$length = count($hhk);
for ($i = 0; $i < $length; ++$i) {
++$this->iter;
if ($array[$hhk[$i]]['h'] > $this->depth) {
$this->depth = $array[$hhk[$i]]['h'];
}
$this->ncx_navmap .= '<navPoint id="navPoint-' . $this->iter . '" playOrder="' . $this->iter . '">' . PHP_EOL;
$this->ncx_navmap .= "\t" . '<navLabel>' . PHP_EOL;
$this->ncx_navmap .= "\t" . "\t" . '<text>' . $array[$hhk[$i]]['txt'] . '</text>' . PHP_EOL;
$this->ncx_navmap .= "\t" . '</navLabel>' . PHP_EOL;
if ($this->first == TRUE) {
$this->ncx_navmap .= "\t" . '<content src="Text/Body.xhtml" />' . PHP_EOL;
$this->first = FALSE;
} else {
$this->ncx_navmap .= "\t" . '<content src="Text/Body.xhtml#' . $array[$hhk[$i]]['id'] . '" />' . PHP_EOL;
}
if ($i == $length - 1) {
$this->navPoint(array_slice($array, $hhk[$i] + 1, NULL));
} elseif (($hhk[$i + 1] - $hhk[$i]) > 1) {
$this->navPoint(array_slice($array, $hhk[$i] + 1, ($hhk[$i + 1] - $hhk[$i]) - 1));
}
$this->ncx_navmap .= '</navPoint>' . PHP_EOL;
}
}
protected function getTOC($content) {
preg_match_all('/<h[0-9].*?>.*?<\/h[0-9]>/i', $content, $matches);
$toc = array();
$headfirst = FALSE;
$headfound = FALSE;
foreach ($matches[0] as $match) {
preg_match_all('/<h([0-9])>\s*?<span\sclass="mw-headline"\sid="(.*?)">(.*?)<\/h[0-9]>/i', $match, $smatch);
if ($smatch[1][0] == 1) {
$headfound = TRUE;
}
if ($smatch[1][0] > 1 && $headfirst == FALSE) {
$toc[] = array('h' => 1, 'id' => preg_replace('/[\+\.\-:]/', '', $smatch[2][0]), 'txt' => trim(strip_tags($smatch[3][0])));
$content = str_replace($match, '<h1 id="' . preg_replace('/[\+\.\-:]/', '', $smatch[2][0]) . '">' . trim(strip_tags($smatch[3][0])) . '</h1>', $content);
$headfirst = TRUE;
} elseif ($smatch[1][0] > 1 && $headfirst == TRUE && $headfound == FALSE) {
$toc[] = array('h' => ($smatch[1][0] - 1), 'id' => preg_replace('/[\+\.\-:]/', '', $smatch[2][0]), 'txt' => trim(strip_tags($smatch[3][0])));
$content = str_replace($match, '<h' . ($smatch[1][0] - 1) . ' id="' . preg_replace('/[\+\.\-:]/', '', $smatch[2][0]) . '">' . trim(strip_tags($smatch[3][0])) . '</h' . ($smatch[1][0] - 1) . '>', $content);
} elseif ($smatch[1][0] == 1 || $headfirst == TRUE && $headfound == TRUE) {
$headfirst = TRUE;
$toc[] = array('h' => $smatch[1][0], 'id' => preg_replace('/[\+\.\-:]/', '', $smatch[2][0]), 'txt' => trim(strip_tags($smatch[3][0])));
$content = str_replace($match, '<h' . $smatch[1][0] . ' id="' . preg_replace('/[\+\.\-:]/', '', $smatch[2][0]) . '">' . trim(strip_tags($smatch[3][0])) . '</h' . $smatch[1][0] . '>', $content);
}
}
$this->ncx_navmap = '<navMap>' . PHP_EOL;
$depth = 1;
$this->navPoint($toc);
$this->ncx_navmap .= '</navMap>';
$this->content = $content;
return TRUE;
}
protected function replaceIMG($content, $remove = FALSE) {
if (!$remove) {
$dirimg = array();
if ($handle = opendir('images/books/' . $this->SID . '/' . $this->BID . '/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != "resized") {
$data = getimagesize('images/books/' . $this->SID . '/' . $this->BID . '/' . $entry);
$width = $data[0];
$height = $data[1];
$dirimg[] = array($entry => array('h' => $height, 'w' => $width));
$dirimgi[] = $entry;
}
}
closedir($handle);
}
foreach ($this->contentImgUrls as $img) {
if (in_array($img, $dirimgi)) {
if ($this->resolution == "original") {
$data = getimagesize('images/books/' . $this->SID . '/' . $this->BID . '/' . $img);
$width = $data[0];
$height = $data[1];
} else {
$width = $this->device[$this->resolution]['w'];
$height = $this->device[$this->resolution]['h'];
}
if ($height > 400 || $width > 400) {
$rep = '<div class="svg_outer svg_inner">
<svg xmlns="http://www.w3.org/2000/svg" height="100%" preserveAspectRatio="xMidYMid meet" version="1.1" viewBox="0 0 ' . $width . ' ' . $height . '" width="100%" xmlns:xlink="http://www.w3.org/1999/xlink">
<image height="' . $height . '" width="' . $width . '" xlink:href="../Images/' . $img . '"></image>
</svg>
</div>';
$stringData = '<div id="' . $img . '"></div>';
$this->replaceImg[] = array('s' => $stringData, 'r' => $rep,);
} else {
$rep = '<p><img alt="' . $img . '" class="baka" src="../Images/' . $img . '" /></p>';
$stringData = '<div id="' . $img . '"></div>';
$this->replaceImg[] = array('s' => $stringData, 'r' => $rep,);
}
$img_org = $img;
$img = str_replace(')', '\)', str_replace('(', '\(', $img));
if (preg_match('/<div class="thumb tright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".+?<\/a><\/div><\/div><\/div><\/div>/', $content)) {
$content = preg_replace('/<div class="thumb tright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".+?<\/a><\/div><\/div><\/div><\/div>/', $stringData, $content);
} elseif (preg_match('/<div style="width: 100%; overflow:auto;">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/p><\/div>/', $content)) {
$content = preg_replace('/<div style="width: 100%; overflow:auto;">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/p><\/div>/', $stringData, $content);
} elseif (preg_match('/<div class="floatright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/div>/', $content)) {
$content = preg_replace('/<div class="floatright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/div>/', $stringData, $content);
} elseif (preg_match('/<div class="floatleft">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/div>/', $content)) {
$content = preg_replace('/<div class="floatleft">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/div>/', $stringData, $content);
} elseif (preg_match('/<a href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".+?<\/a>/', $content)) {
$content = preg_replace('/<a href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".+?<\/a>/', $stringData, $content);
} else {
$this->contentIllustartions .= $stringData . PHP_EOL;
}
$img = $img_org;
$end_tag = explode(".", $img);
$end_tag = end($end_tag);
if (($end_tag == "jpg") || ($end_tag == "jpeg") || ($end_tag == "JPG") || ($end_tag == "JPEG")) {
$this->manifest .= '<item href="Images/' . $img . '" id="' . $img . '" media-type="image/jpeg" />' . PHP_EOL;
} elseif (($end_tag == "png") || ($end_tag == "PNG")) {
$this->manifest .= '<item href="Images/' . $img . '" id="' . $img . '" media-type="image/png" />' . PHP_EOL;
} elseif (($end_tag == "gif") || ($end_tag == "GIF")) {
$this->manifest .= '<item href="Images/' . $img . '" id="' . $img . '" media-type="image/gif" />' . PHP_EOL;
}
} else {
$content = preg_replace('/<div class="thumb tright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/div>.*<\/div><\/div><\/div>/', '', $content);
$content = preg_replace('/<div style="width: 100%; overflow:auto;">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/p><\/div>/', '', $content);
$content = preg_replace('/<div class="floatright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/div>/', '', $content);
$content = preg_replace('/<div class="floatleft">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/div>/', '', $content);
$content = preg_replace('/<p>.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:' . $img . '".*<\/a><\/p>/', '', $content);
}
}
} else {
$content = preg_replace('/<div class="thumb tright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:.*<\/a><\/div>.*<\/div><\/div><\/div>/', $stringData, $content);
$content = preg_replace('/<div style="width: 100%; overflow:auto;">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:.*<\/a><\/p><\/div>/', $stringData, $content);
$content = preg_replace('/<div class="floatright">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:.*<\/a><\/div>/', $stringData, $content);
$content = preg_replace('/<div class="floatleft">.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:.*<\/a><\/div>/', $stringData, $content);
$content = preg_replace('/<p>.*href="\/\/www.baka-tsuki.org\/project\/index.php\?title=File:.*<\/a><\/p>/', $stringData, $content);
$content = preg_replace('/<a.*class="image".*<img.*\/><\/a>/', $stringData, $content);
}
$this->content = $content;
return TRUE;
}
protected function searchFolder($path, $regex) {
$files = glob($path);
if (empty($files)) {
return FALSE;
} else {
foreach ($files as $file) {
$exp = explode('/', $file);
$exp = end($exp);
if (preg_match($regex, $exp)) {
return $exp;
}
}
}
}
protected function removeBeforeHeader($content) {
$content = preg_replace("/<h.>.*?[Ii]llustrations.*?<\/h.>/", "", $content);
preg_match_all('/<h([0-9]).*?>.*?<\/h[0-9]>/i', $content, $matches);
$content = strstr($content, '<h' . $matches[1][0] . '>');
$this->content = $content;
}
protected function forceASCII($ncx, $opf) {
$this->ncx = preg_replace('/[^(\x20-\x7F)]*/', '', $ncx);
$this->opf = preg_replace('/[^(\x20-\x7F)]*/', '', $opf);
}
protected function constructNCX($navmap, $uuid, $depth, $title) {
$this->ncx =
'<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL
. '<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" ' . PHP_EOL
. '"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">' . PHP_EOL
. '<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">' . PHP_EOL
. "\t" . '<head>' . PHP_EOL
. "\t\t" . '<meta name="dtb:uid" content="urn:uuid:' . $uuid . '" />' . PHP_EOL
. "\t\t" . '<meta name="dtb:depth" content="' . $depth . '" />' . PHP_EOL
. "\t\t" . '<meta name="dtb:totalPageCount" content="0" />' . PHP_EOL
. "\t\t" . '<meta name="dtb:maxPageNumber" content="0" />' . PHP_EOL
. "\t" . '</head>' . PHP_EOL
. "\t" . '<docTitle>' . PHP_EOL
. "\t\t" . '<text>' . $title . '</text>' . PHP_EOL
. "\t" . '</docTitle>' . PHP_EOL
. $navmap
. '</ncx>';
}
protected function constructOPF() {
$this->opf =
'<?xml version="1.0" encoding="utf-8" standalone="yes"?>' . PHP_EOL
. '<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId" version="2.0">' . PHP_EOL
. "\t" . '<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">' . PHP_EOL;
if (!empty($this->uuid))
$this->opf .= '<dc:identifier id="BookId" opf:scheme="UUID">urn:uuid:' . $this->uuid . '</dc:identifier>' . PHP_EOL;
if (!empty($this->title))
$this->opf .= '<dc:title>' . $this->fileName . '</dc:title>' . PHP_EOL;
if (!empty($this->illustrator))
$this->opf .= '<dc:contributor opf:role="ill">' . $this->illustrator . '</dc:contributor>' . PHP_EOL;
if (!empty($this->isbn))
$this->opf .= '<dc:identifier opf:scheme="ISBN">' . $this->isbn . '</dc:identifier>' . PHP_EOL;
if (!empty($this->author)) {
$temp = explode(" ", $this->author);
$this->opf .= '<dc:creator opf:file-as="' . $temp[1] . ', ' . $temp[0] . '" opf:role="aut">' . $this->author . '</dc:creator>' . PHP_EOL;
unset($temp);
}
if (!empty($this->description))
$this->opf .= '<dc:description>' . $this->description . '</dc:description>' . PHP_EOL;
if (!empty($this->date)) {
$this->opf .= '<dc:date>' . $this->date . '</dc:date>' . PHP_EOL;
$this->opf .= '<dc:date opf:event="creation">' . $this->date . '</dc:date>' . PHP_EOL;
}
if (!empty($this->editor))
$this->opf .= '<dc:contributor opf:role="edt">' . $this->editor . '</dc:contributor>' . PHP_EOL;
if (!empty($this->translator))
$this->opf .= '<dc:contributor opf:file-as="' . $this->translator . '" opf:role="trl">' . $this->translator . '</dc:contributor>' . PHP_EOL;
if (!empty($this->date))
$this->opf .= '<dc:date opf:event="publication">' . $this->date . '</dc:date>' . PHP_EOL;
if (!empty($this->publisher))
$this->opf .= '<dc:publisher>' . $this->publisher . '</dc:publisher>' . PHP_EOL;
if (!empty($this->language))
$this->opf .= '<dc:language>' . $this->language . '</dc:language>' . PHP_EOL;
if (!empty($this->rights))
$this->opf .= '<dc:rights>' . $this->rights . '</dc:rights>' . PHP_EOL;
$this->opf .= '<meta content="Cover.jpg" name="cover" />' . PHP_EOL
. '</metadata>' . PHP_EOL
. '<manifest>' . PHP_EOL
. '<item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml" />' . PHP_EOL
. '<item href="Text/Cover.xhtml" id="Cover.xhtml" media-type="application/xhtml+xml" />' . PHP_EOL
. '<item href="Styles/stylesheet.css" id="stylesheet.css" media-type="text/css" />' . PHP_EOL
. '<item href="Styles/page_styles.css" id="page_styles.css" media-type="text/css" />' . PHP_EOL
. '<item href="Text/Body.xhtml" id="Body.xhtml" media-type="application/xhtml+xml" />' . PHP_EOL
. '<item href="Images/Cover.jpg" id="Cover.jpg" media-type="image/jpeg" />' . PHP_EOL;
if (!empty($this->manifest))
$this->opf .= $this->manifest;
if (!empty($this->contentIllustartions))
$this->opf .= '<item href="Text/Illustrations.xhtml" id="Illustrations.xhtml" media-type="application/xhtml+xml" />';
$this->opf .= '</manifest>' . PHP_EOL
. '<spine toc="ncx">' . PHP_EOL
. '<itemref idref="Cover.xhtml" />' . PHP_EOL;
if (!empty($this->contentIllustartions))
$this->opf .= '<itemref idref="Illustrations.xhtml" />' . PHP_EOL;
$this->opf .= '<itemref idref="Body.xhtml" />' . PHP_EOL
. '</spine>' . PHP_EOL
. '<guide>' . PHP_EOL
. '<reference href="Text/Cover.xhtml" title="Cover" type="cover" />' . PHP_EOL
. '</guide>' . PHP_EOL
. '</package>';
}
protected function constructCover() {
if ($result = $this->searchFolder("images/books/" . $this->SID . "/" . $this->BID . "/*", '/^[Cc]over.jp[e]?g/')) {
$data = getimagesize("images/books/" . $this->SID . "/" . $this->BID . "/" . $result);
$width = $data[0];
$height = $data[1];
unset($result);
} else {
$data = getimagesize('404.jpg');
$width = $data[0];
$height = $data[1];
}
$this->cover = '<?xml version="1.0" encoding="utf-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' . PHP_EOL . PHP_EOL
. '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">' . PHP_EOL
. '<head>' . PHP_EOL
. '<meta content="true" name="calibre:cover" />' . PHP_EOL . PHP_EOL
. '<title>Cover</title>' . PHP_EOL
. '<style type="text/css">' . PHP_EOL
. '/*<![CDATA[*/' . PHP_EOL . PHP_EOL
. '@page {padding: 0pt; margin:0pt}' . PHP_EOL
. 'body { text-align: center; padding:0pt; margin: 0pt; }' . PHP_EOL
. '/*]]>*/' . PHP_EOL
. '</style>' . PHP_EOL
. '</head>' . PHP_EOL . PHP_EOL
. '<body>' . PHP_EOL
. '<div>' . PHP_EOL;
if ($this->resolution == "original") {
$this->cover .= '<svg xmlns="http://www.w3.org/2000/svg" height="100%" preserveAspectRatio="xMidYMid meet" version="1.1" viewBox="0 0 ' . $width . ' ' . $height . '" width="100%" xmlns:xlink="http://www.w3.org/1999/xlink">' . PHP_EOL
. '<image height="' . $height . '" width="' . $width . '" xlink:href="../Images/Cover.jpg"></image>' . PHP_EOL;
} else {
$this->cover .= '<svg xmlns="http://www.w3.org/2000/svg" height="100%" preserveAspectRatio="xMidYMid meet" version="1.1" viewBox="0 0 ' . $this->device[$this->resolution]['w'] . ' ' . $this->device[$this->resolution]['h'] . '" width="100%" xmlns:xlink="http://www.w3.org/1999/xlink">' . PHP_EOL
. '<image height="' . $this->device[$this->resolution]['h'] . '" width="' . $this->device[$this->resolution]['w'] . '" xlink:href="../Images/Cover.jpg"></image>' . PHP_EOL;
}
$this->cover .= '</svg>' . PHP_EOL
. '</div>' . PHP_EOL
. '</body>' . PHP_EOL
. '</html>';
}
protected function constructIllustrations() {
$body = ''
. '<?xml version="1.0" encoding="utf-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' . PHP_EOL
. '<html xmlns="http://www.w3.org/1999/xhtml">' . PHP_EOL
. '<head>' . PHP_EOL
. '<title></title>' . PHP_EOL
. '<link href="../Styles/stylesheet.css" rel="stylesheet" type="text/css" />' . PHP_EOL
. '<link href="../Styles/page_styles.css" rel="stylesheet" type="text/css" />' . PHP_EOL
. '</head>' . PHP_EOL
. '<body>' . PHP_EOL;
$body .= $this->contentIllustartions;
$body .= '</body>' . PHP_EOL . '</html>';
$this->illustartions = $body;
}
protected function constructContent() {
$body = ''
. '<?xml version="1.0" encoding="utf-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' . PHP_EOL
. '<html xmlns="http://www.w3.org/1999/xhtml">' . PHP_EOL
. '<head>' . PHP_EOL
. '<title></title>' . PHP_EOL
. '<link href="../Styles/stylesheet.css" rel="stylesheet" type="text/css" />' . PHP_EOL
. '<link href="../Styles/page_styles.css" rel="stylesheet" type="text/css" />' . PHP_EOL
. '</head>' . PHP_EOL
. '<body>' . PHP_EOL;
$body .= $this->content;
$body .= '</body>' . PHP_EOL . '</html>';
$this->content = $body;
}
protected function cleaning($what_to_clean, $tidy_config = '') {
$config = array(
'char-encoding' => 'utf8',
'output-xhtml' => true,
'doctype' => 'strict',
'indent' => true,
'indent-spaces' => 2,
'wrap' => 0
);
if ($tidy_config == '') {
$tidy_config = &$config;
}
$tidy = new tidy();
$out = $tidy->repairString($what_to_clean, $tidy_config, 'UTF8');
unset($tidy);
unset($tidy_config);
$out = strstr($out, '<html xmlns="http://www.w3.org/1999/xhtml">');
$out = '<?xml version="1.0" encoding="utf-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' . $out;
return($out);
}
protected function sendFile($file_path, $title) {
$fp = fopen($file_path, "r");
$fstat = fstat($fp);
fclose($fp);
if (file_exists($file_path)) {
header('Content-Type: application/epub+zip');
header("Content-Length: " . $fstat['size']);
header('Content-Disposition: attachment; filename="' . $title . '.epub"');
readfile($file_path);
}
unlink($this->epub);
}
protected function format(&$simpleXmlObject) {
$simpleXmlObject = simplexml_load_string($simpleXmlObject);
if (!is_object($simpleXmlObject)) {
return "";
}
//Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXmlObject->asXML());
return $dom->saveXML();
}
protected function replaseSR() {
foreach ($this->replaceImg as $val) {
$this->content = str_replace($val['s'], $val['r'], $this->content);
if (!empty($this->contentIllustartions)) {
$this->illustartions = str_replace($val['s'], $val['r'], $this->illustartions);
}
}
}
public function processEpub() {
$this->getContent($this->getTitle());
$this->cleanContent($this->content);
$this->getImagesURL($this->content);
$this->removeBeforeHeader($this->content);
$this->trimHeaders($this->content);
$this->replaceIMG($this->content, $this->noImages);
$this->getTOC($this->content);
$this->constructNCX($this->ncx_navmap, $this->getUuid(), $this->depth, $this->fileName);
$this->constructOPF();
$this->constructCover();
$this->constructContent();
if (!empty($this->contentIllustartions)) {
$this->constructIllustrations();
}
$this->content = $this->cleaning($this->content);
//$this->content = str_replace('<br />', ' ', $this->content);
$this->replaseSR();
$this->forceASCII($this->ncx, $this->opf);
$this->ncx = $this->format($this->ncx);
$this->opf = $this->format($this->opf);
$dirimg = array();
if ($handle = opendir('images/books/' . $this->SID . '/' . $this->BID . '/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != "cover.jpg" && $entry != "cover.jpeg" && $entry != "Cover.jpg" && $entry != "Cover.jpeg" && $entry != "resized") {
$dirimg[] = $entry;
}
}
closedir($handle);
}
$zip = new ZipArchive;
if ($zip->open($this->epub) === TRUE) {
$zip->addFromString('OEBPS/Text/Body.xhtml', $this->content);
$zip->addFromString('OEBPS/Text/Cover.xhtml', $this->cover);
if (!empty($this->contentIllustartions)) {
$zip->addFromString('OEBPS/Text/Illustrations.xhtml', $this->illustartions);
}
$zip->addFromString('OEBPS/toc.ncx', $this->ncx);
$zip->addFromString('OEBPS/content.opf', $this->opf);
if ($result = $this->searchFolder("images/books/" . $this->SID . "/" . $this->BID . "/*", '/^[Cc]over.jp[e]?g/')) {
if ($this->resolution == "original") {
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/" . $result, 'OEBPS/Images/Cover.jpg');
} else {
try {
if (file_exists("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $result)) {
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $result, 'OEBPS/Images/Cover.jpg');
} else {
$image = new Imagick("images/books/" . $this->SID . "/" . $this->BID . "/" . $result);
$image->adaptiveResizeImage($this->device[$this->resolution]['h'], $this->device[$this->resolution]['w'], TRUE);
$image->writeimage("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $result);
$image->clear();
$image->destroy();
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $result, 'OEBPS/Images/Cover.jpg');
}
} catch (Exception $e) {
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/" . $img, 'OEBPS/Images/' . $img);
}
}
unset($result);
} else {
$zip->addFile('404.jpg', 'OEBPS/Images/Cover.jpg');
}
if (!$this->noImages) {
foreach ($dirimg as $img) {
if ($this->resolution == "original") {
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/" . $img, 'OEBPS/Images/' . $img);
} else {
$width = $this->device[$this->resolution]['w'];
$height = $this->device[$this->resolution]['h'];
if (!is_dir("images/books/" . $this->SID . "/" . $this->BID . "/resized")) {
mkdir("images/books/" . $this->SID . "/" . $this->BID . "/resized");
}
if (!is_dir("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'])) {
mkdir("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w']);
}
try {
$data = getimagesize("images/books/" . $this->SID . "/" . $this->BID . "/" . $img);
$width = $data[0];
$height = $data[1];
if ($width < 400 || $height < 400) {
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/" . $img, 'OEBPS/Images/' . $img);
} else {
if (file_exists("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $img)) {
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $img, 'OEBPS/Images/' . $img);
} else {
$image = new Imagick("images/books/" . $this->SID . "/" . $this->BID . "/" . $img);
$image->adaptiveResizeImage($this->device[$this->resolution]['h'], $this->device[$this->resolution]['w'], TRUE);
$image->writeimage("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $img);
$image->clear();
$image->destroy();
$zip->addFile("images/books/" . $this->SID . "/" . $this->BID . "/resized/" . $this->device[$this->resolution]['h'] . "x" . $this->device[$this->resolution]['w'] . "/" . $img, 'OEBPS/Images/' . $img);
}
}
} catch (Exception $e) {
$zip->addFile("images/books/" . $this->SID . "/" . $BID . "/" . $img, 'OEBPS/Images/' . $img);
}
}
}
}
$zip->close();
} else {
echo 'Fehler';
}
$this->sendFile($this->epub, $this->fileName);
}
private function getTitle() {
return $this->title;
}
public function setTitle($title) {
$this->setFileName($title);
$this->title = $title;
}
private function getLanguage() {
return $this->language;
}
public function setLanguage($language) {
$this->language = $language;
}
private function getIsbn() {
return $this->isbn;
}
public function setIsbn($isbn) {
$this->isbn = $isbn;
}
private function getUuid() {
return $this->uuid;
}
public function setUuid($uuid) {
$this->uuid = $uuid;
}
private function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
}
private function getAuthor() {
return $this->author;
}
public function setAuthor($author) {
$this->author = $author;
}
private function getPublisher() {
return $this->publisher;
}
public function setPublisher($publisher) {
$this->publisher = $publisher;
}
private function getDate() {
return $this->date;
}
public function setDate($date) {
$this->date = $date;
}
private function getRights() {
return $this->rights;
}
public function setRights($rights) {
$this->rights = $rights;
}
private function getIllustrator() {
return $this->illustrator;
}
public function setIllustrator($illustrator) {
$this->illustrator = $illustrator;
}
private function getEditor() {
return $this->editor;
}
public function setEditor($editor) {
$this->editor = $editor;
}
private function getResolution() {
return $this->resolution;
}
public function setResolution($resolution) {
$this->resolution = $resolution;
}
private function getFileName() {
return $this->fileName;
}
public function setFileName($fileName) {
$bad = array_merge(
array_map('chr', range(0,31)),
array("<", ">", ":", '"', "/", "\\", "|", "?", "*"));
$this->fileName = str_replace($bad, "", $fileName);
}
public function setBID($BID) {
$this->BID = $BID;
}
public function setSID($SID) {
$this->SID = $SID;
}
public function setNoImages($noImages) {
$this->noImages = $noImages;
}
public function setTranslator($translator) {
$this->translator = $translator;
}
}
?>