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