]> git.wh0rd.org - tt-rss.git/blame - plugins/af_readability/init.php
remove reddit textnode hack since the feed is broken in a different way now
[tt-rss.git] / plugins / af_readability / init.php
CommitLineData
1ff7ae42
AD
1<?php
2class 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
41245888
AD
12 function flags() {
13 return array("needs_curl" => true);
14 }
15
1ff7ae42
AD
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);
6dbd6585
AD
28
29 $host->add_filter_action($this, "action_inline", __("Inline content"));
1ff7ae42
AD
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");
5d5f100f 40 if (!is_array($enabled_feeds)) $enabled_feeds = array();
3e220fd1
TE
41
42 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
1ff7ae42
AD
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");
5d5f100f 67 if (!is_array($enabled_feeds)) $enabled_feeds = array();
1ff7ae42
AD
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
6dbd6585
AD
99 function hook_article_filter_action($article, $action) {
100 return $this->process_article($article);
101 }
1ff7ae42 102
6dbd6585 103 function process_article($article) {
1ff7ae42 104
8b08d9d7 105 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
1ff7ae42 106
aa03bac4
AD
107 if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
108
831129f6 109 $ch = curl_init($article["link"]);
aa03bac4 110
831129f6
AD
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);
aa03bac4 115 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
831129f6
AD
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
1ff7ae42
AD
125 $tmp = fetch_file_contents($article["link"]);
126
4d03c5c5 127 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
b7d1306b 128 $tmpdoc = new DOMDocument("1.0", "UTF-8");
831129f6
AD
129
130 if (!$tmpdoc->loadHTML($tmp))
131 return $article;
b7d1306b 132
37b2bca9 133 if (strtolower($tmpdoc->encoding) != 'utf-8') {
b7d1306b
AD
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
1ff7ae42
AD
143 $r = new Readability($tmp, $article["link"]);
144
145 if ($r->init()) {
fd61fd6e
AD
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",
7975ace2 154 rewrite_relative_url($article["link"], $entry->getAttribute("href")));
fd61fd6e
AD
155
156 }
157
158 if ($entry->hasAttribute("src")) {
159 $entry->setAttribute("src",
7975ace2 160 rewrite_relative_url($article["link"], $entry->getAttribute("src")));
fd61fd6e
AD
161
162 }
163
164 }
165
1ff7ae42
AD
166 $article["content"] = $r->articleContent->innerHTML;
167 }
168 }
169
170 return $article;
6dbd6585
AD
171 }
172
173 function hook_article_filter($article) {
174
175 $enabled_feeds = $this->host->get($this, "enabled_feeds");
5d5f100f
TE
176 if (!is_array($enabled_feeds)) return $article;
177
6dbd6585
AD
178 $key = array_search($article["feed"]["id"], $enabled_feeds);
179 if ($key === FALSE) return $article;
180
181 return $this->process_article($article);
1ff7ae42
AD
182
183 }
184
185 function api_version() {
186 return 2;
187 }
188
189 private function filter_unknown_feeds($enabled_feeds) {
190 $tmp = array();
191
3e220fd1 192 foreach ($enabled_feeds as $feed) {
1ff7ae42 193
3e220fd1 194 $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1ff7ae42 195
3e220fd1
TE
196 if (db_num_rows($result) != 0) {
197 array_push($tmp, $feed);
1ff7ae42
AD
198 }
199 }
200
201 return $tmp;
202 }
203
204}
205?>