]>
Commit | Line | Data |
---|---|---|
1ff7ae42 AD |
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); | |
6dbd6585 AD |
24 | |
25 | $host->add_filter_action($this, "action_inline", __("Inline content")); | |
1ff7ae42 AD |
26 | } |
27 | ||
28 | function hook_prefs_tab($args) { | |
29 | if ($args != "prefFeeds") return; | |
30 | ||
31 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_readability settings')."\">"; | |
32 | ||
33 | print_notice("Enable the plugin for specific feeds in the feed editor."); | |
34 | ||
35 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
5d5f100f | 36 | if (!is_array($enabled_feeds)) $enabled_feeds = array(); |
3e220fd1 TE |
37 | |
38 | $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds); | |
1ff7ae42 AD |
39 | $this->host->set($this, "enabled_feeds", $enabled_feeds); |
40 | ||
41 | if (count($enabled_feeds) > 0) { | |
42 | print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>"; | |
43 | ||
44 | print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">"; | |
45 | foreach ($enabled_feeds as $f) { | |
46 | print "<li>" . | |
47 | "<img src='images/pub_set.png' | |
48 | style='vertical-align : middle'> <a href='#' | |
49 | onclick='editFeed($f)'>". | |
50 | getFeedTitle($f) . "</a></li>"; | |
51 | } | |
52 | print "</ul>"; | |
53 | } | |
54 | ||
55 | print "</div>"; | |
56 | } | |
57 | ||
58 | function hook_prefs_edit_feed($feed_id) { | |
59 | print "<div class=\"dlgSec\">".__("Readability")."</div>"; | |
60 | print "<div class=\"dlgSecCont\">"; | |
61 | ||
62 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
5d5f100f | 63 | if (!is_array($enabled_feeds)) $enabled_feeds = array(); |
1ff7ae42 AD |
64 | |
65 | $key = array_search($feed_id, $enabled_feeds); | |
66 | $checked = $key !== FALSE ? "checked" : ""; | |
67 | ||
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>"; | |
71 | ||
72 | print "</div>"; | |
73 | } | |
74 | ||
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(); | |
78 | ||
79 | $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true'; | |
80 | $key = array_search($feed_id, $enabled_feeds); | |
81 | ||
82 | if ($enable) { | |
83 | if ($key === FALSE) { | |
84 | array_push($enabled_feeds, $feed_id); | |
85 | } | |
86 | } else { | |
87 | if ($key !== FALSE) { | |
88 | unset($enabled_feeds[$key]); | |
89 | } | |
90 | } | |
91 | ||
92 | $this->host->set($this, "enabled_feeds", $enabled_feeds); | |
93 | } | |
94 | ||
6dbd6585 AD |
95 | function hook_article_filter_action($article, $action) { |
96 | return $this->process_article($article); | |
97 | } | |
1ff7ae42 | 98 | |
6dbd6585 | 99 | function process_article($article) { |
1ff7ae42 | 100 | |
8b08d9d7 | 101 | if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php"); |
1ff7ae42 | 102 | |
aa03bac4 AD |
103 | if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) { |
104 | ||
831129f6 | 105 | $ch = curl_init($article["link"]); |
aa03bac4 | 106 | |
831129f6 AD |
107 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
108 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
109 | curl_setopt($ch, CURLOPT_HEADER, true); | |
110 | curl_setopt($ch, CURLOPT_NOBODY, true); | |
aa03bac4 | 111 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
831129f6 AD |
112 | curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT); |
113 | ||
114 | @$result = curl_exec($ch); | |
115 | $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | |
116 | ||
117 | if (strpos($content_type, "text/html") === FALSE) | |
118 | return $article; | |
119 | } | |
120 | ||
1ff7ae42 AD |
121 | $tmp = fetch_file_contents($article["link"]); |
122 | ||
4d03c5c5 | 123 | if ($tmp && mb_strlen($tmp) < 65535 * 4) { |
b7d1306b | 124 | $tmpdoc = new DOMDocument("1.0", "UTF-8"); |
831129f6 AD |
125 | |
126 | if (!$tmpdoc->loadHTML($tmp)) | |
127 | return $article; | |
b7d1306b | 128 | |
37b2bca9 | 129 | if (strtolower($tmpdoc->encoding) != 'utf-8') { |
b7d1306b AD |
130 | $tmpxpath = new DOMXPath($tmpdoc); |
131 | ||
132 | foreach ($tmpxpath->query("//meta") as $elem) { | |
133 | $elem->parentNode->removeChild($elem); | |
134 | } | |
135 | ||
136 | $tmp = $tmpdoc->saveHTML(); | |
137 | } | |
138 | ||
1ff7ae42 AD |
139 | $r = new Readability($tmp, $article["link"]); |
140 | ||
141 | if ($r->init()) { | |
fd61fd6e AD |
142 | |
143 | $tmpxpath = new DOMXPath($r->dom); | |
144 | ||
145 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); | |
146 | ||
147 | foreach ($entries as $entry) { | |
148 | if ($entry->hasAttribute("href")) { | |
149 | $entry->setAttribute("href", | |
7975ace2 | 150 | rewrite_relative_url($article["link"], $entry->getAttribute("href"))); |
fd61fd6e AD |
151 | |
152 | } | |
153 | ||
154 | if ($entry->hasAttribute("src")) { | |
155 | $entry->setAttribute("src", | |
7975ace2 | 156 | rewrite_relative_url($article["link"], $entry->getAttribute("src"))); |
fd61fd6e AD |
157 | |
158 | } | |
159 | ||
160 | } | |
161 | ||
1ff7ae42 AD |
162 | $article["content"] = $r->articleContent->innerHTML; |
163 | } | |
164 | } | |
165 | ||
166 | return $article; | |
6dbd6585 AD |
167 | } |
168 | ||
169 | function hook_article_filter($article) { | |
170 | ||
171 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
5d5f100f TE |
172 | if (!is_array($enabled_feeds)) return $article; |
173 | ||
6dbd6585 AD |
174 | $key = array_search($article["feed"]["id"], $enabled_feeds); |
175 | if ($key === FALSE) return $article; | |
176 | ||
177 | return $this->process_article($article); | |
1ff7ae42 AD |
178 | |
179 | } | |
180 | ||
181 | function api_version() { | |
182 | return 2; | |
183 | } | |
184 | ||
185 | private function filter_unknown_feeds($enabled_feeds) { | |
186 | $tmp = array(); | |
187 | ||
3e220fd1 | 188 | foreach ($enabled_feeds as $feed) { |
1ff7ae42 | 189 | |
3e220fd1 | 190 | $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); |
1ff7ae42 | 191 | |
3e220fd1 TE |
192 | if (db_num_rows($result) != 0) { |
193 | array_push($tmp, $feed); | |
1ff7ae42 AD |
194 | } |
195 | } | |
196 | ||
197 | return $tmp; | |
198 | } | |
199 | ||
200 | } | |
201 | ?> |