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