]>
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;
19 const HOOK_HOTKEY_MAP
= 9;
22 const KIND_SYSTEM
= 2;
25 function __construct($link) {
28 $this->storage
= $_SESSION["plugin_storage"];
30 if (!$this->storage
) $this->storage
= array();
33 private function register_plugin($name, $plugin) {
34 //array_push($this->plugins, $plugin);
35 $this->plugins
[$name] = $plugin;
42 function get_plugins() {
43 return $this->plugins
;
46 function get_plugin($name) {
47 return $this->plugins
[$name];
50 function run_hooks($type, $method, $args) {
51 foreach ($this->get_hooks($type) as $hook) {
52 $hook->$method($args);
56 function add_hook($type, $sender) {
57 if (!is_array($this->hooks
[$type])) {
58 $this->hooks
[$type] = array();
61 array_push($this->hooks
[$type], $sender);
64 function del_hook($type, $sender) {
65 if (is_array($this->hooks
[$type])) {
66 $key = array_Search($this->hooks
[$type], $sender);
68 unset($this->hooks
[$type][$key]);
73 function get_hooks($type) {
74 if (isset($this->hooks
[$type])) {
75 return $this->hooks
[$type];
80 function load_all($kind, $owner_uid = false) {
81 $plugins = array_map("basename", glob("plugins/*"));
82 $this->load(join(",", $plugins), $kind, $owner_uid);
85 function load($classlist, $kind, $owner_uid = false) {
86 $plugins = explode(",", $classlist);
88 $this->owner_uid
= (int) $owner_uid;
90 foreach ($plugins as $class) {
91 $class = trim($class);
92 $class_file = strtolower(basename($class));
93 $file = dirname(__FILE__
)."/../plugins/$class_file/$class_file.php";
95 if (!isset($this->plugins
[$class])) {
96 if (file_exists($file)) require_once $file;
98 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
99 $plugin = new $class($this);
102 case $this::KIND_SYSTEM
:
103 if ($this->is_system($plugin)) {
104 $plugin->init($this);
105 $this->register_plugin($class, $plugin);
108 case $this::KIND_USER
:
109 if (!$this->is_system($plugin)) {
110 $plugin->init($this);
111 $this->register_plugin($class, $plugin);
114 case $this::KIND_ALL
:
115 $plugin->init($this);
116 $this->register_plugin($class, $plugin);
124 function is_system($plugin) {
125 $about = $plugin->about();
130 // only system plugins are allowed to modify routing
131 function add_handler($handler, $method, $sender) {
132 $handler = str_replace("-", "_", strtolower($handler));
133 $method = strtolower($method);
135 if ($this->is_system($sender)) {
136 if (!is_array($this->handlers
[$handler])) {
137 $this->handlers
[$handler] = array();
140 $this->handlers
[$handler][$method] = $sender;
144 function del_handler($handler, $method) {
145 $handler = str_replace("-", "_", strtolower($handler));
146 $method = strtolower($method);
148 if ($this->is_system($sender)) {
149 unset($this->handlers
[$handler][$method]);
153 function lookup_handler($handler, $method) {
154 $handler = str_replace("-", "_", strtolower($handler));
155 $method = strtolower($method);
157 if (is_array($this->handlers
[$handler])) {
158 if (isset($this->handlers
[$handler]["*"])) {
159 return $this->handlers
[$handler]["*"];
161 return $this->handlers
[$handler][$method];
168 function add_command($command, $description, $sender) {
169 $command = "-" . str_replace("-", "_", strtolower($command));
171 $this->commands
[$command] = array("description" => $description,
175 function del_command($command) {
176 $command = "-" . strtolower($command);
178 unset($this->commands
[$command]);
181 function lookup_command($command) {
182 $command = "-" . strtolower($command);
184 if (is_array($this->commands
[$command])) {
185 return $this->commands
[$command]["class"];
193 function get_commands() {
194 return $this->commands
;
197 function run_commands($args) {
198 foreach ($this->get_commands() as $command => $data) {
199 if (in_array($command, $args)) {
200 $command = str_replace("-", "", $command);
201 $data["class"]->$command($args);
206 function load_data($force = false) {
207 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
208 $plugin = db_escape_string($plugin);
210 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
211 WHERE owner_uid = '".$this->owner_uid
."'");
213 while ($line = db_fetch_assoc($result)) {
214 $this->storage
[$line["name"]] = unserialize($line["content"]);
217 $_SESSION["plugin_storage"] = $this->storage
;
221 private function save_data($plugin) {
222 if ($this->owner_uid
) {
223 $plugin = db_escape_string($plugin);
225 db_query($this->link
, "BEGIN");
227 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
228 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
230 if (!isset($this->storage
[$plugin]))
231 $this->storage
[$plugin] = array();
233 $content = db_escape_string(serialize($this->storage
[$plugin]));
235 if (db_num_rows($result) != 0) {
236 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
237 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
240 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
241 (name,owner_uid,content) VALUES
242 ('$plugin','".$this->owner_uid
."','$content')");
245 db_query($this->link
, "COMMIT");
249 function set($sender, $name, $value, $sync = true) {
250 $idx = get_class($sender);
252 if (!isset($this->storage
[$idx]))
253 $this->storage
[$idx] = array();
255 $this->storage
[$idx][$name] = $value;
257 $_SESSION["plugin_storage"] = $this->storage
;
259 if ($sync) $this->save_data(get_class($sender));
262 function get($sender, $name, $default_value = false) {
263 $idx = get_class($sender);
265 if (isset($this->storage
[$idx][$name])) {
266 return $this->storage
[$idx][$name];
268 return $default_value;
272 function get_all($sender) {
273 $idx = get_class($sender);
275 return $this->storage
[$idx];
278 function clear_data($sender) {
279 if ($this->owner_uid
) {
280 $idx = get_class($sender);
282 unset($this->storage
[$idx]);
284 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
285 AND owner_uid = " . $this->owner_uid
);
287 $_SESSION["plugin_storage"] = $this->storage
;