]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/WallabagEpub.class.php
Merge branch 'refactor' of github.com:wallabag/wallabag into refactor
[github/wallabag/wallabag.git] / inc / poche / WallabagEpub.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
11class WallabagEpub
12{
13 protected $wallabag;
14 protected $method;
15 protected $id;
16 protected $value;
17
18 public function __construct(Poche $wallabag, $method, $id, $value)
19 {
20 $this->wallabag = $wallabag;
21 $this->method = $method;
22 $this->id = $id;
23 $this->value = $value;
24 }
25
26 /**
27 * handle ePub
28 */
29 public function run()
30 {
31 switch ($this->method) {
32 case 'id':
33 $entryID = filter_var($this->id, FILTER_SANITIZE_NUMBER_INT);
34 $entry = $this->wallabag->store->retrieveOneById($entryID, $this->wallabag->user->getId());
35 $entries = array($entry);
36 $bookTitle = $entry['title'];
37 $bookFileName = substr($bookTitle, 0, 200);
38 break;
39 case 'all':
40 $entries = $this->wallabag->store->retrieveAll($this->wallabag->user->getId());
41 $bookTitle = sprintf(_('All my articles on '), date(_('d.m.y'))); #translatable because each country has it's own date format system
42 $bookFileName = _('Allarticles') . date(_('dmY'));
43 break;
44 case 'tag':
45 $tag = filter_var($this->value, FILTER_SANITIZE_STRING);
46 $tags_id = $this->wallabag->store->retrieveAllTags($this->wallabag->user->getId(), $tag);
47 $tag_id = $tags_id[0]["id"]; // we take the first result, which is supposed to match perfectly. There must be a workaround.
48 $entries = $this->wallabag->store->retrieveEntriesByTag($tag_id, $this->wallabag->user->getId());
49 $bookTitle = sprintf(_('Articles tagged %s'), $tag);
50 $bookFileName = substr(sprintf(_('Tag %s'), $tag), 0, 200);
51 break;
52 case 'category':
53 $category = filter_var($this->value, FILTER_SANITIZE_STRING);
54 $entries = $this->wallabag->store->getEntriesByView($category, $this->wallabag->user->getId());
55 $bookTitle = sprintf(_('All articles in category %s'), $category);
56 $bookFileName = substr(sprintf(_('Category %s'), $category), 0, 200);
57 break;
58 case 'search':
59 $search = filter_var($this->value, FILTER_SANITIZE_STRING);
60 $entries = $this->store->search($search, $this->wallabag->user->getId());
61 $bookTitle = sprintf(_('All articles for search %s'), $search);
62 $bookFileName = substr(sprintf(_('Search %s'), $search), 0, 200);
63 break;
64 case 'default':
65 die(_('Uh, there is a problem while generating epub.'));
66 }
67
68 $content_start =
69 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
70 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
71 . "<head>"
72 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
73 . "<title>wallabag articles book</title>\n"
74 . "</head>\n"
75 . "<body>\n";
76
77 $bookEnd = "</body>\n</html>\n";
78
79 $log = new Logger("wallabag", TRUE);
80 $fileDir = CACHE;
81
82 $book = new EPub(EPub::BOOK_VERSION_EPUB3, DEBUG_POCHE);
83 $log->logLine("new EPub()");
84 $log->logLine("EPub class version: " . EPub::VERSION);
85 $log->logLine("EPub Req. Zip version: " . EPub::REQ_ZIP_VERSION);
86 $log->logLine("Zip version: " . Zip::VERSION);
87 $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL());
88 $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL());
89
90 $book->setTitle(_('wallabag\'s articles'));
91 $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID.
92 //$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.
93 $book->setDescription(_("Some articles saved on my wallabag"));
94 $book->setAuthor("wallabag", "wallabag");
95 $book->setPublisher("wallabag", "wallabag"); // I hope this is a non existant address :)
96 $book->setDate(time()); // Strictly not needed as the book date defaults to time().
97 //$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.
98 $book->setSourceURL("http://$_SERVER[HTTP_HOST]");
99
100 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
101 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "wallabag");
102
103 $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";
104
105 $log->logLine("Add Cover");
106
107 $fullTitle = "<h1> " . $bookTitle . "</h1>\n";
108
109 $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle);
110
111 $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;
112
113 //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE);
114 $book->addChapter("Notices", "Cover2.html", $cover);
115
116 $book->buildTOC();
117
118 foreach ($entries as $entry) { //set tags as subjects
119 $tags = $this->wallabag->store->retrieveTagsByEntry($entry['id']);
120 foreach ($tags as $tag) {
121 $book->setSubject($tag['value']);
122 }
123
124 $log->logLine("Set up parameters");
125
126 $chapter = $content_start . $entry['content'] . $bookEnd;
127 $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub::EXTERNAL_REF_ADD);
128 $log->logLine("Added chapter " . $entry['title']);
129 }
130
131 if (DEBUG_POCHE) {
132 $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation
133 }
134 $book->finalize();
135 $zipData = $book->sendBook($bookFileName);
136 }
137}