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