Files
overwatch/lib/libModOverwatch.class.php
2015-07-27 18:06:37 +00:00

83 lines
2.1 KiB
PHP

<?php
/**
* A simple Module loader.
* --------------------
* Devel: Nick Daniels
* Twttr: @ManicSec
*/
class ModuleLoader {
private $_module_list = array();
public static function startInst() {
static $inst = null;
if ($inst === null) {
$inst = new self;
}
return $inst;
}
public function loadModule($module = null, $path = null, $type = "php") {
if($path == null)
{
$path = dirname(__FILE__);
} else {
$path_specified = true;
}
if($module === null)
die("Error: Module is null.");
if($path_specified != true)
{
if(!file_exists("$path/modules"))
die("Error: Modules directory missing.");
if(!file_exists("$path/modules/$module"))
die("Error: Module '$module' not found.");
if(!file_exists("$path/modules/$module/$module.$type"))
die("Error: Module '$module' corrupted.");
require_once "modules/$module/$module.$type";
} else {
require_once "$path/$module/$module.$type";
}
$this->_module_list[$module]['stat'] = 'loading';
$this->_module_list[$module]['inst'] = $module::loadInst();
if(!call_user_func(array($this->_module_list[$module]['inst'], 'module_loaded'))) {
$this->_module_list[$module]['stat'] = 'unloaded';
die("Error: Module '$module' didn't load.");
}
$this->_module_list[$module]['stat'] = 'loaded';
return $this->_module_list[$module]['inst'];
}
public function unloadModule($module = null) {
$path = dirname(__FILE__);
if($module === null)
die("Error: Module is null.");
if(is_array($this->_module_list[$module]) &&
is_object($this->_module_list[$module]['inst'])) {
$this->_module_list[$module]['stat'] = 'unloaded';
unset($this->_module_list[$module]['inst']);
return true;
}
return false;
}
public function listModules() {
return is_array($this->_module_list)?$this->_module_list:array();
}
}
?>