]>
git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
e43b39f9dd404fd8ea0443225a4866dc45e66927
2 /* create table ttrss_plugin_storage
3 (id serial not null primary key, name varchar(100) not null,
4 owner_uid integer not null references ttrss_users(id) ON DELETE CASCADE,
5 content text not null) - not in schema yet
9 private $hooks = array();
10 private $plugins = array();
11 private $handlers = array();
12 private $commands = array();
13 private $storage = array();
16 const HOOK_ARTICLE_BUTTON
= 1;
17 const HOOK_ARTICLE_FILTER
= 2;
18 const HOOK_PREFS_TAB
= 3;
19 const HOOK_PREFS_TAB_SECTION
= 4;
20 const HOOK_PREFS_TABS
= 5;
21 const HOOK_FEED_PARSED
= 6;
22 const HOOK_UPDATE_TASK
= 7;
23 const HOOK_AUTH_USER
= 8;
26 const KIND_SYSTEM
= 2;
29 function __construct($link) {
32 $this->storage
= $_SESSION["plugin_storage"];
34 if (!$this->storage
) $this->storage
= array();
37 private function register_plugin($name, $plugin) {
38 //array_push($this->plugins, $plugin);
39 $this->plugins
[$name] = $plugin;
46 function get_plugins() {
47 return $this->plugins
;
50 function get_plugin($name) {
51 return $this->plugins
[$name];
54 function run_hooks($type, $method, $args) {
55 foreach ($this->get_hooks($type) as $hook) {
56 $hook->$method($args);
60 function add_hook($type, $sender) {
61 if (!is_array($this->hooks
[$type])) {
62 $this->hooks
[$type] = array();
65 array_push($this->hooks
[$type], $sender);
68 function del_hook($type, $sender) {
69 if (is_array($this->hooks
[$type])) {
70 $key = array_Search($this->hooks
[$type], $sender);
72 unset($this->hooks
[$type][$key]);
77 function get_hooks($type) {
78 if (isset($this->hooks
[$type])) {
79 return $this->hooks
[$type];
84 function load_all($kind) {
85 $plugins = array_map("basename", glob("plugins/*"));
86 $this->load(join(",", $plugins), $kind);
89 function load($classlist, $kind, $owner_uid = false) {
90 $plugins = explode(",", $classlist);
92 $this->owner_uid
= (int) $owner_uid;
94 foreach ($plugins as $class) {
95 $class = trim($class);
96 $class_file = strtolower(basename($class));
97 $file = dirname(__FILE__
)."/../plugins/$class_file/$class_file.php";
99 if (!isset($this->plugins
[$class])) {
100 if (file_exists($file)) require_once $file;
102 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
103 $plugin = new $class($this);
106 case $this::KIND_SYSTEM
:
107 if ($this->is_system($plugin)) {
108 $plugin->init($this);
109 $this->register_plugin($class, $plugin);
112 case $this::KIND_USER
:
113 if (!$this->is_system($plugin)) {
114 $plugin->init($this);
115 $this->register_plugin($class, $plugin);
118 case $this::KIND_ALL
:
119 $plugin->init($this);
120 $this->register_plugin($class, $plugin);
128 function is_system($plugin) {
129 $about = $plugin->about();
134 // only system plugins are allowed to modify routing
135 function add_handler($handler, $method, $sender) {
136 $handler = str_replace("-", "_", strtolower($handler));
137 $method = strtolower($method);
139 if ($this->is_system($sender)) {
140 if (!is_array($this->handlers
[$handler])) {
141 $this->handlers
[$handler] = array();
144 $this->handlers
[$handler][$method] = $sender;
148 function del_handler($handler, $method) {
149 $handler = str_replace("-", "_", strtolower($handler));
150 $method = strtolower($method);
152 if ($this->is_system($sender)) {
153 unset($this->handlers
[$handler][$method]);
157 function lookup_handler($handler, $method) {
158 $handler = str_replace("-", "_", strtolower($handler));
159 $method = strtolower($method);
161 if (is_array($this->handlers
[$handler])) {
162 if (isset($this->handlers
[$handler]["*"])) {
163 return $this->handlers
[$handler]["*"];
165 return $this->handlers
[$handler][$method];
172 function add_command($command, $description, $sender) {
173 $command = "-" . str_replace("-", "_", strtolower($command));
175 $this->commands
[$command] = array("description" => $description,
179 function del_command($command) {
180 $command = "-" . strtolower($command);
182 unset($this->commands
[$command]);
185 function lookup_command($command) {
186 $command = "-" . strtolower($command);
188 if (is_array($this->commands
[$command])) {
189 return $this->commands
[$command]["class"];
197 function get_commands() {
198 return $this->commands
;
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);
210 function load_data($force = false) {
211 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
212 $plugin = db_escape_string($plugin);
214 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
215 WHERE owner_uid = '".$this->owner_uid
."'");
217 while ($line = db_fetch_assoc($result)) {
218 $this->storage
[$line["name"]] = unserialize($line["content"]);
221 $_SESSION["plugin_storage"] = $this->storage
;
225 private function save_data($plugin) {
226 if ($this->owner_uid
) {
227 $plugin = db_escape_string($plugin);
229 db_query($this->link
, "BEGIN");
231 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
232 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
234 if (!isset($this->storage
[$plugin]))
235 $this->storage
[$plugin] = array();
237 $content = db_escape_string(serialize($this->storage
[$plugin]));
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'");
244 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
245 (name,owner_uid,content) VALUES
246 ('$plugin','".$this->owner_uid
."','$content')");
249 db_query($this->link
, "COMMIT");
253 function set($sender, $name, $value, $sync = true) {
254 $idx = get_class($sender);
256 if (!isset($this->storage
[$idx]))
257 $this->storage
[$idx] = array();
259 $this->storage
[$idx][$name] = $value;
261 $_SESSION["plugin_storage"] = $this->storage
;
263 if ($sync) $this->save_data(get_class($sender));
266 function get($sender, $name, $default_value) {
267 $idx = get_class($sender);
269 if (isset($this->storage
[$idx][$name])) {
270 return $this->storage
[$idx][$name];
272 return $default_value;
276 function get_all($sender) {
277 $idx = get_class($sender);
279 return $this->storage
[$idx];