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