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