From ce8724c5edf27e50e757a36ec19f3dfae0a84652 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sat, 16 Mar 2013 12:52:14 +0100 Subject: [PATCH] PMF Plugin Loading --- src/API/PluginAPI.php | 62 +++++++++++++++++++++++++------------------ src/pmf/PMF.php | 12 +++++---- src/pmf/Plugin.php | 11 +++++--- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index 10b9e57d09..cc06449c28 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -50,42 +50,51 @@ class PluginAPI extends stdClass{ } public function load($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] [PluginAPI] 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; + if(strtolower(substr($file, -3)) === "pmf"){ + $pmf = new PMFPlugin($file); + $info = $pmf->getPluginInfo(); + }else{ + $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] [PluginAPI] Failed parsing of ".basename($file)); + return false; } - $info[$i] = $v; + $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; + } + $info["code"] = $content; + $info["class"] = trim(strtolower($info["class"])); } if(!isset($info["name"]) or !isset($info["version"]) or !isset($info["class"]) or !isset($info["author"])){ console("[ERROR] [PluginAPI] Failed parsing of ".basename($file)); + return false; } console("[INFO] [PluginAPI] Loading plugin \"\x1b[32m".$info["name"]."\x1b[0m\" \x1b[35m".$info["version"]." #".intval($info["apiversion"])."\x1b[0m by \x1b[36m".$info["author"]."\x1b[0m"); if(class_exists($info["class"])){ console("[ERROR] [PluginAPI] Failed loading plugin: class exists"); + return false; } - if(eval($content) === false or !class_exists($info["class"])){ + if(eval($info["code"]) === false or !class_exists($info["class"])){ console("[ERROR] [PluginAPI] Failed loading plugin: evaluation error"); + return false; } - $info["code"] = $content; - $info["class"] = trim(strtolower($info["class"])); + $className = $info["class"]; if(isset($info["apiversion"]) and intval($info["apiversion"]) > CURRENT_API_VERSION){ console("[ERROR] [PluginAPI] Plugin \"".$info["name"]."\" uses a newer API! It can crash or corrupt the server!"); @@ -168,7 +177,8 @@ class PluginAPI extends stdClass{ $dir = dir(DATA_PATH."plugins/"); while(false !== ($file = $dir->read())){ if($file{0} !== "."){ - if(strtolower(substr($file, -3)) === "php"){ + $ext = strtolower(substr($file, -3)); + if($ext === "php" or $ext === "pmf"){ $this->load(DATA_PATH."plugins/" . $file); } } diff --git a/src/pmf/PMF.php b/src/pmf/PMF.php index a9b1dcfddd..9cb0425c21 100644 --- a/src/pmf/PMF.php +++ b/src/pmf/PMF.php @@ -59,16 +59,16 @@ class PMF{ if($stat["size"] >= 5){ //Header + 2 Bytes return true; } - fclose($this->fp); + $this->close(); } return false; } public function parseInfo(){ + $this->seek(0); if(fread($this->fp, 3) !== "PMF"){ return false; - } - $this->seek(0); + } $this->version = ord($this->read(1)); switch($this->version){ case 0x01: @@ -87,7 +87,9 @@ class PMF{ public function close(){ unset($this->version, $this->type, $this->file); - fclose($this->fp); + if(is_object($this->fp)){ + fclose($this->fp); + } } public function create($file, $type, $version = PMF_CURRENT_VERSION){ @@ -98,7 +100,7 @@ class PMF{ } } $this->seek(0); - $this->write("PMF" . chr((int) $type) . chr((int) $version)); + $this->write("PMF" . chr((int) $version) . chr((int) $type)); } public function read($length){ diff --git a/src/pmf/Plugin.php b/src/pmf/Plugin.php index cefd95957e..74f175ebc3 100644 --- a/src/pmf/Plugin.php +++ b/src/pmf/Plugin.php @@ -39,8 +39,12 @@ class PMFPlugin extends PMF{ $this->parsePlugin(); } + public function getPluginInfo(){ + return $this->pluginData; + } + protected function parsePlugin(){ - if($this->type !== 0x01){ + if($this->getType() !== 0x01){ return false; } @@ -48,12 +52,13 @@ class PMFPlugin extends PMF{ $this->pluginData["name"] = $this->read(Utils::readShort($this->read(2), false)); $this->pluginData["version"] = $this->read(Utils::readShort($this->read(2), false)); $this->pluginData["author"] = $this->read(Utils::readShort($this->read(2), false)); + $this->pluginData["apiversion"] = Utils::readShort($this->read(2), false); $this->pluginData["class"] = $this->read(Utils::readShort($this->read(2), false)); $this->pluginData["identifier"] = $this->read(Utils::readShort($this->read(2), false)); //Will be used to check for updates - $this->pluginData["extra"] = $this->read(Utils::readShort($this->read(2), false)); //Additional custom plugin data + $this->pluginData["extra"] = gzinflate($this->read(Utils::readShort($this->read(2), false))); //Additional custom plugin data $this->pluginData["code"] = ""; while(!feof($this->fp)){ - $this->pluginData["code"] .= fread($this->fp, 4096); + $this->pluginData["code"] .= $this->read(4096); } $this->pluginData["code"] = gzinflate($this->pluginData["code"]); }