]> git.wh0rd.org - tt-rss.git/blob - plugins/af_readability/init.php
fix previous, oops
[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(__DIR__ . "/classes/Readability.php");
100
101 $tmp = fetch_file_contents($article["link"]);
102
103 if ($tmp) {
104 $r = new Readability($tmp, $article["link"]);
105
106 if ($r->init()) {
107
108 $tmpxpath = new DOMXPath($r->dom);
109
110 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
111
112 foreach ($entries as $entry) {
113 if ($entry->hasAttribute("href")) {
114 $entry->setAttribute("href",
115 rewrite_relative_url($article["link"], $entry->getAttribute("href")));
116
117 }
118
119 if ($entry->hasAttribute("src")) {
120 $entry->setAttribute("src",
121 rewrite_relative_url($article["link"], $entry->getAttribute("src")));
122
123 }
124
125 }
126
127 $article["content"] = $r->articleContent->innerHTML;
128 }
129 }
130
131 return $article;
132
133 }
134
135 function api_version() {
136 return 2;
137 }
138
139 private function filter_unknown_feeds($enabled_feeds) {
140 $tmp = array();
141
142 foreach ($enabled_feeds as $feed) {
143
144 $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
145
146 if (db_num_rows($result) != 0) {
147 array_push($tmp, $feed);
148 }
149 }
150
151 return $tmp;
152 }
153
154 }
155 ?>