ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Module.php
Go to the documentation of this file.
1<?php
2namespace SimpleSAML;
3
12class Module
13{
14
20 public static $modules = array();
21
27 public static $module_info = array();
28
29
39 public static function getModuleDir($module)
40 {
41 $baseDir = dirname(dirname(dirname(__FILE__))).'/modules';
43
44 return $moduleDir;
45 }
46
47
59 public static function isModuleEnabled($module)
60 {
62 return self::isModuleEnabledWithConf($module, $config->getArray('module.enable', array()));
63 }
64
65
66 private static function isModuleEnabledWithConf($module, $mod_config)
67 {
68 if (isset(self::$module_info[$module]['enabled'])) {
69 return self::$module_info[$module]['enabled'];
70 }
71
72 if (!empty(self::$modules) && !in_array($module, self::$modules, true)) {
73 return false;
74 }
75
76 $moduleDir = self::getModuleDir($module);
77
78 if (!is_dir($moduleDir)) {
79 self::$module_info[$module]['enabled'] = false;
80 return false;
81 }
82
83 if (isset($mod_config[$module])) {
84 if (is_bool($mod_config[$module])) {
85 self::$module_info[$module]['enabled'] = $mod_config[$module];
86 return $mod_config[$module];
87 }
88
89 throw new \Exception("Invalid module.enable value for the '$module' module.");
90 }
91
92 if (assert_options(ASSERT_ACTIVE) &&
93 !file_exists($moduleDir.'/default-enable') &&
94 !file_exists($moduleDir.'/default-disable')
95 ) {
96 \SimpleSAML\Logger::error("Missing default-enable or default-disable file for the module $module");
97 }
98
99 if (file_exists($moduleDir.'/enable')) {
100 self::$module_info[$module]['enabled'] = true;
101 return true;
102 }
103
104 if (!file_exists($moduleDir.'/disable') && file_exists($moduleDir.'/default-enable')) {
105 self::$module_info[$module]['enabled'] = true;
106 return true;
107 }
108
109 self::$module_info[$module]['enabled'] = false;
110 return false;
111 }
112
113
121 public static function getModules()
122 {
123 if (!empty(self::$modules)) {
124 return self::$modules;
125 }
126
127 $path = self::getModuleDir('.');
128
129 $dh = scandir($path);
130 if ($dh === false) {
131 throw new \Exception('Unable to open module directory "'.$path.'".');
132 }
133
134 foreach ($dh as $f) {
135 if ($f[0] === '.') {
136 continue;
137 }
138
139 if (!is_dir($path.'/'.$f)) {
140 continue;
141 }
142
143 self::$modules[] = $f;
144 }
145
146 return self::$modules;
147 }
148
149
169 public static function resolveClass($id, $type, $subclass = null)
170 {
171 assert(is_string($id));
172 assert(is_string($type));
173 assert(is_string($subclass) || $subclass === null);
174
175 $tmp = explode(':', $id, 2);
176 if (count($tmp) === 1) { // no module involved
177 $className = $tmp[0];
178 if (!class_exists($className)) {
179 throw new \Exception("Could not resolve '$id': no class named '$className'.");
180 }
181 } else { // should be a module
182 // make sure empty types are handled correctly
183 $type = (empty($type)) ? '_' : '_'.$type.'_';
184
185 // check for the old-style class names
186 $className = 'sspmod_'.$tmp[0].$type.$tmp[1];
187
188 if (!class_exists($className)) {
189 // check for the new-style class names, using namespaces
190 $type = str_replace('_', '\\', $type);
191 $newClassName = 'SimpleSAML\Module\\'.$tmp[0].$type.$tmp[1];
192
193 if (!class_exists($newClassName)) {
194 throw new \Exception("Could not resolve '$id': no class named '$className' or '$newClassName'.");
195 }
196 $className = $newClassName;
197 }
198 }
199
200 if ($subclass !== null && !is_subclass_of($className, $subclass)) {
201 throw new \Exception(
202 'Could not resolve \''.$id.'\': The class \''.$className.'\' isn\'t a subclass of \''.$subclass.'\'.'
203 );
204 }
205
206 return $className;
207 }
208
209
220 public static function getModuleURL($resource, array $parameters = array())
221 {
222 assert(is_string($resource));
223 assert($resource[0] !== '/');
224
225 $url = Utils\HTTP::getBaseURL().'module.php/'.$resource;
226 if (!empty($parameters)) {
227 $url = Utils\HTTP::addURLParameters($url, $parameters);
228 }
229 return $url;
230 }
231
232
242 public static function getModuleHooks($module)
243 {
244 if (isset(self::$modules[$module]['hooks'])) {
245 return self::$modules[$module]['hooks'];
246 }
247
248 $hook_dir = self::getModuleDir($module).'/hooks';
249 if (!is_dir($hook_dir)) {
250 return array();
251 }
252
253 $hooks = array();
254 $files = scandir($hook_dir);
255 foreach ($files as $file) {
256 if ($file[0] === '.') {
257 continue;
258 }
259
260 if (!preg_match('/hook_(\w+)\.php/', $file, $matches)) {
261 continue;
262 }
263 $hook_name = $matches[1];
264 $hook_func = $module.'_hook_'.$hook_name;
265 $hooks[$hook_name] = array('file' => $hook_dir.'/'.$file, 'func' => $hook_func);
266 }
267 return $hooks;
268 }
269
270
281 public static function callHooks($hook, &$data = null)
282 {
283 assert(is_string($hook));
284
285 $modules = self::getModules();
286 $config = \SimpleSAML_Configuration::getOptionalConfig()->getArray('module.enable', array());
287 sort($modules);
288 foreach ($modules as $module) {
289 if (!self::isModuleEnabledWithConf($module, $config)) {
290 continue;
291 }
292
293 if (!isset(self::$module_info[$module]['hooks'])) {
294 self::$module_info[$module]['hooks'] = self::getModuleHooks($module);
295 }
296
297 if (!isset(self::$module_info[$module]['hooks'][$hook])) {
298 continue;
299 }
300
301 require_once(self::$module_info[$module]['hooks'][$hook]['file']);
302
303 if (!is_callable(self::$module_info[$module]['hooks'][$hook]['func'])) {
304 throw new \SimpleSAML_Error_Exception('Invalid hook \''.$hook.'\' for module \''.$module.'\'.');
305 }
306
307 $fn = self::$module_info[$module]['hooks'][$hook]['func'];
308 $fn($data);
309 }
310 }
311}
$path
Definition: aliased.php:25
An exception for terminatinating execution or to throw for unit testing.
static error($string)
Definition: Logger.php:166
static getModuleDir($module)
Retrieve the base directory for a module.
Definition: Module.php:39
static getModules()
Get available modules.
Definition: Module.php:121
static resolveClass($id, $type, $subclass=null)
Resolve module class.
Definition: Module.php:169
static isModuleEnabled($module)
Determine whether a module is enabled.
Definition: Module.php:59
static isModuleEnabledWithConf($module, $mod_config)
Definition: Module.php:66
static getOptionalConfig($filename='config.php', $configSet='simplesaml')
Load a configuration file from a configuration set.
if(!file_exists(getcwd() . '/ilias.ini.php'))
registration confirmation script for ilias
Definition: confirmReg.php:12
if(!array_key_exists('StateId', $_REQUEST)) $id
$config
Definition: bootstrap.php:15
if($modEnd===false) $module
Definition: module.php:59
if( $url===false) if(!SimpleSAML\Module::isModuleEnabled($module)) if(strpos( $url, '\\') !==false) elseif(strpos($url, './') !==false) $moduleDir
Definition: module.php:79
Attribute-related utility methods.
$type