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;
19 protected $author = 'wallabag';
21 public function __construct(Poche
$wallabag, $method, $value)
23 $this->wallabag
= $wallabag;
24 $this->method
= $method;
25 $this->value
= $value;
28 public function prepareData()
30 switch ($this->method
) {
32 $entryID = filter_var($this->value
, FILTER_SANITIZE_NUMBER_INT
);
33 $entry = $this->wallabag
->store
->retrieveOneById($entryID, $this->wallabag
->user
->getId());
34 $this->entries
= array($entry);
35 $this->bookTitle
= $entry['title'];
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
);
41 $this->entries
= $this->wallabag
->store
->retrieveAll($this->wallabag
->user
->getId());
42 $this->bookTitle
= sprintf(_('All my articles on %s'), date(_('d.m.y'))); #translatable because each country has it's own date format system
43 $this->bookFileName
= _('Allarticles') . date(_('dmY'));
44 Tools
::logm('Producing ebook from all articles');
47 $tag = filter_var($this->value
, FILTER_SANITIZE_STRING
);
48 $tags_id = $this->wallabag
->store
->retrieveAllTags($this->wallabag
->user
->getId(), $tag);
49 $tag_id = $tags_id[0]["id"]; // we take the first result, which is supposed to match perfectly. There must be a workaround.
50 $this->entries
= $this->wallabag
->store
->retrieveEntriesByTag($tag_id, $this->wallabag
->user
->getId());
51 $this->bookTitle
= sprintf(_('Articles tagged %s'), $tag);
52 $this->bookFileName
= substr(sprintf(_('Tag %s'), $tag), 0, 200);
53 Tools
::logm('Producing ebook from tag ' . $tag);
56 $category = filter_var($this->value
, FILTER_SANITIZE_STRING
);
57 $this->entries
= $this->wallabag
->store
->getEntriesByView($category, $this->wallabag
->user
->getId());
58 $this->bookTitle
= sprintf(_('Articles in category %s'), $category);
59 $this->bookFileName
= substr(sprintf(_('Category %s'), $category), 0, 200);
60 Tools
::logm('Producing ebook from category ' . $category);
63 $search = filter_var($this->value
, FILTER_SANITIZE_STRING
);
65 $this->entries
= $this->wallabag
->store
->search($search, $this->wallabag
->user
->getId());
66 $this->bookTitle
= sprintf(_('Articles for search %s'), $search);
67 $this->bookFileName
= substr(sprintf(_('Search %s'), $search), 0, 200);
68 Tools
::logm('Producing ebook from search ' . $search);
71 die(_('Uh, there is a problem while generating eBook.'));
76 class WallabagEpub
extends WallabagEBooks
81 public function produceEpub()
83 Tools
::logm('Starting to produce ePub 3 file');
85 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
86 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
88 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
89 . "<title>" . _("wallabag articles book") . "</title>\n"
93 $bookEnd = "</body>\n</html>\n";
95 $log = new Logger("wallabag", TRUE);
98 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
, DEBUG_POCHE
);
99 $log->logLine("new EPub()");
100 $log->logLine("EPub class version: " . EPub
::VERSION
);
101 $log->logLine("EPub Req. Zip version: " . EPub
::REQ_ZIP_VERSION
);
102 $log->logLine("Zip version: " . Zip
::VERSION
);
103 $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL());
104 $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL());
106 Tools
::logm('Filling metadata for ePub...');
108 $book->setTitle($this->bookTitle
);
109 $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub
::IDENTIFIER_URI
); // Could also be the ISBN number, prefered for published books, or a UUID.
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.
111 $book->setDescription(_("Some articles saved on my wallabag"));
112 $book->setAuthor($this->author
,$this->author
);
113 $book->setPublisher("wallabag", "wallabag"); // I hope this is a non existant address :)
114 $book->setDate(time()); // Strictly not needed as the book date defaults to time().
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.
116 $book->setSourceURL("http://$_SERVER[HTTP_HOST]");
118 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, "PHP");
119 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, "wallabag");
121 $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";
123 $log->logLine("Add Cover");
125 $fullTitle = "<h1> " . $this->bookTitle
. "</h1>\n";
127 $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle);
129 $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;
131 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
132 $book->addChapter("Notices", "Cover2.html", $cover);
136 Tools
::logm('Adding actual content...');
138 foreach ($this->entries
as $entry) { //set tags as subjects
139 $tags = $this->wallabag
->store
->retrieveTagsByEntry($entry['id']);
140 foreach ($tags as $tag) {
141 $book->setSubject($tag['value']);
144 $log->logLine("Set up parameters");
146 $chapter = $content_start . $entry['content'] . $bookEnd;
147 $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub
::EXTERNAL_REF_ADD
);
148 $log->logLine("Added chapter " . $entry['title']);
152 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
153 Tools
::logm('Production log available in produced file');
156 $zipData = $book->sendBook($this->bookFileName
);
157 Tools
::logm('Ebook produced');
161 class WallabagMobi
extends WallabagEBooks
165 * @author Sander Kromwijk
168 public function produceMobi($sendByMail = FALSE)
171 Tools
::logm('Starting to produce Mobi file');
173 $content = new MOBIFile();
175 $messages = new Messages(); // for later
177 Tools
::logm('Filling metadata for Mobi...');
179 $content->set("title", $this->bookTitle
);
180 $content->set("author", $this->author
);
181 $content->set("subject", $this->bookTitle
);
184 $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>');
185 $content->appendImage(imagecreatefrompng("themes/baggy/img/apple-touch-icon-152.png"));
186 $content->appendPageBreak();
188 Tools
::logm('Adding actual content...');
190 foreach ($this->entries
as $item) {
191 $content->appendChapterTitle($item['title']);
192 $content->appendParagraph($item['content']);
193 $content->appendPageBreak();
195 $mobi->setContentProvider($content);
198 // we offer file to download
199 $mobi->download($this->bookFileName
.'.mobi');
200 Tools
::logm('Mobi file produced');
204 # Not working yet !!! #
206 // we send file to kindle
208 Tools
::logm('Preparing to send file by email');
211 # testing Mail function
212 if (!function_exists('mail')) {
213 $error = _("Mail function is disabled. You can't send emails from your server");
216 $char_in = array('/', '.', ',', ':', '|'); # we sanitize filename to avoid conflicts with special characters (for instance, / goes for a directory)
217 $mobiExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName
)) . '.mobi';
219 $file = 'cache/' . $mobiExportName;
222 $file_size = filesize($file);
223 $filename = basename($file);
224 $handle = fopen($file, "r");
225 $content = fread($handle, $file_size);
227 $content = chunk_split(base64_encode($content));
229 $uid = md5(uniqid(time()));
231 //generate header for mail
232 $header = "From: wallabag <". $this->wallabag
->user
->email
.">\r\n";
233 $header .= "MIME-Version: 1.0\r\n";
234 $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
235 $header .= "This is a multi-part message in MIME format.\r\n";
236 $header .= "--".$uid."\r\n";
237 $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
238 $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
239 $header .= "send via wallabag\r\n\r\n";
240 $header .= "--".$uid."\r\n";
241 $header .= "Content-Type: application/x-mobipocket-ebook; name=\"".$filename."\"\r\n";
242 $header .= "Content-Transfer-Encoding: base64\r\n";
243 $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
244 $header .= $content."\r\n\r\n";
245 $header .= "--".$uid."--";
247 # trying to get the kindle email adress
248 if (!$this->wallabag
->user
->getConfigValue('kindleemail'))
250 $error = _("You didn't set your kindle's email adress !");
253 mail($this->wallabag
->user
->getConfigValue('kindleemail'), '[wallabag] ' . $this->bookTitle
, "", $header );
254 $messages->add('s', _('The email has been sent to your kindle !'));
255 Tools
::logm('Email sent');
257 $messages->add('e', $error);
264 class WallabagPDF
extends WallabagEbooks
266 public function producePDF()
269 Tools
::logm('Starting to produce PDF file');
271 $pdf = new TCPDF(PDF_PAGE_ORIENTATION
, PDF_UNIT
, PDF_PAGE_FORMAT
, true, 'UTF-8', false);
273 Tools
::logm('Filling metadata for PDF...');
274 $pdf->SetCreator(PDF_CREATOR
);
276 $pdf->SetTitle($this->bookTitle
);
277 $pdf->SetSubject('TCPDF Tutorial');
278 $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
280 Tools
::logm('Adding introduction...');
282 $intro = '<h1>' . $this->bookTitle
. '</h1><div style="text-align:center;" >
283 <p>' . _('Produced by wallabag with tcpdf') . '</p>
284 <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>
285 <img src="themes/baggy/img/apple-touch-icon-152.png" /></div>';
288 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
291 Tools
::logm('Adding actual content...');
292 foreach ($this->entries
as $item) {
294 $html = '<h1>' . $item['title'] . '</h1>';
295 $html .= $item['content'];
296 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
300 // set image scale factor
301 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO
);
304 $pdf->Output($this->bookFileName
. '.pdf', 'FD');