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