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