diff options
Diffstat (limited to 'inc/poche')
-rw-r--r-- | inc/poche/WallabagEBooks.class.php | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/inc/poche/WallabagEBooks.class.php b/inc/poche/WallabagEBooks.class.php index 5f42308e..906a2247 100644 --- a/inc/poche/WallabagEBooks.class.php +++ b/inc/poche/WallabagEBooks.class.php | |||
@@ -16,6 +16,7 @@ class WallabagEBooks | |||
16 | protected $entries; | 16 | protected $entries; |
17 | protected $bookTitle; | 17 | protected $bookTitle; |
18 | protected $bookFileName; | 18 | protected $bookFileName; |
19 | protected $author = 'wallabag'; | ||
19 | 20 | ||
20 | public function __construct(Poche $wallabag, $method, $value) | 21 | public function __construct(Poche $wallabag, $method, $value) |
21 | { | 22 | { |
@@ -33,11 +34,14 @@ class WallabagEBooks | |||
33 | $this->entries = array($entry); | 34 | $this->entries = array($entry); |
34 | $this->bookTitle = $entry['title']; | 35 | $this->bookTitle = $entry['title']; |
35 | $this->bookFileName = substr($this->bookTitle, 0, 200); | 36 | $this->bookFileName = substr($this->bookTitle, 0, 200); |
37 | $this->author = preg_replace('#^w{3}.#', '', Tools::getdomain($entry["url"])); # if only one article, set author to domain name (we strip the eventual www part) | ||
38 | Tools::logm('Producing ebook from article ' . $this->bookTitle); | ||
36 | break; | 39 | break; |
37 | case 'all': | 40 | case 'all': |
38 | $this->entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId()); | 41 | $this->entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId()); |
39 | $this->bookTitle = sprintf(_('All my articles on %s'), date(_('d.m.y'))); #translatable because each country has it's own date format system | 42 | $this->bookTitle = sprintf(_('All my articles on %s'), date(_('d.m.y'))); #translatable because each country has it's own date format system |
40 | $this->bookFileName = _('Allarticles') . date(_('dmY')); | 43 | $this->bookFileName = _('Allarticles') . date(_('dmY')); |
44 | Tools::logm('Producing ebook from all articles'); | ||
41 | break; | 45 | break; |
42 | case 'tag': | 46 | case 'tag': |
43 | $tag = filter_var($this->value, FILTER_SANITIZE_STRING); | 47 | $tag = filter_var($this->value, FILTER_SANITIZE_STRING); |
@@ -46,12 +50,14 @@ class WallabagEBooks | |||
46 | $this->entries = $this->wallabag->store->retrieveEntriesByTag($tag_id, $this->wallabag->user->getId()); | 50 | $this->entries = $this->wallabag->store->retrieveEntriesByTag($tag_id, $this->wallabag->user->getId()); |
47 | $this->bookTitle = sprintf(_('Articles tagged %s'), $tag); | 51 | $this->bookTitle = sprintf(_('Articles tagged %s'), $tag); |
48 | $this->bookFileName = substr(sprintf(_('Tag %s'), $tag), 0, 200); | 52 | $this->bookFileName = substr(sprintf(_('Tag %s'), $tag), 0, 200); |
53 | Tools::logm('Producing ebook from tag ' . $tag); | ||
49 | break; | 54 | break; |
50 | case 'category': | 55 | case 'category': |
51 | $category = filter_var($this->value, FILTER_SANITIZE_STRING); | 56 | $category = filter_var($this->value, FILTER_SANITIZE_STRING); |
52 | $this->entries = $this->wallabag->store->getEntriesByView($category, $this->wallabag->user->getId()); | 57 | $this->entries = $this->wallabag->store->getEntriesByView($category, $this->wallabag->user->getId()); |
53 | $this->bookTitle = sprintf(_('All articles in category %s'), $category); | 58 | $this->bookTitle = sprintf(_('All articles in category %s'), $category); |
54 | $this->bookFileName = substr(sprintf(_('Category %s'), $category), 0, 200); | 59 | $this->bookFileName = substr(sprintf(_('Category %s'), $category), 0, 200); |
60 | Tools::logm('Producing ebook from category ' . $category); | ||
55 | break; | 61 | break; |
56 | case 'search': | 62 | case 'search': |
57 | $search = filter_var($this->value, FILTER_SANITIZE_STRING); | 63 | $search = filter_var($this->value, FILTER_SANITIZE_STRING); |
@@ -59,9 +65,10 @@ class WallabagEBooks | |||
59 | $this->entries = $this->wallabag->store->search($search, $this->wallabag->user->getId()); | 65 | $this->entries = $this->wallabag->store->search($search, $this->wallabag->user->getId()); |
60 | $this->bookTitle = sprintf(_('All articles for search %s'), $search); | 66 | $this->bookTitle = sprintf(_('All articles for search %s'), $search); |
61 | $this->bookFileName = substr(sprintf(_('Search %s'), $search), 0, 200); | 67 | $this->bookFileName = substr(sprintf(_('Search %s'), $search), 0, 200); |
68 | Tools::logm('Producing ebook from search ' . $search); | ||
62 | break; | 69 | break; |
63 | case 'default': | 70 | case 'default': |
64 | die(_('Uh, there is a problem while generating epub.')); | 71 | die(_('Uh, there is a problem while generating eBook.')); |
65 | } | 72 | } |
66 | } | 73 | } |
67 | } | 74 | } |
@@ -73,12 +80,13 @@ class WallabagEpub extends WallabagEBooks | |||
73 | */ | 80 | */ |
74 | public function produceEpub() | 81 | public function produceEpub() |
75 | { | 82 | { |
83 | Tools::logm('Starting to produce ePub 3 file'); | ||
76 | $content_start = | 84 | $content_start = |
77 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | 85 | "<?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" | 86 | . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" |
79 | . "<head>" | 87 | . "<head>" |
80 | . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" | 88 | . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" |
81 | . "<title>wallabag articles book</title>\n" | 89 | . "<title>" . _("wallabag articles book") . "</title>\n" |
82 | . "</head>\n" | 90 | . "</head>\n" |
83 | . "<body>\n"; | 91 | . "<body>\n"; |
84 | 92 | ||
@@ -95,11 +103,13 @@ class WallabagEpub extends WallabagEBooks | |||
95 | $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL()); | 103 | $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL()); |
96 | $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL()); | 104 | $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL()); |
97 | 105 | ||
106 | Tools::logm('Filling metadata for ePub...'); | ||
107 | |||
98 | $book->setTitle($this->bookTitle); | 108 | $book->setTitle($this->bookTitle); |
99 | $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID. | 109 | $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. | 110 | //$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")); | 111 | $book->setDescription(_("Some articles saved on my wallabag")); |
102 | $book->setAuthor("wallabag", "wallabag"); | 112 | $book->setAuthor($this->author,$this->author); |
103 | $book->setPublisher("wallabag", "wallabag"); // I hope this is a non existant address :) | 113 | $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(). | 114 | $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. | 115 | //$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. |
@@ -123,6 +133,8 @@ class WallabagEpub extends WallabagEBooks | |||
123 | 133 | ||
124 | $book->buildTOC(); | 134 | $book->buildTOC(); |
125 | 135 | ||
136 | Tools::logm('Adding actual content...'); | ||
137 | |||
126 | foreach ($this->entries as $entry) { //set tags as subjects | 138 | foreach ($this->entries as $entry) { //set tags as subjects |
127 | $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']); | 139 | $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']); |
128 | foreach ($tags as $tag) { | 140 | foreach ($tags as $tag) { |
@@ -138,9 +150,11 @@ class WallabagEpub extends WallabagEBooks | |||
138 | 150 | ||
139 | if (DEBUG_POCHE) { | 151 | if (DEBUG_POCHE) { |
140 | $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation | 152 | $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation |
153 | Tools::logm('Production log available in produced file'); | ||
141 | } | 154 | } |
142 | $book->finalize(); | 155 | $book->finalize(); |
143 | $zipData = $book->sendBook($this->bookFileName); | 156 | $zipData = $book->sendBook($this->bookFileName); |
157 | Tools::logm('Ebook produced'); | ||
144 | } | 158 | } |
145 | } | 159 | } |
146 | 160 | ||
@@ -151,18 +165,19 @@ class WallabagMobi extends WallabagEBooks | |||
151 | * @author Sander Kromwijk | 165 | * @author Sander Kromwijk |
152 | */ | 166 | */ |
153 | 167 | ||
154 | private $_kindle_email; | ||
155 | |||
156 | public function produceMobi($sendByMail = FALSE) | 168 | public function produceMobi($sendByMail = FALSE) |
157 | { | 169 | { |
158 | 170 | ||
171 | Tools::logm('Starting to produce Mobi file'); | ||
159 | $mobi = new MOBI(); | 172 | $mobi = new MOBI(); |
160 | $content = new MOBIFile(); | 173 | $content = new MOBIFile(); |
161 | 174 | ||
162 | $messages = new Messages(); // for later | 175 | $messages = new Messages(); // for later |
163 | 176 | ||
177 | Tools::logm('Filling metadata for Mobi...'); | ||
178 | |||
164 | $content->set("title", $this->bookTitle); | 179 | $content->set("title", $this->bookTitle); |
165 | $content->set("author", "wallabag"); | 180 | $content->set("author", $this->author); |
166 | $content->set("subject", $this->bookTitle); | 181 | $content->set("subject", $this->bookTitle); |
167 | 182 | ||
168 | # introduction | 183 | # introduction |
@@ -170,6 +185,8 @@ class WallabagMobi extends WallabagEBooks | |||
170 | $content->appendImage(imagecreatefrompng("themes/baggy/img/apple-touch-icon-152.png")); | 185 | $content->appendImage(imagecreatefrompng("themes/baggy/img/apple-touch-icon-152.png")); |
171 | $content->appendPageBreak(); | 186 | $content->appendPageBreak(); |
172 | 187 | ||
188 | Tools::logm('Adding actual content...'); | ||
189 | |||
173 | foreach ($this->entries as $item) { | 190 | foreach ($this->entries as $item) { |
174 | $content->appendChapterTitle($item['title']); | 191 | $content->appendChapterTitle($item['title']); |
175 | $content->appendParagraph($item['content']); | 192 | $content->appendParagraph($item['content']); |
@@ -180,10 +197,19 @@ class WallabagMobi extends WallabagEBooks | |||
180 | if (!$sendByMail) { | 197 | if (!$sendByMail) { |
181 | // we offer file to download | 198 | // we offer file to download |
182 | $mobi->download($this->bookFileName.'.mobi'); | 199 | $mobi->download($this->bookFileName.'.mobi'); |
200 | Tools::logm('Mobi file produced'); | ||
183 | } | 201 | } |
184 | else { | 202 | else { |
185 | // we send file to kindle | 203 | // we send file to kindle |
186 | 204 | ||
205 | Tools::logm('Preparing to send file by email'); | ||
206 | |||
207 | $error = FALSE; | ||
208 | # testing Mail function | ||
209 | if (!function_exists('mail')) { | ||
210 | $error = _('Mail function is unavailable'); | ||
211 | } | ||
212 | |||
187 | $char_in = array('/', '.', ',', ':', '|'); # we sanitize filename to avoid conflicts with special characters (for instance, / goes for a directory) | 213 | $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'; | 214 | $mobiExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName)) . '.mobi'; |
189 | 215 | ||
@@ -216,15 +242,17 @@ class WallabagMobi extends WallabagEBooks | |||
216 | $header .= "--".$uid."--"; | 242 | $header .= "--".$uid."--"; |
217 | 243 | ||
218 | # trying to get the kindle email adress | 244 | # trying to get the kindle email adress |
219 | if ($this->wallabag->user->getConfigValue('kindleemail')) | 245 | if (!$this->wallabag->user->getConfigValue('kindleemail')) |
220 | { | 246 | { |
221 | #do a try...exeption here | 247 | $error = _('You didn\'t set your kindle\'s email adress !'); |
222 | mail( $this->wallabag->user->getConfigValue('kindleemail'), '[wallabag] ' . $this->bookTitle, "", $header ); | ||
223 | $messages->add('s', _('The email has been sent to your kindle !')); | ||
224 | } | 248 | } |
225 | else | 249 | if (!$error) { |
226 | { | 250 | mail($this->wallabag->user->getConfigValue('kindleemail'), '[wallabag] ' . $this->bookTitle, "", $header ); |
227 | $messages->add('e', _('You didn\'t set your kindle\'s email adress !')); | 251 | $messages->add('s', _('The email has been sent to your kindle !')); |
252 | Tools::logm('Email sent'); | ||
253 | } else { | ||
254 | $messages->add('e', $error); | ||
255 | Tools::logm($error); | ||
228 | } | 256 | } |
229 | } | 257 | } |
230 | } | 258 | } |
@@ -236,8 +264,15 @@ class WallabagPDF extends WallabagEbooks | |||
236 | { | 264 | { |
237 | $mpdf = new mPDF('c'); | 265 | $mpdf = new mPDF('c'); |
238 | 266 | ||
239 | # intro | 267 | Tools::logm('Starting to produce PDF file'); |
240 | 268 | ||
269 | Tools::logm('Filling metadata for PDF...'); | ||
270 | |||
271 | # headers | ||
272 | $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Produced with wallabag'); | ||
273 | $mpdf->SetFooter('{PAGENO}'); | ||
274 | |||
275 | # intro | ||
241 | $html = '<h1>' . $this->bookTitle . '<bookmark content="Cover" /></h1><div style="text-align:center;" > | 276 | $html = '<h1>' . $this->bookTitle . '<bookmark content="Cover" /></h1><div style="text-align:center;" > |
242 | <p>' . _('Produced by wallabag with mPDF') . '</p> | 277 | <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> | 278 | <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> |
@@ -245,6 +280,7 @@ class WallabagPDF extends WallabagEbooks | |||
245 | $html .= '<pagebreak type="next-odd" />'; | 280 | $html .= '<pagebreak type="next-odd" />'; |
246 | $i = 1; | 281 | $i = 1; |
247 | 282 | ||
283 | Tools::logm('Adding actual content...'); | ||
248 | foreach ($this->entries as $item) { | 284 | foreach ($this->entries as $item) { |
249 | $html .= '<h1>' . $item['title'] . '<bookmark content="' . $item['title'] . '" /></h1>'; | 285 | $html .= '<h1>' . $item['title'] . '<bookmark content="' . $item['title'] . '" /></h1>'; |
250 | $html .= '<indexentry content="'. $item['title'] .'" />'; | 286 | $html .= '<indexentry content="'. $item['title'] .'" />'; |
@@ -252,17 +288,12 @@ class WallabagPDF extends WallabagEbooks | |||
252 | $html .= '<pagebreak type="next-odd" />'; | 288 | $html .= '<pagebreak type="next-odd" />'; |
253 | $i = $i+1; | 289 | $i = $i+1; |
254 | } | 290 | } |
255 | |||
256 | |||
257 | # headers | ||
258 | $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Produced with wallabag'); | ||
259 | $mpdf->SetFooter('{PAGENO}'); | ||
260 | 291 | ||
261 | $mpdf->WriteHTML($html); | 292 | $mpdf->WriteHTML($html); |
262 | 293 | ||
263 | # remove characters that make mpdf bug | 294 | # remove characters that make mpdf bug |
264 | $char_in = array('/', '.', ',', ':', '|'); | 295 | $char_in = array('/', '.', ',', ':', '|'); |
265 | $pdfExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName . '.pdf')); | 296 | $pdfExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName . '.pdf')); # maybe someone can make a proper regex of this ? |
266 | 297 | ||
267 | # index | 298 | # index |
268 | $html = '<h2>Index<bookmark content="Index" /></h2> | 299 | $html = '<h2>Index<bookmark content="Index" /></h2> |
@@ -278,7 +309,6 @@ class WallabagPDF extends WallabagEbooks | |||
278 | header('Content-Transfer-Encoding: base64'); | 309 | header('Content-Transfer-Encoding: base64'); |
279 | header('Content-Type: application/pdf'); | 310 | header('Content-Type: application/pdf'); |
280 | echo file_get_contents('cache/' . $pdfExportName); | 311 | echo file_get_contents('cache/' . $pdfExportName); |
281 | 312 | Tools::logm('PDF file produced'); | |
282 | //exit; | ||
283 | } | 313 | } |
284 | } \ No newline at end of file | 314 | } \ No newline at end of file |