Last active
December 14, 2015 07:09
-
-
Save Omattyao/5048463 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ExtensionAPI{ | |
private $api, $object, $dcmds, $ecmds, $alias, $help, $config; | |
public function __construct(ServerAPI $api, $cmds) { | |
$this->api = $api; | |
$this->dcmds = $cmds; | |
$this->object = array(); | |
$this->ecmds = array(); | |
$this->config = array(); | |
$this->alias = array(); | |
} | |
public function run($cmd, $params, $sign, &$option){ | |
$output = call_user_func($this->ecmds[$cmd], $cmd, $params, $sign, $option); | |
return $output; | |
} | |
public function register($cmd, $help, $ext){ | |
$cmd = strtolower(trim($cmd)); | |
$class = get_class($ext[0]); | |
if (!is_callable($ext) or array_key_exists($cmd, $this->ecmds) or in_array($cmd, $this->dcmds)) { | |
console("[ERROR] Failed to add \"/$cmd\"."); | |
return false; | |
} | |
$this->ecmds[$cmd] = array($this->object[$class], $ext[1]); | |
$this->help[$cmd] = $help; | |
$this->fillDefaults(array('extension' => array('ADMIN' => array($cmd => on), 'TRUST' => array($cmd => on), 'GUEST' => array($cmd => on))), $this->config); | |
} | |
public function event($event, $ext) { | |
$this->api->event($event, $ext); | |
} | |
public function alias($alias, $original) { | |
if (array_key_exists($alias, $this->alias)) { | |
console("[ERROR][SignConsole] Alias overlap: $alias"); | |
return false; | |
} | |
$this->alias[$alias] = $original; | |
} | |
public function createConfig(scClass $extension, $default = array()) { | |
$class = get_class($extension); | |
$path = FILE_PATH."plugins/SignConsole/".$class."/"; | |
if (!file_exists($path."config.yml")) { | |
@mkdir($path, 0777); | |
$this->api->plugin->writeYAML($path."config.yml", $default); | |
} else { | |
$data = $this->api->plugin->readYAML($path."config.yml"); | |
$this->fillDefaults($default, $data); | |
$this->api->plugin->writeYAML($path."config.yml", $data); | |
} | |
return $path; | |
} | |
public function fillDefaults($default, &$yaml) { | |
foreach($default as $k => $v){ | |
if(is_array($v)){ | |
if(!isset($yaml[$k]) or !is_array($yaml[$k])){ | |
$yaml[$k] = array(); | |
} | |
$this->fillDefaults($v, $yaml[$k]); | |
}elseif(!isset($yaml[$k])){ | |
$yaml[$k] = $v; | |
} | |
} | |
} | |
public function loadExtension($sc) { | |
console("[SignConsole] Loading extensions..."); | |
@mkdir(FILE_PATH."plugins/SignConsole/extensions/"); | |
if ($handle = opendir(FILE_PATH."plugins/SignConsole/extensions/")) { | |
while (false !== ($file =readdir($handle))) { | |
if (!preg_match("/.+.php$/", $file)) { | |
continue; | |
} | |
$file = FILE_PATH."plugins/SignConsole/extensions/".$file; | |
$info = false; | |
$info = $this->loadInfo($file); | |
if ($info == false) { | |
continue; | |
} | |
$class = trim($info["class"]); | |
$this->object[$class] = new $class($this->api, $sc); | |
if (!$this->object[$class] instanceof scClass) { | |
console("[ERROR] [SignConsole] Extension \"\x1b[36m".$info["name"]."\x1b[0m\" doesn't extend the scClass class"); | |
unset($this->object[$class]); | |
continue; | |
} | |
$this->object[$class]->init(); | |
} | |
closedir($handle); | |
} | |
} | |
private function loadInfo($file) { | |
$content = file_get_contents($file); | |
$info = strstr($content, "*/", true); | |
$content = substr(strstr($content, "*/"),2); | |
if(preg_match_all('#([a-zA-Z0-9\-_]*)=([^\r\n]*)#u', $info, $matches) == 0){ //false or 0 matches | |
console("[ERROR] [SignConsole] Failed parsing of ".basename($file)); | |
return false; | |
} | |
$info = array(); | |
foreach($matches[1] as $k => $i){ | |
$v = $matches[2][$k]; | |
switch(strtolower($v)){ | |
case "on": | |
case "true": | |
case "yes": | |
$v = true; | |
break; | |
case "off": | |
case "false": | |
case "no": | |
$v = false; | |
break; | |
} | |
$info[$i] = $v; | |
} | |
if(!isset($info["name"]) or !isset($info["version"]) or !isset($info["class"]) or !isset($info["author"])){ | |
console("[ERROR] [SignConsole] Failed parsing of ".basename($file)); | |
return false; | |
} | |
console("[INFO] [SignConsole] Loading extension \"\x1b[32m".$info["name"]."\x1b[0m\" \x1b[35m".$info["version"]."\x1b[0m by \x1b[36m".$info["author"]."\x1b[0m"); | |
if(class_exists($info["class"])){ | |
console("[ERROR] [SignConsole] Failed loading extension: class exists"); | |
return false; | |
} | |
if(eval($content) === false or !class_exists($info["class"])){ | |
console("[ERROR] [SignConsole] Failed loading extension: evaluation error"); | |
return false; | |
} | |
if(!isset($info["apiversion"]) or intval($info["apiversion"]) < CURRENT_API_VERSION){ | |
console("[ERROR] [SignConsole] Extension \"".$info["name"]."\" uses an outdated API! It can crash or corrupt the server!"); | |
} | |
return $info; | |
} | |
public function getHelp() { | |
return $this->help; | |
} | |
public function getConfig() { | |
return $this->config; | |
} | |
public function getAlias() { | |
return $this->alias; | |
} | |
public function getExtensionCmds() { | |
$cmds = array_keys($this->ecmds); | |
return $cmds; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment