2 use andreskrey\Readability\Readability
;
3 use andreskrey\Readability\Configuration
;
5 class Af_Readability
extends Plugin
{
7 /* @var PluginHost $host */
12 "Try to inline article content using Readability",
17 return array("needs_curl" => true);
21 $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
23 $this->host
->set($this, "enable_share_anything", $enable_share_anything);
25 echo __("Data saved.");
32 if (version_compare(PHP_VERSION
, '5.6.0', '<')) {
36 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
37 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
38 $host->add_hook($host::HOOK_PREFS_EDIT_FEED
, $this);
39 $host->add_hook($host::HOOK_PREFS_SAVE_FEED
, $this);
41 $host->add_filter_action($this, "action_inline", __("Inline content"));
44 function hook_prefs_tab($args) {
45 if ($args != "prefFeeds") return;
47 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
49 if (version_compare(PHP_VERSION
, '5.6.0', '<')) {
50 print_error("This plugin requires PHP version 5.6.");
53 print_notice("Enable the plugin for specific feeds in the feed editor.");
55 print "<form dojoType=\"dijit.form.Form\">";
57 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
59 if (this.validate()) {
60 console.log(dojo.objectToQuery(this.getValues()));
61 new Ajax.Request('backend.php', {
62 parameters: dojo.objectToQuery(this.getValues()),
63 onComplete: function(transport) {
64 notify_info(transport.responseText);
71 print_hidden("op", "pluginhandler");
72 print_hidden("method", "save");
73 print_hidden("plugin", "af_readability");
75 $enable_share_anything = $this->host
->get($this, "enable_share_anything");
77 print_checkbox("enable_share_anything", $enable_share_anything);
78 print " <label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
80 print "<p>"; print_button("submit", __("Save"));
83 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
84 if (!is_array($enabled_feeds)) $enabled_feeds = array();
86 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
87 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
89 if (count($enabled_feeds) > 0) {
90 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
92 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
93 foreach ($enabled_feeds as $f) {
95 "<img src='images/pub_set.png'
96 style='vertical-align : middle'> <a href='#'
97 onclick='editFeed($f)'>".
98 Feeds
::getFeedTitle($f) . "</a></li>";
106 function hook_prefs_edit_feed($feed_id) {
107 print "<div class=\"dlgSec\">".__("Readability")."</div>";
108 print "<div class=\"dlgSecCont\">";
110 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
111 if (!is_array($enabled_feeds)) $enabled_feeds = array();
113 $key = array_search($feed_id, $enabled_feeds);
114 $checked = $key !== FALSE ?
"checked" : "";
116 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
117 name=\"af_readability_enabled\"
118 $checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
123 function hook_prefs_save_feed($feed_id) {
124 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
125 if (!is_array($enabled_feeds)) $enabled_feeds = array();
127 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
128 $key = array_search($feed_id, $enabled_feeds);
131 if ($key === FALSE) {
132 array_push($enabled_feeds, $feed_id);
135 if ($key !== FALSE) {
136 unset($enabled_feeds[$key]);
140 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
144 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
146 function hook_article_filter_action($article, $action) {
147 return $this->process_article($article);
150 public function extract_content($url) {
151 global $fetch_effective_url;
153 $tmp = fetch_file_contents([
155 "http_accept" => "text/*",
156 "type" => "text/html"]);
158 if ($tmp && mb_strlen($tmp) < 1024 * 500) {
159 $tmpdoc = new DOMDocument("1.0", "UTF-8");
161 if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
164 if (strtolower($tmpdoc->encoding
) != 'utf-8') {
165 $tmpxpath = new DOMXPath($tmpdoc);
167 foreach ($tmpxpath->query("//meta") as $elem) {
168 $elem->parentNode
->removeChild($elem);
171 $tmp = $tmpdoc->saveHTML();
174 $r = new Readability(new Configuration());
177 if ($r->parse($tmp)) {
179 $tmpxpath = new DOMXPath($r->getDOMDOcument());
180 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
182 foreach ($entries as $entry) {
183 if ($entry->hasAttribute("href")) {
184 $entry->setAttribute("href",
185 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href")));
189 if ($entry->hasAttribute("src")) {
190 $entry->setAttribute("src",
191 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src")));
196 return $r->getContent();
199 } catch (Exception
$e) {
208 function process_article($article) {
210 $extracted_content = $this->extract_content($article["link"]);
212 # let's see if there's anything of value in there
213 $content_test = trim(strip_tags(sanitize($extracted_content)));
216 $article["content"] = $extracted_content;
222 function hook_article_filter($article) {
224 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
225 if (!is_array($enabled_feeds)) return $article;
227 $key = array_search($article["feed"]["id"], $enabled_feeds);
228 if ($key === FALSE) return $article;
230 return $this->process_article($article);
234 function api_version() {
238 private function filter_unknown_feeds($enabled_feeds) {
241 foreach ($enabled_feeds as $feed) {
243 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
244 $sth->execute([$feed, $_SESSION['uid']]);
246 if ($row = $sth->fetch()) {
247 array_push($tmp, $feed);