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