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