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