]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/WallabagEBooks.class.php
remove unnecessary lines (again)
[github/wallabag/wallabag.git] / inc / poche / WallabagEBooks.class.php
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
11 class WallabagEBooks
12 {
13 protected $wallabag;
14 protected $method;
15 protected $value;
16 protected $entries;
17 protected $bookTitle;
18 protected $bookFileName;
19 protected $author = 'wallabag';
20
21 public function __construct(Poche $wallabag, $method, $value)
22 {
23 $this->wallabag = $wallabag;
24 $this->method = $method;
25 $this->value = $value;
26 }
27
28 public function prepareData()
29 {
30 switch ($this->method) {
31 case 'id':
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 = str_replace('/', '_', 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);
39 break;
40 case 'all':
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');
45 break;
46 case 'tag':
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);
54 break;
55 case 'category':
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);
61 break;
62 case 'search':
63 $search = filter_var($this->value, FILTER_SANITIZE_STRING);
64 Tools::logm($search);
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);
69 break;
70 case 'default':
71 die(_('Uh, there is a problem while generating eBook.'));
72 }
73 }
74 }
75
76 class WallabagEpub extends WallabagEBooks
77 {
78 /**
79 * handle ePub
80 */
81 public function produceEpub()
82 {
83 Tools::logm('Starting to produce ePub 3 file');
84
85 try {
86
87 $content_start =
88 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
89 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
90 . "<head>"
91 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
92 . "<title>" . _("wallabag articles book") . "</title>\n"
93 . "</head>\n"
94 . "<body>\n";
95
96 $bookEnd = "</body>\n</html>\n";
97
98 $log = new Logger("wallabag", TRUE);
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
108 Tools::logm('Filling metadata for ePub...');
109
110 $book->setTitle($this->bookTitle);
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"));
114 $book->setAuthor($this->author,$this->author);
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 /*
124 * Line not used but planned to be used
125 *
126 *$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";
127 */
128
129 $log->logLine("Add Cover");
130
131 $fullTitle = "<h1> " . $this->bookTitle . "</h1>\n";
132
133 $book->setCoverImage("Cover.png", file_get_contents("themes/_global/img/appicon/apple-touch-icon-152.png"), "image/png", $fullTitle);
134
135 $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;
136
137 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
138 $book->addChapter("Notices", "Cover2.html", $cover);
139
140 $book->buildTOC();
141
142 Tools::logm('Adding actual content...');
143
144 foreach ($this->entries as $entry) { //set tags as subjects
145 $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']);
146 foreach ($tags as $tag) {
147 $book->setSubject($tag['value']);
148 }
149
150 $log->logLine("Set up parameters");
151
152 $chapter = $content_start . $entry['content'] . $bookEnd;
153 $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub::EXTERNAL_REF_ADD);
154 $log->logLine("Added chapter " . $entry['title']);
155 }
156
157 if (DEBUG_POCHE) {
158 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
159 Tools::logm('Production log available in produced file');
160 }
161 $book->finalize();
162 $zipData = $book->sendBook($this->bookFileName);
163 Tools::logm('Ebook produced');
164 }
165 catch (Exception $e) {
166 Tools::logm('PHPePub has encountered an error : '.$e->getMessage());
167 $this->wallabag->messages->add('e', $e->getMessage());
168 }
169 }
170 }
171
172 class WallabagMobi extends WallabagEBooks
173 {
174 /**
175 * MOBI Class
176 * @author Sander Kromwijk
177 */
178
179 public function produceMobi()
180 {
181 try {
182 Tools::logm('Starting to produce Mobi file');
183 $mobi = new MOBI();
184 $content = new MOBIFile();
185
186 Tools::logm('Filling metadata for Mobi...');
187
188 $content->set("title", $this->bookTitle);
189 $content->set("author", $this->author);
190 $content->set("subject", $this->bookTitle);
191
192 # introduction
193 $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>');
194 $content->appendImage(imagecreatefrompng("themes/_global/img/appicon/apple-touch-icon-152.png"));
195 $content->appendPageBreak();
196
197 Tools::logm('Adding actual content...');
198
199 foreach ($this->entries as $item) {
200 $content->appendChapterTitle($item['title']);
201 $content->appendParagraph($item['content']);
202 $content->appendPageBreak();
203 }
204 $mobi->setContentProvider($content);
205
206 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
207 $this->bookFileName = preg_replace('/[^A-Za-z0-9\-]/', '', $this->bookFileName);
208
209 // we offer file to download
210 $mobi->download($this->bookFileName.'.mobi');
211 Tools::logm('Mobi file produced');
212 }
213 catch (Exception $e) {
214 Tools::logm('PHPMobi has encountered an error : '.$e->getMessage());
215 $this->wallabag->messages->add('e', $e->getMessage());
216 }
217 }
218 }
219
220 class WallabagPDF extends WallabagEbooks
221 {
222 public function producePDF()
223 {
224
225 Tools::logm('Starting to produce PDF file');
226 @define ('K_TCPDF_THROW_EXCEPTION_ERROR', TRUE);
227 try {
228 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
229
230 Tools::logm('Filling metadata for PDF...');
231 $pdf->SetCreator(PDF_CREATOR);
232 $pdf->SetAuthor('wallabag');
233 $pdf->SetTitle($this->bookTitle);
234 $pdf->SetSubject('Articles via wallabag');
235 $pdf->SetKeywords('wallabag');
236
237 Tools::logm('Adding introduction...');
238 $pdf->AddPage();
239 $intro = '<h1>' . $this->bookTitle . '</h1><div style="text-align:center;" >
240 <p>' . _('Produced by wallabag with tcpdf') . '</p>
241 <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>
242 <img src="themes/_global/img/appicon/apple-touch-icon-152.png" /></div>';
243
244
245 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
246
247 Tools::logm('Adding actual content...');
248 foreach ($this->entries as $item) {
249 $tags = $this->wallabag->store->retrieveTagsByEntry($item['id']);
250 foreach ($tags as $tag) {
251 $pdf->SetKeywords($tag['value']);
252 }
253 $pdf->AddPage();
254 $html = '<h1>' . $item['title'] . '</h1>';
255 $html .= $item['content'];
256 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
257 }
258
259 // set image scale factor
260 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
261
262
263 $pdf->Output('cache/' . $this->bookFileName . '.pdf', 'FD');
264 }
265 catch (Exception $e) {
266 Tools::logm('TCPDF has encountered an error : '.$e->getMessage());
267 $this->wallabag->messages->add('e', $e->getMessage());
268 }
269
270 }
271 }