]>
git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
8 private $storage = array();
11 const HOOK_ARTICLE_BUTTON
= 1;
12 const HOOK_ARTICLE_FILTER
= 2;
13 const HOOK_PREFS_TAB
= 3;
14 const HOOK_PREFS_TAB_SECTION
= 4;
15 const HOOK_PREFS_TABS
= 5;
16 const HOOK_FEED_PARSED
= 6;
17 const HOOK_UPDATE_TASK
= 7;
18 const HOOK_AUTH_USER
= 8;
21 const KIND_SYSTEM
= 2;
24 function __construct($link) {
27 $this->storage
= $_SESSION["plugin_storage"];
29 if (!$this->storage
) $this->storage
= array();
32 private function register_plugin($name, $plugin) {
33 //array_push($this->plugins, $plugin);
34 $this->plugins
[$name] = $plugin;
41 function get_plugins() {
42 return $this->plugins
;
45 function get_plugin($name) {
46 return $this->plugins
[$name];
49 function run_hooks($type, $method, $args) {
50 foreach ($this->get_hooks($type) as $hook) {
51 $hook->$method($args);
55 function add_hook($type, $sender) {
56 if (!is_array($this->hooks
[$type])) {
57 $this->hooks
[$type] = array();
60 array_push($this->hooks
[$type], $sender);
63 function del_hook($type, $sender) {
64 if (is_array($this->hooks
[$type])) {
65 $key = array_Search($this->hooks
[$type], $sender);
67 unset($this->hooks
[$type][$key]);
72 function get_hooks($type) {
73 if (isset($this->hooks
[$type])) {
74 return $this->hooks
[$type];
79 function load_all($kind, $owner_uid = false) {
80 $plugins = array_map("basename", glob("plugins/*"));
81 $this->load(join(",", $plugins), $kind, $owner_uid);
84 function load($classlist, $kind, $owner_uid = false) {
85 $plugins = explode(",", $classlist);
87 $this->owner_uid
= (int) $owner_uid;
89 foreach ($plugins as $class) {
90 $class = trim($class);
91 $class_file = strtolower(basename($class));
92 $file = dirname(__FILE__
)."/../plugins/$class_file/$class_file.php";
94 if (!isset($this->plugins
[$class])) {
95 if (file_exists($file)) require_once $file;
97 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
98 $plugin = new $class($this);
101 case $this::KIND_SYSTEM
:
102 if ($this->is_system($plugin)) {
103 $plugin->init($this);
104 $this->register_plugin($class, $plugin);
107 case $this::KIND_USER
:
108 if (!$this->is_system($plugin)) {
109 $plugin->init($this);
110 $this->register_plugin($class, $plugin);
113 case $this::KIND_ALL
:
114 $plugin->init($this);
115 $this->register_plugin($class, $plugin);
123 function is_system($plugin) {
124 $about = $plugin->about();
129 // only system plugins are allowed to modify routing
130 function add_handler($handler, $method, $sender) {
131 $handler = str_replace("-", "_", strtolower($handler));
132 $method = strtolower($method);
134 if ($this->is_system($sender)) {
135 if (!is_array($this->handlers
[$handler])) {
136 $this->handlers
[$handler] = array();
139 $this->handlers
[$handler][$method] = $sender;
143 function del_handler($handler, $method) {
144 $handler = str_replace("-", "_", strtolower($handler));
145 $method = strtolower($method);
147 if ($this->is_system($sender)) {
148 unset($this->handlers
[$handler][$method]);
152 function lookup_handler($handler, $method) {
153 $handler = str_replace("-", "_", strtolower($handler));
154 $method = strtolower($method);
156 if (is_array($this->handlers
[$handler])) {
157 if (isset($this->handlers
[$handler]["*"])) {
158 return $this->handlers
[$handler]["*"];
160 return $this->handlers
[$handler][$method];
167 function add_command($command, $description, $sender) {
168 $command = "-" . str_replace("-", "_", strtolower($command));
170 $this->commands
[$command] = array("description" => $description,
174 function del_command($command) {
175 $command = "-" . strtolower($command);
177 unset($this->commands
[$command]);
180 function lookup_command($command) {
181 $command = "-" . strtolower($command);
183 if (is_array($this->commands
[$command])) {
184 return $this->commands
[$command]["class"];
192 function get_commands() {
193 return $this->commands
;
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);
205 function load_data($force = false) {
206 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
207 $plugin = db_escape_string($plugin);
209 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
210 WHERE owner_uid = '".$this->owner_uid
."'");
212 while ($line = db_fetch_assoc($result)) {
213 $this->storage
[$line["name"]] = unserialize($line["content"]);
216 $_SESSION["plugin_storage"] = $this->storage
;
220 private function save_data($plugin) {
221 if ($this->owner_uid
) {
222 $plugin = db_escape_string($plugin);
224 db_query($this->link
, "BEGIN");
226 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
227 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
229 if (!isset($this->storage
[$plugin]))
230 $this->storage
[$plugin] = array();
232 $content = db_escape_string(serialize($this->storage
[$plugin]));
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'");
239 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
240 (name,owner_uid,content) VALUES
241 ('$plugin','".$this->owner_uid
."','$content')");
244 db_query($this->link
, "COMMIT");
248 function set($sender, $name, $value, $sync = true) {
249 $idx = get_class($sender);
251 if (!isset($this->storage
[$idx]))
252 $this->storage
[$idx] = array();
254 $this->storage
[$idx][$name] = $value;
256 $_SESSION["plugin_storage"] = $this->storage
;
258 if ($sync) $this->save_data(get_class($sender));
261 function get($sender, $name, $default_value = false) {
262 $idx = get_class($sender);
264 if (isset($this->storage
[$idx][$name])) {
265 return $this->storage
[$idx][$name];
267 return $default_value;
271 function get_all($sender) {
272 $idx = get_class($sender);
274 return $this->storage
[$idx];
277 function clear_data($sender) {
278 if ($this->owner_uid
) {
279 $idx = get_class($sender);
281 unset($this->storage
[$idx]);
283 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
284 AND owner_uid = " . $this->owner_uid
);
286 $_SESSION["plugin_storage"] = $this->storage
;