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