]> git.wh0rd.org - tt-rss.git/blob - plugins/af_readability/init.php
807231390b43be2361715e09f1cff7b214dbe6eb
[tt-rss.git] / plugins / af_readability / init.php
1 <?php
2 use andreskrey\Readability\Readability;
3 use andreskrey\Readability\Configuration;
4
5 class Af_Readability extends Plugin {
6
7 /* @var PluginHost $host */
8 private $host;
9
10 function about() {
11 return array(1.0,
12 "Try to inline article content using Readability",
13 "fox");
14 }
15
16 function flags() {
17 return array("needs_curl" => true);
18 }
19
20 function save() {
21 $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
22
23 $this->host->set($this, "enable_share_anything", $enable_share_anything);
24
25 echo __("Data saved.");
26 }
27
28 function init($host)
29 {
30 $this->host = $host;
31
32 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
33 $host->add_hook($host::HOOK_PREFS_TAB, $this);
34 $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
35 $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
36
37 $host->add_filter_action($this, "action_inline", __("Inline content"));
38 }
39
40 function hook_prefs_tab($args) {
41 if ($args != "prefFeeds") return;
42
43 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
44
45 print_notice("Enable the plugin for specific feeds in the feed editor.");
46
47 print "<form dojoType=\"dijit.form.Form\">";
48
49 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
50 evt.preventDefault();
51 if (this.validate()) {
52 console.log(dojo.objectToQuery(this.getValues()));
53 new Ajax.Request('backend.php', {
54 parameters: dojo.objectToQuery(this.getValues()),
55 onComplete: function(transport) {
56 notify_info(transport.responseText);
57 }
58 });
59 //this.reset();
60 }
61 </script>";
62
63 print_hidden("op", "pluginhandler");
64 print_hidden("method", "save");
65 print_hidden("plugin", "af_readability");
66
67 $enable_share_anything = $this->host->get($this, "enable_share_anything");
68
69 print_checkbox("enable_share_anything", $enable_share_anything);
70 print "&nbsp;<label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
71
72 print "<p>"; print_button("submit", __("Save"));
73 print "</form>";
74
75 $enabled_feeds = $this->host->get($this, "enabled_feeds");
76 if (!is_array($enabled_feeds)) $enabled_feeds = array();
77
78 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
79 $this->host->set($this, "enabled_feeds", $enabled_feeds);
80
81 if (count($enabled_feeds) > 0) {
82 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
83
84 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
85 foreach ($enabled_feeds as $f) {
86 print "<li>" .
87 "<img src='images/pub_set.png'
88 style='vertical-align : middle'> <a href='#'
89 onclick='editFeed($f)'>".
90 Feeds::getFeedTitle($f) . "</a></li>";
91 }
92 print "</ul>";
93 }
94
95 print "</div>";
96 }
97
98 function hook_prefs_edit_feed($feed_id) {
99 print "<div class=\"dlgSec\">".__("Readability")."</div>";
100 print "<div class=\"dlgSecCont\">";
101
102 $enabled_feeds = $this->host->get($this, "enabled_feeds");
103 if (!is_array($enabled_feeds)) $enabled_feeds = array();
104
105 $key = array_search($feed_id, $enabled_feeds);
106 $checked = $key !== FALSE ? "checked" : "";
107
108 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
109 name=\"af_readability_enabled\"
110 $checked>&nbsp;<label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
111
112 print "</div>";
113 }
114
115 function hook_prefs_save_feed($feed_id) {
116 $enabled_feeds = $this->host->get($this, "enabled_feeds");
117 if (!is_array($enabled_feeds)) $enabled_feeds = array();
118
119 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
120 $key = array_search($feed_id, $enabled_feeds);
121
122 if ($enable) {
123 if ($key === FALSE) {
124 array_push($enabled_feeds, $feed_id);
125 }
126 } else {
127 if ($key !== FALSE) {
128 unset($enabled_feeds[$key]);
129 }
130 }
131
132 $this->host->set($this, "enabled_feeds", $enabled_feeds);
133 }
134
135 /**
136 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
137 */
138 function hook_article_filter_action($article, $action) {
139 return $this->process_article($article);
140 }
141
142 public function extract_content($url) {
143 global $fetch_effective_url;
144
145 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
146
147 $tmp = fetch_file_contents([
148 "url" => $url,
149 "http_accept" => "text/*",
150 "type" => "text/html"]);
151
152 if ($tmp && mb_strlen($tmp) < 1024 * 500) {
153 $tmpdoc = new DOMDocument("1.0", "UTF-8");
154
155 if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
156 return false;
157
158 if (strtolower($tmpdoc->encoding) != 'utf-8') {
159 $tmpxpath = new DOMXPath($tmpdoc);
160
161 foreach ($tmpxpath->query("//meta") as $elem) {
162 $elem->parentNode->removeChild($elem);
163 }
164
165 $tmp = $tmpdoc->saveHTML();
166 }
167
168 $r = new Readability(new Configuration());
169
170 try {
171 if ($r->parse($tmp)) {
172
173 $tmpxpath = new DOMXPath($r->getDOMDOcument());
174 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
175
176 foreach ($entries as $entry) {
177 if ($entry->hasAttribute("href")) {
178 $entry->setAttribute("href",
179 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href")));
180
181 }
182
183 if ($entry->hasAttribute("src")) {
184 $entry->setAttribute("src",
185 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src")));
186
187 }
188 }
189
190 return $r->getContent();
191 }
192
193 } catch (ParseException $e) {
194 return false;
195 }
196
197 }
198
199 return false;
200 }
201
202 function process_article($article) {
203
204 $extracted_content = $this->extract_content($article["link"]);
205
206 if ($extracted_content) {
207 $article["content"] = $extracted_content;
208 }
209
210 return $article;
211 }
212
213 function hook_article_filter($article) {
214
215 $enabled_feeds = $this->host->get($this, "enabled_feeds");
216 if (!is_array($enabled_feeds)) return $article;
217
218 $key = array_search($article["feed"]["id"], $enabled_feeds);
219 if ($key === FALSE) return $article;
220
221 return $this->process_article($article);
222
223 }
224
225 function api_version() {
226 return 2;
227 }
228
229 private function filter_unknown_feeds($enabled_feeds) {
230 $tmp = array();
231
232 foreach ($enabled_feeds as $feed) {
233
234 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
235 $sth->execute([$feed, $_SESSION['uid']]);
236
237 if ($row = $sth->fetch()) {
238 array_push($tmp, $feed);
239 }
240 }
241
242 return $tmp;
243 }
244
245 }