]> git.wh0rd.org - tt-rss.git/blame - plugins/af_readability/init.php
add some print_checkbox/print_button calls; rename some plugin preference pane titles
[tt-rss.git] / plugins / af_readability / init.php
CommitLineData
1ff7ae42
AD
1<?php
2class 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
41245888
AD
12 function flags() {
13 return array("needs_curl" => true);
14 }
15
1ff7ae42 16 function save() {
666cd333
AD
17 $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]) == "true";
18
19 $this->host->set($this, "enable_share_anything", $enable_share_anything);
20
21 echo __("Data saved.");
1ff7ae42
AD
22 }
23
24 function init($host)
25 {
26 $this->host = $host;
27
28 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
29 $host->add_hook($host::HOOK_PREFS_TAB, $this);
30 $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
31 $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
6dbd6585
AD
32
33 $host->add_filter_action($this, "action_inline", __("Inline content"));
1ff7ae42
AD
34 }
35
36 function hook_prefs_tab($args) {
37 if ($args != "prefFeeds") return;
38
dc8bd8a6 39 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
1ff7ae42
AD
40
41 print_notice("Enable the plugin for specific feeds in the feed editor.");
42
666cd333
AD
43 print "<form dojoType=\"dijit.form.Form\">";
44
45 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
46 evt.preventDefault();
47 if (this.validate()) {
48 console.log(dojo.objectToQuery(this.getValues()));
49 new Ajax.Request('backend.php', {
50 parameters: dojo.objectToQuery(this.getValues()),
51 onComplete: function(transport) {
52 notify_info(transport.responseText);
53 }
54 });
55 //this.reset();
56 }
57 </script>";
58
328118d1
AD
59 print_hidden("op", "pluginhandler");
60 print_hidden("method", "save");
61 print_hidden("plugin", "af_readability");
666cd333
AD
62
63 $enable_share_anything = $this->host->get($this, "enable_share_anything");
666cd333 64
dc8bd8a6
AD
65 print_checkbox("enable_share_anything", $enable_share_anything);
66 print "&nbsp;<label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
666cd333 67
dc8bd8a6 68 print "<p>"; print_button("submit", __("Save"));
666cd333
AD
69 print "</form>";
70
1ff7ae42 71 $enabled_feeds = $this->host->get($this, "enabled_feeds");
5d5f100f 72 if (!is_array($enabled_feeds)) $enabled_feeds = array();
3e220fd1
TE
73
74 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
1ff7ae42
AD
75 $this->host->set($this, "enabled_feeds", $enabled_feeds);
76
77 if (count($enabled_feeds) > 0) {
78 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
79
80 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
81 foreach ($enabled_feeds as $f) {
82 print "<li>" .
83 "<img src='images/pub_set.png'
84 style='vertical-align : middle'> <a href='#'
85 onclick='editFeed($f)'>".
86 getFeedTitle($f) . "</a></li>";
87 }
88 print "</ul>";
89 }
90
91 print "</div>";
92 }
93
94 function hook_prefs_edit_feed($feed_id) {
95 print "<div class=\"dlgSec\">".__("Readability")."</div>";
96 print "<div class=\"dlgSecCont\">";
97
98 $enabled_feeds = $this->host->get($this, "enabled_feeds");
5d5f100f 99 if (!is_array($enabled_feeds)) $enabled_feeds = array();
1ff7ae42
AD
100
101 $key = array_search($feed_id, $enabled_feeds);
102 $checked = $key !== FALSE ? "checked" : "";
103
104 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
105 name=\"af_readability_enabled\"
106 $checked>&nbsp;<label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
107
108 print "</div>";
109 }
110
111 function hook_prefs_save_feed($feed_id) {
112 $enabled_feeds = $this->host->get($this, "enabled_feeds");
113 if (!is_array($enabled_feeds)) $enabled_feeds = array();
114
115 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
116 $key = array_search($feed_id, $enabled_feeds);
117
118 if ($enable) {
119 if ($key === FALSE) {
120 array_push($enabled_feeds, $feed_id);
121 }
122 } else {
123 if ($key !== FALSE) {
124 unset($enabled_feeds[$key]);
125 }
126 }
127
128 $this->host->set($this, "enabled_feeds", $enabled_feeds);
129 }
130
6dbd6585
AD
131 function hook_article_filter_action($article, $action) {
132 return $this->process_article($article);
133 }
1ff7ae42 134
666cd333 135 public function extract_content($url) {
8b08d9d7 136 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
1ff7ae42 137
aa03bac4
AD
138 if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
139
666cd333 140 $ch = curl_init($url);
aa03bac4 141
831129f6
AD
142 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
143 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
144 curl_setopt($ch, CURLOPT_HEADER, true);
145 curl_setopt($ch, CURLOPT_NOBODY, true);
aa03bac4 146 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
831129f6
AD
147 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
148
149 @$result = curl_exec($ch);
150 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
151
152 if (strpos($content_type, "text/html") === FALSE)
666cd333 153 return false;
831129f6
AD
154 }
155
666cd333 156 $tmp = fetch_file_contents($url);
1ff7ae42 157
e487e92d 158 if ($tmp && mb_strlen($tmp) < 1024 * 500) {
b7d1306b 159 $tmpdoc = new DOMDocument("1.0", "UTF-8");
831129f6 160
f45a1152 161 if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
666cd333 162 return false;
b7d1306b 163
37b2bca9 164 if (strtolower($tmpdoc->encoding) != 'utf-8') {
b7d1306b
AD
165 $tmpxpath = new DOMXPath($tmpdoc);
166
167 foreach ($tmpxpath->query("//meta") as $elem) {
168 $elem->parentNode->removeChild($elem);
169 }
170
171 $tmp = $tmpdoc->saveHTML();
172 }
173
666cd333 174 $r = new Readability($tmp, $url);
1ff7ae42
AD
175
176 if ($r->init()) {
fd61fd6e
AD
177 $tmpxpath = new DOMXPath($r->dom);
178
179 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
180
181 foreach ($entries as $entry) {
182 if ($entry->hasAttribute("href")) {
183 $entry->setAttribute("href",
666cd333 184 rewrite_relative_url($url, $entry->getAttribute("href")));
fd61fd6e
AD
185
186 }
187
188 if ($entry->hasAttribute("src")) {
189 $entry->setAttribute("src",
666cd333 190 rewrite_relative_url($url, $entry->getAttribute("src")));
fd61fd6e
AD
191
192 }
193
194 }
195
666cd333 196 return $r->articleContent->innerHTML;
1ff7ae42
AD
197 }
198 }
199
666cd333
AD
200 return false;
201 }
202
203 function process_article($article) {
204
205 $extracted_content = $this->extract_content($article["link"]);
206
207 if ($extracted_content) {
208 $article["content"] = $extracted_content;
209 }
210
1ff7ae42 211 return $article;
6dbd6585
AD
212 }
213
214 function hook_article_filter($article) {
215
216 $enabled_feeds = $this->host->get($this, "enabled_feeds");
5d5f100f
TE
217 if (!is_array($enabled_feeds)) return $article;
218
6dbd6585
AD
219 $key = array_search($article["feed"]["id"], $enabled_feeds);
220 if ($key === FALSE) return $article;
221
222 return $this->process_article($article);
1ff7ae42
AD
223
224 }
225
226 function api_version() {
227 return 2;
228 }
229
230 private function filter_unknown_feeds($enabled_feeds) {
231 $tmp = array();
232
3e220fd1 233 foreach ($enabled_feeds as $feed) {
1ff7ae42 234
3e220fd1 235 $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1ff7ae42 236
3e220fd1
TE
237 if (db_num_rows($result) != 0) {
238 array_push($tmp, $feed);
1ff7ae42
AD
239 }
240 }
241
242 return $tmp;
243 }
244
245}
246?>