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 $last_registered;
14 private static $instance;
16 const API_VERSION
= 1;
18 const HOOK_ARTICLE_BUTTON
= 1;
19 const HOOK_ARTICLE_FILTER
= 2;
20 const HOOK_PREFS_TAB
= 3;
21 const HOOK_PREFS_TAB_SECTION
= 4;
22 const HOOK_PREFS_TABS
= 5;
23 const HOOK_FEED_PARSED
= 6;
24 const HOOK_UPDATE_TASK
= 7;
25 const HOOK_AUTH_USER
= 8;
26 const HOOK_HOTKEY_MAP
= 9;
27 const HOOK_RENDER_ARTICLE
= 10;
28 const HOOK_RENDER_ARTICLE_CDM
= 11;
29 const HOOK_FEED_FETCHED
= 12;
30 const HOOK_SANITIZE
= 13;
31 const HOOK_RENDER_ARTICLE_API
= 14;
32 const HOOK_TOOLBAR_BUTTON
= 15;
33 const HOOK_ACTION_ITEM
= 16;
34 const HOOK_HEADLINE_TOOLBAR_BUTTON
= 17;
35 const HOOK_HOTKEY_INFO
= 18;
36 const HOOK_ARTICLE_LEFT_BUTTON
= 19;
39 const KIND_SYSTEM
= 2;
42 function __construct() {
43 $this->dbh
= Db
::get();
44 $this->storage
= $_SESSION["plugin_storage"];
46 if (!$this->storage
) $this->storage
= array();
49 private function __clone() {
53 public static function getInstance() {
54 if (self
::$instance == null)
55 self
::$instance = new self();
57 return self
::$instance;
60 private function register_plugin($name, $plugin) {
61 //array_push($this->plugins, $plugin);
62 $this->plugins
[$name] = $plugin;
66 header("Content-type: text/plain");
68 print "One of the plugins called obsolete host method get_link(). This plugin needs to be updated or removed.\n\n";
70 print "List of plugins loaded: " . join(" ,", array_keys($this->plugins
)) . "\n\n";
72 print "Last plugin initialized (possible culprit): " . $this->last_registered
. "\n";
80 function get_plugins() {
81 return $this->plugins
;
84 function get_plugin($name) {
85 return $this->plugins
[$name];
88 function run_hooks($type, $method, $args) {
89 foreach ($this->get_hooks($type) as $hook) {
90 $hook->$method($args);
94 function add_hook($type, $sender) {
95 if (!is_array($this->hooks
[$type])) {
96 $this->hooks
[$type] = array();
99 array_push($this->hooks
[$type], $sender);
102 function del_hook($type, $sender) {
103 if (is_array($this->hooks
[$type])) {
104 $key = array_Search($this->hooks
[$type], $sender);
105 if ($key !== FALSE) {
106 unset($this->hooks
[$type][$key]);
111 function get_hooks($type) {
112 if (isset($this->hooks
[$type])) {
113 return $this->hooks
[$type];
118 function load_all($kind, $owner_uid = false) {
119 $plugins = array_map("basename", glob("plugins/*"));
120 $this->load(join(",", $plugins), $kind, $owner_uid);
123 function load($classlist, $kind, $owner_uid = false) {
124 $plugins = explode(",", $classlist);
126 $this->owner_uid
= (int) $owner_uid;
128 foreach ($plugins as $class) {
129 $class = trim($class);
130 $class_file = strtolower(basename($class));
132 if (!is_dir(dirname(__FILE__
)."/../plugins/$class_file")) continue;
134 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
136 if (!isset($this->plugins
[$class])) {
137 if (file_exists($file)) require_once $file;
139 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
140 $plugin = new $class($this);
142 $plugin_api = $plugin->api_version();
144 if ($plugin_api < PluginHost
::API_VERSION
) {
145 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost
::API_VERSION
. ", got: $plugin_api)", E_USER_WARNING
);
149 $this->last_registered
= $class;
152 case $this::KIND_SYSTEM
:
153 if ($this->is_system($plugin)) {
154 $plugin->init($this);
155 $this->register_plugin($class, $plugin);
158 case $this::KIND_USER
:
159 if (!$this->is_system($plugin)) {
160 $plugin->init($this);
161 $this->register_plugin($class, $plugin);
164 case $this::KIND_ALL
:
165 $plugin->init($this);
166 $this->register_plugin($class, $plugin);
174 function is_system($plugin) {
175 $about = $plugin->about();
180 // only system plugins are allowed to modify routing
181 function add_handler($handler, $method, $sender) {
182 $handler = str_replace("-", "_", strtolower($handler));
183 $method = strtolower($method);
185 if ($this->is_system($sender)) {
186 if (!is_array($this->handlers
[$handler])) {
187 $this->handlers
[$handler] = array();
190 $this->handlers
[$handler][$method] = $sender;
194 function del_handler($handler, $method) {
195 $handler = str_replace("-", "_", strtolower($handler));
196 $method = strtolower($method);
198 if ($this->is_system($sender)) {
199 unset($this->handlers
[$handler][$method]);
203 function lookup_handler($handler, $method) {
204 $handler = str_replace("-", "_", strtolower($handler));
205 $method = strtolower($method);
207 if (is_array($this->handlers
[$handler])) {
208 if (isset($this->handlers
[$handler]["*"])) {
209 return $this->handlers
[$handler]["*"];
211 return $this->handlers
[$handler][$method];
218 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
219 $command = str_replace("-", "_", strtolower($command));
221 $this->commands
[$command] = array("description" => $description,
223 "arghelp" => $arghelp,
227 function del_command($command) {
228 $command = "-" . strtolower($command);
230 unset($this->commands
[$command]);
233 function lookup_command($command) {
234 $command = "-" . strtolower($command);
236 if (is_array($this->commands
[$command])) {
237 return $this->commands
[$command]["class"];
245 function get_commands() {
246 return $this->commands
;
249 function run_commands($args) {
250 foreach ($this->get_commands() as $command => $data) {
251 if (isset($args[$command])) {
252 $command = str_replace("-", "", $command);
253 $data["class"]->$command($args);
258 function load_data($force = false) {
259 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
260 $plugin = $this->dbh
->escape_string($plugin);
262 $result = $this->dbh
->query("SELECT name, content FROM ttrss_plugin_storage
263 WHERE owner_uid = '".$this->owner_uid
."'");
265 while ($line = $this->dbh
->fetch_assoc($result)) {
266 $this->storage
[$line["name"]] = unserialize($line["content"]);
269 $_SESSION["plugin_storage"] = $this->storage
;
273 private function save_data($plugin) {
274 if ($this->owner_uid
) {
275 $plugin = $this->dbh
->escape_string($plugin);
277 $this->dbh
->query("BEGIN");
279 $result = $this->dbh
->query("SELECT id FROM ttrss_plugin_storage WHERE
280 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
282 if (!isset($this->storage
[$plugin]))
283 $this->storage
[$plugin] = array();
285 $content = $this->dbh
->escape_string(serialize($this->storage
[$plugin]));
287 if ($this->dbh
->num_rows($result) != 0) {
288 $this->dbh
->query("UPDATE ttrss_plugin_storage SET content = '$content'
289 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
292 $this->dbh
->query("INSERT INTO ttrss_plugin_storage
293 (name,owner_uid,content) VALUES
294 ('$plugin','".$this->owner_uid
."','$content')");
297 $this->dbh
->query("COMMIT");
301 function set($sender, $name, $value, $sync = true) {
302 $idx = get_class($sender);
304 if (!isset($this->storage
[$idx]))
305 $this->storage
[$idx] = array();
307 $this->storage
[$idx][$name] = $value;
309 $_SESSION["plugin_storage"] = $this->storage
;
311 if ($sync) $this->save_data(get_class($sender));
314 function get($sender, $name, $default_value = false) {
315 $idx = get_class($sender);
317 if (isset($this->storage
[$idx][$name])) {
318 return $this->storage
[$idx][$name];
320 return $default_value;
324 function get_all($sender) {
325 $idx = get_class($sender);
327 return $this->storage
[$idx];
330 function clear_data($sender) {
331 if ($this->owner_uid
) {
332 $idx = get_class($sender);
334 unset($this->storage
[$idx]);
336 $this->dbh
->query("DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
337 AND owner_uid = " . $this->owner_uid
);
339 $_SESSION["plugin_storage"] = $this->storage
;
343 function set_debug($debug) {
344 $this->debug
= $debug;
347 function get_debug() {
351 // Plugin feed functions are *EXPERIMENTAL*!
353 // cat_id: only -1 is supported (Special)
354 function add_feed($cat_id, $title, $icon, $sender) {
355 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
357 $id = count($this->feeds
[$cat_id]);
359 array_push($this->feeds
[$cat_id],
360 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
365 function get_feeds($cat_id) {
366 return $this->feeds
[$cat_id];
369 // convert feed_id (e.g. -129) to pfeed_id first
370 function get_feed_handler($pfeed_id) {
371 foreach ($this->feeds
as $cat) {
372 foreach ($cat as $feed) {
373 if ($feed['id'] == $pfeed_id) {
374 return $feed['sender'];
380 static function pfeed_to_feed_id($label) {
381 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
384 static function feed_to_pfeed_id($feed) {
385 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
388 function add_api_method($name, $sender) {
389 if ($this->is_system($sender)) {
390 $this->api_methods
[strtolower($name)] = $sender;
394 function get_api_method($name) {
395 return $this->api_methods
[$name];