init($file, $mode, $pointer, $override); } function __destruct(){ if(is_resource($this->handle)) fclose($this->handle); } public function init($file, $mode = "r", $pointer = null, $override = false){ if(!is_file($file)){ $this->state = false; return false; } $lockMode = LOCK_SH; if($mode == "r"){ $modeParam = "r"; } else if($mode == "w"){ if(!$pointer) $pointer = END; if($override) $modeParam = "w"; else{ if($pointer == HEAD){ $modeParam = "c"; } else if($pointer == END){ $modeParam = "a"; } } } else if($mode == "rw"){ if(!$pointer) $pointer = HEAD; if($override) $modeParam = "w+"; else{ if($pointer == HEAD){ $modeParam = "r+"; } else if($pointer == END){ $modeParam = "a+"; } } } else{ $this->state = false; return false; } if($modeParam != "r") $lockMode = LOCK_EX; $this->handle = fopen($file, $modeParam); if(!$this->lock($lockMode)){ $this->state = false; return false; } $this->file = $file; $this->fileSize = filesize($this->file); } public function getContent($length = -1, $position = null){ if(!$this->state) return null; if($position === null) $position = $this->position; if($position < 0) return null; if($length === -1) $length = $this->fileSize; $this->seek($position); if($length <= $this->segLength || ($this->fileSize - $position) < $this->segLength){ return fread($this->handle, $length); } $data = null; $round = ceil($length / $this->segLength); for($i = 0; $i < $round; $i++){ $data .= fread($this->handle, $this->segLength); if($position == 0){ $this->seek(($position += $this->segLength + 1)); } else{ $this->seek(($position += $this->segLength)); } } return $data; } public function seek($position = 0){ return fseek($this->handle, $position); } public function write($data, $length = -1){ if(!$this->state || !$data) return false; if($length === -1) return fwrite($this->handle, $data); if($length > 0) return fwrite($this->handle, $data, $length); return false; } public function lock($mode = LOCK_EX){ return flock($this->handle, $mode); } public function unlock(){ return flock($this->handle, LOCK_UN); } public static function create($file){ return fopen($file, "w") !== false ? true : false; } public static function save($file, $content, $length = -1){ $fileObj = new self($file, "w", null, true); return $fileObj->write($content, $length); } //make new dirs, will create all unexist dirs on the path public static function mkdir($path, $chmod = 0755){ return is_dir($path) || (self::mkdir(dirname($path), $chmod) && mkdir($path, $chmod)); } public static function rm($target, $recursive = false){ if(file_exists($target)){ if($recursive){ if(is_dir($target)){ $handle = opendir($target); while($subTarget = readdir($handle)){ if($subTarget !== "." && $subTarget !== ".."){ $position = "{$target}/{$subTarget}"; if(is_dir($position)){ self::rm($position); } else{ unlink($position); } } } closedir($handle); if(rmdir($target)){ return true; } } } if(is_file($target)){ return unlink($target); } } return false; } public static function rmdir($dir){ return self::rm($dir, true); } //read the first and last 512byte data and convert to hex then check it whether have trojans signature code or not public static function checkHex($file){ $handle = fopen($file, "rb"); $fileSize = filesize($file); fseek($handle, 0); if($fileSize > 512){ $hexCode = bin2hex(fread($handle, 512)); fseek($handle, $fileSize - 512); $hexCode .= bin2hex(fread($handle, 512)); } else{ $hexCode = bin2hex(fread($handle, $fileSize)); } fclose($handle); /** * match <% ( ) %> * *