]>
Commit | Line | Data |
---|---|---|
1ff7ae42 | 1 | <?php |
2aaefbfa AD |
2 | use andreskrey\Readability\Readability; |
3 | use andreskrey\Readability\Configuration; | |
4 | ||
1ff7ae42 AD |
5 | class Af_Readability extends Plugin { |
6 | ||
0f4487d3 | 7 | /* @var PluginHost $host */ |
1ff7ae42 AD |
8 | private $host; |
9 | ||
10 | function about() { | |
11 | return array(1.0, | |
12 | "Try to inline article content using Readability", | |
13 | "fox"); | |
14 | } | |
15 | ||
41245888 AD |
16 | function flags() { |
17 | return array("needs_curl" => true); | |
18 | } | |
19 | ||
1ff7ae42 | 20 | function save() { |
da9ea57d | 21 | $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]); |
666cd333 AD |
22 | |
23 | $this->host->set($this, "enable_share_anything", $enable_share_anything); | |
24 | ||
25 | echo __("Data saved."); | |
1ff7ae42 AD |
26 | } |
27 | ||
28 | function init($host) | |
29 | { | |
30 | $this->host = $host; | |
31 | ||
6d95e535 AD |
32 | if (version_compare(PHP_VERSION, '5.6.0', '<')) { |
33 | return; | |
34 | } | |
35 | ||
1ff7ae42 AD |
36 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
37 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
38 | $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); | |
39 | $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); | |
6dbd6585 AD |
40 | |
41 | $host->add_filter_action($this, "action_inline", __("Inline content")); | |
1ff7ae42 AD |
42 | } |
43 | ||
44 | function hook_prefs_tab($args) { | |
45 | if ($args != "prefFeeds") return; | |
46 | ||
dc8bd8a6 | 47 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">"; |
1ff7ae42 | 48 | |
6d95e535 AD |
49 | if (version_compare(PHP_VERSION, '5.6.0', '<')) { |
50 | print_error("This plugin requires PHP version 5.6."); | |
51 | } | |
52 | ||
1ff7ae42 AD |
53 | print_notice("Enable the plugin for specific feeds in the feed editor."); |
54 | ||
666cd333 AD |
55 | print "<form dojoType=\"dijit.form.Form\">"; |
56 | ||
57 | print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> | |
58 | evt.preventDefault(); | |
59 | if (this.validate()) { | |
60 | console.log(dojo.objectToQuery(this.getValues())); | |
61 | new Ajax.Request('backend.php', { | |
62 | parameters: dojo.objectToQuery(this.getValues()), | |
63 | onComplete: function(transport) { | |
64 | notify_info(transport.responseText); | |
65 | } | |
66 | }); | |
67 | //this.reset(); | |
68 | } | |
69 | </script>"; | |
70 | ||
328118d1 AD |
71 | print_hidden("op", "pluginhandler"); |
72 | print_hidden("method", "save"); | |
73 | print_hidden("plugin", "af_readability"); | |
666cd333 AD |
74 | |
75 | $enable_share_anything = $this->host->get($this, "enable_share_anything"); | |
666cd333 | 76 | |
dc8bd8a6 AD |
77 | print_checkbox("enable_share_anything", $enable_share_anything); |
78 | print " <label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>"; | |
666cd333 | 79 | |
dc8bd8a6 | 80 | print "<p>"; print_button("submit", __("Save")); |
666cd333 AD |
81 | print "</form>"; |
82 | ||
1ff7ae42 | 83 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
5d5f100f | 84 | if (!is_array($enabled_feeds)) $enabled_feeds = array(); |
3e220fd1 TE |
85 | |
86 | $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds); | |
1ff7ae42 AD |
87 | $this->host->set($this, "enabled_feeds", $enabled_feeds); |
88 | ||
89 | if (count($enabled_feeds) > 0) { | |
90 | print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>"; | |
91 | ||
92 | print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">"; | |
93 | foreach ($enabled_feeds as $f) { | |
94 | print "<li>" . | |
95 | "<img src='images/pub_set.png' | |
96 | style='vertical-align : middle'> <a href='#' | |
97 | onclick='editFeed($f)'>". | |
86a8351c | 98 | Feeds::getFeedTitle($f) . "</a></li>"; |
1ff7ae42 AD |
99 | } |
100 | print "</ul>"; | |
101 | } | |
102 | ||
103 | print "</div>"; | |
104 | } | |
105 | ||
106 | function hook_prefs_edit_feed($feed_id) { | |
107 | print "<div class=\"dlgSec\">".__("Readability")."</div>"; | |
108 | print "<div class=\"dlgSecCont\">"; | |
109 | ||
110 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
5d5f100f | 111 | if (!is_array($enabled_feeds)) $enabled_feeds = array(); |
1ff7ae42 AD |
112 | |
113 | $key = array_search($feed_id, $enabled_feeds); | |
114 | $checked = $key !== FALSE ? "checked" : ""; | |
115 | ||
116 | print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\" | |
117 | name=\"af_readability_enabled\" | |
118 | $checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>"; | |
119 | ||
120 | print "</div>"; | |
121 | } | |
122 | ||
123 | function hook_prefs_save_feed($feed_id) { | |
124 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
125 | if (!is_array($enabled_feeds)) $enabled_feeds = array(); | |
126 | ||
da9ea57d | 127 | $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]); |
1ff7ae42 AD |
128 | $key = array_search($feed_id, $enabled_feeds); |
129 | ||
130 | if ($enable) { | |
131 | if ($key === FALSE) { | |
132 | array_push($enabled_feeds, $feed_id); | |
133 | } | |
134 | } else { | |
135 | if ($key !== FALSE) { | |
136 | unset($enabled_feeds[$key]); | |
137 | } | |
138 | } | |
139 | ||
140 | $this->host->set($this, "enabled_feeds", $enabled_feeds); | |
141 | } | |
142 | ||
21ce7d9e AD |
143 | /** |
144 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
145 | */ | |
6dbd6585 AD |
146 | function hook_article_filter_action($article, $action) { |
147 | return $this->process_article($article); | |
148 | } | |
1ff7ae42 | 149 | |
666cd333 | 150 | public function extract_content($url) { |
e26cb061 | 151 | global $fetch_effective_url; |
831129f6 | 152 | |
b037ffd6 AD |
153 | $tmp = fetch_file_contents([ |
154 | "url" => $url, | |
155 | "http_accept" => "text/*", | |
156 | "type" => "text/html"]); | |
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 | ||
2aaefbfa | 174 | $r = new Readability(new Configuration()); |
1ff7ae42 | 175 | |
2aaefbfa AD |
176 | try { |
177 | if ($r->parse($tmp)) { | |
fd61fd6e | 178 | |
2aaefbfa AD |
179 | $tmpxpath = new DOMXPath($r->getDOMDOcument()); |
180 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); | |
fd61fd6e | 181 | |
2aaefbfa AD |
182 | foreach ($entries as $entry) { |
183 | if ($entry->hasAttribute("href")) { | |
184 | $entry->setAttribute("href", | |
185 | rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href"))); | |
fd61fd6e | 186 | |
2aaefbfa | 187 | } |
fd61fd6e | 188 | |
2aaefbfa AD |
189 | if ($entry->hasAttribute("src")) { |
190 | $entry->setAttribute("src", | |
191 | rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src"))); | |
fd61fd6e | 192 | |
2aaefbfa | 193 | } |
fd61fd6e AD |
194 | } |
195 | ||
2aaefbfa | 196 | return $r->getContent(); |
fd61fd6e AD |
197 | } |
198 | ||
c925f4e3 | 199 | } catch (Exception $e) { |
2aaefbfa | 200 | return false; |
1ff7ae42 | 201 | } |
2aaefbfa | 202 | |
1ff7ae42 AD |
203 | } |
204 | ||
666cd333 AD |
205 | return false; |
206 | } | |
207 | ||
208 | function process_article($article) { | |
209 | ||
210 | $extracted_content = $this->extract_content($article["link"]); | |
211 | ||
6d95e535 AD |
212 | # let's see if there's anything of value in there |
213 | $content_test = trim(strip_tags(sanitize($extracted_content))); | |
214 | ||
215 | if ($content_test) { | |
666cd333 AD |
216 | $article["content"] = $extracted_content; |
217 | } | |
218 | ||
1ff7ae42 | 219 | return $article; |
6dbd6585 AD |
220 | } |
221 | ||
222 | function hook_article_filter($article) { | |
223 | ||
224 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
5d5f100f TE |
225 | if (!is_array($enabled_feeds)) return $article; |
226 | ||
6dbd6585 AD |
227 | $key = array_search($article["feed"]["id"], $enabled_feeds); |
228 | if ($key === FALSE) return $article; | |
229 | ||
230 | return $this->process_article($article); | |
1ff7ae42 AD |
231 | |
232 | } | |
233 | ||
234 | function api_version() { | |
235 | return 2; | |
236 | } | |
237 | ||
238 | private function filter_unknown_feeds($enabled_feeds) { | |
239 | $tmp = array(); | |
240 | ||
3e220fd1 | 241 | foreach ($enabled_feeds as $feed) { |
1ff7ae42 | 242 | |
0f4487d3 AD |
243 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?"); |
244 | $sth->execute([$feed, $_SESSION['uid']]); | |
1ff7ae42 | 245 | |
0f4487d3 | 246 | if ($row = $sth->fetch()) { |
3e220fd1 | 247 | array_push($tmp, $feed); |
1ff7ae42 AD |
248 | } |
249 | } | |
250 | ||
251 | return $tmp; | |
252 | } | |
253 | ||
254 | } |