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