]>
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)) {
37 $enabled_feeds = array();
39 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
41 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
43 if (count($enabled_feeds) > 0) {
44 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
46 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
47 foreach ($enabled_feeds as $f) {
49 "<img src='images/pub_set.png'
50 style='vertical-align : middle'> <a href='#'
51 onclick='editFeed($f)'>".
52 getFeedTitle($f) . "</a></li>";
60 function hook_prefs_edit_feed($feed_id) {
61 print "<div class=\"dlgSec\">".__("Readability")."</div>";
62 print "<div class=\"dlgSecCont\">";
64 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
65 if (!array($enabled_feeds)) $enabled_feeds = array();
67 $key = array_search($feed_id, $enabled_feeds);
68 $checked = $key !== FALSE ?
"checked" : "";
70 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
71 name=\"af_readability_enabled\"
72 $checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
77 function hook_prefs_save_feed($feed_id) {
78 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
79 if (!is_array($enabled_feeds)) $enabled_feeds = array();
81 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
82 $key = array_search($feed_id, $enabled_feeds);
86 array_push($enabled_feeds, $feed_id);
90 unset($enabled_feeds[$key]);
94 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
97 function hook_article_filter_action($article, $action) {
98 return $this->process_article($article);
101 function process_article($article) {
103 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
105 if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
107 $ch = curl_init($article["link"]);
109 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
110 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
111 curl_setopt($ch, CURLOPT_HEADER
, true);
112 curl_setopt($ch, CURLOPT_NOBODY
, true);
113 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
114 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
116 @$result = curl_exec($ch);
117 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
119 if (strpos($content_type, "text/html") === FALSE)
123 $tmp = fetch_file_contents($article["link"]);
125 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
126 $tmpdoc = new DOMDocument("1.0", "UTF-8");
128 if (!$tmpdoc->loadHTML($tmp))
131 if (strtolower($tmpdoc->encoding
) != 'utf-8') {
132 $tmpxpath = new DOMXPath($tmpdoc);
134 foreach ($tmpxpath->query("//meta") as $elem) {
135 $elem->parentNode
->removeChild($elem);
138 $tmp = $tmpdoc->saveHTML();
141 $r = new Readability($tmp, $article["link"]);
145 $tmpxpath = new DOMXPath($r->dom
);
147 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
149 foreach ($entries as $entry) {
150 if ($entry->hasAttribute("href")) {
151 $entry->setAttribute("href",
152 rewrite_relative_url($article["link"], $entry->getAttribute("href")));
156 if ($entry->hasAttribute("src")) {
157 $entry->setAttribute("src",
158 rewrite_relative_url($article["link"], $entry->getAttribute("src")));
164 $article["content"] = $r->articleContent
->innerHTML
;
171 function hook_article_filter($article) {
173 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
174 $key = array_search($article["feed"]["id"], $enabled_feeds);
175 if ($key === FALSE) return $article;
177 return $this->process_article($article);
181 function api_version() {
185 private function filter_unknown_feeds($enabled_feeds) {
188 if (!empty($enabled_feeds)) {
189 foreach ($enabled_feeds as $feed) {
191 $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
193 if (db_num_rows($result) != 0) {
194 array_push($tmp, $feed);