diff options
Diffstat (limited to 'inc/poche')
-rwxr-xr-x | inc/poche/Poche.class.php | 115 | ||||
-rwxr-xr-x | inc/poche/config.inc.default.php | 3 | ||||
-rwxr-xr-x | inc/poche/global.inc.php | 5 | ||||
-rw-r--r-- | inc/poche/pochePictures.php | 58 |
4 files changed, 175 insertions, 6 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index 515d4cac..1b69cd61 100755 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php | |||
@@ -1149,4 +1149,119 @@ class Poche | |||
1149 | 1149 | ||
1150 | return new HTMLPurifier($config); | 1150 | return new HTMLPurifier($config); |
1151 | } | 1151 | } |
1152 | |||
1153 | /** | ||
1154 | * handle epub | ||
1155 | */ | ||
1156 | public function createEpub() { | ||
1157 | |||
1158 | switch ($_GET['method']) { | ||
1159 | case 'id': | ||
1160 | $entryID = filter_var($_GET['id'],FILTER_SANITIZE_NUMBER_INT); | ||
1161 | $entry = $this->store->retrieveOneById($entryID, $this->user->getId()); | ||
1162 | $entries = array($entry); | ||
1163 | $bookTitle = $entry['title']; | ||
1164 | $bookFileName = substr($bookTitle, 0, 200); | ||
1165 | break; | ||
1166 | case 'all': | ||
1167 | $entries = $this->store->retrieveAll($this->user->getId()); | ||
1168 | $bookTitle = sprintf(_('All my articles on '), date(_('d.m.y'))); #translatable because each country has it's own date format system | ||
1169 | $bookFileName = _('Allarticles') . date(_('dmY')); | ||
1170 | break; | ||
1171 | case 'tag': | ||
1172 | $tag = filter_var($_GET['tag'],FILTER_SANITIZE_STRING); | ||
1173 | $tags_id = $this->store->retrieveAllTags($this->user->getId(),$tag); | ||
1174 | $tag_id = $tags_id[0]["id"]; // we take the first result, which is supposed to match perfectly. There must be a workaround. | ||
1175 | $entries = $this->store->retrieveEntriesByTag($tag_id,$this->user->getId()); | ||
1176 | $bookTitle = sprintf(_('Articles tagged %s'),$tag); | ||
1177 | $bookFileName = substr(sprintf(_('Tag %s'),$tag), 0, 200); | ||
1178 | break; | ||
1179 | case 'category': | ||
1180 | $category = filter_var($_GET['category'],FILTER_SANITIZE_STRING); | ||
1181 | $entries = $this->store->getEntriesByView($category,$this->user->getId()); | ||
1182 | $bookTitle = sprintf(_('All articles in category %s'), $category); | ||
1183 | $bookFileName = substr(sprintf(_('Category %s'),$category), 0, 200); | ||
1184 | break; | ||
1185 | case 'search': | ||
1186 | $search = filter_var($_GET['search'],FILTER_SANITIZE_STRING); | ||
1187 | $entries = $this->store->search($search,$this->user->getId()); | ||
1188 | $bookTitle = sprintf(_('All articles for search %s'), $search); | ||
1189 | $bookFileName = substr(sprintf(_('Search %s'), $search), 0, 200); | ||
1190 | break; | ||
1191 | case 'default': | ||
1192 | die(_('Uh, there is a problem while generating epub.')); | ||
1193 | |||
1194 | } | ||
1195 | |||
1196 | $content_start = | ||
1197 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | ||
1198 | . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" | ||
1199 | . "<head>" | ||
1200 | . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" | ||
1201 | . "<title>wallabag articles book</title>\n" | ||
1202 | . "</head>\n" | ||
1203 | . "<body>\n"; | ||
1204 | |||
1205 | $bookEnd = "</body>\n</html>\n"; | ||
1206 | |||
1207 | $log = new Logger("wallabag", TRUE); | ||
1208 | $fileDir = CACHE; | ||
1209 | |||
1210 | |||
1211 | $book = new EPub(EPub::BOOK_VERSION_EPUB3); | ||
1212 | $log->logLine("new EPub()"); | ||
1213 | $log->logLine("EPub class version: " . EPub::VERSION); | ||
1214 | $log->logLine("EPub Req. Zip version: " . EPub::REQ_ZIP_VERSION); | ||
1215 | $log->logLine("Zip version: " . Zip::VERSION); | ||
1216 | $log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL()); | ||
1217 | $log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL()); | ||
1218 | |||
1219 | $book->setTitle(_('wallabag\'s articles')); | ||
1220 | $book->setIdentifier("http://$_SERVER[HTTP_HOST]", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID. | ||
1221 | //$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. | ||
1222 | $book->setDescription(_("Some articles saved on my wallabag")); | ||
1223 | $book->setAuthor("wallabag","wallabag"); | ||
1224 | $book->setPublisher("wallabag","wallabag"); // I hope this is a non existant address :) | ||
1225 | $book->setDate(time()); // Strictly not needed as the book date defaults to time(). | ||
1226 | //$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. | ||
1227 | $book->setSourceURL("http://$_SERVER[HTTP_HOST]"); | ||
1228 | |||
1229 | $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP"); | ||
1230 | $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "wallabag"); | ||
1231 | |||
1232 | $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"; | ||
1233 | |||
1234 | $log->logLine("Add Cover"); | ||
1235 | |||
1236 | $fullTitle = "<h1> " . $bookTitle . "</h1>\n"; | ||
1237 | |||
1238 | $book->setCoverImage("Cover.png", file_get_contents("themes/baggy/img/apple-touch-icon-152.png"), "image/png", $fullTitle); | ||
1239 | |||
1240 | $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; | ||
1241 | |||
1242 | //$book->addChapter("Table of Contents", "TOC.xhtml", NULL, false, EPub::EXTERNAL_REF_IGNORE); | ||
1243 | $book->addChapter("Notices", "Cover2.html", $cover); | ||
1244 | |||
1245 | $book->buildTOC(); | ||
1246 | |||
1247 | foreach ($entries as $entry) { //set tags as subjects | ||
1248 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | ||
1249 | foreach ($tags as $tag) { | ||
1250 | $book->setSubject($tag['value']); | ||
1251 | } | ||
1252 | |||
1253 | $log->logLine("Set up parameters"); | ||
1254 | |||
1255 | $chapter = $content_start . $entry['content'] . $bookEnd; | ||
1256 | $book->addChapter($entry['title'], htmlspecialchars($entry['title']) . ".html", $chapter, true, EPub::EXTERNAL_REF_ADD); | ||
1257 | $log->logLine("Added chapter " . $entry['title']); | ||
1258 | } | ||
1259 | |||
1260 | if (DEBUG_POCHE) { | ||
1261 | $epuplog = $book->getLog(); | ||
1262 | $book->addChapter("Log", "Log.html", $content_start . $log->getLog() . "\n</pre>" . $bookEnd); // log generation | ||
1263 | } | ||
1264 | $book->finalize(); | ||
1265 | $zipData = $book->sendBook($bookFileName); | ||
1266 | } | ||
1152 | } | 1267 | } |
diff --git a/inc/poche/config.inc.default.php b/inc/poche/config.inc.default.php index edc42fc9..ffcd205d 100755 --- a/inc/poche/config.inc.default.php +++ b/inc/poche/config.inc.default.php | |||
@@ -30,7 +30,8 @@ | |||
30 | 30 | ||
31 | @define ('MODE_DEMO', FALSE); | 31 | @define ('MODE_DEMO', FALSE); |
32 | @define ('DEBUG_POCHE', FALSE); | 32 | @define ('DEBUG_POCHE', FALSE); |
33 | @define ('DOWNLOAD_PICTURES', FALSE); | 33 | @define ('DOWNLOAD_PICTURES', FALSE); # This can slow down the process of adding articles |
34 | @define ('REGENERATE_PICTURES_QUALITY', 75); | ||
34 | @define ('CONVERT_LINKS_FOOTNOTES', FALSE); | 35 | @define ('CONVERT_LINKS_FOOTNOTES', FALSE); |
35 | @define ('REVERT_FORCED_PARAGRAPH_ELEMENTS', FALSE); | 36 | @define ('REVERT_FORCED_PARAGRAPH_ELEMENTS', FALSE); |
36 | @define ('SHARE_TWITTER', TRUE); | 37 | @define ('SHARE_TWITTER', TRUE); |
diff --git a/inc/poche/global.inc.php b/inc/poche/global.inc.php index 14e9dd93..8cf86d03 100755 --- a/inc/poche/global.inc.php +++ b/inc/poche/global.inc.php | |||
@@ -31,6 +31,11 @@ require_once INCLUDES . '/3rdparty/FlattrItem.class.php'; | |||
31 | 31 | ||
32 | require_once INCLUDES . '/3rdparty/htmlpurifier/HTMLPurifier.auto.php'; | 32 | require_once INCLUDES . '/3rdparty/htmlpurifier/HTMLPurifier.auto.php'; |
33 | 33 | ||
34 | # epub library | ||
35 | require_once INCLUDES . '/3rdparty/libraries/PHPePub/Logger.php'; | ||
36 | require_once INCLUDES . '/3rdparty/libraries/PHPePub/EPub.php'; | ||
37 | require_once INCLUDES . '/3rdparty/libraries/PHPePub/EPubChapterSplitter.php'; | ||
38 | |||
34 | # Composer its autoloader for automatically loading Twig | 39 | # Composer its autoloader for automatically loading Twig |
35 | if (! file_exists(ROOT . '/vendor/autoload.php')) { | 40 | if (! file_exists(ROOT . '/vendor/autoload.php')) { |
36 | Poche::$canRenderTemplates = false; | 41 | Poche::$canRenderTemplates = false; |
diff --git a/inc/poche/pochePictures.php b/inc/poche/pochePictures.php index e4b0b160..7c319a85 100644 --- a/inc/poche/pochePictures.php +++ b/inc/poche/pochePictures.php | |||
@@ -14,6 +14,7 @@ | |||
14 | function filtre_picture($content, $url, $id) | 14 | function filtre_picture($content, $url, $id) |
15 | { | 15 | { |
16 | $matches = array(); | 16 | $matches = array(); |
17 | $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice | ||
17 | preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER); | 18 | preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER); |
18 | foreach($matches as $i => $link) { | 19 | foreach($matches as $i => $link) { |
19 | $link[1] = trim($link[1]); | 20 | $link[1] = trim($link[1]); |
@@ -22,8 +23,17 @@ function filtre_picture($content, $url, $id) | |||
22 | $filename = basename(parse_url($absolute_path, PHP_URL_PATH)); | 23 | $filename = basename(parse_url($absolute_path, PHP_URL_PATH)); |
23 | $directory = create_assets_directory($id); | 24 | $directory = create_assets_directory($id); |
24 | $fullpath = $directory . '/' . $filename; | 25 | $fullpath = $directory . '/' . $filename; |
25 | download_pictures($absolute_path, $fullpath); | 26 | |
26 | $content = str_replace($matches[$i][2], $fullpath, $content); | 27 | if (in_array($absolute_path, $processing_pictures) === true) { |
28 | // replace picture's URL only if processing is OK : already processing -> go to next picture | ||
29 | continue; | ||
30 | } | ||
31 | |||
32 | if (download_pictures($absolute_path, $fullpath) === true) { | ||
33 | $content = str_replace($matches[$i][2], $fullpath, $content); | ||
34 | } | ||
35 | |||
36 | $processing_pictures[] = $absolute_path; | ||
27 | } | 37 | } |
28 | 38 | ||
29 | } | 39 | } |
@@ -64,17 +74,55 @@ function get_absolute_link($relative_link, $url) { | |||
64 | 74 | ||
65 | /** | 75 | /** |
66 | * Téléchargement des images | 76 | * Téléchargement des images |
77 | * | ||
78 | * @return bool true if the download and processing is OK, false else | ||
67 | */ | 79 | */ |
68 | function download_pictures($absolute_path, $fullpath) | 80 | function download_pictures($absolute_path, $fullpath) |
69 | { | 81 | { |
70 | $rawdata = Tools::getFile($absolute_path); | 82 | $rawdata = Tools::getFile($absolute_path); |
83 | $fullpath = urldecode($fullpath); | ||
71 | 84 | ||
72 | if(file_exists($fullpath)) { | 85 | if(file_exists($fullpath)) { |
73 | unlink($fullpath); | 86 | unlink($fullpath); |
74 | } | 87 | } |
75 | $fp = fopen($fullpath, 'x'); | 88 | |
76 | fwrite($fp, $rawdata); | 89 | // check extension |
77 | fclose($fp); | 90 | $file_ext = strrchr($fullpath, '.'); |
91 | $whitelist = array(".jpg",".jpeg",".gif",".png"); | ||
92 | if (!(in_array($file_ext, $whitelist))) { | ||
93 | Tools::logm('processed image with not allowed extension. Skipping ' . $fullpath); | ||
94 | return false; | ||
95 | } | ||
96 | |||
97 | // check headers | ||
98 | $imageinfo = getimagesize($absolute_path); | ||
99 | if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') { | ||
100 | Tools::logm('processed image with bad header. Skipping ' . $fullpath); | ||
101 | return false; | ||
102 | } | ||
103 | |||
104 | // regenerate image | ||
105 | $im = imagecreatefromstring($rawdata); | ||
106 | if ($im === false) { | ||
107 | Tools::logm('error while regenerating image ' . $fullpath); | ||
108 | return false; | ||
109 | } | ||
110 | |||
111 | switch ($imageinfo['mime']) { | ||
112 | case 'image/gif': | ||
113 | $result = imagegif($im, $fullpath); | ||
114 | break; | ||
115 | case 'image/jpeg': | ||
116 | case 'image/jpg': | ||
117 | $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY); | ||
118 | break; | ||
119 | case 'image/png': | ||
120 | $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY / 100 * 9)); | ||
121 | break; | ||
122 | } | ||
123 | imagedestroy($im); | ||
124 | |||
125 | return $result; | ||
78 | } | 126 | } |
79 | 127 | ||
80 | /** | 128 | /** |