123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- /**
- * The Feeler framework, licensed under LGPL license
- * Author: Rick Guo
- */
- defined("HEAD") or define("HEAD", 1);
- defined("END") or define("END", 2);
- class File{
- public $segLength = 524288; //to read and write slice in segments, this set every segment's length
- public $state = true;
- public $position = 0;
-
- protected $handle;
- protected $fileSize;
- protected $file;
- function __construct($file, $mode = "r", $pointer = null, $override = false){
- $this->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 <% ( ) %>
- * <? ( ) ?>
- * <script /script>
- */
- if(preg_match("/(3c25.*?28.*?29.*?253e)|(3c3f.*?28.*?29.*?3f3e)|(3C534352495054.*?2F5343524950543E)|(3C736372697074.*?2F7363726970743E)/is", $hexCode))
- return false;
- else
- return true;
- }
-
- //get the extension of the file
- public static function getExt($fileName){
- if(is_file($fileName)){
- $fileInfo = pathinfo($fileName);
- return isset($fileInfo["extension"]) ? $fileInfo["extension"] : null;
- }
- else{
- return strtolower(substr(strrchr($fileName, "."), 1));
- }
- }
-
- public static function getName($fileName){
- if(preg_match("/^.*\/(.*)(?:\..*)$/", $fileName, $matches)){
- return $matches[1];
- }
-
- return null;
- }
-
- public static function getFullName($file){
- if(preg_match("/^.*\/(.*\..*)$/", $file, $matches)){
- return $matches[1];
- }
-
- return null;
- }
-
- public static function getPath($file){
- if(preg_match("/^(.*\/)(.*\..*)$/", $file, $matches)){
- return $matches[1];
- }
-
- return null;
- }
-
- //get file size
- public static function getSize($file){
- return is_file($file) ? filesize($file) : null;
- }
-
- public static function getTypeList(){
- return array(
- "255216" => "jpg",
- "13780" => "png",
- "7173" => "gif",
- "6677" => "bmp",
- "6063" => "xml",
- "60104" => "html",
- "208207" => array("xls", "doc"),
- "8075" => array("zip", "docx", "xlsx"),
- "8297" => "rar",
- );
- }
-
- //get file's type according to start of 2bytes binary data
- public static function getType($file){
- if(!is_file($file)){
- return null;
- }
-
- $handle = fopen($file, "rb");
- if(!$handle){
- return null;
- }
- $bin = fread($handle, 2);
- fclose($handle);
-
- if($bin){
- $strs = unpack("C2str", $bin);
-
- $typeCode = $strs["str1"].$strs["str2"];
- $types = self::getTypeList();
-
- foreach($types as $key => $type){
- if((string)$key === $typeCode){
- return $type;
- }
- }
- }
-
- return null;
- }
- }
|