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