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