4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
8 private $storage = array();
9 private $feeds = array();
10 private $api_methods = array();
13 private static $instance;
15 const HOOK_ARTICLE_BUTTON
= 1;
16 const HOOK_ARTICLE_FILTER
= 2;
17 const HOOK_PREFS_TAB
= 3;
18 const HOOK_PREFS_TAB_SECTION
= 4;
19 const HOOK_PREFS_TABS
= 5;
20 const HOOK_FEED_PARSED
= 6;
21 const HOOK_UPDATE_TASK
= 7;
22 const HOOK_AUTH_USER
= 8;
23 const HOOK_HOTKEY_MAP
= 9;
24 const HOOK_RENDER_ARTICLE
= 10;
25 const HOOK_RENDER_ARTICLE_CDM
= 11;
26 const HOOK_FEED_FETCHED
= 12;
27 const HOOK_SANITIZE
= 13;
28 const HOOK_RENDER_ARTICLE_API
= 14;
29 const HOOK_TOOLBAR_BUTTON
= 15;
30 const HOOK_ACTION_ITEM
= 16;
31 const HOOK_HEADLINE_TOOLBAR_BUTTON
= 17;
32 const HOOK_HOTKEY_INFO
= 18;
33 const HOOK_ARTICLE_LEFT_BUTTON
= 19;
36 const KIND_SYSTEM
= 2;
39 function __construct($ignored = false) {
40 $this->dbh
= Db
::get();
41 $this->storage
= $_SESSION["plugin_storage"];
43 if (!$this->storage
) $this->storage
= array();
46 private function __clone() {
50 public static function getInstance() {
51 if (self
::$instance == null)
52 self
::$instance = new self();
54 return self
::$instance;
57 private function register_plugin($name, $plugin) {
58 //array_push($this->plugins, $plugin);
59 $this->plugins
[$name] = $plugin;
66 function get_plugins() {
67 return $this->plugins
;
70 function get_plugin($name) {
71 return $this->plugins
[$name];
74 function run_hooks($type, $method, $args) {
75 foreach ($this->get_hooks($type) as $hook) {
76 $hook->$method($args);
80 function add_hook($type, $sender) {
81 if (!is_array($this->hooks
[$type])) {
82 $this->hooks
[$type] = array();
85 array_push($this->hooks
[$type], $sender);
88 function del_hook($type, $sender) {
89 if (is_array($this->hooks
[$type])) {
90 $key = array_Search($this->hooks
[$type], $sender);
92 unset($this->hooks
[$type][$key]);
97 function get_hooks($type) {
98 if (isset($this->hooks
[$type])) {
99 return $this->hooks
[$type];
104 function load_all($kind, $owner_uid = false) {
105 $plugins = array_map("basename", glob("plugins/*"));
106 $this->load(join(",", $plugins), $kind, $owner_uid);
109 function load($classlist, $kind, $owner_uid = false) {
110 $plugins = explode(",", $classlist);
112 $this->owner_uid
= (int) $owner_uid;
114 foreach ($plugins as $class) {
115 $class = trim($class);
116 $class_file = strtolower(basename($class));
118 if (!is_dir(dirname(__FILE__
)."/../plugins/$class_file")) continue;
120 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
122 if (!isset($this->plugins
[$class])) {
123 if (file_exists($file)) require_once $file;
125 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
126 $plugin = new $class($this);
129 case $this::KIND_SYSTEM
:
130 if ($this->is_system($plugin)) {
131 $plugin->init($this);
132 $this->register_plugin($class, $plugin);
135 case $this::KIND_USER
:
136 if (!$this->is_system($plugin)) {
137 $plugin->init($this);
138 $this->register_plugin($class, $plugin);
141 case $this::KIND_ALL
:
142 $plugin->init($this);
143 $this->register_plugin($class, $plugin);
151 function is_system($plugin) {
152 $about = $plugin->about();
157 // only system plugins are allowed to modify routing
158 function add_handler($handler, $method, $sender) {
159 $handler = str_replace("-", "_", strtolower($handler));
160 $method = strtolower($method);
162 if ($this->is_system($sender)) {
163 if (!is_array($this->handlers
[$handler])) {
164 $this->handlers
[$handler] = array();
167 $this->handlers
[$handler][$method] = $sender;
171 function del_handler($handler, $method) {
172 $handler = str_replace("-", "_", strtolower($handler));
173 $method = strtolower($method);
175 if ($this->is_system($sender)) {
176 unset($this->handlers
[$handler][$method]);
180 function lookup_handler($handler, $method) {
181 $handler = str_replace("-", "_", strtolower($handler));
182 $method = strtolower($method);
184 if (is_array($this->handlers
[$handler])) {
185 if (isset($this->handlers
[$handler]["*"])) {
186 return $this->handlers
[$handler]["*"];
188 return $this->handlers
[$handler][$method];
195 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
196 $command = str_replace("-", "_", strtolower($command));
198 $this->commands
[$command] = array("description" => $description,
200 "arghelp" => $arghelp,
204 function del_command($command) {
205 $command = "-" . strtolower($command);
207 unset($this->commands
[$command]);
210 function lookup_command($command) {
211 $command = "-" . strtolower($command);
213 if (is_array($this->commands
[$command])) {
214 return $this->commands
[$command]["class"];
222 function get_commands() {
223 return $this->commands
;
226 function run_commands($args) {
227 foreach ($this->get_commands() as $command => $data) {
228 if (isset($args[$command])) {
229 $command = str_replace("-", "", $command);
230 $data["class"]->$command($args);
235 function load_data($force = false) {
236 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
237 $plugin = $this->dbh
->escape_string($plugin);
239 $result = $this->dbh
->query("SELECT name, content FROM ttrss_plugin_storage
240 WHERE owner_uid = '".$this->owner_uid
."'");
242 while ($line = $this->dbh
->fetch_assoc($result)) {
243 $this->storage
[$line["name"]] = unserialize($line["content"]);
246 $_SESSION["plugin_storage"] = $this->storage
;
250 private function save_data($plugin) {
251 if ($this->owner_uid
) {
252 $plugin = $this->dbh
->escape_string($plugin);
254 $this->dbh
->query("BEGIN");
256 $result = $this->dbh
->query("SELECT id FROM ttrss_plugin_storage WHERE
257 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
259 if (!isset($this->storage
[$plugin]))
260 $this->storage
[$plugin] = array();
262 $content = $this->dbh
->escape_string(serialize($this->storage
[$plugin]));
264 if ($this->dbh
->num_rows($result) != 0) {
265 $this->dbh
->query("UPDATE ttrss_plugin_storage SET content = '$content'
266 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
269 $this->dbh
->query("INSERT INTO ttrss_plugin_storage
270 (name,owner_uid,content) VALUES
271 ('$plugin','".$this->owner_uid
."','$content')");
274 $this->dbh
->query("COMMIT");
278 function set($sender, $name, $value, $sync = true) {
279 $idx = get_class($sender);
281 if (!isset($this->storage
[$idx]))
282 $this->storage
[$idx] = array();
284 $this->storage
[$idx][$name] = $value;
286 $_SESSION["plugin_storage"] = $this->storage
;
288 if ($sync) $this->save_data(get_class($sender));
291 function get($sender, $name, $default_value = false) {
292 $idx = get_class($sender);
294 if (isset($this->storage
[$idx][$name])) {
295 return $this->storage
[$idx][$name];
297 return $default_value;
301 function get_all($sender) {
302 $idx = get_class($sender);
304 return $this->storage
[$idx];
307 function clear_data($sender) {
308 if ($this->owner_uid
) {
309 $idx = get_class($sender);
311 unset($this->storage
[$idx]);
313 $this->dbh
->query("DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
314 AND owner_uid = " . $this->owner_uid
);
316 $_SESSION["plugin_storage"] = $this->storage
;
320 function set_debug($debug) {
321 $this->debug
= $debug;
324 function get_debug() {
328 // Plugin feed functions are *EXPERIMENTAL*!
330 // cat_id: only -1 is supported (Special)
331 function add_feed($cat_id, $title, $icon, $sender) {
332 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
334 $id = count($this->feeds
[$cat_id]);
336 array_push($this->feeds
[$cat_id],
337 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
342 function get_feeds($cat_id) {
343 return $this->feeds
[$cat_id];
346 // convert feed_id (e.g. -129) to pfeed_id first
347 function get_feed_handler($pfeed_id) {
348 foreach ($this->feeds
as $cat) {
349 foreach ($cat as $feed) {
350 if ($feed['id'] == $pfeed_id) {
351 return $feed['sender'];
357 static function pfeed_to_feed_id($label) {
358 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
361 static function feed_to_pfeed_id($feed) {
362 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
365 function add_api_method($name, $sender) {
366 if ($this->is_system($sender)) {
367 $this->api_methods
[strtolower($name)] = $sender;
371 function get_api_method($name) {
372 return $this->api_methods
[$name];