]>
git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
a6ba72fc6fb1d648312a4fb2e9b38adac2fcc4ec
4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
8 private $storage = array();
9 private $feeds = array();
13 const HOOK_ARTICLE_BUTTON
= 1;
14 const HOOK_ARTICLE_FILTER
= 2;
15 const HOOK_PREFS_TAB
= 3;
16 const HOOK_PREFS_TAB_SECTION
= 4;
17 const HOOK_PREFS_TABS
= 5;
18 const HOOK_FEED_PARSED
= 6;
19 const HOOK_UPDATE_TASK
= 7;
20 const HOOK_AUTH_USER
= 8;
21 const HOOK_HOTKEY_MAP
= 9;
22 const HOOK_RENDER_ARTICLE
= 10;
23 const HOOK_RENDER_ARTICLE_CDM
= 11;
24 const HOOK_FEED_FETCHED
= 12;
25 const HOOK_SANITIZE
= 13;
26 const HOOK_RENDER_ARTICLE_API
= 14;
29 const KIND_SYSTEM
= 2;
32 function __construct($link) {
35 $this->storage
= $_SESSION["plugin_storage"];
37 if (!$this->storage
) $this->storage
= array();
40 private function register_plugin($name, $plugin) {
41 //array_push($this->plugins, $plugin);
42 $this->plugins
[$name] = $plugin;
49 function get_plugins() {
50 return $this->plugins
;
53 function get_plugin($name) {
54 return $this->plugins
[$name];
57 function run_hooks($type, $method, $args) {
58 foreach ($this->get_hooks($type) as $hook) {
59 $hook->$method($args);
63 function add_hook($type, $sender) {
64 if (!is_array($this->hooks
[$type])) {
65 $this->hooks
[$type] = array();
68 array_push($this->hooks
[$type], $sender);
71 function del_hook($type, $sender) {
72 if (is_array($this->hooks
[$type])) {
73 $key = array_Search($this->hooks
[$type], $sender);
75 unset($this->hooks
[$type][$key]);
80 function get_hooks($type) {
81 if (isset($this->hooks
[$type])) {
82 return $this->hooks
[$type];
87 function load_all($kind, $owner_uid = false) {
88 $plugins = array_map("basename", glob("plugins/*"));
89 $this->load(join(",", $plugins), $kind, $owner_uid);
92 function load($classlist, $kind, $owner_uid = false) {
93 $plugins = explode(",", $classlist);
95 $this->owner_uid
= (int) $owner_uid;
97 foreach ($plugins as $class) {
98 $class = trim($class);
99 $class_file = strtolower(basename($class));
100 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
102 if (!isset($this->plugins
[$class])) {
103 if (file_exists($file)) require_once $file;
105 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
106 $plugin = new $class($this);
109 case $this::KIND_SYSTEM
:
110 if ($this->is_system($plugin)) {
111 $plugin->init($this);
112 $this->register_plugin($class, $plugin);
115 case $this::KIND_USER
:
116 if (!$this->is_system($plugin)) {
117 $plugin->init($this);
118 $this->register_plugin($class, $plugin);
121 case $this::KIND_ALL
:
122 $plugin->init($this);
123 $this->register_plugin($class, $plugin);
131 function is_system($plugin) {
132 $about = $plugin->about();
137 // only system plugins are allowed to modify routing
138 function add_handler($handler, $method, $sender) {
139 $handler = str_replace("-", "_", strtolower($handler));
140 $method = strtolower($method);
142 if ($this->is_system($sender)) {
143 if (!is_array($this->handlers
[$handler])) {
144 $this->handlers
[$handler] = array();
147 $this->handlers
[$handler][$method] = $sender;
151 function del_handler($handler, $method) {
152 $handler = str_replace("-", "_", strtolower($handler));
153 $method = strtolower($method);
155 if ($this->is_system($sender)) {
156 unset($this->handlers
[$handler][$method]);
160 function lookup_handler($handler, $method) {
161 $handler = str_replace("-", "_", strtolower($handler));
162 $method = strtolower($method);
164 if (is_array($this->handlers
[$handler])) {
165 if (isset($this->handlers
[$handler]["*"])) {
166 return $this->handlers
[$handler]["*"];
168 return $this->handlers
[$handler][$method];
175 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
176 $command = str_replace("-", "_", strtolower($command));
178 $this->commands
[$command] = array("description" => $description,
180 "arghelp" => $arghelp,
184 function del_command($command) {
185 $command = "-" . strtolower($command);
187 unset($this->commands
[$command]);
190 function lookup_command($command) {
191 $command = "-" . strtolower($command);
193 if (is_array($this->commands
[$command])) {
194 return $this->commands
[$command]["class"];
202 function get_commands() {
203 return $this->commands
;
206 function run_commands($args) {
207 foreach ($this->get_commands() as $command => $data) {
208 if (isset($args[$command])) {
209 $command = str_replace("-", "", $command);
210 $data["class"]->$command($args);
215 function load_data($force = false) {
216 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
217 $plugin = db_escape_string($this->link
, $plugin);
219 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
220 WHERE owner_uid = '".$this->owner_uid
."'");
222 while ($line = db_fetch_assoc($result)) {
223 $this->storage
[$line["name"]] = unserialize($line["content"]);
226 $_SESSION["plugin_storage"] = $this->storage
;
230 private function save_data($plugin) {
231 if ($this->owner_uid
) {
232 $plugin = db_escape_string($this->link
, $plugin);
234 db_query($this->link
, "BEGIN");
236 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
237 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
239 if (!isset($this->storage
[$plugin]))
240 $this->storage
[$plugin] = array();
242 $content = db_escape_string($this->link
, serialize($this->storage
[$plugin]));
244 if (db_num_rows($result) != 0) {
245 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
246 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
249 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
250 (name,owner_uid,content) VALUES
251 ('$plugin','".$this->owner_uid
."','$content')");
254 db_query($this->link
, "COMMIT");
258 function set($sender, $name, $value, $sync = true) {
259 $idx = get_class($sender);
261 if (!isset($this->storage
[$idx]))
262 $this->storage
[$idx] = array();
264 $this->storage
[$idx][$name] = $value;
266 $_SESSION["plugin_storage"] = $this->storage
;
268 if ($sync) $this->save_data(get_class($sender));
271 function get($sender, $name, $default_value = false) {
272 $idx = get_class($sender);
274 if (isset($this->storage
[$idx][$name])) {
275 return $this->storage
[$idx][$name];
277 return $default_value;
281 function get_all($sender) {
282 $idx = get_class($sender);
284 return $this->storage
[$idx];
287 function clear_data($sender) {
288 if ($this->owner_uid
) {
289 $idx = get_class($sender);
291 unset($this->storage
[$idx]);
293 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
294 AND owner_uid = " . $this->owner_uid
);
296 $_SESSION["plugin_storage"] = $this->storage
;
300 function set_debug($debug) {
301 $this->debug
= $debug;
304 function get_debug() {
308 // Plugin feed functions are *EXPERIMENTAL*!
310 // cat_id: only -1 is supported (Special)
311 function add_feed($cat_id, $title, $icon, $sender) {
312 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
314 $id = count($this->feeds
[$cat_id]);
316 array_push($this->feeds
[$cat_id],
317 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
322 function get_feeds($cat_id) {
323 return $this->feeds
[$cat_id];
326 // convert feed_id (e.g. -129) to pfeed_id first
327 function get_feed_handler($pfeed_id) {
328 foreach ($this->feeds
as $cat) {
329 foreach ($cat as $feed) {
330 if ($feed['id'] == $pfeed_id) {
331 return $feed['sender'];
337 static function pfeed_to_feed_id($label) {
338 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
341 static function feed_to_pfeed_id($feed) {
342 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);