]>
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 | ||
14 | const HOOK_ARTICLE_BUTTON = 1; | |
15 | const HOOK_ARTICLE_FILTER = 2; | |
16 | const HOOK_PREFS_TAB = 3; | |
17 | const HOOK_PREFS_TAB_SECTION = 4; | |
18 | const HOOK_PREFS_TABS = 5; | |
19 | const HOOK_FEED_PARSED = 6; | |
20 | const HOOK_UPDATE_TASK = 7; | |
21 | const HOOK_AUTH_USER = 8; | |
22 | const HOOK_HOTKEY_MAP = 9; | |
23 | const HOOK_RENDER_ARTICLE = 10; | |
24 | const HOOK_RENDER_ARTICLE_CDM = 11; | |
25 | const HOOK_FEED_FETCHED = 12; | |
26 | const HOOK_SANITIZE = 13; | |
27 | const HOOK_RENDER_ARTICLE_API = 14; | |
28 | const HOOK_TOOLBAR_BUTTON = 15; | |
29 | const HOOK_ACTION_ITEM = 16; | |
30 | const HOOK_HEADLINE_TOOLBAR_BUTTON = 17; | |
31 | const HOOK_HOTKEY_INFO = 18; | |
32 | const HOOK_ARTICLE_LEFT_BUTTON = 19; | |
33 | ||
34 | const KIND_ALL = 1; | |
35 | const KIND_SYSTEM = 2; | |
36 | const KIND_USER = 3; | |
37 | ||
38 | function __construct($dbh) { | |
39 | $this->dbh = $dbh; | |
40 | $this->storage = $_SESSION["plugin_storage"]; | |
41 | ||
42 | if (!$this->storage) $this->storage = array(); | |
43 | } | |
44 | ||
45 | private function register_plugin($name, $plugin) { | |
46 | //array_push($this->plugins, $plugin); | |
47 | $this->plugins[$name] = $plugin; | |
48 | } | |
49 | ||
50 | function get_dbh() { | |
51 | return $this->dbh; | |
52 | } | |
53 | ||
54 | function get_plugins() { | |
55 | return $this->plugins; | |
56 | } | |
57 | ||
58 | function get_plugin($name) { | |
59 | return $this->plugins[$name]; | |
60 | } | |
61 | ||
62 | function run_hooks($type, $method, $args) { | |
63 | foreach ($this->get_hooks($type) as $hook) { | |
64 | $hook->$method($args); | |
65 | } | |
66 | } | |
67 | ||
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) { | |
86 | if (isset($this->hooks[$type])) { | |
87 | return $this->hooks[$type]; | |
88 | } else { | |
89 | return array(); | |
90 | } | |
91 | } | |
92 | function load_all($kind, $owner_uid = false) { | |
93 | $plugins = array_map("basename", glob("plugins/*")); | |
94 | $this->load(join(",", $plugins), $kind, $owner_uid); | |
95 | } | |
96 | ||
97 | function load($classlist, $kind, $owner_uid = false) { | |
98 | $plugins = explode(",", $classlist); | |
99 | ||
100 | $this->owner_uid = (int) $owner_uid; | |
101 | ||
102 | foreach ($plugins as $class) { | |
103 | $class = trim($class); | |
104 | $class_file = strtolower(basename($class)); | |
105 | ||
106 | if (!is_dir(dirname(__FILE__)."/../plugins/$class_file")) continue; | |
107 | ||
108 | $file = dirname(__FILE__)."/../plugins/$class_file/init.php"; | |
109 | ||
110 | if (!isset($this->plugins[$class])) { | |
111 | if (file_exists($file)) require_once $file; | |
112 | ||
113 | if (class_exists($class) && is_subclass_of($class, "Plugin")) { | |
114 | $plugin = new $class($this); | |
115 | ||
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 | } | |
134 | } | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | function is_system($plugin) { | |
140 | $about = $plugin->about(); | |
141 | ||
142 | return @$about[3]; | |
143 | } | |
144 | ||
145 | // only system plugins are allowed to modify routing | |
146 | function add_handler($handler, $method, $sender) { | |
147 | $handler = str_replace("-", "_", strtolower($handler)); | |
148 | $method = strtolower($method); | |
149 | ||
150 | if ($this->is_system($sender)) { | |
151 | if (!is_array($this->handlers[$handler])) { | |
152 | $this->handlers[$handler] = array(); | |
153 | } | |
154 | ||
155 | $this->handlers[$handler][$method] = $sender; | |
156 | } | |
157 | } | |
158 | ||
159 | function del_handler($handler, $method) { | |
160 | $handler = str_replace("-", "_", strtolower($handler)); | |
161 | $method = strtolower($method); | |
162 | ||
163 | if ($this->is_system($sender)) { | |
164 | unset($this->handlers[$handler][$method]); | |
165 | } | |
166 | } | |
167 | ||
168 | function lookup_handler($handler, $method) { | |
169 | $handler = str_replace("-", "_", strtolower($handler)); | |
170 | $method = strtolower($method); | |
171 | ||
172 | if (is_array($this->handlers[$handler])) { | |
173 | if (isset($this->handlers[$handler]["*"])) { | |
174 | return $this->handlers[$handler]["*"]; | |
175 | } else { | |
176 | return $this->handlers[$handler][$method]; | |
177 | } | |
178 | } | |
179 | ||
180 | return false; | |
181 | } | |
182 | ||
183 | function add_command($command, $description, $sender, $suffix = "", $arghelp = "") { | |
184 | $command = str_replace("-", "_", strtolower($command)); | |
185 | ||
186 | $this->commands[$command] = array("description" => $description, | |
187 | "suffix" => $suffix, | |
188 | "arghelp" => $arghelp, | |
189 | "class" => $sender); | |
190 | } | |
191 | ||
192 | function del_command($command) { | |
193 | $command = "-" . strtolower($command); | |
194 | ||
195 | unset($this->commands[$command]); | |
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) { | |
216 | if (isset($args[$command])) { | |
217 | $command = str_replace("-", "", $command); | |
218 | $data["class"]->$command($args); | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | function load_data($force = false) { | |
224 | if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) { | |
225 | $plugin = db_escape_string( $plugin); | |
226 | ||
227 | $result = db_query( "SELECT name, content FROM ttrss_plugin_storage | |
228 | WHERE owner_uid = '".$this->owner_uid."'"); | |
229 | ||
230 | while ($line = db_fetch_assoc($result)) { | |
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) { | |
240 | $plugin = db_escape_string( $plugin); | |
241 | ||
242 | db_query( "BEGIN"); | |
243 | ||
244 | $result = db_query("SELECT id FROM ttrss_plugin_storage WHERE | |
245 | owner_uid= '".$this->owner_uid."' AND name = '$plugin'"); | |
246 | ||
247 | if (!isset($this->storage[$plugin])) | |
248 | $this->storage[$plugin] = array(); | |
249 | ||
250 | $content = db_escape_string( serialize($this->storage[$plugin])); | |
251 | ||
252 | if (db_num_rows($result) != 0) { | |
253 | db_query( "UPDATE ttrss_plugin_storage SET content = '$content' | |
254 | WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'"); | |
255 | ||
256 | } else { | |
257 | db_query( "INSERT INTO ttrss_plugin_storage | |
258 | (name,owner_uid,content) VALUES | |
259 | ('$plugin','".$this->owner_uid."','$content')"); | |
260 | } | |
261 | ||
262 | db_query( "COMMIT"); | |
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 | ||
279 | function get($sender, $name, $default_value = false) { | |
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 | } | |
294 | ||
295 | function clear_data($sender) { | |
296 | if ($this->owner_uid) { | |
297 | $idx = get_class($sender); | |
298 | ||
299 | unset($this->storage[$idx]); | |
300 | ||
301 | db_query( "DELETE FROM ttrss_plugin_storage WHERE name = '$idx' | |
302 | AND owner_uid = " . $this->owner_uid); | |
303 | ||
304 | $_SESSION["plugin_storage"] = $this->storage; | |
305 | } | |
306 | } | |
307 | ||
308 | function set_debug($debug) { | |
309 | $this->debug = $debug; | |
310 | } | |
311 | ||
312 | function get_debug() { | |
313 | return $this->debug; | |
314 | } | |
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 | ||
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 | } | |
362 | } | |
363 | ?> |