3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
8 * @license http://opensource.org/licenses/MIT see COPYING file
18 protected $bookFileName;
20 public function __construct(Poche
$wallabag, $method, $value)
22 $this->wallabag
= $wallabag;
23 $this->method
= $method;
24 $this->value
= $value;
27 public function prepareData()
29 switch ($this->method
) {
31 $entryID = filter_var($this->value
, FILTER_SANITIZE_NUMBER_INT
);
32 $entry = $this->wallabag
->store
->retrieveOneById($entryID, $this->wallabag
->user
->getId());
33 $this->entries
= array($entry);
34 $this->bookTitle
= $entry['title'];
35 $this->bookFileName
= substr($this->bookTitle
, 0, 200);
38 $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
40 $this->bookFileName
= _('Allarticles') . date(_('dmY'));
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.
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);
51 $category = filter_var($this->value
, FILTER_SANITIZE_STRING
);
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);
57 $search = filter_var($this->value
, FILTER_SANITIZE_STRING
);
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);
64 die(_('Uh, there is a problem while generating epub.'));
69 class WallabagEpub
extends WallabagEBooks
74 public function produceEpub()
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"
80 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
81 . "<title>wallabag articles book</title>\n"
85 $bookEnd = "</body>\n</html>\n";
87 $log = new Logger("wallabag", TRUE);
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());
98 $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.
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]");
108 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, "PHP");
109 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, "wallabag");
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";
113 $log->logLine("Add Cover");
115 $fullTitle = "<h1> " . $this->bookTitle
. "</h1>\n";
117 $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle);
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;
121 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
122 $book->addChapter("Notices", "Cover2.html", $cover);
126 foreach ($this->entries
as $entry) { //set tags as subjects
127 $tags = $this->wallabag
->store
->retrieveTagsByEntry($entry['id']);
128 foreach ($tags as $tag) {
129 $book->setSubject($tag['value']);
132 $log->logLine("Set up parameters");
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']);
140 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
143 $zipData = $book->sendBook($this->bookFileName
);
147 class WallabagMobi
extends WallabagEBooks
151 * @author Sander Kromwijk
154 private $_kindle_email;
156 public function produceMobi($sendByMail = FALSE)
160 $content = new MOBIFile();
162 $messages = new Messages(); // for later
164 $content->set("title", $this->bookTitle
);
165 $content->set("author", "wallabag");
166 $content->set("subject", $this->bookTitle
);
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>');
170 $content->appendImage(imagecreatefrompng("themes/baggy/img/apple-touch-icon-152.png"));
171 $content->appendPageBreak();
173 foreach ($this->entries
as $item) {
174 $content->appendChapterTitle($item['title']);
175 $content->appendParagraph($item['content']);
176 $content->appendPageBreak();
178 $mobi->setContentProvider($content);
181 // we offer file to download
182 $mobi->download($this->bookFileName
.'.mobi');
185 // we send file to kindle
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';
190 $file = 'cache/' . $mobiExportName;
193 $file_size = filesize($file);
194 $filename = basename($file);
195 $handle = fopen($file, "r");
196 $content = fread($handle, $file_size);
198 $content = chunk_split(base64_encode($content));
200 $uid = md5(uniqid(time()));
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."--";
218 # trying to get the kindle email adress
219 if ($this->wallabag
->user
->getConfigValue('kindleemail'))
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 !'));
227 $messages->add('e', _('You didn\'t set your kindle\'s email adress !'));
233 class WallabagPDF
extends WallabagEbooks
235 public function producePDF()
237 $mpdf = new mPDF('c');
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" />';
248 foreach ($this->entries
as $item) {
249 $html .= '<h1>' . $item['title'] . '<bookmark content="' . $item['title'] . '" /></h1>';
250 $html .= '<indexentry content="'. $item['title'] .'" />';
251 $html .= $item['content'];
252 $html .= '<pagebreak type="next-odd" />';
258 $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Produced with wallabag');
259 $mpdf->SetFooter('{PAGENO}');
261 $mpdf->WriteHTML($html);
263 # remove characters that make mpdf bug
264 $char_in = array('/', '.', ',', ':', '|');
265 $pdfExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName
. '.pdf'));
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" />
272 $mpdf->WriteHTML($html);
274 $mpdf->Output('cache/' . $pdfExportName);
276 header('Content-Disposition: attachment; filename="' . $pdfExportName . '"');
278 header('Content-Transfer-Encoding: base64');
279 header('Content-Type: application/pdf');
280 echo file_get_contents('cache/' . $pdfExportName);