]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/Wallabag/Ebooks.php
new folders
[github/wallabag/wallabag.git] / src / Wallabag / Wallabag / Ebooks.php
CommitLineData
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
6ad93dff
NL
11namespace Wallabag\Wallabag;
12
13class Ebooks
2f26729c 14{
4188f38a 15 protected $wallabag;
2f26729c 16 protected $method;
2f26729c 17 protected $value;
4188f38a 18 protected $entries;
19 protected $bookTitle;
20 protected $bookFileName;
f8c37985 21 protected $author = 'wallabag';
2f26729c 22
6ad93dff 23 public function __construct(Wallabag $wallabag, $method, $value)
2f26729c
NL
24 {
25 $this->wallabag = $wallabag;
26 $this->method = $method;
2f26729c
NL
27 $this->value = $value;
28 }
29
4188f38a 30 public function prepareData()
2f26729c
NL
31 {
32 switch ($this->method) {
33 case 'id':
2b58426b 34 $entryID = filter_var($this->value, FILTER_SANITIZE_NUMBER_INT);
2f26729c 35 $entry = $this->wallabag->store->retrieveOneById($entryID, $this->wallabag->user->getId());
fb9df0c2 36 $this->entries = array($entry);
37 $this->bookTitle = $entry['title'];
38 $this->bookFileName = substr($this->bookTitle, 0, 200);
f8c37985 39 $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)
40 Tools::logm('Producing ebook from article ' . $this->bookTitle);
2f26729c
NL
41 break;
42 case 'all':
fb9df0c2 43 $this->entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId());
deab6280 44 $this->bookTitle = sprintf(_('All my articles on %s'), date(_('d.m.y'))); #translatable because each country has it's own date format system
fb9df0c2 45 $this->bookFileName = _('Allarticles') . date(_('dmY'));
f8c37985 46 Tools::logm('Producing ebook from all articles');
2f26729c
NL
47 break;
48 case 'tag':
49 $tag = filter_var($this->value, FILTER_SANITIZE_STRING);
50 $tags_id = $this->wallabag->store->retrieveAllTags($this->wallabag->user->getId(), $tag);
51 $tag_id = $tags_id[0]["id"]; // we take the first result, which is supposed to match perfectly. There must be a workaround.
fb9df0c2 52 $this->entries = $this->wallabag->store->retrieveEntriesByTag($tag_id, $this->wallabag->user->getId());
53 $this->bookTitle = sprintf(_('Articles tagged %s'), $tag);
54 $this->bookFileName = substr(sprintf(_('Tag %s'), $tag), 0, 200);
f8c37985 55 Tools::logm('Producing ebook from tag ' . $tag);
2f26729c
NL
56 break;
57 case 'category':
58 $category = filter_var($this->value, FILTER_SANITIZE_STRING);
fb9df0c2 59 $this->entries = $this->wallabag->store->getEntriesByView($category, $this->wallabag->user->getId());
ab86a512 60 $this->bookTitle = sprintf(_('Articles in category %s'), $category);
fb9df0c2 61 $this->bookFileName = substr(sprintf(_('Category %s'), $category), 0, 200);
f8c37985 62 Tools::logm('Producing ebook from category ' . $category);
2f26729c
NL
63 break;
64 case 'search':
65 $search = filter_var($this->value, FILTER_SANITIZE_STRING);
fb9df0c2 66 Tools::logm($search);
67 $this->entries = $this->wallabag->store->search($search, $this->wallabag->user->getId());
ab86a512 68 $this->bookTitle = sprintf(_('Articles for search %s'), $search);
fb9df0c2 69 $this->bookFileName = substr(sprintf(_('Search %s'), $search), 0, 200);
f8c37985 70 Tools::logm('Producing ebook from search ' . $search);
2f26729c
NL
71 break;
72 case 'default':
f8c37985 73 die(_('Uh, there is a problem while generating eBook.'));
2f26729c 74 }
4188f38a 75 }
76}
2f26729c 77
4188f38a 78class WallabagEpub extends WallabagEBooks
79{
80 /**
81 * handle ePub
82 */
83 public function produceEpub()
84 {
f8c37985 85 Tools::logm('Starting to produce ePub 3 file');
2f26729c
NL
86 $content_start =
87 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
88 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
89 . "<head>"
90 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
f8c37985 91 . "<title>" . _("wallabag articles book") . "</title>\n"
2f26729c
NL
92 . "</head>\n"
93 . "<body>\n";
94
95 $bookEnd = "</body>\n</html>\n";
96
97 $log = new Logger("wallabag", TRUE);
98 $fileDir = CACHE;
99
100 $book = new EPub(EPub::BOOK_VERSION_EPUB3, DEBUG_POCHE);
101 $log->logLine("new EPub()");
102 $log->logLine("EPub class version: " . EPub::VERSION);
103 $log->logLine("EPub Req. Zip version: " . EPub::REQ_ZIP_VERSION);
104 $log->logLine("Zip version: " . Zip::VERSION);
105 $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL());
106 $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL());
107
f8c37985 108 Tools::logm('Filling metadata for ePub...');
109
fb9df0c2 110 $book->setTitle($this->bookTitle);
2f26729c
NL
111 $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID.
112 //$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.
113 $book->setDescription(_("Some articles saved on my wallabag"));
f8c37985 114 $book->setAuthor($this->author,$this->author);
2f26729c
NL
115 $book->setPublisher("wallabag", "wallabag"); // I hope this is a non existant address :)
116 $book->setDate(time()); // Strictly not needed as the book date defaults to time().
117 //$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.
118 $book->setSourceURL("http://$_SERVER[HTTP_HOST]");
119
120 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
121 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "wallabag");
122
123 $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";
124
125 $log->logLine("Add Cover");
126
fb9df0c2 127 $fullTitle = "<h1> " . $this->bookTitle . "</h1>\n";
2f26729c 128
dc69d3e8 129 $book->setCoverImage("Cover.png", file_get_contents("themes/_global/img/appicon/apple-touch-icon-152.png"), "image/png", $fullTitle);
2f26729c
NL
130
131 $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;
132
133 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
134 $book->addChapter("Notices", "Cover2.html", $cover);
135
136 $book->buildTOC();
137
f8c37985 138 Tools::logm('Adding actual content...');
139
fb9df0c2 140 foreach ($this->entries as $entry) { //set tags as subjects
2f26729c
NL
141 $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']);
142 foreach ($tags as $tag) {
143 $book->setSubject($tag['value']);
144 }
145
146 $log->logLine("Set up parameters");
147
148 $chapter = $content_start . $entry['content'] . $bookEnd;
149 $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub::EXTERNAL_REF_ADD);
150 $log->logLine("Added chapter " . $entry['title']);
151 }
152
153 if (DEBUG_POCHE) {
154 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
f8c37985 155 Tools::logm('Production log available in produced file');
2f26729c
NL
156 }
157 $book->finalize();
fb9df0c2 158 $zipData = $book->sendBook($this->bookFileName);
f8c37985 159 Tools::logm('Ebook produced');
2f26729c 160 }
4188f38a 161}
162
163class WallabagMobi extends WallabagEBooks
164{
165 /**
fb9df0c2 166 * MOBI Class
167 * @author Sander Kromwijk
4188f38a 168 */
169
9f86454b 170 public function produceMobi()
4188f38a 171 {
4188f38a 172
f8c37985 173 Tools::logm('Starting to produce Mobi file');
fb9df0c2 174 $mobi = new MOBI();
fb9df0c2 175 $content = new MOBIFile();
dc59f164 176
177 $messages = new Messages(); // for later
f8c37985 178
179 Tools::logm('Filling metadata for Mobi...');
180
fb9df0c2 181 $content->set("title", $this->bookTitle);
f8c37985 182 $content->set("author", $this->author);
deab6280 183 $content->set("subject", $this->bookTitle);
fb9df0c2 184
185 # introduction
deab6280 186 $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>');
dc69d3e8 187 $content->appendImage(imagecreatefrompng("themes/_global/img/appicon/apple-touch-icon-152.png"));
fb9df0c2 188 $content->appendPageBreak();
189
f8c37985 190 Tools::logm('Adding actual content...');
191
fb9df0c2 192 foreach ($this->entries as $item) {
193 $content->appendChapterTitle($item['title']);
194 $content->appendParagraph($item['content']);
195 $content->appendPageBreak();
4188f38a 196 }
fb9df0c2 197 $mobi->setContentProvider($content);
4188f38a 198
9f86454b 199 // we offer file to download
200 $mobi->download($this->bookFileName.'.mobi');
201 Tools::logm('Mobi file produced');
fb9df0c2 202 }
4188f38a 203}
204
205class WallabagPDF extends WallabagEbooks
206{
207 public function producePDF()
208 {
4188f38a 209
f8c37985 210 Tools::logm('Starting to produce PDF file');
fb9df0c2 211
824f8c45 212 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
f8c37985 213
824f8c45 214 Tools::logm('Filling metadata for PDF...');
215 $pdf->SetCreator(PDF_CREATOR);
216 $pdf->SetAuthor('');
217 $pdf->SetTitle($this->bookTitle);
3329f1bf
NL
218 $pdf->SetSubject($this->bookTitle);
219
824f8c45 220 Tools::logm('Adding introduction...');
221 $pdf->AddPage();
222 $intro = '<h1>' . $this->bookTitle . '</h1><div style="text-align:center;" >
223 <p>' . _('Produced by wallabag with tcpdf') . '</p>
deab6280 224 <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>
dc69d3e8 225 <img src="themes/_global/img/appicon/apple-touch-icon-152.png" /></div>';
fb9df0c2 226
824f8c45 227
228 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
229
230 $i = 1;
f8c37985 231 Tools::logm('Adding actual content...');
fb9df0c2 232 foreach ($this->entries as $item) {
824f8c45 233 $pdf->AddPage();
234 $html = '<h1>' . $item['title'] . '</h1>';
fb9df0c2 235 $html .= $item['content'];
824f8c45 236 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
deab6280 237 $i = $i+1;
fb9df0c2 238 }
fb9df0c2 239
824f8c45 240 // set image scale factor
241 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
824f8c45 242
deab6280 243
3329f1bf 244 $pdf->Output(CACHE . '/' . $this->bookFileName . '.pdf', 'FD');
fb9df0c2 245
4188f38a 246 }
dc69d3e8 247}