]> git.wh0rd.org - tt-rss.git/blame - classes/pluginhost.php
experimental support for per-user plugins (bump schema)
[tt-rss.git] / classes / pluginhost.php
CommitLineData
19c73507
AD
1<?php
2class PluginHost {
3 private $link;
4 private $hooks = array();
5 private $plugins = array();
8dcb2b47 6 private $handlers = array();
73f28fe9 7 private $commands = array();
19c73507
AD
8
9 const HOOK_ARTICLE_BUTTON = 1;
10 const HOOK_ARTICLE_FILTER = 2;
6065f3ad
AD
11 const HOOK_PREFS_TAB = 3;
12 const HOOK_PREFS_SECTION = 4;
6cbe53c9 13 const HOOK_PREFS_TABS = 5;
4412b877 14 const HOOK_FEED_PARSED = 6;
41b82aa4 15 const HOOK_UPDATE_TASK = 7;
19c73507
AD
16
17 function __construct($link) {
18 $this->link = $link;
19 }
20
21 private function register_plugin($name, $plugin) {
22 //array_push($this->plugins, $plugin);
23 $this->plugins[$name] = $plugin;
24 }
25
26 function get_link() {
27 return $this->link;
28 }
29
30 function get_plugins() {
31 return $this->plugins;
32 }
33
34 function get_plugin($name) {
35 return $this->plugins[$name];
36 }
37
6065f3ad
AD
38 function run_hooks($type, $method, $args) {
39 foreach ($this->get_hooks($type) as $hook) {
40 $hook->$method($args);
41 }
42 }
43
19c73507
AD
44 function add_hook($type, $sender) {
45 if (!is_array($this->hooks[$type])) {
46 $this->hooks[$type] = array();
47 }
48
49 array_push($this->hooks[$type], $sender);
50 }
51
52 function del_hook($type, $sender) {
53 if (is_array($this->hooks[$type])) {
54 $key = array_Search($this->hooks[$type], $sender);
55 if ($key !== FALSE) {
56 unset($this->hooks[$type][$key]);
57 }
58 }
59 }
60
61 function get_hooks($type) {
19b3992b
AD
62 if (isset($this->hooks[$type])) {
63 return $this->hooks[$type];
64 } else {
65 return array();
66 }
19c73507 67 }
7a866114
AD
68 function load_all() {
69 $plugins = array_map("basename", glob("plugins/*"));
70 $this->load(join(",", $plugins));
71 }
19c73507
AD
72
73 function load($classlist) {
74 $plugins = explode(",", $classlist);
75
76 foreach ($plugins as $class) {
77 $class = trim($class);
8dcb2b47 78 $class_file = strtolower(basename($class));
19c73507
AD
79 $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php";
80
de612e7a
AD
81 if (!isset($this->plugins[$class])) {
82 if (file_exists($file)) require_once $file;
19c73507 83
de612e7a
AD
84 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
85 $plugin = new $class($this);
19c73507 86
de612e7a
AD
87 $this->register_plugin($class, $plugin);
88 }
19c73507
AD
89 }
90 }
91 }
92
de612e7a
AD
93 function is_system($plugin) {
94 $about = $plugin->_about();
95
96 return @$about[3];
97 }
98
99 // only system plugins are allowed to modify routing
8dcb2b47 100 function add_handler($handler, $method, $sender) {
6cbe53c9 101 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
102 $method = strtolower($method);
103
de612e7a
AD
104 if ($this->is_system($sender)) {
105 if (!is_array($this->handlers[$handler])) {
106 $this->handlers[$handler] = array();
107 }
8dcb2b47 108
de612e7a
AD
109 $this->handlers[$handler][$method] = $sender;
110 }
8dcb2b47
AD
111 }
112
113 function del_handler($handler, $method) {
6cbe53c9 114 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
115 $method = strtolower($method);
116
de612e7a
AD
117 if ($this->is_system($sender)) {
118 unset($this->handlers[$handler][$method]);
119 }
8dcb2b47
AD
120 }
121
122 function lookup_handler($handler, $method) {
6cbe53c9 123 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
124 $method = strtolower($method);
125
126 if (is_array($this->handlers[$handler])) {
6cbe53c9
AD
127 if (isset($this->handlers[$handler]["*"])) {
128 return $this->handlers[$handler]["*"];
129 } else {
130 return $this->handlers[$handler][$method];
131 }
8dcb2b47
AD
132 }
133
134 return false;
135 }
73f28fe9 136
de612e7a 137 // only system plugins are allowed to modify commands
73f28fe9
AD
138 function add_command($command, $description, $sender) {
139 $command = "-" . str_replace("-", "_", strtolower($command));
140
de612e7a
AD
141 if ($this->is_system($sender)) {
142 $this->commands[$command] = array("description" => $description,
143 "class" => $sender);
144 }
73f28fe9
AD
145 }
146
147 function del_command($command) {
148 $command = "-" . strtolower($command);
149
de612e7a
AD
150 if ($this->is_system($sender)) {
151 unset($this->commands[$command]);
152 }
73f28fe9
AD
153 }
154
155 function lookup_command($command) {
156 $command = "-" . strtolower($command);
157
158 if (is_array($this->commands[$command])) {
159 return $this->commands[$command]["class"];
160 } else {
161 return false;
162 }
163
164 return false;
165 }
166
167 function get_commands() {
168 return $this->commands;
169 }
170
171 function run_commands($args) {
172 foreach ($this->get_commands() as $command => $data) {
173 if (in_array($command, $args)) {
174 $command = str_replace("-", "", $command);
175 $data["class"]->$command($args);
176 }
177 }
178 }
179
19c73507
AD
180}
181?>