<?php
function stripslashes_array($array) {
         $array = is_array($array) ? array_map('stripslashes_array',$array) : stripslashes($array);
          return $array;
	}

function addslashes_array($array){
         $array = is_array($array) ? array_map('addslashes_array',$array) : addslashes($array);
         return $array;
       }

//支持mb_substr时使用此函数截取字串
function my_mb_substr($string,$start=0,$len=8) {
	return mb_strlen($string)>$len ? mb_substr($string,$start,$len)."..." : $string;
}

function xml_escape($string) {
	return str_replace(array('&','"',"'",'<','>'),
		array('&amp;','&quot;','&apos;','&lt;','&gt;'),
		$string	
	);
}

//不支持mb_substr函数时使用，支持UTF-8
function utf8_substr($str,$start,$end) {
preg_match_all("/[\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]/", $str, $ar);

    if(func_num_args() >= 3) {
        $end = func_get_arg(2);
        return join("",array_slice($ar[0],$start,$end))."...";
    } else {
        return join("",array_slice($ar[0],$start))."...";
    }
}

//is_true_email 直接验证邮件是否真实有效(LINUX FREEBSD有效)
function server_parse($socket, $response, $line = __LINE__){
    while (substr($server_response, 3, 1) != ' '){
        if (!($server_response = fgets($socket, 512))){
            return false;
        }
    }
    if (!(substr($server_response, 0, 3) == $response)){
        return false;
    }
    return true;
}

function is_true_email($strMail){
    $arrMail=explode("@",$strMail);
    if(! getmxrr ( $arrMail[1], $mxhosts ,$bb))return -1;
    if($mxhosts[0]=="")return -1;//不能获得主机的MX信息
    $cc=false;
    for($i=0;$i<count($mxhosts);$i++){
        echo $mxhosts[$i]." ";
        if( $socket = @fsockopen($mxhosts[$i], 25, $errno, $errstr, 5) ){
            if(server_parse($socket, "220", __LINE__)){
                $cc=true;
                break;
            }
        }
    }
    if($cc==false)return -2;//不能打开主机
    fputs($socket, "HELO ".$arrMail[1]." ");
    if(!server_parse($socket, "250", __LINE__))return -3;

    srand((double)microtime()*1000000);
    $randval = substr(md5(uniqid(rand())),0,6);
    fputs($socket, "MAIL FROM:<$randval@dayanmei.com> ");
    //使用时请将testemail.com改成你网站的域名
    if(!server_parse($socket, "250", __LINE__))return -4;

    fputs($socket, "RCPT TO:<$strMail> ");
    if(!server_parse($socket, "250", __LINE__))return -5;//无效的用户名

    fclose($socket);
    return true;
}

function is_email($str){
	return preg_match('/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/',$str) ? true : false;
}

// 高亮显示PHP
function phphighlight($code) {
	if (floor(phpversion())<4) {
		$buffer = $code;
	} else {
		$code = preg_replace("/<style .*?<\/style>/is", "", $code);
		$code = preg_replace("/<script .*?<\/script>/is", "", $code);
		$code = preg_replace("/<br\s*\/?>/i", "\n", $code);
		$code = preg_replace("/<\/?p>/i", "\n", $code);
		$code = preg_replace("/<\/?td>/i", "\n", $code);
		$code = preg_replace("/<\/?div>/i", "\n", $code);
		$code = preg_replace("/<\/?blockquote>/i", "\n", $code);
		$code = preg_replace("/<\/?li>/i", "\n", $code);
		$code = strip_tags($code);
		$code = preg_replace("/\&\#.*?\;/i", "", $code);
		$code = str_replace("&nbsp;&nbsp;&nbsp;", "&nbsp;&nbsp;", $code);
		$code = str_replace("&nbsp;&nbsp;", "&nbsp;", $code);
		$code = str_replace("&nbsp;", "\t", $code);
		$code = str_replace("&quot;", '"', $code);
		$code = str_replace("<br>", "", $code);
		$code = str_replace("<br />", "", $code);
		$code = str_replace("&gt;", ">", $code);
		$code = str_replace("&lt;", "<", $code);
		$code = str_replace("&amp;", "&", $code);
		//$code = str_replace('$', '\$', $code);
		if (!strpos($code,"<?\n") and substr($code,0,4)!="<?\n") {
			$code="<?".trim($code)."?>";
			$addedtags=1;
		}
		ob_start();
		$oldlevel=error_reporting(0);
		highlight_string($code);
		error_reporting($oldlevel);
		$buffer = ob_get_contents();
		ob_end_clean();
		if ($addedtags) {
		  $openingpos = strpos($buffer,"&lt;?");
		  $closingpos = strrpos($buffer, "?");
		  $buffer=substr($buffer, 0, $openingpos).substr($buffer, $openingpos+5, $closingpos-($openingpos+5)).substr($buffer, $closingpos+5);
		}
		$buffer = str_replace("&quot;", "\"", $buffer);
	}
	return $buffer;
}

function writedata($file,$data,$mode){
	$fp=@fopen($file,"$mode") or die("建立文件{$file}失败");
	fwrite($fp,$data);
	fclose($fp);
}

//自定义md5加密函数
function php_md5($string) {
	$string_md5 = md5($string);
	//转换小写为大写
	$string_md5 = strtoupper($string_md5);
	//倒转顺序
	$string_md5 = strrev($string_md5);
	//再md5加密一次
	return md5($string_md5);
}

function array2file($file,$array){
	$fp = fopen($file, 'wb');
	fwrite($fp, serialize($array));
	fclose($fp);
	}

function file2array($file){
	if(!file_exists($file)){
		exitstr("{$file} does no exist");
	}
	$handle=fopen($file,"rb");
	$contents=fread($handle,filesize($file));
	fclose($handle);
	return unserialize($contents);
	}

//输出并停止,用于测试
function ExitStr($str){
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
	echo "<pre>";
	print_r($str);
	echo "</pre>";
	exit;
	}

//页面跳转
function ReferTo($url,$msg,$sec){
         echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
         echo "<meta http-equiv=refresh content=$sec;URL=$url>";
  	if(is_array($msg)){
                 foreach($msg as $key=>$value){
                         echo $key."=>".$value."<br>";
                         }
                 }else{
                 echo $msg;
                 }
         exit;
         }
?>