<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_magic_quotes_runtime(0);
define('l_path', substr(dirname(__FILE__), 0, -4));
define('l_ok',true);
if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}
function filter($string, $force = 0) {
	if(!$GLOBALS['magic_quotes_gpc'] || $force) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = filter($val, $force);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}
$magic_quotes_gpc = get_magic_quotes_gpc();
$register_globals = @ini_get('register_globals');
if(!$register_globals || !$magic_quotes_gpc) {
	@extract(filter($_POST));
	@extract(filter($_GET));
	if(!$magic_quotes_gpc) {
		$_FILES = filter($_FILES);
	}
}
//创建目录
function mkpaths($paths){
	if($paths) {
		$listpath = explode('/',$paths);
		$mkpath='';
		for($i=1;$i<=count($listpath);$i++) {
			$mkpath = $mkpath.'/'.$listpath[$i];
			if(!is_dir(l_path.'.'.$mkpath)){
				if(!@mkdir(l_path.'.'.$mkpath,0777)) {
						exit('&#x6307;&#x5b9a;&#x76ee;&#x5f55;&#x4e0d;&#x5b58;&#x5728;&#x6216;&#x4e0d;&#x5141;&#x8bb8;&#x64cd;&#x4f5c;&#x2c;&#x8bf7;&#x68c0;&#x67e5;&#x76ee;&#x5f55;&#x6743;&#x9650;&#x662f;&#x5426;&#x4e3a;&#x20; 0777');
				}
			}
		}
	}
}
//判断数字
function ispage($number) {
	return !empty($number) && preg_match ("/^([0-9]+)$/", $number);
}
//写入文件
function writetofile($file_name,$data,$method='w') {
	if($filenum=@fopen($file_name,$method)){
		flock($filenum,LOCK_EX);
		$file_data=fwrite($filenum,$data);
		fclose($filenum);
		//require_once $file_name;
		return $file_data;
	}else{
		exit('Can not fopen please check the file or PHP.INI');
	}
}
//读取文件
function readfromfile($file){
	if(!@$fp = fopen($file, 'r')) {
		return false;
	} else {
		$str = @fread($fp, filesize($file));
		fclose($fp);
		return $str;
	}
}

function set_gzip($content)
{    
    if(extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))
    {
     $content = gzencode($content,9);
    }
    return $content;
}
function gzdecode($data)
{
    $flags = ord(substr($data, 3, 1));
    $headerlen = 10;
    $extralen = 0;
    $filenamelen = 0;
    if ($flags & 4) 
    {
        $extralen = unpack('v' ,substr($data, 10, 2));
        $extralen = $extralen[1];
        $headerlen += 2 + $extralen;
    }
    if ($flags & 8)
    $headerlen = strpos($data, chr(0), $headerlen) + 1;
    if ($flags & 16)
    $headerlen = strpos($data, chr(0), $headerlen) + 1;
    if ($flags & 2)
    $headerlen += 2;
    $unpacked = @gzinflate(substr($data, $headerlen));
    if ($unpacked === FALSE)
    $unpacked = $data;
    return $unpacked;
}

// 重新载入页面
function cw_404(){
global $webwzurl;
echo "<script>\n";
echo "window.location=\"".$webwzurl."\";\n";
echo "</script>";
exit();
}
// 重新载入页面
function js_reload(){
	echo "<script type='text/javascript'>parent.document.location.reload();;</script>";
}
//自动检测内容是编码进行转换 get_encoding($data,"GB2312");
function get_encoding($data,$to)
{
$encode_arr = array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP');
$encoded = mb_detect_encoding($data, $encode_arr);
$data = mb_convert_encoding($data,$to,$encoded);
return $data;
}
//验证文件修改时间
function gethchtml($filename,$evryhour=0){
	if(file_exists($filename)){
		if($evryhour==0){
    return "ok";
		}elseif((time()-filemtime($filename))<($evryhour*60)){
    return "ok";
		}
  }
}
//格式化路径
function FixPath($aim, $baseUrl) 
{ 
        $strSource = $baseUrl; 
        if(preg_match_all('/([^:\\/]+)\//', $strSource, $aryResult1, PREG_PATTERN_ORDER)){ 
                /// baseUrl存在路径分隔符/，则获取其路径层次 
                $intUrlLevel= count($aryResult1[0]); 
        }else 
        { 
                /// baseUrl不存在路径分隔符/，则生成后面需要的数据 
                $intUrlLevel = 0; 
                preg_match_all('/http:\/\/(.+?)$/', $strSource, $aryResult1, PREG_PATTERN_ORDER); 
                $aryResult1[0][0] = $aryResult1[1][0] . "/"; 
        } 

        $strSource = $aim; 
		if(substr($strSource,0,7) == 'http://'){ 
             return $aim; 
        }else{
			if(preg_match_all("/^\//",$strSource, $aryResult2, PREG_PATTERN_ORDER)){ 
					/// aim以/开头，则为根目录路径 
					$strAimUrl = "http://" . $aryResult1[1][0] . str_replace("../","",$aim); 
			}else{ 
					/// 分析aim的../，即相对路径的层次 
					preg_match_all('/(\\.\\.\/)/', $strSource, $aryResult2, PREG_PATTERN_ORDER); 
					$intAimUrlLevel= count($aryResult2[0]); 
					/// 获取aim的相对路径的层次 
					if($intUrlLevel<=$intAimUrlLevel){ 
							/// baseUrl的路径层次小于aim的相对路径的层次，则从根目录开始 
							$strAimUrl = "http://" . $aryResult1[0][0] . str_replace("../","",$aim); 
					}else{ 
							/// baseUrl的路径层次大于aim的相对路径的层次，则生成大于部分的路径 
							$strAimUrl = "http://" . implode("",array_slice ($aryResult1[0], 0, $intUrlLevel - $intAimUrlLevel)) . str_replace("../","",$aim); 
					} 
			} 
			return $strAimUrl; 
		}
}
function geshuaurl($body, $url) {
preg_match_all("/(href|HREF)=[\"|'|]((.*)\.(html|htm|shtml|asp|php|jsp))/isU",$body,$urls_array);
if(count($urls_array[2])) {
$curls = array_unique($urls_array[2]);
foreach($curls as $key => $urlid ){
$nurlid=FixPath($urlid,$url);
$body=str_replace($urlid,$nurlid,$body); 
}
}
return $body;
}
//采集目标地址/进行编码转换
function get_url_content($url) {
global $charset;
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5; 
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
$file_contents = get_encoding($file_contents, $charset );
return $file_contents;
}
//截取
function cut($string,$start,$end)
{
	$message = explode($start,$string);
	$message = explode($end,$message[1]); 
	return trim($message[0]);
}
function cutdel($string,$start,$end)
{
	$message = explode($start,$string);
	$message = explode($end,$message[1]); 
	return $message[0];
}
//分页
function showpage($add,$patt,$page,$allpage){
//共有'.$allpage.'页
$re1 = '<div class="pages">';
$re="";
for ($i = $page-5;$i <= $page+5 && $i <= $allpage; $i++)
{
if ($i > 0)
{
if ($i == $page)
{
$re .= " <strong>$i</strong> ";
} else 
{
$re .= " <a href=\"".$add.$patt."/".$i."/\">$i</a> ";
}
}
}
$ss=1;
if ($page > 6) $re = "<a href=\"".$add.$patt."/".$ss."/\" title=\"首页\">首页</a> ...".$re;
if ($page + 5 < $allpage) $re .= "... <a href=\"".$add.$patt."/".$allpage."/\" title=\"尾页\">尾页</a>";
$re = $re1.$re."</div>";
if ($allpage <= 1) $re = '';
return $re;
}
//数字除法
function get_szi($str,$c=""){
if($c){
$urlidno = strstr($str, ",");
if($urlidno){
$str=str_replace(",","/",$str);
} else {
$ks = explode("-",$str);
$str=($ks[0]*8888);
if($ks[1]) $str=$str+$ks[1];
}
} else {
$urlidno = strstr($str, "/");
if($urlidno){
$str=str_replace("/",",",$str);
} else {
$str=floor($str/8888)."-".$str%8888;
}
}
return $str;
}
//base64加密解密
function bbb($str,$de=0) {
	if ($de) {
		return base64_decode($str);
	}else{
		return base64_encode($str);
	}
}

//读取数组
function get_data($dataname) {
	include l_path.'./db/'.$dataname.'.php';
	return $weblistdb;
}
//保存数组
function set_data($dataname,$dataarray) {
$datafile=l_path.'./db/'.$dataname.'.php';
$datacontent="<?php\r\n".'$weblistdb='.var_export($dataarray,true).';'."\r\n?>";
return writetofile($datafile,$datacontent);
}
//删除数组
function del_data($dataname,$dataarray,$delid=0) {
array_splice($dataarray,intval($delid),1);
if (set_data($dataname,$dataarray)) {
return 'ok';
}else{
return 'error';
}
}

//图片地址处理
function Getpics($body)
{
global $weburl;
  //$body = strtolower($body);
	$img_array = array();
	preg_match_all("/(src|SRC)=[\"|'| ]{0,}((http|HTTP):\/\/(.*)\.(gif|jpg|GIF|JPG|png))/isU",$body,$img_array);
	$img_array = array_unique($img_array[2]);
	foreach($img_array as $key=>$value)
	{
		$url = $value;
		$fileurl = "/pic.php?s=".base64_encode($url);
		$body = str_replace($url,$fileurl,$body);
	  }
	return $body;
}

//搜索热门关键字
function get_keys($n=8){
global $weburl,$sourl;
$keyss = file(l_path."./incs/key.txt");
if(count($keyss)<$n) $n=count($keyss);
$r_keys=array_rand($keyss,$n);
for($i=0;$i<$n;$i++) {
//$ii=($i+1);urlencode(
$keys.="<span><a href='/".$sourl.bbb(bbb(trim($keyss[$r_keys[$i]])))."/'>".trim($keyss[$r_keys[$i]])."</a></span>";
}
return $keys;
}
function repinfozz( $text )
{
    $text = str_replace( "*", "(.*?)", $text );
    $text = str_replace( "/", "\\/", $text );
    $text = "/".$text."/is";
    return $text;
}
function rephtml($content,$htmltrim)
 {
		$htmlarr=explode(',',$htmltrim);
		foreach ($htmlarr as $curhtml) {
			if (in_array($curhtml,array('br','img'))) {
				$searcharray[]='/<'.$curhtml.'([^>]*?)>/is';
				$replacearray[]='';
			} else if (in_array($curhtml,array('script','style','iframe'))) {
				$searcharray[]='/<'.$curhtml.'([^>]*?)>([\s\S]*)<\/'.$curhtml.'>/is';
				$replacearray[]='';
			} else {
				$searcharray[]='/<'.$curhtml.'([^>]*?)>(.*?)<\/'.$curhtml.'>/is';
				$replacearray[]='$2';
			}
		}
    return preg_replace($searcharray,$replacearray,$content);
	}
//伪原创
function keyshtml($content){
require l_path.'./incs/keyword.php';
return strtr($content,$keyword_arr);
unset($keyword_arr);
}
//获取文件后缀名
function hdhzm($file)
{
$extend =explode("." , $file);
$va=count($extend)-1;
return $extend[$va];
}  
require_once l_path.'./config.inc.php';
?>