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