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