]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/WallabagEBooks.class.php
updated mpdf to 5.7.2
[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;
2f26729c 19
2b58426b 20 public function __construct(Poche $wallabag, $method, $value)
2f26729c
NL
21 {
22 $this->wallabag = $wallabag;
23 $this->method = $method;
2f26729c
NL
24 $this->value = $value;
25 }
26
4188f38a 27 public function prepareData()
2f26729c
NL
28 {
29 switch ($this->method) {
30 case 'id':
2b58426b 31 $entryID = filter_var($this->value, FILTER_SANITIZE_NUMBER_INT);
2f26729c 32 $entry = $this->wallabag->store->retrieveOneById($entryID, $this->wallabag->user->getId());
fb9df0c2 33 $this->entries = array($entry);
34 $this->bookTitle = $entry['title'];
35 $this->bookFileName = substr($this->bookTitle, 0, 200);
2f26729c
NL
36 break;
37 case 'all':
fb9df0c2 38 $this->entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId());
39 $this->bookTitle = sprintf(_('All my articles on '), date(_('d.m.y'))); #translatable because each country has it's own date format system
40 $this->bookFileName = _('Allarticles') . date(_('dmY'));
2f26729c
NL
41 break;
42 case 'tag':
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.
fb9df0c2 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);
2f26729c
NL
49 break;
50 case 'category':
51 $category = filter_var($this->value, FILTER_SANITIZE_STRING);
fb9df0c2 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);
2f26729c
NL
55 break;
56 case 'search':
57 $search = filter_var($this->value, FILTER_SANITIZE_STRING);
fb9df0c2 58 Tools::logm($search);
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);
2f26729c
NL
62 break;
63 case 'default':
64 die(_('Uh, there is a problem while generating epub.'));
65 }
4188f38a 66 }
67}
2f26729c 68
4188f38a 69class WallabagEpub extends WallabagEBooks
70{
71 /**
72 * handle ePub
73 */
74 public function produceEpub()
75 {
2f26729c
NL
76 $content_start =
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"
79 . "<head>"
80 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
81 . "<title>wallabag articles book</title>\n"
82 . "</head>\n"
83 . "<body>\n";
84
85 $bookEnd = "</body>\n</html>\n";
86
87 $log = new Logger("wallabag", TRUE);
88 $fileDir = CACHE;
89
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());
97
fb9df0c2 98 $book->setTitle($this->bookTitle);
2f26729c
NL
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]");
107
108 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
109 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "wallabag");
110
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";
112
113 $log->logLine("Add Cover");
114
fb9df0c2 115 $fullTitle = "<h1> " . $this->bookTitle . "</h1>\n";
2f26729c
NL
116
117 $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle);
118
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;
120
121 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
122 $book->addChapter("Notices", "Cover2.html", $cover);
123
124 $book->buildTOC();
125
fb9df0c2 126 foreach ($this->entries as $entry) { //set tags as subjects
2f26729c
NL
127 $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']);
128 foreach ($tags as $tag) {
129 $book->setSubject($tag['value']);
130 }
131
132 $log->logLine("Set up parameters");
133
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']);
137 }
138
139 if (DEBUG_POCHE) {
140 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
141 }
142 $book->finalize();
fb9df0c2 143 $zipData = $book->sendBook($this->bookFileName);
2f26729c 144 }
4188f38a 145}
146
147class WallabagMobi extends WallabagEBooks
148{
149 /**
fb9df0c2 150 * MOBI Class
151 * @author Sander Kromwijk
4188f38a 152 */
153
fb9df0c2 154 public function produceMobi($send = FALSE)
4188f38a 155 {
4188f38a 156
fb9df0c2 157 # Good try
158 $mobi = new MOBI();
159
160 $content = new MOBIFile();
161
162 $content->set("title", $this->bookTitle);
163 $content->set("author", "wallabag");
164
165 # introduction
166 //$content->appendChapterTitle("Cover");
167 $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>');
168 $content->appendImage(imagecreatefrompng("themes/baggy/img/apple-touch-icon-152.png"));
169 $content->appendPageBreak();
170
171 foreach ($this->entries as $item) {
172 $content->appendChapterTitle($item['title']);
173 $content->appendParagraph($item['content']);
174 $content->appendPageBreak();
4188f38a 175 }
fb9df0c2 176 $mobi->setContentProvider($content);
4188f38a 177
fb9df0c2 178 $mobi->download($this->bookFileName.".mobi");
179 }
4188f38a 180}
181
182class WallabagPDF extends WallabagEbooks
183{
184 public function producePDF()
185 {
fb9df0c2 186 //$this->prepareData();
4188f38a 187 $mpdf = new mPDF('c');
188
fb9df0c2 189 # intro
190
191 $html = '<h1>' . $this->bookTitle . '</h1><img src="themes/baggy/img/apple-touch-icon-152.png" />';
192
193 foreach ($this->entries as $item) {
194 $html .= '<h1>' . $item['title'] . '</h1>';
195 $html .= '<indexentry content="'. $item['title'] .'" />';
196 $html .= $item['content'];
197 }
198
199 //$mpdf->h2toc = array('H1'=>0);
200
201 # headers
202 $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Produced with wallabag');
203 $mpdf->SetFooter('{PAGENO}');
204
205 $mpdf->WriteHTML($html);
206
207 # remove characters that make mpdf bug
208 $char_in = array('/', '.', ',', ':', '|');
209 $pdfExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName . '.pdf'));
210
211 $mpdf->Output('cache/' . $pdfExportName);
212
213 header('Content-Disposition: attachment; filename="' . $pdfExportName . '"');
214
215 header('Content-Transfer-Encoding: base64');
216 header('Content-Type: application/pdf');
217 echo file_get_contents('cache/' . $pdfExportName);
218
219 //exit;
4188f38a 220 }
221}