]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/WallabagEpub.class.php
diaspora on all themes
[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 WallabagEpub
12 {
13 protected $wallabag;
14 protected $method;
15 protected $value;
16
17 public function __construct(Poche $wallabag, $method, $value)
18 {
19 $this->wallabag = $wallabag;
20 $this->method = $method;
21 $this->value = $value;
22 }
23
24 /**
25 * handle ePub
26 */
27 public function run()
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 $content_start =
67 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
68 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
69 . "<head>"
70 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
71 . "<title>wallabag articles book</title>\n"
72 . "</head>\n"
73 . "<body>\n";
74
75 $bookEnd = "</body>\n</html>\n";
76
77 $log = new Logger("wallabag", TRUE);
78 $fileDir = CACHE;
79
80 $book = new EPub(EPub::BOOK_VERSION_EPUB3, DEBUG_POCHE);
81 $log->logLine("new EPub()");
82 $log->logLine("EPub class version: " . EPub::VERSION);
83 $log->logLine("EPub Req. Zip version: " . EPub::REQ_ZIP_VERSION);
84 $log->logLine("Zip version: " . Zip::VERSION);
85 $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL());
86 $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL());
87
88 $book->setTitle($bookTitle);
89 $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID.
90 //$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.
91 $book->setDescription(_("Some articles saved on my wallabag"));
92 $book->setAuthor("wallabag", "wallabag");
93 $book->setPublisher("wallabag", "wallabag"); // I hope this is a non existant address :)
94 $book->setDate(time()); // Strictly not needed as the book date defaults to time().
95 //$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.
96 $book->setSourceURL("http://$_SERVER[HTTP_HOST]");
97
98 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
99 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "wallabag");
100
101 $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";
102
103 $log->logLine("Add Cover");
104
105 $fullTitle = "<h1> " . $bookTitle . "</h1>\n";
106
107 $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle);
108
109 $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;
110
111 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
112 $book->addChapter("Notices", "Cover2.html", $cover);
113
114 $book->buildTOC();
115
116 foreach ($entries as $entry) { //set tags as subjects
117 $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']);
118 foreach ($tags as $tag) {
119 $book->setSubject($tag['value']);
120 }
121
122 $log->logLine("Set up parameters");
123
124 $chapter = $content_start . $entry['content'] . $bookEnd;
125 $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub::EXTERNAL_REF_ADD);
126 $log->logLine("Added chapter " . $entry['title']);
127 }
128
129 if (DEBUG_POCHE) {
130 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
131 }
132 $book->finalize();
133 $zipData = $book->sendBook($bookFileName);
134 }
135 }