]> git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
Merge pull request #74 from gvmelle/master
[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 private $debug;
11
12 const HOOK_ARTICLE_BUTTON = 1;
13 const HOOK_ARTICLE_FILTER = 2;
14 const HOOK_PREFS_TAB = 3;
15 const HOOK_PREFS_TAB_SECTION = 4;
16 const HOOK_PREFS_TABS = 5;
17 const HOOK_FEED_PARSED = 6;
18 const HOOK_UPDATE_TASK = 7;
19 const HOOK_AUTH_USER = 8;
20 const HOOK_HOTKEY_MAP = 9;
21 const HOOK_RENDER_ARTICLE = 10;
22 const HOOK_RENDER_ARTICLE_CDM = 11;
23 const HOOK_FEED_FETCHED = 12;
24 const HOOK_SANITIZE = 13;
25 const HOOK_RENDER_ARTICLE_API = 14;
26
27 const KIND_ALL = 1;
28 const KIND_SYSTEM = 2;
29 const KIND_USER = 3;
30
31 function __construct($link) {
32 $this->link = $link;
33
34 $this->storage = $_SESSION["plugin_storage"];
35
36 if (!$this->storage) $this->storage = array();
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
56 function run_hooks($type, $method, $args) {
57 foreach ($this->get_hooks($type) as $hook) {
58 $hook->$method($args);
59 }
60 }
61
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) {
80 if (isset($this->hooks[$type])) {
81 return $this->hooks[$type];
82 } else {
83 return array();
84 }
85 }
86 function load_all($kind, $owner_uid = false) {
87 $plugins = array_map("basename", glob("plugins/*"));
88 $this->load(join(",", $plugins), $kind, $owner_uid);
89 }
90
91 function load($classlist, $kind, $owner_uid = false) {
92 $plugins = explode(",", $classlist);
93
94 $this->owner_uid = (int) $owner_uid;
95
96 foreach ($plugins as $class) {
97 $class = trim($class);
98 $class_file = strtolower(basename($class));
99 $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
100
101 if (!isset($this->plugins[$class])) {
102 if (file_exists($file)) require_once $file;
103
104 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
105 $plugin = new $class($this);
106
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 }
125 }
126 }
127 }
128 }
129
130 function is_system($plugin) {
131 $about = $plugin->about();
132
133 return @$about[3];
134 }
135
136 // only system plugins are allowed to modify routing
137 function add_handler($handler, $method, $sender) {
138 $handler = str_replace("-", "_", strtolower($handler));
139 $method = strtolower($method);
140
141 if ($this->is_system($sender)) {
142 if (!is_array($this->handlers[$handler])) {
143 $this->handlers[$handler] = array();
144 }
145
146 $this->handlers[$handler][$method] = $sender;
147 }
148 }
149
150 function del_handler($handler, $method) {
151 $handler = str_replace("-", "_", strtolower($handler));
152 $method = strtolower($method);
153
154 if ($this->is_system($sender)) {
155 unset($this->handlers[$handler][$method]);
156 }
157 }
158
159 function lookup_handler($handler, $method) {
160 $handler = str_replace("-", "_", strtolower($handler));
161 $method = strtolower($method);
162
163 if (is_array($this->handlers[$handler])) {
164 if (isset($this->handlers[$handler]["*"])) {
165 return $this->handlers[$handler]["*"];
166 } else {
167 return $this->handlers[$handler][$method];
168 }
169 }
170
171 return false;
172 }
173
174 function add_command($command, $description, $sender) {
175 $command = str_replace("-", "_", strtolower($command));
176
177 $this->commands[$command] = array("description" => $description,
178 "class" => $sender);
179 }
180
181 function del_command($command) {
182 $command = "-" . strtolower($command);
183
184 unset($this->commands[$command]);
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) {
205 if (isset($args[$command])) {
206 $command = str_replace("-", "", $command);
207 $data["class"]->$command($args);
208 }
209 }
210 }
211
212 function load_data($force = false) {
213 if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) {
214 $plugin = db_escape_string($this->link, $plugin);
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) {
229 $plugin = db_escape_string($this->link, $plugin);
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
239 $content = db_escape_string($this->link, serialize($this->storage[$plugin]));
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
268 function get($sender, $name, $default_value = false) {
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 }
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 }
296
297 function set_debug($debug) {
298 $this->debug = $debug;
299 }
300
301 function get_debug() {
302 return $this->debug;
303 }
304 }
305 ?>