-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_plugins.php
246 lines (215 loc) · 6.53 KB
/
lib_plugins.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/*
* lib_plugins.php
*
* Copyright (C) 2016-2023 Null Team
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
?>
<?php
require_once ("ansql/debug.php");
abstract class Plugin
{
static $plugin_classes = array();
/*
Example of defining $hooks
protected $_hooks = array(
array(
"name" => "predefined_network_settings",
"callback" => "secMccMnc",
"unicity_tags" => array("mcc,mnc")
),
array(
"name" => "global_sim_options" ,
"callback" => "simProgrammerLink"
)
);
*/
/**
* Includes plugins located in plugins/ directory
* Function should be called between including defaults.php & config.php like Plugin::includePlugins(), because some plugins might modify global variables from defaults.php
* When plugins are included hooks for them are automatically registered, you won't need to register hooks manually
*/
public static function includePlugins()
{
if (!is_dir("plugins"))
return;
$handle = opendir("plugins");
while (false !== ($file = readdir($handle))) {
if (substr($file,-4) != '.php')
continue;
else {
require_once("plugins/".$file);
}
}
$classes = get_declared_classes();
foreach ($classes as $class) {
if (self::checkAncestors($class)) {
self::$plugin_classes[] = $class;
$obj = new $class;
$cb = array($obj,"registerHooks");
if (is_callable($cb))
call_user_func($cb);
}
}
}
/**
* Includes javascript for plugins located in plugins/ directory
* Function should be called between HTML tags <head> and </head>
* A plugin must implement "includeJs" method in which it outputs the desired js
*/
public static function includeJSPlugins()
{
if (!is_array(self::$plugin_classes) || !count(self::$plugin_classes))
return;
foreach (self::$plugin_classes as $class) {
$obj = new $class;
$cb = array($obj,"includeJs");
if (is_callable($cb))
call_user_func($cb);
}
}
/**
* Includes css for plugins located in plugins/ directory
* Function should be called between HTML tags <head> and </head>
* A plugin must implement "includeCSS" method in which it outputs the desired css
*/
public static function includeCSSPlugins()
{
if (!count(self::$plugin_classes))
return;
foreach (self::$plugin_classes as $class) {
$obj = new $class;
$cb = array($obj,"includeCSS");
if (is_callable($cb))
call_user_func($cb);
}
}
/**
* Register hook $hook_handler to be called for hook name $hook_name. If unicity tags are specified then system will return an error in case
* @global type $plugin_hooks
* @param String $hook_name
* @param Callback $hook_handler string
* @param String $plugin_name
* @param Array $unicity_tags
* @param Bool $class, if is true then $hook_handler will be a method name, else will be a function name
*/
public static function registerHook($hook_name, $hook_handler, $unicity_tags=array(), $plugin_name=null)
{
global $plugin_hooks;
global $plugin_errors;
if (!isset($plugin_hooks))
$plugin_hooks = array();
if (!array_key_exists($hook_name, $plugin_hooks))
$plugin_hooks[$hook_name] = array(
"unique_tags" => array(),
"hooks" => array()
);
$obj_class = get_called_class();
if (!$plugin_name)
$plugin_name = $obj_class;
if (is_array($unicity_tags)) {
foreach ($unicity_tags as $tag) {
if (isset($plugin_hooks[$hook_name]["unique_tags"][$tag]))
$plugin_errors[] = "Invalid plugin combination '$plugin_name' - '".$plugin_hooks[$hook_name]["unique_tags"][$tag]."' ($tag). Problems registering hook '$hook_handler' on '$hook_name' for plugin '$plugin_name'.";
}
}
if ($obj_class!="Plugin") {
$obj = new $obj_class;
$cb = array($obj,$hook_handler);
} else {
$cb = $hook_handler;
}
if (!is_callable($cb)) {
$plugin_errors[] = "Hook handler is not callable: $hook_handler.";
return;
}
$plugin_hooks[$hook_name]["hooks"][] = $cb;
}
/**
* Call registered hooks for specific hook name
* @global type $plugin_hooks
* @param type $hook_name
*/
public static function callHooks($hook_name, $params=array())
{
global $plugin_hooks;
if (isset($plugin_hooks[$hook_name]["hooks"])) {
foreach ($plugin_hooks[$hook_name]["hooks"] as $cb_hook) {
if (is_callable($cb_hook)) {
call_user_func_array($cb_hook,$params);
} else {
Debug::trigger_report("critical", "Tried to call hook '$cb_hook', but it's not callable.");
}
}
}
}
/**
* Register all hooks for the class. This is done automatically when plugins are included, it should not be called individually
*/
protected function registerHooks()
{
if (!isset($this->_hooks) || !is_array($this->_hooks))
return;
foreach($this->_hooks as $hook ) {
$unicity_tags = (isset($hook["unicity_tags"])) ? $hook["unicity_tags"] : null;
self::registerHook($hook["name"], $hook["callback"], $unicity_tags);
}
}
/**
* Check class ancestors to see if one of them is Plugin
* @param $class String
* @return Bool
*/
protected static function checkAncestors($class)
{
$parent = get_parent_class($class);
if ($parent=="Plugin")
return true;
elseif (!$parent)
return false;
return self::checkAncestors($parent);
}
}
/**
* Includes plugins located in plugins/ directory
* Function should be called between including defaults.php & config.php, because some plugins might modify global variables from defaults.php
*/
function include_plugins()
{
Plugin::includePlugins();
}
/**
* Includes javascript for plugins located in plugins/ directory
* Function should be called between html tags <head> && </head>
*/
function include_js_plugins()
{
Plugin::includeJSPlugins();
}
/**
* Includes css for plugins located in plugins/ directory
* Function should be called between html tags <head> && </head>
*/
function include_cs_plugins()
{
Plugin::includeCSSPlugins();
}
function register_hook($hook_name, $hook_handler, $plugin_name=NULL, $unicity_tags=array())
{
Plugin::registerHook($hook_name, $hook_handler, $unicity_tags, $plugin_name);
}
function call_hooks($hook_name,$params=array())
{
Plugin::callHooks($hook_name,$params);
}