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;
27 const HOOK_TOOLBAR_BUTTON
= 15;
28 const HOOK_ACTION_ITEM
= 16;
29 const HOOK_HEADLINE_TOOLBAR_BUTTON
= 17;
30 const HOOK_HOTKEY_INFO
= 18;
33 const KIND_SYSTEM
= 2;
36 function __construct($link) {
39 $this->storage
= $_SESSION["plugin_storage"];
41 if (!$this->storage
) $this->storage
= array();
44 private function register_plugin($name, $plugin) {
45 //array_push($this->plugins, $plugin);
46 $this->plugins
[$name] = $plugin;
53 function get_plugins() {
54 return $this->plugins
;
57 function get_plugin($name) {
58 return $this->plugins
[$name];
61 function run_hooks($type, $method, $args) {
62 foreach ($this->get_hooks($type) as $hook) {
63 $hook->$method($args);
67 function add_hook($type, $sender) {
68 if (!is_array($this->hooks
[$type])) {
69 $this->hooks
[$type] = array();
72 array_push($this->hooks
[$type], $sender);
75 function del_hook($type, $sender) {
76 if (is_array($this->hooks
[$type])) {
77 $key = array_Search($this->hooks
[$type], $sender);
79 unset($this->hooks
[$type][$key]);
84 function get_hooks($type) {
85 if (isset($this->hooks
[$type])) {
86 return $this->hooks
[$type];
91 function load_all($kind, $owner_uid = false) {
92 $plugins = array_map("basename", glob("plugins/*"));
93 $this->load(join(",", $plugins), $kind, $owner_uid);
96 function load($classlist, $kind, $owner_uid = false) {
97 $plugins = explode(",", $classlist);
99 $this->owner_uid
= (int) $owner_uid;
101 foreach ($plugins as $class) {
102 $class = trim($class);
103 $class_file = strtolower(basename($class));
104 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
106 if (!isset($this->plugins
[$class])) {
107 if (file_exists($file)) require_once $file;
109 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
110 $plugin = new $class($this);
113 case $this::KIND_SYSTEM
:
114 if ($this->is_system($plugin)) {
115 $plugin->init($this);
116 $this->register_plugin($class, $plugin);
119 case $this::KIND_USER
:
120 if (!$this->is_system($plugin)) {
121 $plugin->init($this);
122 $this->register_plugin($class, $plugin);
125 case $this::KIND_ALL
:
126 $plugin->init($this);
127 $this->register_plugin($class, $plugin);
135 function is_system($plugin) {
136 $about = $plugin->about();
141 // only system plugins are allowed to modify routing
142 function add_handler($handler, $method, $sender) {
143 $handler = str_replace("-", "_", strtolower($handler));
144 $method = strtolower($method);
146 if ($this->is_system($sender)) {
147 if (!is_array($this->handlers
[$handler])) {
148 $this->handlers
[$handler] = array();
151 $this->handlers
[$handler][$method] = $sender;
155 function del_handler($handler, $method) {
156 $handler = str_replace("-", "_", strtolower($handler));
157 $method = strtolower($method);
159 if ($this->is_system($sender)) {
160 unset($this->handlers
[$handler][$method]);
164 function lookup_handler($handler, $method) {
165 $handler = str_replace("-", "_", strtolower($handler));
166 $method = strtolower($method);
168 if (is_array($this->handlers
[$handler])) {
169 if (isset($this->handlers
[$handler]["*"])) {
170 return $this->handlers
[$handler]["*"];
172 return $this->handlers
[$handler][$method];
179 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
180 $command = str_replace("-", "_", strtolower($command));
182 $this->commands
[$command] = array("description" => $description,
184 "arghelp" => $arghelp,
188 function del_command($command) {
189 $command = "-" . strtolower($command);
191 unset($this->commands
[$command]);
194 function lookup_command($command) {
195 $command = "-" . strtolower($command);
197 if (is_array($this->commands
[$command])) {
198 return $this->commands
[$command]["class"];
206 function get_commands() {
207 return $this->commands
;
210 function run_commands($args) {
211 foreach ($this->get_commands() as $command => $data) {
212 if (isset($args[$command])) {
213 $command = str_replace("-", "", $command);
214 $data["class"]->$command($args);
219 function load_data($force = false) {
220 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
221 $plugin = db_escape_string($this->link
, $plugin);
223 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
224 WHERE owner_uid = '".$this->owner_uid
."'");
226 while ($line = db_fetch_assoc($result)) {
227 $this->storage
[$line["name"]] = unserialize($line["content"]);
230 $_SESSION["plugin_storage"] = $this->storage
;
234 private function save_data($plugin) {
235 if ($this->owner_uid
) {
236 $plugin = db_escape_string($this->link
, $plugin);
238 db_query($this->link
, "BEGIN");
240 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
241 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
243 if (!isset($this->storage
[$plugin]))
244 $this->storage
[$plugin] = array();
246 $content = db_escape_string($this->link
, serialize($this->storage
[$plugin]));
248 if (db_num_rows($result) != 0) {
249 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
250 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
253 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
254 (name,owner_uid,content) VALUES
255 ('$plugin','".$this->owner_uid
."','$content')");
258 db_query($this->link
, "COMMIT");
262 function set($sender, $name, $value, $sync = true) {
263 $idx = get_class($sender);
265 if (!isset($this->storage
[$idx]))
266 $this->storage
[$idx] = array();
268 $this->storage
[$idx][$name] = $value;
270 $_SESSION["plugin_storage"] = $this->storage
;
272 if ($sync) $this->save_data(get_class($sender));
275 function get($sender, $name, $default_value = false) {
276 $idx = get_class($sender);
278 if (isset($this->storage
[$idx][$name])) {
279 return $this->storage
[$idx][$name];
281 return $default_value;
285 function get_all($sender) {
286 $idx = get_class($sender);
288 return $this->storage
[$idx];
291 function clear_data($sender) {
292 if ($this->owner_uid
) {
293 $idx = get_class($sender);
295 unset($this->storage
[$idx]);
297 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
298 AND owner_uid = " . $this->owner_uid
);
300 $_SESSION["plugin_storage"] = $this->storage
;
304 function set_debug($debug) {
305 $this->debug
= $debug;
308 function get_debug() {
312 // Plugin feed functions are *EXPERIMENTAL*!
314 // cat_id: only -1 is supported (Special)
315 function add_feed($cat_id, $title, $icon, $sender) {
316 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
318 $id = count($this->feeds
[$cat_id]);
320 array_push($this->feeds
[$cat_id],
321 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
326 function get_feeds($cat_id) {
327 return $this->feeds
[$cat_id];
330 // convert feed_id (e.g. -129) to pfeed_id first
331 function get_feed_handler($pfeed_id) {
332 foreach ($this->feeds
as $cat) {
333 foreach ($cat as $feed) {
334 if ($feed['id'] == $pfeed_id) {
335 return $feed['sender'];
341 static function pfeed_to_feed_id($label) {
342 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
345 static function feed_to_pfeed_id($feed) {
346 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);