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