]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_readability/init.php
Merge pull request #468 from GregThib/patch-1
[tt-rss.git] / plugins / af_readability / init.php
1 <?php
2 class Af_Readability extends Plugin {
3
4         private $host;
5
6         function about() {
7                 return array(1.0,
8                         "Try to inline article content using Readability",
9                         "fox");
10         }
11
12         function save() {
13                 //
14         }
15
16         function init($host)
17         {
18                 $this->host = $host;
19
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);
24         }
25
26         function hook_prefs_tab($args) {
27                 if ($args != "prefFeeds") return;
28
29                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_readability settings')."\">";
30
31                 print_notice("Enable the plugin for specific feeds in the feed editor.");
32
33                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
34                 if (!array($enabled_feeds)) $enabled_feeds = array();
35
36                 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
37                 $this->host->set($this, "enabled_feeds", $enabled_feeds);
38
39                 if (count($enabled_feeds) > 0) {
40                         print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
41
42                         print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
43                         foreach ($enabled_feeds as $f) {
44                                 print "<li>" .
45                                         "<img src='images/pub_set.png'
46                                                 style='vertical-align : middle'> <a href='#'
47                                                 onclick='editFeed($f)'>".
48                                         getFeedTitle($f) . "</a></li>";
49                         }
50                         print "</ul>";
51                 }
52
53                 print "</div>";
54         }
55
56         function hook_prefs_edit_feed($feed_id) {
57                 print "<div class=\"dlgSec\">".__("Readability")."</div>";
58                 print "<div class=\"dlgSecCont\">";
59
60                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
61                 if (!array($enabled_feeds)) $enabled_feeds = array();
62
63                 $key = array_search($feed_id, $enabled_feeds);
64                 $checked = $key !== FALSE ? "checked" : "";
65
66                 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
67                         name=\"af_readability_enabled\"
68                         $checked>&nbsp;<label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
69
70                 print "</div>";
71         }
72
73         function hook_prefs_save_feed($feed_id) {
74                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
75                 if (!is_array($enabled_feeds)) $enabled_feeds = array();
76
77                 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
78                 $key = array_search($feed_id, $enabled_feeds);
79
80                 if ($enable) {
81                         if ($key === FALSE) {
82                                 array_push($enabled_feeds, $feed_id);
83                         }
84                 } else {
85                         if ($key !== FALSE) {
86                                 unset($enabled_feeds[$key]);
87                         }
88                 }
89
90                 $this->host->set($this, "enabled_feeds", $enabled_feeds);
91         }
92
93         function hook_article_filter($article) {
94
95                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
96                 $key = array_search($article["feed"]["id"], $enabled_feeds);
97                 if ($key === FALSE) return $article;
98
99                 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
100
101                 if (function_exists("curl_init")) {
102                         $ch = curl_init($article["link"]);
103                         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
104                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
105                         curl_setopt($ch, CURLOPT_HEADER, true);
106                         curl_setopt($ch, CURLOPT_NOBODY, true);
107                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
108                                 !ini_get("safe_mode") && !ini_get("open_basedir"));
109                         curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
110
111                         @$result = curl_exec($ch);
112                         $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
113
114                         if (strpos($content_type, "text/html") === FALSE)
115                                 return $article;
116                 }
117
118                 $tmp = fetch_file_contents($article["link"]);
119
120                 if ($tmp) {
121                         $tmpdoc = new DOMDocument("1.0", "UTF-8");
122
123                         if (!$tmpdoc->loadHTML($tmp))
124                                 return $article;
125
126                         if (strtolower($tmpdoc->encoding) != 'utf-8') {
127                                 $tmpxpath = new DOMXPath($tmpdoc);
128
129                                 foreach ($tmpxpath->query("//meta") as $elem) {
130                                         $elem->parentNode->removeChild($elem);
131                                 }
132
133                                 $tmp = $tmpdoc->saveHTML();
134                         }
135
136                         $r = new Readability($tmp, $article["link"]);
137
138                         if ($r->init()) {
139
140                                 $tmpxpath = new DOMXPath($r->dom);
141
142                                 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
143
144                                 foreach ($entries as $entry) {
145                                         if ($entry->hasAttribute("href")) {
146                                                 $entry->setAttribute("href",
147                                                         rewrite_relative_url($article["link"], $entry->getAttribute("href")));
148
149                                         }
150
151                                         if ($entry->hasAttribute("src")) {
152                                                 $entry->setAttribute("src",
153                                                         rewrite_relative_url($article["link"], $entry->getAttribute("src")));
154
155                                         }
156
157                                 }
158
159                                 $article["content"] = $r->articleContent->innerHTML;
160                         }
161                 }
162
163                 return $article;
164
165         }
166
167         function api_version() {
168                 return 2;
169         }
170
171         private function filter_unknown_feeds($enabled_feeds) {
172                 $tmp = array();
173
174                 foreach ($enabled_feeds as $feed) {
175
176                         $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
177
178                         if (db_num_rows($result) != 0) {
179                                 array_push($tmp, $feed);
180                         }
181                 }
182
183                 return $tmp;
184         }
185
186 }
187 ?>