]>
Commit | Line | Data |
---|---|---|
2f26729c NL |
1 | <?php |
2 | /** | |
3 | * wallabag, self hostable application allowing you to not miss any content anymore | |
4 | * | |
5 | * @category wallabag | |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
7 | * @copyright 2013 | |
8 | * @license http://opensource.org/licenses/MIT see COPYING file | |
9 | */ | |
10 | ||
4188f38a | 11 | class WallabagEBooks |
2f26729c | 12 | { |
4188f38a | 13 | protected $wallabag; |
2f26729c | 14 | protected $method; |
2f26729c | 15 | protected $value; |
4188f38a | 16 | protected $entries; |
17 | protected $bookTitle; | |
18 | protected $bookFileName; | |
2f26729c | 19 | |
2b58426b | 20 | public function __construct(Poche $wallabag, $method, $value) |
2f26729c NL |
21 | { |
22 | $this->wallabag = $wallabag; | |
23 | $this->method = $method; | |
2f26729c NL |
24 | $this->value = $value; |
25 | } | |
26 | ||
4188f38a | 27 | public function prepareData() |
2f26729c NL |
28 | { |
29 | switch ($this->method) { | |
30 | case 'id': | |
2b58426b | 31 | $entryID = filter_var($this->value, FILTER_SANITIZE_NUMBER_INT); |
2f26729c | 32 | $entry = $this->wallabag->store->retrieveOneById($entryID, $this->wallabag->user->getId()); |
fb9df0c2 | 33 | $this->entries = array($entry); |
34 | $this->bookTitle = $entry['title']; | |
35 | $this->bookFileName = substr($this->bookTitle, 0, 200); | |
2f26729c NL |
36 | break; |
37 | case 'all': | |
fb9df0c2 | 38 | $this->entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId()); |
deab6280 | 39 | $this->bookTitle = sprintf(_('All my articles on %s'), date(_('d.m.y'))); #translatable because each country has it's own date format system |
fb9df0c2 | 40 | $this->bookFileName = _('Allarticles') . date(_('dmY')); |
2f26729c NL |
41 | break; |
42 | case 'tag': | |
43 | $tag = filter_var($this->value, FILTER_SANITIZE_STRING); | |
44 | $tags_id = $this->wallabag->store->retrieveAllTags($this->wallabag->user->getId(), $tag); | |
45 | $tag_id = $tags_id[0]["id"]; // we take the first result, which is supposed to match perfectly. There must be a workaround. | |
fb9df0c2 | 46 | $this->entries = $this->wallabag->store->retrieveEntriesByTag($tag_id, $this->wallabag->user->getId()); |
47 | $this->bookTitle = sprintf(_('Articles tagged %s'), $tag); | |
48 | $this->bookFileName = substr(sprintf(_('Tag %s'), $tag), 0, 200); | |
2f26729c NL |
49 | break; |
50 | case 'category': | |
51 | $category = filter_var($this->value, FILTER_SANITIZE_STRING); | |
fb9df0c2 | 52 | $this->entries = $this->wallabag->store->getEntriesByView($category, $this->wallabag->user->getId()); |
53 | $this->bookTitle = sprintf(_('All articles in category %s'), $category); | |
54 | $this->bookFileName = substr(sprintf(_('Category %s'), $category), 0, 200); | |
2f26729c NL |
55 | break; |
56 | case 'search': | |
57 | $search = filter_var($this->value, FILTER_SANITIZE_STRING); | |
fb9df0c2 | 58 | Tools::logm($search); |
59 | $this->entries = $this->wallabag->store->search($search, $this->wallabag->user->getId()); | |
60 | $this->bookTitle = sprintf(_('All articles for search %s'), $search); | |
61 | $this->bookFileName = substr(sprintf(_('Search %s'), $search), 0, 200); | |
2f26729c NL |
62 | break; |
63 | case 'default': | |
64 | die(_('Uh, there is a problem while generating epub.')); | |
65 | } | |
4188f38a | 66 | } |
67 | } | |
2f26729c | 68 | |
4188f38a | 69 | class WallabagEpub extends WallabagEBooks |
70 | { | |
71 | /** | |
72 | * handle ePub | |
73 | */ | |
74 | public function produceEpub() | |
75 | { | |
2f26729c NL |
76 | $content_start = |
77 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | |
78 | . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" | |
79 | . "<head>" | |
80 | . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" | |
81 | . "<title>wallabag articles book</title>\n" | |
82 | . "</head>\n" | |
83 | . "<body>\n"; | |
84 | ||
85 | $bookEnd = "</body>\n</html>\n"; | |
86 | ||
87 | $log = new Logger("wallabag", TRUE); | |
88 | $fileDir = CACHE; | |
89 | ||
90 | $book = new EPub(EPub::BOOK_VERSION_EPUB3, DEBUG_POCHE); | |
91 | $log->logLine("new EPub()"); | |
92 | $log->logLine("EPub class version: " . EPub::VERSION); | |
93 | $log->logLine("EPub Req. Zip version: " . EPub::REQ_ZIP_VERSION); | |
94 | $log->logLine("Zip version: " . Zip::VERSION); | |
95 | $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL()); | |
96 | $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL()); | |
97 | ||
fb9df0c2 | 98 | $book->setTitle($this->bookTitle); |
2f26729c NL |
99 | $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID. |
100 | //$book->setLanguage("en"); // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc. | |
101 | $book->setDescription(_("Some articles saved on my wallabag")); | |
102 | $book->setAuthor("wallabag", "wallabag"); | |
103 | $book->setPublisher("wallabag", "wallabag"); // I hope this is a non existant address :) | |
104 | $book->setDate(time()); // Strictly not needed as the book date defaults to time(). | |
105 | //$book->setRights("Copyright and licence information specific for the book."); // As this is generated, this _could_ contain the name or licence information of the user who purchased the book, if needed. If this is used that way, the identifier must also be made unique for the book. | |
106 | $book->setSourceURL("http://$_SERVER[HTTP_HOST]"); | |
107 | ||
108 | $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP"); | |
109 | $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "wallabag"); | |
110 | ||
111 | $cssData = "body {\n margin-left: .5em;\n margin-right: .5em;\n text-align: justify;\n}\n\np {\n font-family: serif;\n font-size: 10pt;\n text-align: justify;\n text-indent: 1em;\n margin-top: 0px;\n margin-bottom: 1ex;\n}\n\nh1, h2 {\n font-family: sans-serif;\n font-style: italic;\n text-align: center;\n background-color: #6b879c;\n color: white;\n width: 100%;\n}\n\nh1 {\n margin-bottom: 2px;\n}\n\nh2 {\n margin-top: -2px;\n margin-bottom: 2px;\n}\n"; | |
112 | ||
113 | $log->logLine("Add Cover"); | |
114 | ||
fb9df0c2 | 115 | $fullTitle = "<h1> " . $this->bookTitle . "</h1>\n"; |
2f26729c NL |
116 | |
117 | $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle); | |
118 | ||
119 | $cover = $content_start . '<div style="text-align:center;"><p>' . _('Produced by wallabag with PHPePub') . '</p><p>'. _('Please open <a href="https://github.com/wallabag/wallabag/issues" >an issue</a> if you have trouble with the display of this E-Book on your device.') . '</p></div>' . $bookEnd; | |
120 | ||
121 | //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE); | |
122 | $book->addChapter("Notices", "Cover2.html", $cover); | |
123 | ||
124 | $book->buildTOC(); | |
125 | ||
fb9df0c2 | 126 | foreach ($this->entries as $entry) { //set tags as subjects |
2f26729c NL |
127 | $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']); |
128 | foreach ($tags as $tag) { | |
129 | $book->setSubject($tag['value']); | |
130 | } | |
131 | ||
132 | $log->logLine("Set up parameters"); | |
133 | ||
134 | $chapter = $content_start . $entry['content'] . $bookEnd; | |
135 | $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub::EXTERNAL_REF_ADD); | |
136 | $log->logLine("Added chapter " . $entry['title']); | |
137 | } | |
138 | ||
139 | if (DEBUG_POCHE) { | |
140 | $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation | |
141 | } | |
142 | $book->finalize(); | |
fb9df0c2 | 143 | $zipData = $book->sendBook($this->bookFileName); |
2f26729c | 144 | } |
4188f38a | 145 | } |
146 | ||
147 | class WallabagMobi extends WallabagEBooks | |
148 | { | |
149 | /** | |
fb9df0c2 | 150 | * MOBI Class |
151 | * @author Sander Kromwijk | |
4188f38a | 152 | */ |
153 | ||
dc59f164 | 154 | private $_kindle_email; |
155 | ||
156 | public function produceMobi($sendByMail = FALSE) | |
4188f38a | 157 | { |
4188f38a | 158 | |
fb9df0c2 | 159 | $mobi = new MOBI(); |
fb9df0c2 | 160 | $content = new MOBIFile(); |
dc59f164 | 161 | |
162 | $messages = new Messages(); // for later | |
fb9df0c2 | 163 | |
164 | $content->set("title", $this->bookTitle); | |
165 | $content->set("author", "wallabag"); | |
deab6280 | 166 | $content->set("subject", $this->bookTitle); |
fb9df0c2 | 167 | |
168 | # introduction | |
deab6280 | 169 | $content->appendParagraph('<div style="text-align:center;" ><p>' . _('Produced by wallabag with PHPMobi') . '</p><p>'. _('Please open <a href="https://github.com/wallabag/wallabag/issues" >an issue</a> if you have trouble with the display of this E-Book on your device.') . '</p></div>'); |
fb9df0c2 | 170 | $content->appendImage(imagecreatefrompng("themes/baggy/img/apple-touch-icon-152.png")); |
171 | $content->appendPageBreak(); | |
172 | ||
173 | foreach ($this->entries as $item) { | |
174 | $content->appendChapterTitle($item['title']); | |
175 | $content->appendParagraph($item['content']); | |
176 | $content->appendPageBreak(); | |
4188f38a | 177 | } |
fb9df0c2 | 178 | $mobi->setContentProvider($content); |
4188f38a | 179 | |
dc59f164 | 180 | if (!$sendByMail) { |
181 | // we offer file to download | |
182 | $mobi->download($this->bookFileName.'.mobi'); | |
183 | } | |
184 | else { | |
185 | // we send file to kindle | |
186 | ||
187 | $char_in = array('/', '.', ',', ':', '|'); # we sanitize filename to avoid conflicts with special characters (for instance, / goes for a directory) | |
188 | $mobiExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName . '.mobi')); | |
189 | ||
190 | $file = 'cache/' . $mobiExportName; | |
191 | $mobi->save($file); | |
192 | ||
193 | $file_size = filesize($file); | |
194 | $filename = basename($file); | |
195 | $handle = fopen($file, "r"); | |
196 | $content = fread($handle, $file_size); | |
197 | fclose($handle); | |
198 | $content = chunk_split(base64_encode($content)); | |
199 | ||
200 | $uid = md5(uniqid(time())); | |
201 | ||
202 | //generate header for mail | |
203 | $header = "From: wallabag <". $this->wallabag->user->email .">\r\n"; | |
204 | $header .= "MIME-Version: 1.0\r\n"; | |
205 | $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; | |
206 | $header .= "This is a multi-part message in MIME format.\r\n"; | |
207 | $header .= "--".$uid."\r\n"; | |
208 | $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; | |
209 | $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; | |
210 | $header .= "send via wallabag\r\n\r\n"; | |
211 | $header .= "--".$uid."\r\n"; | |
212 | $header .= "Content-Type: application/x-mobipocket-ebook; name=\"".$filename."\"\r\n"; | |
213 | $header .= "Content-Transfer-Encoding: base64\r\n"; | |
214 | $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; | |
215 | $header .= $content."\r\n\r\n"; | |
216 | $header .= "--".$uid."--"; | |
217 | ||
218 | # trying to get the kindle email adress | |
219 | if ($this->wallabag->user->getConfigValue('kindleemail')) | |
220 | { | |
221 | #do a try...exeption here | |
222 | mail( $this->wallabag->user->getConfigValue('kindleemail'), '[wallabag] ' . $this->bookTitle, "", $header ); | |
223 | $messages->add('s', _('The email has been sent to your kindle !')); | |
224 | } | |
225 | else | |
226 | { | |
227 | $messages->add('e', _('You didn\'t set your kindle\'s email adress !')); | |
228 | } | |
229 | } | |
fb9df0c2 | 230 | } |
4188f38a | 231 | } |
232 | ||
233 | class WallabagPDF extends WallabagEbooks | |
234 | { | |
235 | public function producePDF() | |
236 | { | |
237 | $mpdf = new mPDF('c'); | |
238 | ||
fb9df0c2 | 239 | # intro |
240 | ||
deab6280 | 241 | $html = '<h1>' . $this->bookTitle . '<bookmark content="Cover" /></h1><div style="text-align:center;" > |
242 | <p>' . _('Produced by wallabag with mPDF') . '</p> | |
243 | <p>'. _('Please open <a href="https://github.com/wallabag/wallabag/issues" >an issue</a> if you have trouble with the display of this E-Book on your device.') . '</p> | |
244 | <img src="themes/baggy/img/apple-touch-icon-152.png" /></div>'; | |
245 | $html .= '<pagebreak type="next-odd" />'; | |
246 | $i = 1; | |
fb9df0c2 | 247 | |
248 | foreach ($this->entries as $item) { | |
deab6280 | 249 | $html .= '<h1>' . $item['title'] . '<bookmark content="' . $item['title'] . '" /></h1>'; |
fb9df0c2 | 250 | $html .= '<indexentry content="'. $item['title'] .'" />'; |
251 | $html .= $item['content']; | |
deab6280 | 252 | $html .= '<pagebreak type="next-odd" />'; |
253 | $i = $i+1; | |
fb9df0c2 | 254 | } |
255 | ||
fb9df0c2 | 256 | |
257 | # headers | |
258 | $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Produced with wallabag'); | |
259 | $mpdf->SetFooter('{PAGENO}'); | |
260 | ||
261 | $mpdf->WriteHTML($html); | |
262 | ||
263 | # remove characters that make mpdf bug | |
264 | $char_in = array('/', '.', ',', ':', '|'); | |
265 | $pdfExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName . '.pdf')); | |
deab6280 | 266 | |
267 | # index | |
268 | $html = '<h2>Index<bookmark content="Index" /></h2> | |
269 | <indexinsert cols="2" offset="5" usedivletters="on" div-font-size="15" gap="5" font="Trebuchet" div-font="sans-serif" links="on" /> | |
270 | '; | |
271 | ||
272 | $mpdf->WriteHTML($html); | |
fb9df0c2 | 273 | |
274 | $mpdf->Output('cache/' . $pdfExportName); | |
275 | ||
276 | header('Content-Disposition: attachment; filename="' . $pdfExportName . '"'); | |
277 | ||
278 | header('Content-Transfer-Encoding: base64'); | |
279 | header('Content-Type: application/pdf'); | |
280 | echo file_get_contents('cache/' . $pdfExportName); | |
281 | ||
282 | //exit; | |
4188f38a | 283 | } |
284 | } |