]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/WallabagEBooks.class.php
5f42308ebd108d88ebb40ecd485acf8aba7cf048
[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
20 public function __construct(Poche $wallabag, $method, $value)
21 {
22 $this->wallabag = $wallabag;
23 $this->method = $method;
24 $this->value = $value;
25 }
26
27 public function prepareData()
28 {
29 switch ($this->method) {
30 case 'id':
31 $entryID = filter_var($this->value, FILTER_SANITIZE_NUMBER_INT);
32 $entry = $this->wallabag->store->retrieveOneById($entryID, $this->wallabag->user->getId());
33 $this->entries = array($entry);
34 $this->bookTitle = $entry['title'];
35 $this->bookFileName = substr($this->bookTitle, 0, 200);
36 break;
37 case 'all':
38 $this->entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId());
39 $this->bookTitle = sprintf(_('All my articles on %s'), date(_('d.m.y'))); #translatable because each country has it's own date format system
40 $this->bookFileName = _('Allarticles') . date(_('dmY'));
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.
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);
49 break;
50 case 'category':
51 $category = filter_var($this->value, FILTER_SANITIZE_STRING);
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);
55 break;
56 case 'search':
57 $search = filter_var($this->value, FILTER_SANITIZE_STRING);
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);
62 break;
63 case 'default':
64 die(_('Uh, there is a problem while generating epub.'));
65 }
66 }
67 }
68
69 class WallabagEpub extends WallabagEBooks
70 {
71 /**
72 * handle ePub
73 */
74 public function produceEpub()
75 {
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
98 $book->setTitle($this->bookTitle);
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
115 $fullTitle = "<h1> " . $this->bookTitle . "</h1>\n";
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
126 foreach ($this->entries as $entry) { //set tags as subjects
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();
143 $zipData = $book->sendBook($this->bookFileName);
144 }
145 }
146
147 class WallabagMobi extends WallabagEBooks
148 {
149 /**
150 * MOBI Class
151 * @author Sander Kromwijk
152 */
153
154 private $_kindle_email;
155
156 public function produceMobi($sendByMail = FALSE)
157 {
158
159 $mobi = new MOBI();
160 $content = new MOBIFile();
161
162 $messages = new Messages(); // for later
163
164 $content->set("title", $this->bookTitle);
165 $content->set("author", "wallabag");
166 $content->set("subject", $this->bookTitle);
167
168 # introduction
169 $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>');
170 $content->appendImage(imagecreatefrompng("themes/baggy/img/apple-touch-icon-152.png"));
171 $content->appendPageBreak();
172
173 foreach ($this->entries as $item) {
174 $content->appendChapterTitle($item['title']);
175 $content->appendParagraph($item['content']);
176 $content->appendPageBreak();
177 }
178 $mobi->setContentProvider($content);
179
180 if (!$sendByMail) {
181 // we offer file to download
182 $mobi->download($this->bookFileName.'.mobi');
183 }
184 else {
185 // we send file to kindle
186
187 $char_in = array('/', '.', ',', ':', '|'); # we sanitize filename to avoid conflicts with special characters (for instance, / goes for a directory)
188 $mobiExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName)) . '.mobi';
189
190 $file = 'cache/' . $mobiExportName;
191 $mobi->save($file);
192
193 $file_size = filesize($file);
194 $filename = basename($file);
195 $handle = fopen($file, "r");
196 $content = fread($handle, $file_size);
197 fclose($handle);
198 $content = chunk_split(base64_encode($content));
199
200 $uid = md5(uniqid(time()));
201
202 //generate header for mail
203 $header = "From: wallabag <". $this->wallabag->user->email .">\r\n";
204 $header .= "MIME-Version: 1.0\r\n";
205 $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
206 $header .= "This is a multi-part message in MIME format.\r\n";
207 $header .= "--".$uid."\r\n";
208 $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
209 $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
210 $header .= "send via wallabag\r\n\r\n";
211 $header .= "--".$uid."\r\n";
212 $header .= "Content-Type: application/x-mobipocket-ebook; name=\"".$filename."\"\r\n";
213 $header .= "Content-Transfer-Encoding: base64\r\n";
214 $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
215 $header .= $content."\r\n\r\n";
216 $header .= "--".$uid."--";
217
218 # trying to get the kindle email adress
219 if ($this->wallabag->user->getConfigValue('kindleemail'))
220 {
221 #do a try...exeption here
222 mail( $this->wallabag->user->getConfigValue('kindleemail'), '[wallabag] ' . $this->bookTitle, "", $header );
223 $messages->add('s', _('The email has been sent to your kindle !'));
224 }
225 else
226 {
227 $messages->add('e', _('You didn\'t set your kindle\'s email adress !'));
228 }
229 }
230 }
231 }
232
233 class WallabagPDF extends WallabagEbooks
234 {
235 public function producePDF()
236 {
237 $mpdf = new mPDF('c');
238
239 # intro
240
241 $html = '<h1>' . $this->bookTitle . '<bookmark content="Cover" /></h1><div style="text-align:center;" >
242 <p>' . _('Produced by wallabag with mPDF') . '</p>
243 <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>
244 <img src="themes/baggy/img/apple-touch-icon-152.png" /></div>';
245 $html .= '<pagebreak type="next-odd" />';
246 $i = 1;
247
248 foreach ($this->entries as $item) {
249 $html .= '<h1>' . $item['title'] . '<bookmark content="' . $item['title'] . '" /></h1>';
250 $html .= '<indexentry content="'. $item['title'] .'" />';
251 $html .= $item['content'];
252 $html .= '<pagebreak type="next-odd" />';
253 $i = $i+1;
254 }
255
256
257 # headers
258 $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|Produced with wallabag');
259 $mpdf->SetFooter('{PAGENO}');
260
261 $mpdf->WriteHTML($html);
262
263 # remove characters that make mpdf bug
264 $char_in = array('/', '.', ',', ':', '|');
265 $pdfExportName = preg_replace('/\s+/', '-', str_replace($char_in, '-', $this->bookFileName . '.pdf'));
266
267 # index
268 $html = '<h2>Index<bookmark content="Index" /></h2>
269 <indexinsert cols="2" offset="5" usedivletters="on" div-font-size="15" gap="5" font="Trebuchet" div-font="sans-serif" links="on" />
270 ';
271
272 $mpdf->WriteHTML($html);
273
274 $mpdf->Output('cache/' . $pdfExportName);
275
276 header('Content-Disposition: attachment; filename="' . $pdfExportName . '"');
277
278 header('Content-Transfer-Encoding: base64');
279 header('Content-Type: application/pdf');
280 echo file_get_contents('cache/' . $pdfExportName);
281
282 //exit;
283 }
284 }