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