]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_readability/init.php
2 class Af_Readability
extends Plugin
{
8 "Try to inline article content using Readability",
20 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
21 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
22 $host->add_hook($host::HOOK_PREFS_EDIT_FEED
, $this);
23 $host->add_hook($host::HOOK_PREFS_SAVE_FEED
, $this);
25 $host->add_filter_action($this, "action_inline", __("Inline content"));
28 function hook_prefs_tab($args) {
29 if ($args != "prefFeeds") return;
31 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_readability settings')."\">";
33 print_notice("Enable the plugin for specific feeds in the feed editor.");
35 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
36 if (!array($enabled_feeds)) $enabled_feeds = array();
38 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
39 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
41 if (count($enabled_feeds) > 0) {
42 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
44 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
45 foreach ($enabled_feeds as $f) {
47 "<img src='images/pub_set.png'
48 style='vertical-align : middle'> <a href='#'
49 onclick='editFeed($f)'>".
50 getFeedTitle($f) . "</a></li>";
58 function hook_prefs_edit_feed($feed_id) {
59 print "<div class=\"dlgSec\">".__("Readability")."</div>";
60 print "<div class=\"dlgSecCont\">";
62 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
63 if (!array($enabled_feeds)) $enabled_feeds = array();
65 $key = array_search($feed_id, $enabled_feeds);
66 $checked = $key !== FALSE ?
"checked" : "";
68 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
69 name=\"af_readability_enabled\"
70 $checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
75 function hook_prefs_save_feed($feed_id) {
76 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
77 if (!is_array($enabled_feeds)) $enabled_feeds = array();
79 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
80 $key = array_search($feed_id, $enabled_feeds);
84 array_push($enabled_feeds, $feed_id);
88 unset($enabled_feeds[$key]);
92 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
95 function hook_article_filter_action($article, $action) {
96 return $this->process_article($article);
99 function process_article($article) {
101 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
103 if (function_exists("curl_init")) {
104 $ch = curl_init($article["link"]);
105 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
106 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
107 curl_setopt($ch, CURLOPT_HEADER
, true);
108 curl_setopt($ch, CURLOPT_NOBODY
, true);
109 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, !ini_get("open_basedir"));
110 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
112 @$result = curl_exec($ch);
113 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
115 if (strpos($content_type, "text/html") === FALSE)
119 $tmp = fetch_file_contents($article["link"]);
121 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
122 $tmpdoc = new DOMDocument("1.0", "UTF-8");
124 if (!$tmpdoc->loadHTML($tmp))
127 if (strtolower($tmpdoc->encoding
) != 'utf-8') {
128 $tmpxpath = new DOMXPath($tmpdoc);
130 foreach ($tmpxpath->query("//meta") as $elem) {
131 $elem->parentNode
->removeChild($elem);
134 $tmp = $tmpdoc->saveHTML();
137 $r = new Readability($tmp, $article["link"]);
141 $tmpxpath = new DOMXPath($r->dom
);
143 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
145 foreach ($entries as $entry) {
146 if ($entry->hasAttribute("href")) {
147 $entry->setAttribute("href",
148 rewrite_relative_url($article["link"], $entry->getAttribute("href")));
152 if ($entry->hasAttribute("src")) {
153 $entry->setAttribute("src",
154 rewrite_relative_url($article["link"], $entry->getAttribute("src")));
160 $article["content"] = $r->articleContent
->innerHTML
;
167 function hook_article_filter($article) {
169 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
170 $key = array_search($article["feed"]["id"], $enabled_feeds);
171 if ($key === FALSE) return $article;
173 return $this->process_article($article);
177 function api_version() {
181 private function filter_unknown_feeds($enabled_feeds) {
184 foreach ($enabled_feeds as $feed) {
186 $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
188 if (db_num_rows($result) != 0) {
189 array_push($tmp, $feed);