]> git.wh0rd.org - tt-rss.git/blob - plugins/af_readability/init.php
a52b745b857f90ca37c1f166f9742cbafd25dbeb
[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 //
18 }
19
20 function init($host)
21 {
22 $this->host = $host;
23
24 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
25 $host->add_hook($host::HOOK_PREFS_TAB, $this);
26 $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
27 $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
28
29 $host->add_filter_action($this, "action_inline", __("Inline content"));
30 }
31
32 function hook_prefs_tab($args) {
33 if ($args != "prefFeeds") return;
34
35 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_readability settings')."\">";
36
37 print_notice("Enable the plugin for specific feeds in the feed editor.");
38
39 $enabled_feeds = $this->host->get($this, "enabled_feeds");
40 if (!is_array($enabled_feeds)) $enabled_feeds = array();
41
42 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
43 $this->host->set($this, "enabled_feeds", $enabled_feeds);
44
45 if (count($enabled_feeds) > 0) {
46 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
47
48 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
49 foreach ($enabled_feeds as $f) {
50 print "<li>" .
51 "<img src='images/pub_set.png'
52 style='vertical-align : middle'> <a href='#'
53 onclick='editFeed($f)'>".
54 getFeedTitle($f) . "</a></li>";
55 }
56 print "</ul>";
57 }
58
59 print "</div>";
60 }
61
62 function hook_prefs_edit_feed($feed_id) {
63 print "<div class=\"dlgSec\">".__("Readability")."</div>";
64 print "<div class=\"dlgSecCont\">";
65
66 $enabled_feeds = $this->host->get($this, "enabled_feeds");
67 if (!is_array($enabled_feeds)) $enabled_feeds = array();
68
69 $key = array_search($feed_id, $enabled_feeds);
70 $checked = $key !== FALSE ? "checked" : "";
71
72 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
73 name=\"af_readability_enabled\"
74 $checked>&nbsp;<label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
75
76 print "</div>";
77 }
78
79 function hook_prefs_save_feed($feed_id) {
80 $enabled_feeds = $this->host->get($this, "enabled_feeds");
81 if (!is_array($enabled_feeds)) $enabled_feeds = array();
82
83 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
84 $key = array_search($feed_id, $enabled_feeds);
85
86 if ($enable) {
87 if ($key === FALSE) {
88 array_push($enabled_feeds, $feed_id);
89 }
90 } else {
91 if ($key !== FALSE) {
92 unset($enabled_feeds[$key]);
93 }
94 }
95
96 $this->host->set($this, "enabled_feeds", $enabled_feeds);
97 }
98
99 function hook_article_filter_action($article, $action) {
100 return $this->process_article($article);
101 }
102
103 function process_article($article) {
104
105 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
106
107 if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
108
109 $ch = curl_init($article["link"]);
110
111 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
112 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
113 curl_setopt($ch, CURLOPT_HEADER, true);
114 curl_setopt($ch, CURLOPT_NOBODY, true);
115 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
116 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
117
118 @$result = curl_exec($ch);
119 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
120
121 if (strpos($content_type, "text/html") === FALSE)
122 return $article;
123 }
124
125 $tmp = fetch_file_contents($article["link"]);
126
127 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
128 $tmpdoc = new DOMDocument("1.0", "UTF-8");
129
130 if (!$tmpdoc->loadHTML($tmp))
131 return $article;
132
133 if (strtolower($tmpdoc->encoding) != 'utf-8') {
134 $tmpxpath = new DOMXPath($tmpdoc);
135
136 foreach ($tmpxpath->query("//meta") as $elem) {
137 $elem->parentNode->removeChild($elem);
138 }
139
140 $tmp = $tmpdoc->saveHTML();
141 }
142
143 $r = new Readability($tmp, $article["link"]);
144
145 if ($r->init()) {
146
147 $tmpxpath = new DOMXPath($r->dom);
148
149 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
150
151 foreach ($entries as $entry) {
152 if ($entry->hasAttribute("href")) {
153 $entry->setAttribute("href",
154 rewrite_relative_url($article["link"], $entry->getAttribute("href")));
155
156 }
157
158 if ($entry->hasAttribute("src")) {
159 $entry->setAttribute("src",
160 rewrite_relative_url($article["link"], $entry->getAttribute("src")));
161
162 }
163
164 }
165
166 $article["content"] = $r->articleContent->innerHTML;
167 }
168 }
169
170 return $article;
171 }
172
173 function hook_article_filter($article) {
174
175 $enabled_feeds = $this->host->get($this, "enabled_feeds");
176 if (!is_array($enabled_feeds)) return $article;
177
178 $key = array_search($article["feed"]["id"], $enabled_feeds);
179 if ($key === FALSE) return $article;
180
181 return $this->process_article($article);
182
183 }
184
185 function api_version() {
186 return 2;
187 }
188
189 private function filter_unknown_feeds($enabled_feeds) {
190 $tmp = array();
191
192 foreach ($enabled_feeds as $feed) {
193
194 $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
195
196 if (db_num_rows($result) != 0) {
197 array_push($tmp, $feed);
198 }
199 }
200
201 return $tmp;
202 }
203
204 }
205 ?>