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