diff options
Diffstat (limited to 'inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php')
-rw-r--r-- | inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php b/inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php new file mode 100644 index 00000000..e9792816 --- /dev/null +++ b/inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php | |||
@@ -0,0 +1,136 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Description of MultipleFileHandler | ||
5 | * | ||
6 | * @author Sander | ||
7 | */ | ||
8 | abstract class MultipleFileHandler extends ContentProvider { | ||
9 | /** | ||
10 | * @var array | ||
11 | */ | ||
12 | private $files = array(); | ||
13 | /** | ||
14 | * @var array | ||
15 | */ | ||
16 | private $images = array(); | ||
17 | /** | ||
18 | * @var array | ||
19 | */ | ||
20 | private $metadata = array(); | ||
21 | |||
22 | private $toc = array(); | ||
23 | |||
24 | /** | ||
25 | * Add a page to the file | ||
26 | * @param string $contents Contents of the chapter/page | ||
27 | * @param string $title Optional, title of the chapter/page. Will automatically add a h2 | ||
28 | * before the contents | ||
29 | */ | ||
30 | public function addPage($contents, $title = ""){ | ||
31 | if($title != ""){ | ||
32 | //TODO: Add in TOC (and add a way of generating it | ||
33 | $contents = "<h2>".$title."</h2>".$contents."<mbp:pagebreak>"; | ||
34 | } | ||
35 | $pos = 0; | ||
36 | |||
37 | if(sizeof($this->toc) > 0){ | ||
38 | $lastToc = $this->toc[sizeof($this->toc)-1]; | ||
39 | $lastFile = $this->files[sizeof($this->files)-1]; | ||
40 | $pos = $lastToc["pos"] + strlen($lastFile) + 1; | ||
41 | } | ||
42 | |||
43 | $this->files[] = $contents; | ||
44 | $this->toc[] = array("title"=>$title, "pos"=>$pos); | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * Add an image to the file | ||
49 | * @param string $imageContents Data string containing the binary data of the image | ||
50 | * @return int The reference of the image | ||
51 | */ | ||
52 | public function addImage($imageContents){ | ||
53 | $this->images[] = $imageContents; | ||
54 | return sizeof($this->images)-1; | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Add an image to the file | ||
59 | * @param string $url Url to the image | ||
60 | * @return int The reference of the image, false if the image couldn't be downloaded | ||
61 | */ | ||
62 | public function addImageFromUrl($url){ | ||
63 | $image = ImageHandler::DownloadImage($url); | ||
64 | |||
65 | if($image === false) return false; | ||
66 | return $this->addImage($image); | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Set the metadata | ||
71 | * @param string $key Key | ||
72 | * @param string $value Value | ||
73 | */ | ||
74 | public function setMetadata($key, $value){ | ||
75 | $this->metadata[$key] = $value; | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Get the text data to be integrated in the MOBI file | ||
80 | * @return string | ||
81 | */ | ||
82 | public function getTextData(){ | ||
83 | $data = implode("\n", $this->files); | ||
84 | $begin = "<html><head><guide><reference title='CONTENT' type='toc' filepos=0000000000 /></guide></head><body>"; | ||
85 | $beforeTOC = $begin.$data; | ||
86 | |||
87 | $tocPos = strlen($beforeTOC); | ||
88 | |||
89 | $toc = $this->generateTOC(strlen($begin)); | ||
90 | |||
91 | $customBegin = "<html><head><guide><reference title='CONTENT' type='toc' filepos=".$this->forceLength($tocPos, 10)." /></guide></head><body>"; | ||
92 | $data = $customBegin.$data.$toc."</body></html>"; | ||
93 | return $data; | ||
94 | } | ||
95 | |||
96 | public function forceLength($n, $l){ | ||
97 | $str = $n.""; | ||
98 | $cur = strlen($str); | ||
99 | while($cur < $l){ | ||
100 | $str = "0".$str; | ||
101 | $cur++; | ||
102 | } | ||
103 | return $str; | ||
104 | } | ||
105 | |||
106 | public function generateTOC($base = 0){ | ||
107 | $toc = "<h2>Contents</h2>"; | ||
108 | $toc .= "<blockquote><table summary='Table of Contents'><b><col/><col/><tbody>"; | ||
109 | for($i = 0, $len = sizeof($this->toc); $i < $len; $i++){ | ||
110 | $entry = $this->toc[$i]; | ||
111 | $position = $entry["pos"]+$base; | ||
112 | $toc .= "<tr><td>".($i+1).".</td><td><a filepos=".$position.">".$entry["title"]."</a></td></tr>"; | ||
113 | } | ||
114 | $toc .= "</tbody></b></table></blockquote>"; | ||
115 | |||
116 | return $toc; | ||
117 | } | ||
118 | /** | ||
119 | * Get the images (an array containing the jpeg data). Array entry 0 will | ||
120 | * correspond to image record 0. | ||
121 | * @return array | ||
122 | */ | ||
123 | public function getImages(){ | ||
124 | return $this->images; | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * Get the metadata in the form of a hashtable (for example, title or author). | ||
129 | * @return array | ||
130 | */ | ||
131 | public function getMetaData(){ | ||
132 | return $this->metadata; | ||
133 | } | ||
134 | |||
135 | } | ||
136 | ?> | ||