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