<?php
/*
===========================用户控制器
^^^^^^^^^^^^^^^^^^成员
------添加(on_insert)
------修改(on_update)
------删除(on_del)

*/
class cls_ctl_file
{
	private $root_path;
	private $confg;
	function __construct($msg_config=array())
	{
		global $obj_tab_sys_config;
		$arr_cfg_other=$obj_tab_sys_config->get_model("other");
		$str_root="";
		if(isset($msg_config["root_path"]))
		{
			$str_root=$msg_config["root_path"];
		}else{
			if(isset($arr_cfg_other["path"])){
				$str_root=$arr_cfg_other["path"];
			}else{
				$str_root="/upload";
			}
		}
		$str_root=format_path($str_root);
		if(substr($str_root,0,1)!="/") $str_root="/".$str_root;
		$this->config=array();
		$this->config["root"]=$str_root;
		$this->root_path=get_real_path("",$str_root);
	}
	function get_config($key){
		if(isset($this->config[$key])){
			return $this->config[$key];
		}else{
			return "";
		}
	}
	function get_real_path($msg_path="")
	{
		if(empty($msg_path)) return $this->root_path;
		$str_path=format_path($msg_path);
		if(strlen($str_path)>strlen($this->root_path))
		{
			$str_x=substr($str_path,0,strlen($this->root_path));
			if($str_x!=$this->root_path)
			{
				if(substr($str_path,0,1)!="/") $str_path="/".$str_path;
				$str_path=$this->root_path.$str_path;
			}
		}else{
			if(substr($str_path,0,1)!="/") $str_path="/".$str_path;
			$str_path=$this->root_path.$str_path;
		}
		return $str_path;
	}
	function on_dir_create($msg_path, $msg_mode = 0777)
	{
		$return_val="";
		$str_path=self::get_real_path($msg_path);
		$return_val=dir_create($str_path,$msg_mode);
		return $return_val;
	}

	function on_dir_copy($msg_fromdir,$msg_name,$msg_todir)
	{
		$return_val="";
		$str_fromdir = self::get_real_path($msg_fromdir);
		$str_todir = self::get_real_path($msg_todir);
		$return_val=dir_copy($str_fromdir,$msg_name,$str_todir);
		return $return_val;
	}
	function on_file_copy($msg_frompath,$msg_name,$msg_todir,$msg_isover=0)
	{
		$return_val="";
		$str_topath="";
		$str_frompath = self::get_real_path($msg_frompath);
		$str_todir = self::get_real_path($msg_todir);

		$return_val=file_copy($str_frompath,$msg_name,$str_todir,$msg_isover);
		return $return_val;
	}
	function get_dir_list($msg_path, $msg_exts = '', $msg_list= array())
	{
		$arr_list=array();
		$str_path = self::get_real_path($msg_path);
		$arr_list = get_dir_list($str_path,$msg_exts,$msg_list);
		return $arr_list;
	}
	function get_dirs($msg_path="")
	{
		$arr_items=array();
		$str_path=self::get_real_path($msg_path);
		$arr_items=get_dirs($str_path);
		return $arr_items;
	}
	function on_dir_rename($msg_oldname,$msg_newname)
	{
		$return_msg="";
		$str_oldname=self::get_real_path($msg_oldname);
		$str_newname=self::get_real_path($msg_newname);
		$return_msg=dir_rename($str_oldname,$str_newname);
		return $return_msg;
	}
	function on_file_rename($msg_oldname,$msg_newname)
	{
		$return_msg="";
		$str_oldname=self::get_real_path($msg_oldname);
		$str_newname=self::get_real_path($msg_newname);
		$return_msg=file_rename($str_oldname,$str_newname);
		return $return_msg;
	}
	function on_file_create($msg_path,$msg_content)
	{
		$return_msg="";
		$str_path=self::get_real_path($msg_path);
		$msg_content=format_request_base($msg_content,true);
		$msg_content=self::format_content($msg_content);
		$return_msg=file_create($str_path,$msg_content);
		return $return_msg;
	}
	function format_content($msg_content){
		$arr_replace1=array("<\?"=>"&lt;?","\?>"=>"?&gt;");
		foreach($arr_replace1 as $item=>$key){
			$msg_content=preg_replace("/".$item."/is", $key, $msg_content);
		}
		$arr_replace2=array("eval"=>"//","file_put_contents"=>"//","file_get_contents"=>"//","\$obj_db"=>"//","\scls_sql"=>"//","include_once"=>"//","require"=>"//","require_once"=>"//");
		foreach($arr_replace2 as $item=>$key){
			$msg_content=preg_replace("/({.*?)(".$item.")((\(|\s|;|\"\').*?})/is", "\\1".$key."\\3", $msg_content);
		}
		$msg_content=preg_replace("/({)(include\(.*?)(http:)(.*?\)})/is", "\\1//\\2\\4", $msg_content);
		return $msg_content;
	}
	function get_files($msg_path="")
	{
		$str_path=self::get_real_path($msg_path);
		$arr_items=array();
		$arr_items=get_files($str_path);
		return $arr_items;
	}
	function get_file_perms($msg_path)
	{
		$str_path=self::get_real_path($msg_path);
		$arr_file=array();
		$arr_file=get_file_perms;
		return $arr_file;
	}
    function on_dir_delete($msg_dir)
	{
		$str_dir = self::get_real_path($msg_dir);
		$return_val=dir_delete($str_dir);
		return $return_val;
	}
	function on_file_delete($msg_file)
	{
		$str_file = self::get_real_path($msg_file);
		$return_val=file_delete($str_file);
		return $return_val;
	}
	function get_file_content($msg_file){
		if(empty($msg_file)) return "";
		$str_path = self::get_real_path($msg_file);
		if(!is_file($str_path)) return "";
		$str_content=file_get_contents($str_path);
		return $str_content;
	}
}
?>