]> git.wh0rd.org - tt-rss.git/blame - classes/pluginhost.php
move authentication modules to plugins/
[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 11 const HOOK_PREFS_TAB = 3;
699daf58 12 const HOOK_PREFS_TAB_SECTION = 4;
6cbe53c9 13 const HOOK_PREFS_TABS = 5;
4412b877 14 const HOOK_FEED_PARSED = 6;
41b82aa4 15 const HOOK_UPDATE_TASK = 7;
0f28f81f 16 const HOOK_AUTH_USER = 8;
19c73507 17
d2a421e3
AD
18 const KIND_ALL = 1;
19 const KIND_SYSTEM = 2;
20 const KIND_USER = 3;
21
19c73507
AD
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
6065f3ad
AD
43 function run_hooks($type, $method, $args) {
44 foreach ($this->get_hooks($type) as $hook) {
45 $hook->$method($args);
46 }
47 }
48
19c73507
AD
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) {
19b3992b
AD
67 if (isset($this->hooks[$type])) {
68 return $this->hooks[$type];
69 } else {
70 return array();
71 }
19c73507 72 }
d2a421e3 73 function load_all($kind) {
7a866114 74 $plugins = array_map("basename", glob("plugins/*"));
d2a421e3 75 $this->load(join(",", $plugins), $kind);
7a866114 76 }
19c73507 77
d2a421e3 78 function load($classlist, $kind) {
19c73507
AD
79 $plugins = explode(",", $classlist);
80
81 foreach ($plugins as $class) {
82 $class = trim($class);
8dcb2b47 83 $class_file = strtolower(basename($class));
19c73507
AD
84 $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php";
85
de612e7a
AD
86 if (!isset($this->plugins[$class])) {
87 if (file_exists($file)) require_once $file;
19c73507 88
de612e7a
AD
89 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
90 $plugin = new $class($this);
19c73507 91
d2a421e3
AD
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 }
de612e7a 110 }
19c73507
AD
111 }
112 }
113 }
114
de612e7a 115 function is_system($plugin) {
d2a421e3 116 $about = $plugin->about();
de612e7a
AD
117
118 return @$about[3];
119 }
120
121 // only system plugins are allowed to modify routing
8dcb2b47 122 function add_handler($handler, $method, $sender) {
6cbe53c9 123 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
124 $method = strtolower($method);
125
de612e7a
AD
126 if ($this->is_system($sender)) {
127 if (!is_array($this->handlers[$handler])) {
128 $this->handlers[$handler] = array();
129 }
8dcb2b47 130
de612e7a
AD
131 $this->handlers[$handler][$method] = $sender;
132 }
8dcb2b47
AD
133 }
134
135 function del_handler($handler, $method) {
6cbe53c9 136 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
137 $method = strtolower($method);
138
de612e7a
AD
139 if ($this->is_system($sender)) {
140 unset($this->handlers[$handler][$method]);
141 }
8dcb2b47
AD
142 }
143
144 function lookup_handler($handler, $method) {
6cbe53c9 145 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
146 $method = strtolower($method);
147
148 if (is_array($this->handlers[$handler])) {
6cbe53c9
AD
149 if (isset($this->handlers[$handler]["*"])) {
150 return $this->handlers[$handler]["*"];
151 } else {
152 return $this->handlers[$handler][$method];
153 }
8dcb2b47
AD
154 }
155
156 return false;
157 }
73f28fe9
AD
158
159 function add_command($command, $description, $sender) {
160 $command = "-" . str_replace("-", "_", strtolower($command));
161
d2a421e3
AD
162 $this->commands[$command] = array("description" => $description,
163 "class" => $sender);
73f28fe9
AD
164 }
165
166 function del_command($command) {
167 $command = "-" . strtolower($command);
168
d2a421e3 169 unset($this->commands[$command]);
73f28fe9
AD
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
19c73507
AD
197}
198?>