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