]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_readability/init.php
a52b745b857f90ca37c1f166f9742cbafd25dbeb
2 class Af_Readability
extends Plugin
{
8 "Try to inline article content using Readability",
13 return array("needs_curl" => true);
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);
29 $host->add_filter_action($this, "action_inline", __("Inline content"));
32 function hook_prefs_tab($args) {
33 if ($args != "prefFeeds") return;
35 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_readability settings')."\">";
37 print_notice("Enable the plugin for specific feeds in the feed editor.");
39 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
40 if (!is_array($enabled_feeds)) $enabled_feeds = array();
42 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
43 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
45 if (count($enabled_feeds) > 0) {
46 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
48 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
49 foreach ($enabled_feeds as $f) {
51 "<img src='images/pub_set.png'
52 style='vertical-align : middle'> <a href='#'
53 onclick='editFeed($f)'>".
54 getFeedTitle($f) . "</a></li>";
62 function hook_prefs_edit_feed($feed_id) {
63 print "<div class=\"dlgSec\">".__("Readability")."</div>";
64 print "<div class=\"dlgSecCont\">";
66 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
67 if (!is_array($enabled_feeds)) $enabled_feeds = array();
69 $key = array_search($feed_id, $enabled_feeds);
70 $checked = $key !== FALSE ?
"checked" : "";
72 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
73 name=\"af_readability_enabled\"
74 $checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
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();
83 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
84 $key = array_search($feed_id, $enabled_feeds);
88 array_push($enabled_feeds, $feed_id);
92 unset($enabled_feeds[$key]);
96 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
99 function hook_article_filter_action($article, $action) {
100 return $this->process_article($article);
103 function process_article($article) {
105 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
107 if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
109 $ch = curl_init($article["link"]);
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
);
118 @$result = curl_exec($ch);
119 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
121 if (strpos($content_type, "text/html") === FALSE)
125 $tmp = fetch_file_contents($article["link"]);
127 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
128 $tmpdoc = new DOMDocument("1.0", "UTF-8");
130 if (!$tmpdoc->loadHTML($tmp))
133 if (strtolower($tmpdoc->encoding
) != 'utf-8') {
134 $tmpxpath = new DOMXPath($tmpdoc);
136 foreach ($tmpxpath->query("//meta") as $elem) {
137 $elem->parentNode
->removeChild($elem);
140 $tmp = $tmpdoc->saveHTML();
143 $r = new Readability($tmp, $article["link"]);
147 $tmpxpath = new DOMXPath($r->dom
);
149 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
151 foreach ($entries as $entry) {
152 if ($entry->hasAttribute("href")) {
153 $entry->setAttribute("href",
154 rewrite_relative_url($article["link"], $entry->getAttribute("href")));
158 if ($entry->hasAttribute("src")) {
159 $entry->setAttribute("src",
160 rewrite_relative_url($article["link"], $entry->getAttribute("src")));
166 $article["content"] = $r->articleContent
->innerHTML
;
173 function hook_article_filter($article) {
175 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
176 if (!is_array($enabled_feeds)) return $article;
178 $key = array_search($article["feed"]["id"], $enabled_feeds);
179 if ($key === FALSE) return $article;
181 return $this->process_article($article);
185 function api_version() {
189 private function filter_unknown_feeds($enabled_feeds) {
192 foreach ($enabled_feeds as $feed) {
194 $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
196 if (db_num_rows($result) != 0) {
197 array_push($tmp, $feed);