]>
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();
12 const HOOK_ARTICLE_BUTTON
= 1;
13 const HOOK_ARTICLE_FILTER
= 2;
14 const HOOK_PREFS_TAB
= 3;
15 const HOOK_PREFS_TAB_SECTION
= 4;
16 const HOOK_PREFS_TABS
= 5;
17 const HOOK_FEED_PARSED
= 6;
18 const HOOK_UPDATE_TASK
= 7;
19 const HOOK_AUTH_USER
= 8;
20 const HOOK_HOTKEY_MAP
= 9;
21 const HOOK_RENDER_ARTICLE
= 10;
22 const HOOK_RENDER_ARTICLE_CDM
= 11;
23 const HOOK_FEED_FETCHED
= 12;
24 const HOOK_SANITIZE
= 13;
25 const HOOK_RENDER_ARTICLE_API
= 14;
28 const KIND_SYSTEM
= 2;
31 function __construct($link) {
34 $this->storage
= $_SESSION["plugin_storage"];
36 if (!$this->storage
) $this->storage
= array();
39 private function register_plugin($name, $plugin) {
40 //array_push($this->plugins, $plugin);
41 $this->plugins
[$name] = $plugin;
48 function get_plugins() {
49 return $this->plugins
;
52 function get_plugin($name) {
53 return $this->plugins
[$name];
56 function run_hooks($type, $method, $args) {
57 foreach ($this->get_hooks($type) as $hook) {
58 $hook->$method($args);
62 function add_hook($type, $sender) {
63 if (!is_array($this->hooks
[$type])) {
64 $this->hooks
[$type] = array();
67 array_push($this->hooks
[$type], $sender);
70 function del_hook($type, $sender) {
71 if (is_array($this->hooks
[$type])) {
72 $key = array_Search($this->hooks
[$type], $sender);
74 unset($this->hooks
[$type][$key]);
79 function get_hooks($type) {
80 if (isset($this->hooks
[$type])) {
81 return $this->hooks
[$type];
86 function load_all($kind, $owner_uid = false) {
87 $plugins = array_map("basename", glob("plugins/*"));
88 $this->load(join(",", $plugins), $kind, $owner_uid);
91 function load($classlist, $kind, $owner_uid = false) {
92 $plugins = explode(",", $classlist);
94 $this->owner_uid
= (int) $owner_uid;
96 foreach ($plugins as $class) {
97 $class = trim($class);
98 $class_file = strtolower(basename($class));
99 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
101 if (!isset($this->plugins
[$class])) {
102 if (file_exists($file)) require_once $file;
104 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
105 $plugin = new $class($this);
108 case $this::KIND_SYSTEM
:
109 if ($this->is_system($plugin)) {
110 $plugin->init($this);
111 $this->register_plugin($class, $plugin);
114 case $this::KIND_USER
:
115 if (!$this->is_system($plugin)) {
116 $plugin->init($this);
117 $this->register_plugin($class, $plugin);
120 case $this::KIND_ALL
:
121 $plugin->init($this);
122 $this->register_plugin($class, $plugin);
130 function is_system($plugin) {
131 $about = $plugin->about();
136 // only system plugins are allowed to modify routing
137 function add_handler($handler, $method, $sender) {
138 $handler = str_replace("-", "_", strtolower($handler));
139 $method = strtolower($method);
141 if ($this->is_system($sender)) {
142 if (!is_array($this->handlers
[$handler])) {
143 $this->handlers
[$handler] = array();
146 $this->handlers
[$handler][$method] = $sender;
150 function del_handler($handler, $method) {
151 $handler = str_replace("-", "_", strtolower($handler));
152 $method = strtolower($method);
154 if ($this->is_system($sender)) {
155 unset($this->handlers
[$handler][$method]);
159 function lookup_handler($handler, $method) {
160 $handler = str_replace("-", "_", strtolower($handler));
161 $method = strtolower($method);
163 if (is_array($this->handlers
[$handler])) {
164 if (isset($this->handlers
[$handler]["*"])) {
165 return $this->handlers
[$handler]["*"];
167 return $this->handlers
[$handler][$method];
174 function add_command($command, $description, $sender) {
175 $command = str_replace("-", "_", strtolower($command));
177 $this->commands
[$command] = array("description" => $description,
181 function del_command($command) {
182 $command = "-" . strtolower($command);
184 unset($this->commands
[$command]);
187 function lookup_command($command) {
188 $command = "-" . strtolower($command);
190 if (is_array($this->commands
[$command])) {
191 return $this->commands
[$command]["class"];
199 function get_commands() {
200 return $this->commands
;
203 function run_commands($args) {
204 foreach ($this->get_commands() as $command => $data) {
205 if (isset($args[$command])) {
206 $command = str_replace("-", "", $command);
207 $data["class"]->$command($args);
212 function load_data($force = false) {
213 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
214 $plugin = db_escape_string($this->link
, $plugin);
216 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
217 WHERE owner_uid = '".$this->owner_uid
."'");
219 while ($line = db_fetch_assoc($result)) {
220 $this->storage
[$line["name"]] = unserialize($line["content"]);
223 $_SESSION["plugin_storage"] = $this->storage
;
227 private function save_data($plugin) {
228 if ($this->owner_uid
) {
229 $plugin = db_escape_string($this->link
, $plugin);
231 db_query($this->link
, "BEGIN");
233 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
234 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
236 if (!isset($this->storage
[$plugin]))
237 $this->storage
[$plugin] = array();
239 $content = db_escape_string($this->link
, serialize($this->storage
[$plugin]));
241 if (db_num_rows($result) != 0) {
242 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
243 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
246 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
247 (name,owner_uid,content) VALUES
248 ('$plugin','".$this->owner_uid
."','$content')");
251 db_query($this->link
, "COMMIT");
255 function set($sender, $name, $value, $sync = true) {
256 $idx = get_class($sender);
258 if (!isset($this->storage
[$idx]))
259 $this->storage
[$idx] = array();
261 $this->storage
[$idx][$name] = $value;
263 $_SESSION["plugin_storage"] = $this->storage
;
265 if ($sync) $this->save_data(get_class($sender));
268 function get($sender, $name, $default_value = false) {
269 $idx = get_class($sender);
271 if (isset($this->storage
[$idx][$name])) {
272 return $this->storage
[$idx][$name];
274 return $default_value;
278 function get_all($sender) {
279 $idx = get_class($sender);
281 return $this->storage
[$idx];
284 function clear_data($sender) {
285 if ($this->owner_uid
) {
286 $idx = get_class($sender);
288 unset($this->storage
[$idx]);
290 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
291 AND owner_uid = " . $this->owner_uid
);
293 $_SESSION["plugin_storage"] = $this->storage
;
297 function set_debug($debug) {
298 $this->debug
= $debug;
301 function get_debug() {