]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/WallabagEpub.class.php
add pdf and mobi libraries
[github/wallabag/wallabag.git] / inc / poche / WallabagEpub.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 $entries = array($entry);
34 $bookTitle = $entry['title'];
35 $bookFileName = substr($bookTitle, 0, 200);
36 break;
37 case 'all':
38 $entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId());
39 $bookTitle = sprintf(_('All my articles on '), date(_('d.m.y'))); #translatable because each country has it's own date format system
40 $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 $entries = $this->wallabag->store->retrieveEntriesByTag($tag_id, $this->wallabag->user->getId());
47 $bookTitle = sprintf(_('Articles tagged %s'), $tag);
48 $bookFileName = substr(sprintf(_('Tag %s'), $tag), 0, 200);
49 break;
50 case 'category':
51 $category = filter_var($this->value, FILTER_SANITIZE_STRING);
52 $entries = $this->wallabag->store->getEntriesByView($category, $this->wallabag->user->getId());
53 $bookTitle = sprintf(_('All articles in category %s'), $category);
54 $bookFileName = substr(sprintf(_('Category %s'), $category), 0, 200);
55 break;
56 case 'search':
57 $search = filter_var($this->value, FILTER_SANITIZE_STRING);
58 $entries = $this->store->search($search, $this->wallabag->user->getId());
59 $bookTitle = sprintf(_('All articles for search %s'), $search);
60 $bookFileName = substr(sprintf(_('Search %s'), $search), 0, 200);
61 break;
62 case 'default':
63 die(_('Uh, there is a problem while generating epub.'));
64 }
65 }
66 }
67
68 class WallabagEpub extends WallabagEBooks
69 {
70 /**
71 * handle ePub
72 */
73 public function produceEpub()
74 {
75 $content_start =
76 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
77 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
78 . "<head>"
79 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
80 . "<title>wallabag articles book</title>\n"
81 . "</head>\n"
82 . "<body>\n";
83
84 $bookEnd = "</body>\n</html>\n";
85
86 $log = new Logger("wallabag", TRUE);
87 $fileDir = CACHE;
88
89 $book = new EPub(EPub::BOOK_VERSION_EPUB3, DEBUG_POCHE);
90 $log->logLine("new EPub()");
91 $log->logLine("EPub class version: " . EPub::VERSION);
92 $log->logLine("EPub Req. Zip version: " . EPub::REQ_ZIP_VERSION);
93 $log->logLine("Zip version: " . Zip::VERSION);
94 $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL());
95 $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL());
96
97 $book->setTitle($bookTitle);
98 $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID.
99 //$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.
100 $book->setDescription(_("Some articles saved on my wallabag"));
101 $book->setAuthor("wallabag", "wallabag");
102 $book->setPublisher("wallabag", "wallabag"); // I hope this is a non existant address :)
103 $book->setDate(time()); // Strictly not needed as the book date defaults to time().
104 //$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.
105 $book->setSourceURL("http://$_SERVER[HTTP_HOST]");
106
107 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
108 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "wallabag");
109
110 $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";
111
112 $log->logLine("Add Cover");
113
114 $fullTitle = "<h1> " . $bookTitle . "</h1>\n";
115
116 $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle);
117
118 $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;
119
120 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
121 $book->addChapter("Notices", "Cover2.html", $cover);
122
123 $book->buildTOC();
124
125 foreach ($entries as $entry) { //set tags as subjects
126 $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']);
127 foreach ($tags as $tag) {
128 $book->setSubject($tag['value']);
129 }
130
131 $log->logLine("Set up parameters");
132
133 $chapter = $content_start . $entry['content'] . $bookEnd;
134 $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub::EXTERNAL_REF_ADD);
135 $log->logLine("Added chapter " . $entry['title']);
136 }
137
138 if (DEBUG_POCHE) {
139 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
140 }
141 $book->finalize();
142 $zipData = $book->sendBook($bookFileName);
143 }
144 }
145
146 class WallabagMobi extends WallabagEBooks
147 {
148 /**
149 * Adapted from News2Kindle
150 * @author Jakub Westfalewski <jwest@jwest.pl>
151 *
152 */
153
154 public function produceMobi()
155 {
156 $storage = new Storage('static');
157 $this->prepareData();
158 foreach ($entries as $i => $item) {
159 $content = $item['content'];
160 $images = new Images($storage, $content);
161 $content = $images->convert();
162 $storage->add_content
163 (
164 md5($item['title']),
165 mb_convert_encoding($item['title'], 'HTML-ENTITIES', 'utf-8'),
166 $content,
167 $item['url']],
168 ""
169 );
170 }
171 $articles = $storage->get_contents();
172 $toc = array();
173 $articles_count = count($articles);
174
175 foreach($articles as $article){
176 if(array_key_exists($article->website->title, $toc)){
177 $toc[$article->website->title]->articles[] = $article;
178 }else{
179 $toc[$article->website->title] = (object)array(
180 'articles' => array($article),
181 'title' => $article->website->title,
182 'streamId' => $article->website->streamId,
183 'url' => $article->website->htmlUrl,
184 );
185 }
186 }
187
188 $mobi = new MOBI();
189 $mobi->setData($content);
190 $mobi->setOptions(array(
191 'title' => 'Articles from '.date('Y-m-d'),
192 'author' => 'wallabag',
193 'subject' => 'Articles from '.date('Y-m-d'),
194 ));
195
196 $images = array();
197
198 //prepare images for mobi format
199 foreach ( $storage->info('images') as $n => $image )
200 {
201 $images[$n] = new FileRecord(new Record(file_get_contents($storage->get_path() . $image)));
202 }
203
204 $mobi->setImages($images);
205 $mobi->save( $storage->get_path(FALSE) . 'articles-' . date('Y-m-d') . '.mobi');
206
207 $storage->clean();
208
209 if ($send) {
210 $files = glob($storage->get_path(FALSE).'*.mobi');
211 $mail = new Send(KINDLEMAIL,MAIL);
212 foreach ( $files as $file_mobi )
213 {
214 $mail->send( $file_mobi );
215 }
216 // clean cache
217 foreach ( $files as $file_mobi )
218 {
219 unlink( $file_mobi );
220 }
221 }
222 }
223 }
224
225 class WallabagPDF extends WallabagEbooks
226 {
227 public function producePDF()
228 {
229 $mpdf = new mPDF('c');
230
231 $mpdf->WriteHTML($html);
232 $mpdf->Output();
233 exit;
234 }
235 }