]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/WallabagEBooks.class.php
mobi (not tested on actual device) and pdf working
[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());
deab6280 39 $this->bookTitle = sprintf(_('All my articles on %s'), date(_('d.m.y'))); #translatable because each country has it's own date format system
fb9df0c2 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 $mobi = new MOBI();
158
159 $content = new MOBIFile();
160
161 $content->set("title", $this->bookTitle);
162 $content->set("author", "wallabag");
deab6280 163 $content->set("subject", $this->bookTitle);
fb9df0c2 164
165 # introduction
166 //$content->appendChapterTitle("Cover");
deab6280 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>');
fb9df0c2 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 {
186 $mpdf = new mPDF('c');
187
fb9df0c2 188 # intro
189
deab6280 190 $html = '<h1>' . $this->bookTitle . '<bookmark content="Cover" /></h1><div style="text-align:center;" >
191 <p>' . _('Produced by wallabag with mPDF') . '</p>
192 <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>
193 <img src="themes/baggy/img/apple-touch-icon-152.png" /></div>';
194 $html .= '<pagebreak type="next-odd" />';
195 $i = 1;
fb9df0c2 196
197 foreach ($this->entries as $item) {
deab6280 198 $html .= '<h1>' . $item['title'] . '<bookmark content="' . $item['title'] . '" /></h1>';
fb9df0c2 199 $html .= '<indexentry content="'. $item['title'] .'" />';
200 $html .= $item['content'];
deab6280 201 $html .= '<pagebreak type="next-odd" />';
202 $i = $i+1;
fb9df0c2 203 }
204
fb9df0c2 205
206 # headers
207 $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Produced with wallabag');
208 $mpdf->SetFooter('{PAGENO}');
209
210 $mpdf->WriteHTML($html);
211
212 # remove characters that make mpdf bug
213 $char_in = array('/', '.', ',', ':', '|');
214 $pdfExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName . '.pdf'));
deab6280 215
216 # index
217 $html = '<h2>Index<bookmark content="Index" /></h2>
218 <indexinsert cols="2" offset="5" usedivletters="on" div-font-size="15" gap="5" font="Trebuchet" div-font="sans-serif" links="on" />
219 ';
220
221 $mpdf->WriteHTML($html);
fb9df0c2 222
223 $mpdf->Output('cache/' . $pdfExportName);
224
225 header('Content-Disposition: attachment; filename="' . $pdfExportName . '"');
226
227 header('Content-Transfer-Encoding: base64');
228 header('Content-Type: application/pdf');
229 echo file_get_contents('cache/' . $pdfExportName);
230
231 //exit;
4188f38a 232 }
233}