<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	系统基础核心函数库
*
*	$Id: library.php,v2.0 14:01 2009-03-31 Leilei $
*/

//大端(Big Endian)与小端(Little Endian)检测
function tester_endian(){
	
	/*
	list ($test) = array_values (unpack ('L1L', pack ('V',1)));
	
	if ($test != 1){
		return true;	//大端	UCS-2
	}else{
		return false;	//小端	UCS-2BE
	}
	*/
	
	//判断是big-endian还是little-endian
	$foo = 0x3456789a;
	switch (pack ('L', $foo)) {
		case pack ('V', $foo):
			//little-endian
			return "little";
		break;
		
		case pack ('V', $foo):
			//big-endian
			//Nothing
			return "big";
		break;
		
		default:
	}
	
}

function long2ip_fix($ip_32) {
	$ip = long2ip($ip_32);
	
	//先判断是big-endian还是little-endian
	$foo = 0x3456789a;
	switch (pack ('L', $foo)) {
		case pack ('V', $foo):             //little-endian
			$tmp = split("\.",$ip);
			$ip = $tmp[3].".".$tmp[2].".".$tmp[1].".".$tmp[0];
		break;
		case pack ('V', $foo):
			//big-endian
			//Nothing
		break;
		default:
	}
	return $ip;
} 


//随机颜色
function rand_color(){
	$string="0123456789ABCDEF";
	$rand='';
	for($i=0;$i<6;$i++){
		$rand .= substr($string,mt_rand(0,strlen($string)-1),1);
	} 
	$color="#".$rand;
	return $color;
}

//随机打乱数组并保持索引
function rand_array( $array ){
	
	//重建数组
	$temp = array();
	
	//数组长度
	$leng = count($array);
	
	//遍历数组
	for( $i=0; $i<$leng; $i++ ) {
		
		//随机索引
		$rand = array_rand($array);
		
		//重复索引
		if( isset($temp[$rand]) ){
			$i--;
			continue;
		}
		
		//新增到数组
		$temp[$rand] = $array[$rand];
		
		//删除原索引
		//array_splice($array, array_index($array,$rand) , 1);

	}
	
	return $temp;
}

function unescape($str) { 
	$str = rawurldecode($str); 
	preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r); 
	$ar = $r[0]; 
	foreach($ar as $k=>$v) { 
		  if(substr($v,0,2) == "%u") 
				   $ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4))); 
		  elseif(substr($v,0,3) == "&#x") 
				   $ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,3,-1))); 
		  elseif(substr($v,0,2) == "&#") { 
				   $ar[$k] = iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1))); 
		  } 
	} 
	return join("",$ar); 
} 


//合并路径
function readpath($path="") { 
	if ($path==""||!is_string($path)) return ; // ----0x0007---- 路径参数错误 
	
	$old = $path;
	$path = explode("/", $path); // 分割路径 
	$cur_path = array(""); 
	
	for ($i=0,$j=count($path);$i<$j;$i++) {
		
		if ($path[$i]=="..") array_pop($cur_path); 
		elseif ($path[$i]=="."||$path[$i]==str_repeat(".", strlen($path[$i]))) continue; // 忽略无用的相对路径地址 . 和 .... 等 
		else array_push($cur_path, $path[$i]); 
		
	} 
	$path = implode("/", $cur_path);
	
	//修正HTTP路径
	$path = str_replace('/http:/','http://',$path);
	$path = str_replace('/https:/','https://',$path);
	
	unset($cur_path);
	
	return $path.($old[strlen($old)-1]=="/"?"/":""); 
}

//取主机信息
function get_host(){
	return isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
}

//获取当前主机地址，不包含子目录
function GetHost(){
	if(isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on'){
		$host = 'https://';
	}else{
		$host = 'http://';
	}
	
	//部分环境下有错误，字域名是 * 号
	//$host .= $_SERVER['HTTP_HOST'];
	$host .= isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');  
	
	if($_SERVER['SERVER_PORT'] != 80){
		$host .= ':'.$_SERVER['SERVER_PORT'];
	}
	
	return $host."/";
}

//获取当前主域名
function GetDomain(){
	preg_match("/.*\.(.+?)\.(.+?)$/",$_SERVER["HTTP_HOST"],$matches);
	
	$domain = $matches[1].".".$matches[2];
	
	return $domain;
}

/*
	当前目录绝对地址
	如：http://localhost/veryide/module/
*/
function uripath($SYS=FALSE){
	if(isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on'){
		$host = 'https://';
	}else{
		$host = 'http://';
	}
	
	//部分环境下有错误，字域名是 * 号
	//$host .= $_SERVER['SERVER_NAME'];
	$host .= isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');  
	
	if($_SERVER['SERVER_PORT'] != 80){
		$host .= ':'.$_SERVER['SERVER_PORT'];
	}
	
	if( $SYS ){
		$host .= VI_BASE;
	}else{
		$host .= dirname($_SERVER['SCRIPT_NAME']).'/';		
	}	
	
	return $host;
}

//chr字符转html
function chrToHTML($Str){
	$Str = str_Replace(CHR(10), "<br />",$Str);
	$Str = str_Replace(CHR(32), " ",$Str);
	$Str = str_Replace(CHR(9), "　",$Str);
	$Str = str_Replace(CHR(34),"&quot;",$Str);
	$Str = str_Replace(CHR(39),"&#39;",$Str);
	$Str = str_Replace(CHR(13), "",$Str);
	$Str = str_Replace(CHR(10)&CHR(10), "<p>",$Str);
	
	return $Str;
}

/*
	格式化标签
	
	$tag	标签
	$url	链接地址
	$target	打开方式
	$space	间隔符
*/
function format_tag( $tag, $url, $target, $space ){

	$tag=str_replace(","," ",$tag);
	$tag_list=explode(" ",$tag);
	$tag_url="";
	for($i=0;$i<count($tag_list);$i++){
		$tag_url.='<a href="'.$url.urlencode($tag_list[$i]).'" target="'.$target.'">'.$tag_list[$i].'</a>';
		
		if($i<count($tag_list)){
			$tag_url.=$space;
		}
	}
	return $tag_url;
}

/*
	截取过长URL(只能用于文件显示)
	$url	URL
	$len	最大长度
*/
function format_url( $url, $len ){
	$str_len = strlen($url);
	if($str_len>$len){
		$str=substr($url,0,$len/2);
		$str.="...";
		$str.=substr($url,$str_len-$len/2,$len/2);
	}else{
		$str=$url;
	}
	return $str;
}

/*
	日期时间相加
	$interval	类型
	$number		数量
	$date		时间	
*/
function DateAdd ($interval, $number, $date) { 
	$date_time_array = getdate($date); 
	$hours = $date_time_array["hours"]; 
	$minutes = $date_time_array["minutes"]; 
	$seconds = $date_time_array["seconds"]; 
	$month = $date_time_array["mon"]; 
	$day = $date_time_array["mday"]; 
	$year = $date_time_array["year"]; 
	
	switch ($interval) {
		//年
		case "yyyy": $year =$number; break;
		
		//季度
		case "q": $month =($number*3); break;
		
		//月份
		case "m": $month =$number; break;
		
		case "y": 
		case "d": 
		case "w": $day =$number; break;
		
		//周
		case "ww": $day =($number*7); break;
		
		//小时
		case "h": $hours =$number; break;
		
		//分
		case "n": $minutes =$number; break;
		
		//秒
		case "s": $seconds =$number; break; 
	}
	
	$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year); 
	return $timestamp;
} 


//日期比较函数 
function FormatDateDiff($d1,$d2=""){
	
	if(!is_numeric($d1)) $d1=strtotime($d1); 
	if(!is_numeric($d2)) $d2=strtotime($d2);
	
	$s=$d2-$d1;

	if( floor($s / 31104000)>0 ){
	
		$str=floor($s / 31536000)." 年";
	}else{
		
		if( floor($s / 2592000)>0 ){
			$str=floor($s / 2592000)." 月";
		}else{
			if( floor($s / 86400)>0 ){
				$str=floor($s / 86400)." 天";
			}else{
				if( floor($s / 3600)>0 ){
					$str=floor($s / 3600)." 小时";
				}else{
					if( floor($s / 60)>0 ){
						$str=floor($s / 60)." 分钟";
					}else{
						$str=$s ." 秒";
					}
				}
			}
		}
	}
	 
	return $str; 
}

/*
	比较日期时间
*/
function DateDiff($interval,$date1,$date2){
	$timedifference=$date2-$date1;
	switch($interval){
		case   "w":$retval=ceil($timedifference/604800);break;
		case   "d":$retval=ceil($timedifference/86400);break;
		case   "h":$retval=ceil($timedifference/3600);break;
		case   "n":$retval=ceil($timedifference/60);break;
		case   "s":$retval=$timedifference;break;
	}
	return $retval;
}

/*
	获取请求
	$key		索引
*/
function GetQuery( $key ){
	$str = isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : "" );
	return $str;
}

/*
	获取数值
	$key		索引
	$default	默认值
*/
function GetNumber( $key, $default = 0 ){
	$num = GetQuery( $key );
	$num = is_numeric($num) ? intval($num) : $default;
	return $num;
}



/*
	获取页码
	$key		索引
*/
function GetPage( $key ){
	$num = GetNumber( $key, 1 );
	$num = $num < 1 ? 1 : $num;
	return $num;
}


/* 
Utf-8、gb2312都支持的汉字截取函数 
GetSubStr(字符串, 截取长度, 开始长度, 编码); 
编码默认为 utf-8 
开始长度默认为 0 
*/ 
function GetSubStr($string, $sublen, $start = 0, $code = 'UTF-8'){ 
    if( strtolower($code) == 'utf-8') 
    { 
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
        preg_match_all($pa, $string, $t_string); 
 
        if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; 
        return join('', array_slice($t_string[0], $start, $sublen)); 
    } 
    else 
    { 
        $start = $start*2; 
        $sublen = $sublen*2; 
        $strlen = strlen($string); 
        $tmpstr = ''; 
 
        for($i=0; $i< $strlen; $i++) 
        { 
            if($i>=$start && $i< ($start+$sublen)) 
            { 
                if(ord(substr($string, $i, 1))>129) 
                { 
                    $tmpstr.= substr($string, $i, 2); 
                } 
                else 
                { 
                    $tmpstr.= substr($string, $i, 1); 
                } 
            } 
            if(ord(substr($string, $i, 1))>129) $i++; 
        } 
        if(strlen($tmpstr)< $strlen ) $tmpstr.= "..."; 
        return $tmpstr; 
    } 
} 

//中文截取2，单字节截取模式
function gbk_substr($str,$slen,$startdd=0){
	$restr = "";
	$c = "";
	$str_len = strlen($str);
	if($str_len < $startdd+1) return "";
	if($str_len < $startdd + $slen || $slen==0) $slen = $str_len - $startdd;
	$enddd = $startdd + $slen - 1;
	for($i=0;$i<$str_len;$i++)
	{
		if($startdd==0) $restr .= $c;
		else if($i > $startdd) $restr .= $c;
		
		if(ord($str[$i])>0x80){
			if($str_len>$i+1) $c = $str[$i].$str[$i+1];
			$i++;
		}
		else{	$c = $str[$i]; }

		if($i >= $enddd){
			if(strlen($restr)+strlen($c)>$slen) break;
			else{ $restr .= $c; break; }
		}
	}
	if($restr != $str) $restr.="...";
	return $restr;
}
function cn_midstr($str,$start,$len){
	return gbk_substr($str,$slen,$startdd);
}

	
//截取字符串
function utf_substr($str,$len,$flag=true){
	if(!$str) return "";

	$src=$str;
	
	for($i=0;$i<$len;$i++){
		$temp_str=substr($str,0,1);
		if(ord($temp_str) > 127){
			$i++;
			if($i<$len)
			{
			$new_str[]=substr($str,0,3);
			$str=substr($str,3);
			}
		}else{
			$new_str[]=substr($str,0,1);
			$str=substr($str,1);
		}
	}
	
	$new_str=join($new_str);
	
	if( $new_str!==$src && $flag){
		$new_str.="...";
	}
	return $new_str;
}

//隐藏IP
function hide_ip($ip){
	$iparray = explode('.', $ip);
	return $iparray[0].".".$iparray[1].".*."."*";
}

//隐藏电话号码
function hide_phone($ip){

	if(strlen($ip)<6) return $ip;

	$str = substr($ip,0,strlen($ip)-4);
	$str = str_pad($str, strlen($ip) , "*");
	return $str;
}


//隐藏电子邮件
function hide_email($ip){

	if(strlen($ip)<6) return $ip;

	$at = strpos($ip,"@");
	if($at){
		$str = substr($ip,0,$at-4)."***";
		$str .= substr($ip,$at);
		
		return $str;
	}else{
		return $ip;
	}
}

//获取IP
function GetIP(){
	if(!empty($_SERVER["HTTP_CLIENT_IP"])) $cip = $_SERVER["HTTP_CLIENT_IP"];
	else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	else if(!empty($_SERVER["REMOTE_ADDR"])) $cip = $_SERVER["REMOTE_ADDR"];
	else $cip = "";
	return $cip;
}

//获得当前的脚本网址
function GetCurUrl(){
	if(!empty($_SERVER["REQUEST_URI"])){
		$scriptName = $_SERVER["REQUEST_URI"];
		$nowurl = $scriptName;
	}else{
		$scriptName = $_SERVER["PHP_SELF"];
		if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;
		else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
	}
	return $nowurl;
}

//获得当前的绝对网址
function GetAbsUrl(){
	return (isset($_SERVER['HTTP_SCHEME']) && $_SERVER['HTTP_SCHEME'] =='https'?'https':'http').'://'.$_SERVER['SERVER_NAME'].($_SERVER["SERVER_PORT"]!='80'?':'.$_SERVER["SERVER_PORT"]:'').$_SERVER["REQUEST_URI"];	
}

//获得当前的文件名
function GetCurFile(){
	$scriptName = split("/",$_SERVER["PHP_SELF"]);		
	return $scriptName[count($scriptName)-1];
}

//把全角数字转为半角数字
function GetAlabNum($fnum){
	$nums = array("０","１","２","３","４","５","６","７","８","９");
	$fnums = "0123456789";
	for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum);
	$fnum = ereg_replace("[^0-9\.]|^0{1,}","",$fnum);
	if($fnum=="") $fnum=0;
	return $fnum;
}

function Text2Html($txt){
	$txt = str_replace("  ","　",$txt);
	$txt = str_replace("<","&lt;",$txt);
	$txt = str_replace(">","&gt;",$txt);
	$txt = preg_replace("/[\r\n]{1,}/isU","<br/>\r\n",$txt);
	return $txt;
}

//清除HTML标记
function ClearHtml($str){
	$str = str_replace('<','&lt;',$str);
	$str = str_replace('>','&gt;',$str);
	return $str;
}

 //取得文件夹大小
function GetFolderSize( $d ) {
  $dir=dir($d); $size=0;
  while (false !== $e = $dir->read()) { if ($e[0] == '.') {continue;} $c_dir=$d.'/'.$e; if(is_dir($c_dir)) $size=$size+GetFolderSize($c_dir); else $size=$size+filesize($c_dir); } $dir->close(); return $size;
}

//返回换算后的文件大小
function GetFileSize__($size,$t){
	$str="";
	switch ($t){
		case "byte":
			$size=$size;
			break;
		case "kb":
			$size=$size*1024;
			break;
		case "mb":
			$size=$size*1024*1000;
		case "gb":
			$size=$size*1024*1024*1000;			
	}
		
	if($size/1024/1024/1024>1){
		$str=round($size/1024/1024/1024,2)." GB";
	}elseif($size/1024/1024>1){
		$str=round($size/1024/1024,2)." MB";
	}elseif($size/1024>1){
		$str=round($size/1024,2)." KB";
	}else{
		$str=$size." Byte";
	}
	return $str;
}

function GetFileSize( $fileSize ) {    
	 $size   =   sprintf ( " %u " ,   $fileSize );
	 $sizename   =   array ( "  Bytes " , "  KB " , "  MB " , "  GB " , "  TB " , "  PB " , "  EB " , "  ZB " , "  YB " );    
	 return   round ( $size / pow ( 1024 ,  ( $i   =   floor ( log ( $size ,   1024 )))) ,  2 )  .   $sizename [ $i ];
} 
//echo  setupSize( ' 200000 ' )


/*
	得到文件扩展名
	$file	文件名
*/
function fileext( $filename ) {
	return addslashes(trim(substr(strrchr($filename, '.'), 1, 10)));
}

/*
	测试字符串是否在另一字符串中出现
	$string		当前字符串
	$find		要搜索的字符串
*/
function strexists($string, $find) {
	return !(strpos($string, $find) === FALSE);
}

/*
	调试信息
*/
function debuginfo() {
	global $_G;
	if(getglobal('setting/debug')) {
		$db = & DB::object();
		$_G['debuginfo'] = array('time' => number_format((dmicrotime() - $_G['starttime']), 6), 'queries' => $db->querynum, 'memory' => ucwords($_G['memory']));
		return TRUE;
	} else {
		return FALSE;
	}
}

function sizecount($size) {
	if($size >= 1073741824) {
		$size = round($size / 1073741824 * 100) / 100 . ' GB';
	} elseif($size >= 1048576) {
		$size = round($size / 1048576 * 100) / 100 . ' MB';
	} elseif($size >= 1024) {
		$size = round($size / 1024 * 100) / 100 . ' KB';
	} else {
		$size = $size . ' Bytes';
	}
	return $size;
}

function site() {
	return $_SERVER['HTTP_HOST'];
}


/*
	根据年月返回当月最大天数
	$e_y	年
	$e_m	月
	$e_d	日
*/
function GetDay($e_y,$e_m,$e_d){
	if (($e_m==4 || $e_m==6 || $e_m==9 || $e_m==11) && $e_d>30){
		$e_d=30;
	}
	
	$e_mod=$e_y % 4;
	if ($e_mod>0 && $e_d>28 && $e_m==2){
		$e_d=28;
	}
	return $e_d;	
}

/*
	完整页码
	$page		当前页码
	$row_count	总记录数
	$page_size	页数
	$url		URL
	$style		样式名称
*/
function GetPageLink($page,$row_count,$page_size,$url,$style){	
	//总页数
	$page_count=ceil($row_count/$page_size);
	
	$page_link="<div class='".$style."'>";

	//第一页
	if($page<1){
		$page=1;
	}elseif ($page>$page_count){
		$page=$page_count;
	}
	
	//链接组
	$page_group=floor(($page-1)/$page_size);

	//开始和结束
	$start=$page_group*$page_size+1;
	if($row_count==0){
		$start=0;
	}
	$end=($page_group+1)*$page_size;
	if($end>$page_count){
		$end=$page_count;
	}
	
	if($page!=1 && $start>$page_size){
		$page_link.='<a href="'.$url.'1" title="第1页">1</a>';
		if($page_count>$start-1 && $start>1){
			$page_link.='<a href="'.$url.($start-1).'" title="上一组,第'.$i.'页">'.($start-1).'</a>';
		}
		$page_link.='...';
	}
	
	for($i=$start;$i<=$end;$i++){
		if($i==$page){
			$page_link.='<strong>'.$i.'</strong>';
		}else{
			$page_link.='<a href="'.$url.$i.'" title="第'.$i.'页">'.$i.'</a>';
		}		
	}
	
	if($page!=$page_count){
		$page_link.='...';
		if($page_count>=$end+1){
			$page_link.='<a href="'.$url.($end+1).'" title="下一组,第'.$i.'页">'.($end+1).'</a>';
		}
		$page_link.='<a href="'.$url.$page_count.'" title="最末页">'.$page_count.'</a>';
	}
	
	$page_link.="<span>页次:$page/".$page_count." ".$page_size."条/页,共".$row_count."条 </span></div>";
	
	return $page_link;
}

/*
	简单页码
	$num	
	$perpage	
	$curpage	当前页码
	$mpurl		URL
	$style		样式名称
*/
function simplepage($num, $perpage, $curpage, $mpurl) {
	$return = '';
	$lang['next'] = lang('core', 'nextpage');
	$lang['prev'] = lang('core', 'prevpage');
	$next = $num == $perpage ? '<a class="nxt" href="'.$mpurl.'&amp;page='.($curpage + 1).'">'.$lang['next'].'</a>' : '';
	$prev = $curpage > 1 ? '<span class="pgb"><a href="'.$mpurl.'&amp;page='.($curpage - 1).'">'.$lang['prev'].'</a></span>' : '';
	if($next || $prev) {
		$return = '<div class="pg">'.$prev.$next.'</div>';
	}
	return $return;
}


//CSS名称
function getcssname($cssdir) {
	global $Project;

	$css = sreadfile($cssdir);
	if($css) {
		preg_match("/\[name\](.+?)\[\/name\]/i", $css, $mathes);
		if(!empty($mathes[1])){
			$name = $mathes[1];
		
			require_once(dirname(__FILE__)."/verify.php");
			
			if(is_gb2312($name) && $Project["charset"]=="utf-8"){
				$name = iconv("gbk" ,$Project["charset"], $name);
			}
			
			if(is_utf8($name) && $Project["charset"]=="gbk"){
				$name = iconv("utf-8" ,$Project["charset"], $name);
			}
		}
		
	} else {
		$name = 'No name';
	}
	return $name;
}

//获取文件内容
function sreadfile($filename) {
	$content = '';
	if(function_exists('file_get_contents')) {
		@$content = file_get_contents($filename);
	} else {
		if(@$fp = fopen($filename, 'r')) {
			@$content = fread($fp, filesize($filename));
			@fclose($fp);
		}
	}
	return $content;
}

//创建文件
function create_file($file,$text){

    if (!$handle=fopen($file,"w")) {
        return false;
    }
	
	if(!fwrite($handle, $text)){
		fclose($handle);
		return false;
	}else{
		fclose($handle);
		return true;
	}

}

//创建文件夹
function create_dir($file){

	//如果不存在
	if ( !file_exists($file) ){
		
		//创建目录
		create_dir(dirname($file));
		
		//更改权限
		mkdir($file,0777);		chmod($file,0777);
		
	}		
	
	return $file;
}


//删除文件夹
function delete_dir($dir){
	$path=$dir;
	if(substr($dir,-1) != '/') $dir .= '/';
	if(is_dir($dir)) {
		if ($dp = opendir($dir)) {
			while (($file=readdir($dp)) !== false) {
				if (is_dir($dir.$file) && $file!='.' && $file!='..') {
					delete_dir($dir.$file);
				}else {
				if (!is_dir($dir.$file)) {
					if(!@unlink($dir.$file)){
						return false;
					}
				}
				}
			}
			closedir($dp);
			
			if(!@rmdir($dir)){
				return false;
			}
		}
	}
	return true;
}

//获取指定长度随机字符
function rand_string($length){
	$hash = "";
	$chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789abcdefghijklmnpqrstuvwxyz";
	$max = strlen($chars) - 1;
	mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $length; $i++) {
		$hash .= $chars[mt_rand(0, $max)];
	}
	return $hash;
}

?>