]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_readability/init.php
2 class Af_Readability
extends Plugin
{
4 /* @var PluginHost $host */
9 "Try to inline article content using Readability",
14 return array("needs_curl" => true);
18 $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
20 $this->host
->set($this, "enable_share_anything", $enable_share_anything);
22 echo __("Data saved.");
29 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
30 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
31 $host->add_hook($host::HOOK_PREFS_EDIT_FEED
, $this);
32 $host->add_hook($host::HOOK_PREFS_SAVE_FEED
, $this);
34 $host->add_filter_action($this, "action_inline", __("Inline content"));
37 function hook_prefs_tab($args) {
38 if ($args != "prefFeeds") return;
40 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
42 print_notice("Enable the plugin for specific feeds in the feed editor.");
44 print "<form dojoType=\"dijit.form.Form\">";
46 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
48 if (this.validate()) {
49 console.log(dojo.objectToQuery(this.getValues()));
50 new Ajax.Request('backend.php', {
51 parameters: dojo.objectToQuery(this.getValues()),
52 onComplete: function(transport) {
53 notify_info(transport.responseText);
60 print_hidden("op", "pluginhandler");
61 print_hidden("method", "save");
62 print_hidden("plugin", "af_readability");
64 $enable_share_anything = $this->host
->get($this, "enable_share_anything");
66 print_checkbox("enable_share_anything", $enable_share_anything);
67 print " <label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
69 print "<p>"; print_button("submit", __("Save"));
72 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
73 if (!is_array($enabled_feeds)) $enabled_feeds = array();
75 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
76 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
78 if (count($enabled_feeds) > 0) {
79 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
81 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
82 foreach ($enabled_feeds as $f) {
84 "<img src='images/pub_set.png'
85 style='vertical-align : middle'> <a href='#'
86 onclick='editFeed($f)'>".
87 Feeds
::getFeedTitle($f) . "</a></li>";
95 function hook_prefs_edit_feed($feed_id) {
96 print "<div class=\"dlgSec\">".__("Readability")."</div>";
97 print "<div class=\"dlgSecCont\">";
99 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
100 if (!is_array($enabled_feeds)) $enabled_feeds = array();
102 $key = array_search($feed_id, $enabled_feeds);
103 $checked = $key !== FALSE ?
"checked" : "";
105 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
106 name=\"af_readability_enabled\"
107 $checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
112 function hook_prefs_save_feed($feed_id) {
113 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
114 if (!is_array($enabled_feeds)) $enabled_feeds = array();
116 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
117 $key = array_search($feed_id, $enabled_feeds);
120 if ($key === FALSE) {
121 array_push($enabled_feeds, $feed_id);
124 if ($key !== FALSE) {
125 unset($enabled_feeds[$key]);
129 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
133 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
135 function hook_article_filter_action($article, $action) {
136 return $this->process_article($article);
139 public function extract_content($url) {
140 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
142 if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
144 $ch = curl_init($url);
146 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
147 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
148 curl_setopt($ch, CURLOPT_HEADER
, true);
149 curl_setopt($ch, CURLOPT_NOBODY
, true);
150 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
151 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
154 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
156 if (strpos($content_type, "text/html") === FALSE)
160 $tmp = fetch_file_contents($url);
162 if ($tmp && mb_strlen($tmp) < 1024 * 500) {
163 $tmpdoc = new DOMDocument("1.0", "UTF-8");
165 if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
168 if (strtolower($tmpdoc->encoding
) != 'utf-8') {
169 $tmpxpath = new DOMXPath($tmpdoc);
171 foreach ($tmpxpath->query("//meta") as $elem) {
172 $elem->parentNode
->removeChild($elem);
175 $tmp = $tmpdoc->saveHTML();
178 $r = new Readability($tmp, $url);
181 $tmpxpath = new DOMXPath($r->dom
);
183 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
185 foreach ($entries as $entry) {
186 if ($entry->hasAttribute("href")) {
187 $entry->setAttribute("href",
188 rewrite_relative_url($url, $entry->getAttribute("href")));
192 if ($entry->hasAttribute("src")) {
193 $entry->setAttribute("src",
194 rewrite_relative_url($url, $entry->getAttribute("src")));
200 return $r->articleContent
->innerHTML
;
207 function process_article($article) {
209 $extracted_content = $this->extract_content($article["link"]);
211 if ($extracted_content) {
212 $article["content"] = $extracted_content;
218 function hook_article_filter($article) {
220 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
221 if (!is_array($enabled_feeds)) return $article;
223 $key = array_search($article["feed"]["id"], $enabled_feeds);
224 if ($key === FALSE) return $article;
226 return $this->process_article($article);
230 function api_version() {
234 private function filter_unknown_feeds($enabled_feeds) {
237 foreach ($enabled_feeds as $feed) {
239 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
240 $sth->execute([$feed, $_SESSION['uid']]);
242 if ($row = $sth->fetch()) {
243 array_push($tmp, $feed);