<?php

//自动删除缓存

$fileNum=0;
function deldir($dir,$tnums)
{
	global $fileNum;
	$dh=opendir($dir);
	while ($file=readdir($dh))
	{
		if($file!="." && $file!="..")
		{
			$fullpath=$dir."/".$file;
			
			
			if(!is_dir($fullpath))
			{
				
				$isfile = is_file($fullpath);
				$jgtime = time()-filemtime($fullpath);
				if($isfile && $jgtime>$tnums)
				{
					unlink($fullpath);
					$fileNum=$fileNum+1;
				}
				
			}
			else
			{
				deldir($fullpath,$tnums);
			}
		}
	}
	closedir($dh);
}


function url_base64_encode($str)
{
	if($str=="")
		return "";
    $code=base64_encode($str);//$code='dHQ=';
    $code=str_replace('+',"!",$code);//把所用"+"替换成"!"
    $code=str_replace('/',",",$code);//把所用"/"替换成"*"
    $code=str_replace('=',"",$code);//把所用"="删除掉
    return $code;//$code='dHQ!'
}
function url_base64_decode($code)
{
	if($code=="")
		return "";
    $code=str_replace("!",'+',$code);//把所用"+"替换成"!"
    $code=str_replace(",",'/',$code);//把所用"/"替换成"*"
    $str=base64_decode($code);
    return $str;
}


function setPic($url,$width,$height,$title){
	global $WYCpictype;
	global $rootroad;
	global $weijingtai;
	global $wjtpagelist;
	global $wjtpagehz;
	global $wjtpagepicture;
	
	$title = htmlspecialchars($title,ENT_QUOTES);

	switch($WYCpictype){
		case "js":
			echo "<script language=\"javascript\">setPic('".base64_encode($url)."',".$width.",".$height.",'".$title."');</script>";
			break;
		case "local":
			if($weijingtai){
			echo "<img src='".$rootroad."/".$wjtpagepicture.base64_encode($url).".jpg' alt='".$title."' title='".$title."'";
			}else{
			echo "<img src='".$rootroad."/pic.php?url=".base64_encode($url)."' alt='".$title."' title='".$title."'";
			}
		
			if($width>0){ echo " width='".$width."' ";}
			if($height>0){ echo " height='".$height."' ";}
			echo (" />");
			break;
		break;
		default:
			echo "<script language=\"javascript\">setPic('".base64_encode($url)."',".$width.",".$height.",'".$title."');</script>";
			break;
		
	}
	
}

//伪静态脚本
function mod_rewrite(){
	global $_GET;
	global $wjtpagelist;
	global $wjtpageshop;
	if($_GET["rule"]!=""){
		mod_rewrite_rule($_GET["rule"]);	
	}
	
	//echo $_SERVER["SCRIPT_NAME"].$_SERVER["PATH_INFO"];
	
	if(isset($_SERVER["REQUEST_URI"])){
		$nav=$_SERVER["REQUEST_URI"];
	}else{
		if(isset($_SERVER["PATH_INFO"])){
			$nav = $_SERVER["PATH_INFO"];
		}
		if(isset($_SERVER["ORIG_PATH_INFO"])){
			$nav = $_SERVER["ORIG_PATH_INFO"];
		}
	}
	if(!isset($nav)){
		return;
	}
	if(strpos("---".$nav,"?")){
		return;
	}
	if(strpos("---".$nav,".php/")==0){
		return;
	}

	
	//$arr = explode($script_name,($nav));
	
	
	if(Count($arr)>1){
		$nav = $arr[1];
	}
	$arr = explode(".htm",$nav);
	$nav = $arr[0];
	
	
	
	if(substr($nav,0,1)=="/"){
		$nav = substr($nav,1);
	}else{
		return;
	}
	
	$vars = explode("/",$nav);

	$vars = $vars[count($vars)-1];
	
	
	mod_rewrite_rule($vars,true);
}

function mod_rewrite_rule($vars,$isold=false){
	global $_GET;
	global $wjtpagelist;
	global $wjtpageshop;
	$vars = explode("-",$vars);
	$script_name=$_SERVER["SCRIPT_NAME"];


	if(strpos($script_name,"product.php")>0){
		$_GET["iid"]=$vars[0];
		return $_GET;
	}
	if(strpos($script_name,"list.php")>0 && Count($vars)>0){
	
		if(isset($vars[0]) && $vars[0]!="") $_GET["catid"]=$vars[0];
		if(isset($vars[1]))	$_GET["sortnum"]=$vars[1];
		if(isset($vars[2])) $_GET["sp"]=$vars[2];
		if(isset($vars[3])) $_GET["ep"]=$vars[3];
		
		if(isset($vars[4])){
			$q = $vars[4];
			if($isold){
				$_GET["q"]=$q;
			}else{
				$_GET["q_jt"]=$q;
			}
		}
		if(isset($vars[5])) $_GET["page"]=$vars[5];
				
		return $_GET;
	}
	if(strpos($script_name,"shopsearch.php")>0 && Count($vars)>0){
	
		if(isset($vars[0]) && $vars[0]!="") $_GET["catid"]=$vars[0];
		if(isset($vars[1])){
			$q = $vars[1];
			if($isold){
				$_GET["q"]=$q;
			}else{
				$_GET["q_jt"]=$q;
			}
		}
		if(isset($vars[2])) $_GET["page"]=$vars[2];
				
		return $_GET;
	}
	if(strpos($script_name,"shop.php")>0 && Count($vars)>0){
	
		if(isset($vars[0]) && $vars[0]!=""){
			if($isold){
				$_GET["nick"]=$vars[0];
			}else{
				$_GET["nick_jt"]=$vars[0];
			}
		}
		if(isset($vars[1]))	$_GET["sortnum"]=$vars[1];
		if(isset($vars[2])) $_GET["sp"]=$vars[2];
		if(isset($vars[3])) $_GET["ep"]=$vars[3];
		
		if(isset($vars[4])) $_GET["page"]=$vars[4];
				
		return $_GET;
	}

	for($i=0;$i<Count($vars)-1;$i+=2){
		$_GET[$vars[$i]]=$vars[$i+1];
		global $page_whtml ;
		$page_whtml = true;
	}
	return $_GET;
	
}

function var_request($key, $default){
	$value = $default;
	if(isset($_GET[$key])) {
		$value = $_GET[$key];
	}
	if(isset($_POST[$key])) {
		$value = $_POST[$key];
	}
	if($value=="") $value=$default;
	return $value;
}
function redirect_to($url) {
  echo '<meta http-equiv="refresh" content="0; url='.$url.'">';
  exit;
}
function checkadmin(){
	global $manage_adminname;
	global $manage_adminpass;
	
	$adminuser = $_COOKIE['COOKIEadminuser'];
	if(empty($adminuser) || $adminuser == "") $adminuser = $_SESSION['adminuser'];
	
	$adminpass = $_COOKIE['COOKIEadminpass'];
	if(empty($adminpass) || $adminpass == "") $adminpass = $_SESSION['adminpass'];
	
	$adminpass = md5($adminpass);

	if($adminuser!=$manage_adminname || $adminpass != $manage_adminpass){
		echo("<script language='JavaScript'>top.location.href='index.php';</script>");
		exit;
	}
	return true;

}

function get_object_vars_final($obj){
	if(is_object($obj)){
		$obj=get_object_vars($obj);
	}
	if(is_array($obj)){
		foreach ($obj as $key=>$value){
			$obj[$key]=get_object_vars_final($value);
		}
	}
	return $obj;
}


//获得一组APP
function getoneapp(){
	global $ArrayAppKey;
	global $ArrayAppSecret;
	$appkey_arr = explode(",",$ArrayAppKey);
	$appsecret_arr = explode(",",$ArrayAppSecret);
	if(application("nowapp",ROOT."appnum.php")==""){
		setapplication("nowapp","0",ROOT."appnum.php",true);
	}
	$i = application("nowapp",ROOT."appnum.php");
	
	if($i>count($appkey_arr)-1){
		$i=count($appkey_arr)-1;
		setapplication("nowapp",$i,ROOT."appnum.php",true);
	}
	
	$arrayapp = array();
	$arrayapp[0] = $appkey_arr[$i];
	$arrayapp[1] = $appsecret_arr[$i];
	
	
	
	
	return $arrayapp;
}
//更换一组APP
function getoneappChange(){

	global $ArrayAppKey;
	global $ArrayAppSecret;
	$appkey_arr = explode(",",$ArrayAppKey);
	$appsecret_arr = explode(",",$ArrayAppSecret);
	

	
	if(application("nowapp",ROOT."appnum.php")==""){
		setapplication("nowapp","0",ROOT."appnum.php",true);
	}
	$i = application("nowapp",ROOT."appnum.php");
	
	if(Count($appkey_arr)>1){
		$i++;
		if($i==Count($appkey_arr)){
			$i=0;
		}
	}
	setapplication("nowapp",$i,ROOT."appnum.php",true);
	
	
	$arrayapp = array();
	$arrayapp[0] = $appkey_arr[$i];
	$arrayapp[1] = $appsecret_arr[$i];
				
	
	return $arrayapp;
}


function Newiconv($_input_charset,$_output_charset,$input ) {

              $output = "";

              if($_input_charset == $_output_charset || $input ==null) {

                     $output = $input;

              } elseif (function_exists("mb_convert_encoding")){

                     $output = mb_convert_encoding($input,$_output_charset,$_input_charset);

              } elseif(function_exists("iconv")) {

                     $output = iconv($_input_charset,$_output_charset,$input);

              } else die("对不起，你的服务器系统无法进行字符转码.请联系空间商。");

              return $output;

       }

		$str.="NvbT7M1L/Ntdu5+jwvYT4oaHR0cDovL2NubXlzb2Z0LmNvbSm9+ND";
		
		function setcnstr($str)
		{
			global $wjturlencode;
			
			if($wjturlencode=="base64"){
				return url_base64_encode($str);
			}
			if($wjturlencode=="2urlencode"){
				return urlencode(urlencode($str));
			}
			if($wjturlencode=="urlencode"){
				return urlencode($str);
			}
			
			return ($str);
		}
		function getcnstr($str)
		{
			global $wjturlencode;
			
			
			if($wjturlencode=="base64"){
				
				return url_base64_decode($str);
			}
			if($wjturlencode=="2urlencode"){
				return urldecode(urldecode($str));
			}
			if($wjturlencode=="urlencode"){
				return urldecode($str);
			}
			return urldecode($str);
			
		}
		
		$str.="QubrC8qOsyOe5+8T6srvQ6NKqytrIqKOsx+vPwtTYw+K30bDmyrnTw6GjPGJyPsn5w";
		function geturl(){
			$S = strtolower($_SERVER["SERVER_NAME"]);
			$domain = array('com','cn','name','org','net','hk','cc','info','tv','biz','mo.cn','movi'); //域名后缀 有新的就扩展这吧
			$SS = $S;
			$dd = implode('|',$domain);
			$SS = preg_replace('/(\.('.$dd.'))*\.('.$dd.')$/iU','',$SS); //把后面的域名后缀部分去掉
			$SS = explode('.',$SS);
			$SS = array_pop($SS);  //取最后的主域名
			$SS = substr($S,strrpos($S,$SS));  //加上后缀拼成完成的主域名
			return $SS;
		}

		$str.="/c6ubrC8s6o0ru52be9zfjWt2NubXlzb2Z0LmNvbaGjyOe5+8T6srvKx9Taudm3vbm6wvK78tXfzrTI";
		$yz_pageurl = strtolower($_SERVER["SERVER_NAME"]);
			
		function fyanzheng(){
			return true;
		}
		
		$str.="obXD0/LD+8rayKi7ubfH0qrKudPDt8fK2siosOYss/b";
		$str.="P1tK7x9DOysziLLrzufvX1Li6ITwvZGl2Pg==";
	
		$str = base64_decode($str);
		

//屏蔽词语功能。
$pingbikeywordnum = 0;
function setsplitkeyword($keyarrstr,$str,$newstr)
{
	global $pingbikeywordnum;
	//检查屏蔽词语
	$keylist = explode(",",$keyarrstr);
	$returnstr = $str;
	
	$arrkey = array();
	for($i=0;$i<count($keylist);$i++)
	{
		if(!empty($keylist[$i]))
			$arrkey[$keylist[$i]] = "*";
	}
	$newreturnstr = strtr($returnstr,$arrkey);
	
	$pingbikeywordnum = strlen($returnstr) - strlen($newreturnstr);
	

	if($pingbikeywordnum>3)
		exit("本页检测到多个违禁词语，被屏蔽");
 
	return $newreturnstr;
	
}

//屏蔽类别或者商品ID检查。
function checkpingbi($keyarrstr,$str)
{
	//检查屏蔽词语
	$keylist = explode(",",$keyarrstr);
	$returnstr = false;
	
	for($i=0;$i<count($keylist);$i++)
	{
		if($keylist[$i]!="" && $keylist[$i] == $str) {
				$returnstr = true;
		}
	}
	return $returnstr;
}

if(md5($yz_pageurl)!=application($yz_pageurl,ROOT."appnum.php")){
	if(!fyanzheng()){
		echo("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><DIV style='font-size:14px;height:40px;'><font color=red>您的尚未购买授权，所以页面显示不正常。请联系(QQ:619614661)进行购买，如果您不需要授权，请下载免费版使用。</div>");
	}
}



//生成伪静态规则文件
function writerulefile()
{
	$WJTconfig = application("WJTconfig",ROOT."applicationdate.php");
	if(isset($WJTconfig["pagelist"])){
		$pagelist=$WJTconfig["pagelist"];
		$pageproduct=$WJTconfig["pageproduct"];
		$urlencode=$WJTconfig["urlencode"];
		$pageshop=$WJTconfig["pageshop"];
		$pagehz=$WJTconfig["pagehz"];
	}else{
		$pagelist="list.php/";
		$pageproduct="product.php/";
		$urlencode="true";
		$pageshop="shop.php/";
		$pagehz=".html";
	}
	
		//生成apache伪静态规则文件
	$str = file_get_contents("../rule/rule_apache.php");
	$str = str_replace("[rule_pagelist]",$pagelist,$str);
	$str = str_replace("[rule_pageproduct]",$pageproduct,$str);
	$str = str_replace("[rule_pageshop]",$pageshop,$str);
	$str = str_replace("[rule_pagehz]",$pagehz,$str);
	file_put_contents("../.htaccess",$str);
	//生成apache伪静态规则文件
	$str = file_get_contents("../rule/rule_iis.php");
	$str = str_replace("[rule_pagelist]",$pagelist,$str);
	$str = str_replace("[rule_pageproduct]",$pageproduct,$str);
	$str = str_replace("[rule_pageshop]",$pageshop,$str);
	$str = str_replace("[rule_pagehz]",$pagehz,$str);
	file_put_contents("../httpd.ini",$str);
	
	//生成apache伪静态规则文件
	$str = file_get_contents("../rule/rule_nginx.php");
	$str = str_replace("[rule_pagelist]",str_replace(".","\.",$pagelist),$str);
	$str = str_replace("[rule_pageproduct]",str_replace(".","\.",$pageproduct),$str);
	$str = str_replace("[rule_pageshop]",str_replace(".","\.",$pageshop),$str);
	$str = str_replace("[rule_pagehz]",str_replace(".","\.",$pagehz),$str);
	file_put_contents("../nginx.conf",$str);


	
	
}
function updatearraysort($arr,$i,$tp){
	if($i>0 && $tp=="up" && $i<count($arr)){
		$tmp = $arr[$i-1];
		$arr[$i-1] = $arr[$i];
		$arr[$i] = $tmp;
	}
	if($i<count($arr)-1 && $tp=="down" && $i>=0){
		$tmp = $arr[$i+1];
		$arr[$i+1] = $arr[$i];
		$arr[$i] = $tmp;
	}

	return $arr;
}


function getsearchurl($keyword,$cid=0,$isshop=0,$snum=-1,$smallprise=1,$maxprise=1000000,$havehouzhui=true){
	if($snum==-1){
		$snum = Show_list_sort;
	}
	if(intval($isshop)==1){
		return getsearchshopurl($keyword,$cid);
	}
	
	global $weijingtai;
	global $rootroad;
	global $wjtpagelist;
	global $wjtpagehz;
	global $wjturlencode;
	
	if($cid==""){
		$cid = 0;	
	}
	
	$keyword = str_replace("/"," ",$keyword);

	if($weijingtai){
		if(empty($keyword) && $snum==Show_list_sort && $smallprise==1 && $maxprise==1000000 && $havehouzhui){
			return $rootroad."/".$wjtpagelist.$cid.($havehouzhui?$wjtpagehz:"");
		}else{
			return $rootroad."/".$wjtpagelist.$cid."-".$snum."-".$smallprise."-".$maxprise."-".setcnstr($keyword).($havehouzhui?$wjtpagehz:"");
		}
	}else{
		if(empty($keyword) && $snum==0 && $smallprise==1 && $maxprise==1000000){
			return $rootroad."/list.php?catid=".$cid;
		}else{
			return $rootroad."/list.php?q=".urlencode($keyword)."&catid=".$cid."&sortnum=".$snum."&sp=".$smallprise."&ep=".$maxprise;
		}
	}

}
function getsearchshopurl($keyword,$cid=0,$havehouzhui=true){
	global $weijingtai;
	global $rootroad;
	global $wjtpageshopsearch;
	global $wjtpagehz;
	global $wjturlencode;
	
	if($cid==""){
		$cid = 0;	
	}
	
	$keyword = str_replace("/"," ",$keyword);

	if($weijingtai){
		if(empty($keyword)&&$havehouzhui){
			return $rootroad."/".$wjtpageshopsearch.$cid.($havehouzhui?$wjtpagehz:"");
		}else{
			return $rootroad."/".$wjtpageshopsearch.$cid."-".setcnstr($keyword).($havehouzhui?$wjtpagehz:"");
		}
	}else{
		if(empty($keyword)){
			return $rootroad."/shopsearch.php?catid=".$cid;
		}else{
			return $rootroad."/shopsearch.php?q=".urlencode($keyword)."&catid=".$cid;
		}
	}

}
function getproducturl($num_iid){
	global $weijingtai;
	global $rootroad;
	global $wjtpageproduct;
	global $wjtpagehz;

	if($weijingtai=="1"){
		$urlview =$rootroad."/".$wjtpageproduct.$num_iid.$wjtpagehz;
	}else{
		$urlview =$rootroad."/product.php?iid=".$num_iid;
	}
	return $urlview;
}
function getshopurl($nick,$sortnum=0,$sp=1,$ep=1000000,$havehouzhui=true){
	global $weijingtai;
	global $rootroad;
	global $wjtpageshop;
	global $wjtpagehz;

	if($weijingtai=="1"){
		if($sortnum==0&&$sp==1&&$ep==1000000&&$havehouzhui){
		$urlview =$rootroad."/".$wjtpageshop.setcnstr($nick).($havehouzhui?$wjtpagehz:"");
		}else{
		$urlview =$rootroad."/".$wjtpageshop.setcnstr($nick)."-".$sortnum."-".$sp."-".$ep.($havehouzhui?$wjtpagehz:"");
		}
	}else{
		$urlview =$rootroad."/shop.php?nick=".urlencode($nick)."&sortnum=".$sortnum."&sp=".$sp."&ep=".$ep;
	}
	return $urlview;

}
function ischengrencid($str){
	return false;
}



function proSortNumToSort($sortnum)
{
	$sort = "commissionNum_desc";
	if($sortnum=="0")$sort="commissionNum_desc";
	if($sortnum=="1")$sort="credit_desc";
	if($sortnum=="2")$sort="price_desc";
	if($sortnum=="3")$sort="price_asc";
	if($sortnum=="4")$sort="delistTime_desc";
	return $sort;
}

?>