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