]> git.wh0rd.org - tt-rss.git/blame - plugins/af_readability/init.php
update autoloader to consider namespaces for third party libraries: placed and loaded...
[tt-rss.git] / plugins / af_readability / init.php
CommitLineData
1ff7ae42 1<?php
2aaefbfa
AD
2use andreskrey\Readability\Readability;
3use andreskrey\Readability\Configuration;
4
1ff7ae42
AD
5class Af_Readability extends Plugin {
6
0f4487d3 7 /* @var PluginHost $host */
1ff7ae42
AD
8 private $host;
9
10 function about() {
11 return array(1.0,
12 "Try to inline article content using Readability",
13 "fox");
14 }
15
41245888
AD
16 function flags() {
17 return array("needs_curl" => true);
18 }
19
1ff7ae42 20 function save() {
da9ea57d 21 $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
666cd333
AD
22
23 $this->host->set($this, "enable_share_anything", $enable_share_anything);
24
25 echo __("Data saved.");
1ff7ae42
AD
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);
6dbd6585
AD
36
37 $host->add_filter_action($this, "action_inline", __("Inline content"));
1ff7ae42
AD
38 }
39
40 function hook_prefs_tab($args) {
41 if ($args != "prefFeeds") return;
42
dc8bd8a6 43 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
1ff7ae42
AD
44
45 print_notice("Enable the plugin for specific feeds in the feed editor.");
46
666cd333
AD
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
328118d1
AD
63 print_hidden("op", "pluginhandler");
64 print_hidden("method", "save");
65 print_hidden("plugin", "af_readability");
666cd333
AD
66
67 $enable_share_anything = $this->host->get($this, "enable_share_anything");
666cd333 68
dc8bd8a6
AD
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>";
666cd333 71
dc8bd8a6 72 print "<p>"; print_button("submit", __("Save"));
666cd333
AD
73 print "</form>";
74
1ff7ae42 75 $enabled_feeds = $this->host->get($this, "enabled_feeds");
5d5f100f 76 if (!is_array($enabled_feeds)) $enabled_feeds = array();
3e220fd1
TE
77
78 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
1ff7ae42
AD
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)'>".
86a8351c 90 Feeds::getFeedTitle($f) . "</a></li>";
1ff7ae42
AD
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");
5d5f100f 103 if (!is_array($enabled_feeds)) $enabled_feeds = array();
1ff7ae42
AD
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
da9ea57d 119 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
1ff7ae42
AD
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
21ce7d9e
AD
135 /**
136 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
137 */
6dbd6585
AD
138 function hook_article_filter_action($article, $action) {
139 return $this->process_article($article);
140 }
1ff7ae42 141
666cd333 142 public function extract_content($url) {
e26cb061 143 global $fetch_effective_url;
831129f6 144
e26cb061 145 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
831129f6 146
b037ffd6
AD
147 $tmp = fetch_file_contents([
148 "url" => $url,
149 "http_accept" => "text/*",
150 "type" => "text/html"]);
1ff7ae42 151
e487e92d 152 if ($tmp && mb_strlen($tmp) < 1024 * 500) {
b7d1306b 153 $tmpdoc = new DOMDocument("1.0", "UTF-8");
831129f6 154
f45a1152 155 if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
666cd333 156 return false;
b7d1306b 157
37b2bca9 158 if (strtolower($tmpdoc->encoding) != 'utf-8') {
b7d1306b
AD
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
2aaefbfa 168 $r = new Readability(new Configuration());
1ff7ae42 169
2aaefbfa
AD
170 try {
171 if ($r->parse($tmp)) {
fd61fd6e 172
2aaefbfa
AD
173 $tmpxpath = new DOMXPath($r->getDOMDOcument());
174 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
fd61fd6e 175
2aaefbfa
AD
176 foreach ($entries as $entry) {
177 if ($entry->hasAttribute("href")) {
178 $entry->setAttribute("href",
179 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href")));
fd61fd6e 180
2aaefbfa 181 }
fd61fd6e 182
2aaefbfa
AD
183 if ($entry->hasAttribute("src")) {
184 $entry->setAttribute("src",
185 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src")));
fd61fd6e 186
2aaefbfa 187 }
fd61fd6e
AD
188 }
189
2aaefbfa 190 return $r->getContent();
fd61fd6e
AD
191 }
192
2aaefbfa
AD
193 } catch (ParseException $e) {
194 return false;
1ff7ae42 195 }
2aaefbfa 196
1ff7ae42
AD
197 }
198
666cd333
AD
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
1ff7ae42 210 return $article;
6dbd6585
AD
211 }
212
213 function hook_article_filter($article) {
214
215 $enabled_feeds = $this->host->get($this, "enabled_feeds");
5d5f100f
TE
216 if (!is_array($enabled_feeds)) return $article;
217
6dbd6585
AD
218 $key = array_search($article["feed"]["id"], $enabled_feeds);
219 if ($key === FALSE) return $article;
220
221 return $this->process_article($article);
1ff7ae42
AD
222
223 }
224
225 function api_version() {
226 return 2;
227 }
228
229 private function filter_unknown_feeds($enabled_feeds) {
230 $tmp = array();
231
3e220fd1 232 foreach ($enabled_feeds as $feed) {
1ff7ae42 233
0f4487d3
AD
234 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
235 $sth->execute([$feed, $_SESSION['uid']]);
1ff7ae42 236
0f4487d3 237 if ($row = $sth->fetch()) {
3e220fd1 238 array_push($tmp, $feed);
1ff7ae42
AD
239 }
240 }
241
242 return $tmp;
243 }
244
245}