load(); $this -> update(); } public function load(){ if(file_exists($this -> db)){ $data = file_get_contents($this -> db); if(!$this -> commands = unserialize($data)) $this -> commands = array(); unset($data); } else $this -> commands = array(); } private function sortCMP($a, $b){ if($a["level"] == $b["level"]) return (strcmp($a["trigger"], $b["trigger"]) > 0) ? 1 : -1; return ($a["level"] < $b["level"]) ? -1 : 1; } public function sort(){ //create temporary array foreach($this -> commands as $trigger => $arr){ $temp[$trigger]["array"] = $arr; $temp[$trigger]["level"] = $arr["level"]; $temp[$trigger]["trigger"] = $trigger; } //sort that temporary array by permission level and alphabetical^^ usort($temp, array($this, "sortCMP")); //store the sorted data again unset($this -> commands); foreach($temp as $cmd){ $this -> commands[$cmd["trigger"]]["filename"] = $cmd["array"]["filename"]; $this -> commands[$cmd["trigger"]]["context"] = $cmd["array"]["context"]; $this -> commands[$cmd["trigger"]]["level"] = $cmd["array"]["level"]; } unset($temp); } public function getList($level){ foreach($this -> commands as $command => $arr){ //Query commands: * Prefix commands: (prefix) Global commands: nothing $char = ($arr["context"] == "query" && $arr["context"] != "global") ? "*" : "#"; if($arr["level"] <= $level && $arr["context"] != "channel"){ $c[] = $command.$char."(".$arr["level"].")"; } } return implode(", ", $c); } public function update(){ //delete non-existing commands foreach($this -> commands as $command => $arr){ if(!file_exists($this -> getPath().$arr["filename"])) $this -> delCode($command); } //register the new commands $files = scandir($this -> codedir); foreach($files as $file){ if(preg_match("/.php$/", $file)) $this -> addCode($file); } $this -> sort(); } public function save(){ file_put_contents($this -> db, serialize($this -> commands)); } public function __destruct(){ $this -> save(); } /* * returns code to be evaluated and stuff */ public function getCode($command, $context, $level){ if(isset($this -> commands[$command]) && file_exists($this -> getPath().$this -> commands[$command]["filename"])){ if(($this -> commands[$command]["context"] == $context || $this -> commands[$command]["context"] == "global") && $this -> commands[$command]["level"] <= $level){ $file = $this -> getPath().$this -> commands[$command]["filename"]; $replace = array(""); $code = str_replace($replace, "", file_get_contents($file)); //access granted; returning code to eval() return str_replace("#BOT_PATH", $this -> getPath(), $code); } elseif($this -> commands[$command]["context"] != $context && $this -> commands[$command]["context"] != "global"){ //wrong context! return 3; } else{ //Permission denied return 2; } } else{ //Command does not exist yet return 0; } } public function addCode($filename){ $file = $this -> getPath().$filename; if(file_exists($file)){ $content = file($file); if(preg_match("/#BOT_INCLUDE/", $content[1])){ $data = explode(" ", str_replace("\"", "", $content[1])); unset($content); $command = trim($data[1]); $level = trim($data[3]); if(!is_numeric($level)) $level = 0; $this -> commands[$command]["filename"] = $filename; $this -> commands[$command]["context"] = trim($data[2]); $this -> commands[$command]["level"] = $level; return true; } else{ return false; } } else{ return false; } } public function delCode($command){ unset($this -> commands[$command]); } public function getLevel($command){ return $this -> commands[$command]["level"]; } public function getContext($command){ return $this -> commands[$command]["context"]; } private function getPath(){ return $this -> codedir.$this -> seperator; } } ?>