]> git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
add HOOK_ARTICLE_LEFT_BUTTON
[tt-rss.git] / classes / pluginhost.php
1 <?php
2 class PluginHost {
3 private $link;
4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
8 private $storage = array();
9 private $feeds = array();
10 private $owner_uid;
11 private $debug;
12
13 const HOOK_ARTICLE_BUTTON = 1;
14 const HOOK_ARTICLE_FILTER = 2;
15 const HOOK_PREFS_TAB = 3;
16 const HOOK_PREFS_TAB_SECTION = 4;
17 const HOOK_PREFS_TABS = 5;
18 const HOOK_FEED_PARSED = 6;
19 const HOOK_UPDATE_TASK = 7;
20 const HOOK_AUTH_USER = 8;
21 const HOOK_HOTKEY_MAP = 9;
22 const HOOK_RENDER_ARTICLE = 10;
23 const HOOK_RENDER_ARTICLE_CDM = 11;
24 const HOOK_FEED_FETCHED = 12;
25 const HOOK_SANITIZE = 13;
26 const HOOK_RENDER_ARTICLE_API = 14;
27 const HOOK_TOOLBAR_BUTTON = 15;
28 const HOOK_ACTION_ITEM = 16;
29 const HOOK_HEADLINE_TOOLBAR_BUTTON = 17;
30 const HOOK_HOTKEY_INFO = 18;
31 const HOOK_ARTICLE_LEFT_BUTTON = 19;
32
33 const KIND_ALL = 1;
34 const KIND_SYSTEM = 2;
35 const KIND_USER = 3;
36
37 function __construct($link) {
38 $this->link = $link;
39
40 $this->storage = $_SESSION["plugin_storage"];
41
42 if (!$this->storage) $this->storage = array();
43 }
44
45 private function register_plugin($name, $plugin) {
46 //array_push($this->plugins, $plugin);
47 $this->plugins[$name] = $plugin;
48 }
49
50 function get_link() {
51 return $this->link;
52 }
53
54 function get_plugins() {
55 return $this->plugins;
56 }
57
58 function get_plugin($name) {
59 return $this->plugins[$name];
60 }
61
62 function run_hooks($type, $method, $args) {
63 foreach ($this->get_hooks($type) as $hook) {
64 $hook->$method($args);
65 }
66 }
67
68 function add_hook($type, $sender) {
69 if (!is_array($this->hooks[$type])) {
70 $this->hooks[$type] = array();
71 }
72
73 array_push($this->hooks[$type], $sender);
74 }
75
76 function del_hook($type, $sender) {
77 if (is_array($this->hooks[$type])) {
78 $key = array_Search($this->hooks[$type], $sender);
79 if ($key !== FALSE) {
80 unset($this->hooks[$type][$key]);
81 }
82 }
83 }
84
85 function get_hooks($type) {
86 if (isset($this->hooks[$type])) {
87 return $this->hooks[$type];
88 } else {
89 return array();
90 }
91 }
92 function load_all($kind, $owner_uid = false) {
93 $plugins = array_map("basename", glob("plugins/*"));
94 $this->load(join(",", $plugins), $kind, $owner_uid);
95 }
96
97 function load($classlist, $kind, $owner_uid = false) {
98 $plugins = explode(",", $classlist);
99
100 $this->owner_uid = (int) $owner_uid;
101
102 foreach ($plugins as $class) {
103 $class = trim($class);
104 $class_file = strtolower(basename($class));
105 $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
106
107 if (!isset($this->plugins[$class])) {
108 if (file_exists($file)) require_once $file;
109
110 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
111 $plugin = new $class($this);
112
113 switch ($kind) {
114 case $this::KIND_SYSTEM:
115 if ($this->is_system($plugin)) {
116 $plugin->init($this);
117 $this->register_plugin($class, $plugin);
118 }
119 break;
120 case $this::KIND_USER:
121 if (!$this->is_system($plugin)) {
122 $plugin->init($this);
123 $this->register_plugin($class, $plugin);
124 }
125 break;
126 case $this::KIND_ALL:
127 $plugin->init($this);
128 $this->register_plugin($class, $plugin);
129 break;
130 }
131 }
132 }
133 }
134 }
135
136 function is_system($plugin) {
137 $about = $plugin->about();
138
139 return @$about[3];
140 }
141
142 // only system plugins are allowed to modify routing
143 function add_handler($handler, $method, $sender) {
144 $handler = str_replace("-", "_", strtolower($handler));
145 $method = strtolower($method);
146
147 if ($this->is_system($sender)) {
148 if (!is_array($this->handlers[$handler])) {
149 $this->handlers[$handler] = array();
150 }
151
152 $this->handlers[$handler][$method] = $sender;
153 }
154 }
155
156 function del_handler($handler, $method) {
157 $handler = str_replace("-", "_", strtolower($handler));
158 $method = strtolower($method);
159
160 if ($this->is_system($sender)) {
161 unset($this->handlers[$handler][$method]);
162 }
163 }
164
165 function lookup_handler($handler, $method) {
166 $handler = str_replace("-", "_", strtolower($handler));
167 $method = strtolower($method);
168
169 if (is_array($this->handlers[$handler])) {
170 if (isset($this->handlers[$handler]["*"])) {
171 return $this->handlers[$handler]["*"];
172 } else {
173 return $this->handlers[$handler][$method];
174 }
175 }
176
177 return false;
178 }
179
180 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
181 $command = str_replace("-", "_", strtolower($command));
182
183 $this->commands[$command] = array("description" => $description,
184 "suffix" => $suffix,
185 "arghelp" => $arghelp,
186 "class" => $sender);
187 }
188
189 function del_command($command) {
190 $command = "-" . strtolower($command);
191
192 unset($this->commands[$command]);
193 }
194
195 function lookup_command($command) {
196 $command = "-" . strtolower($command);
197
198 if (is_array($this->commands[$command])) {
199 return $this->commands[$command]["class"];
200 } else {
201 return false;
202 }
203
204 return false;
205 }
206
207 function get_commands() {
208 return $this->commands;
209 }
210
211 function run_commands($args) {
212 foreach ($this->get_commands() as $command => $data) {
213 if (isset($args[$command])) {
214 $command = str_replace("-", "", $command);
215 $data["class"]->$command($args);
216 }
217 }
218 }
219
220 function load_data($force = false) {
221 if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) {
222 $plugin = db_escape_string($this->link, $plugin);
223
224 $result = db_query($this->link, "SELECT name, content FROM ttrss_plugin_storage
225 WHERE owner_uid = '".$this->owner_uid."'");
226
227 while ($line = db_fetch_assoc($result)) {
228 $this->storage[$line["name"]] = unserialize($line["content"]);
229 }
230
231 $_SESSION["plugin_storage"] = $this->storage;
232 }
233 }
234
235 private function save_data($plugin) {
236 if ($this->owner_uid) {
237 $plugin = db_escape_string($this->link, $plugin);
238
239 db_query($this->link, "BEGIN");
240
241 $result = db_query($this->link,"SELECT id FROM ttrss_plugin_storage WHERE
242 owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
243
244 if (!isset($this->storage[$plugin]))
245 $this->storage[$plugin] = array();
246
247 $content = db_escape_string($this->link, serialize($this->storage[$plugin]));
248
249 if (db_num_rows($result) != 0) {
250 db_query($this->link, "UPDATE ttrss_plugin_storage SET content = '$content'
251 WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
252
253 } else {
254 db_query($this->link, "INSERT INTO ttrss_plugin_storage
255 (name,owner_uid,content) VALUES
256 ('$plugin','".$this->owner_uid."','$content')");
257 }
258
259 db_query($this->link, "COMMIT");
260 }
261 }
262
263 function set($sender, $name, $value, $sync = true) {
264 $idx = get_class($sender);
265
266 if (!isset($this->storage[$idx]))
267 $this->storage[$idx] = array();
268
269 $this->storage[$idx][$name] = $value;
270
271 $_SESSION["plugin_storage"] = $this->storage;
272
273 if ($sync) $this->save_data(get_class($sender));
274 }
275
276 function get($sender, $name, $default_value = false) {
277 $idx = get_class($sender);
278
279 if (isset($this->storage[$idx][$name])) {
280 return $this->storage[$idx][$name];
281 } else {
282 return $default_value;
283 }
284 }
285
286 function get_all($sender) {
287 $idx = get_class($sender);
288
289 return $this->storage[$idx];
290 }
291
292 function clear_data($sender) {
293 if ($this->owner_uid) {
294 $idx = get_class($sender);
295
296 unset($this->storage[$idx]);
297
298 db_query($this->link, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
299 AND owner_uid = " . $this->owner_uid);
300
301 $_SESSION["plugin_storage"] = $this->storage;
302 }
303 }
304
305 function set_debug($debug) {
306 $this->debug = $debug;
307 }
308
309 function get_debug() {
310 return $this->debug;
311 }
312
313 // Plugin feed functions are *EXPERIMENTAL*!
314
315 // cat_id: only -1 is supported (Special)
316 function add_feed($cat_id, $title, $icon, $sender) {
317 if (!$this->feeds[$cat_id]) $this->feeds[$cat_id] = array();
318
319 $id = count($this->feeds[$cat_id]);
320
321 array_push($this->feeds[$cat_id],
322 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
323
324 return $id;
325 }
326
327 function get_feeds($cat_id) {
328 return $this->feeds[$cat_id];
329 }
330
331 // convert feed_id (e.g. -129) to pfeed_id first
332 function get_feed_handler($pfeed_id) {
333 foreach ($this->feeds as $cat) {
334 foreach ($cat as $feed) {
335 if ($feed['id'] == $pfeed_id) {
336 return $feed['sender'];
337 }
338 }
339 }
340 }
341
342 static function pfeed_to_feed_id($label) {
343 return PLUGIN_FEED_BASE_INDEX - 1 - abs($label);
344 }
345
346 static function feed_to_pfeed_id($feed) {
347 return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed);
348 }
349
350 }
351 ?>