<?php
class SF_skin2{

	public static function echoThemeCss(){
		global $zbp;
		if($zbp->Config('sf_skin2')->rancolors==1){
			$array=array('black.css','green.css','pink.css','purple.css','style.css');
			$key=array_rand($array);
			echo '<link rel="stylesheet" rev="stylesheet" href="'.$zbp->host.'zb_users/theme/sf_skin2/style/'.$array[$key].'" type="text/css" media="all"/>';
		}else{
			echo '<link rel="stylesheet" rev="stylesheet" href="'.$zbp->host.'zb_users/theme/sf_skin2/style/'.$zbp->style.'.css" type="text/css" media="all"/>';
		}
	}

	public static function  echoxiangguanwenzhang($article){
		global $zbp;
		$aid=$article->ID;
		$tagid=$article->Tags;
		$tagrd=array_rand($tagid);
		if( sizeof($tagid)>0 && ($tagid[$tagrd]->Count)>1){
			$tagi='%{'.$tagrd.'}%';
			$where = array(array('=','log_Status','0'),array('like','log_Tag',$tagi),array('<>','log_ID',$aid));
		}else{
			$cid=$article->Category->ID;
			$where = array(array('=','log_Status','0'),array('=','log_CateID',$cid),array('<>','log_ID',$aid));
		}
		$str="";
		$select=array('log_ID');
		$sql=$zbp->db->sql->Select($GLOBALS['table']['Post'],$select,$where,'','');
		$array=$zbp->db->Query($sql);
		$ids=array();
		if(count($array)>0){
			foreach($array as $u){
				$ids[]=GetValueInArray($u,'log_ID');
			}
		}
		$newids=array();
		if(count($ids)==0){
			return "";
		}else if(count($ids)<=5){
			$newids=$ids;
		}else{
			$rand_keys = array_rand($ids, 5);
			foreach($rand_keys as $key => $value)
			{
				$newids[] = $ids[$value];
			}
		}
		$or=array();
		foreach($newids as $key => $value){
			$or[]=array('log_ID',$value);
		}
		$where=array(array("ARRAY",$or));
		$array = $zbp->GetArticleList(array('*'),$where,'','','');
		foreach ($array as $related) {
			if(($related->ID)!=$aid){
				SF_img1::getPics($related,122,88,4);
				$temp=mt_rand(1,15);
				$temp=$zbp->host."/zb_users/theme/".$zbp->theme."/style/images/random/tb".$temp.".jpg";
				if($related->sf_img_count>0){
					$temp=$related->sf_img[0];
				}
				$str .='<li><div class="related_posts_pic"><div class="thumbnail">';
				$str .='<a href="'.$related->Url.'" rel="bookmark" title="'.$related->Title.'"><img src="'.$temp.'" width="122px" height="88px" alt="'.$related->Title.'" style="opacity: 1;"></a>';
				$str .='</div></div><div class="related_posts_tittle">';
				$str .='<a href="'.$related->Url.'" title="详细阅读 《'.$related->Title.'》" rel="nofollow">'.$related->Title.'</a></div></li>';
			}
		}
		echo $str;
	}

	public static function changePagebar(&$pagebar){
		global $zbp;
		if($pagebar->PageCount==0)return null;
		$pagebar->buttons=array();
		$pagebar->PageAll = ceil($pagebar->Count / $pagebar->PageCount);
		$pagebar->PageFirst = 1;
		$pagebar->PageLast = $pagebar->PageAll;

		$pagebar->PagePrevious=$pagebar->PageNow-1;
		if($pagebar->PagePrevious<1){$pagebar->PagePrevious=1;}
		$pagebar->PageNext=$pagebar->PageNow+1;
		if($pagebar->PageNext>$pagebar->PageAll){$pagebar->PageNext=$pagebar->PageAll;}

		if($pagebar->PageNow <> $pagebar->PageFirst){
			$pagebar->UrlRule->Rules['{%page%}']=$pagebar->PagePrevious;
			$pagebar->buttons['上一页'] = $pagebar->UrlRule->Make();
			$pagebar->prevbutton=$pagebar->buttons['上一页'];
		}
		$num=0;
		$step=1;
		$numarray=array();
		if($pagebar->PageBarCount>0){
			$num++;
			$numarray[]=$pagebar->PageNow;
		}
		while($num<$pagebar->PageBarCount){
			$flag=0;
			if(($pagebar->PageNow+$step)<=$pagebar->PageLast && (($pagebar->PageNow+$step)>0) && in_array($pagebar->PageNow+$step,$numarray)==false){
				$num++;
				$flag=1;
				$numarray[]=$pagebar->PageNow+$step;
			}
			if($num==$pagebar->PageBarCount){
				break;
			}
			$step=$step*(-1);
			if(($pagebar->PageNow+$step)<=$pagebar->PageLast && (($pagebar->PageNow+$step)>0) && in_array($pagebar->PageNow+$step,$numarray)==false){
				$num++;
				$flag=1;
				$numarray[]=$pagebar->PageNow+$step;
			}
			if($num==$pagebar->PageBarCount){
				break;
			}
			$step=$step*(-1);
			if(($step)<=$pagebar->PageLast && ($step>0) && in_array($step,$numarray)==false){
				$num++;
				$flag=1;
				$numarray[]=$step;
			}
			if($num==$pagebar->PageBarCount){
				break;
			}
			$step=$step*(-1);
			if(($pagebar->PageLast+$step+1)<=$pagebar->PageLast && (($pagebar->PageLast+$step+1)>0) && in_array($pagebar->PageLast+$step+1,$numarray)==false){
				$num++;
				$flag=1;
				$numarray[]=$pagebar->PageLast+$step+1;
			}
			if($num==$pagebar->PageBarCount){
				break;
			}
			$step=$step*(-1);
			$step++;
			if($flag==0){
				break;
			}
		}
		sort($numarray);
		if(in_array(1,$numarray)==false){
			$pagebar->UrlRule->Rules['{%page%}']=$pagebar->PageFirst;
			$pagebar->buttons['首页']=$pagebar->UrlRule->Make();
		}
		if(in_array($pagebar->PageLast,$numarray)==false){
			$pagebar->UrlRule->Rules['{%page%}']=$pagebar->PageLast;
			$pagebar->buttons['末页']=$pagebar->UrlRule->Make();
		}
		$last=null;
		$first=1;
		foreach($numarray as $now){
			if($last==null){
			}else if(($last+1)==$now){
			}else if($first==1){
				$pagebar->buttons['...&nbsp']='javascript:void(0);';
				$first=2;
			}else{
				$pagebar->buttons['&nbsp...']='javascript:void(0);';
			}
			$pagebar->UrlRule->Rules['{%page%}']=$now;
			$pagebar->buttons[$now]=$pagebar->UrlRule->Make();
			$last=$now;
		}
		
		if($pagebar->PageNow <> $pagebar->PageNext){
			$pagebar->UrlRule->Rules['{%page%}']=$pagebar->PageNext;
			$pagebar->buttons['下一页']=$pagebar->UrlRule->Make();
			$pagebar->nextbutton=$pagebar->buttons['下一页'];
		}	
	}
	//输出标签
	public static function showAdminTitle(){
		global $zbp;
		echo '<li><a index="1" href="'.$zbp->host.'zb_users/theme/sf_skin2/main.php" style="cursor: pointer;"><span>基本设置</span></a></li> ';
		echo '<li><a index="2" href="'.$zbp->host.'zb_users/theme/sf_skin2/config/me.php" style="cursor: pointer;"><span>个人信息</span></a></li> ';
	}

	//检查配置
	public static function checkconfig(){
		global $zbp;
		if(!$zbp->Config('sf_skin2')->HasKey('version')) {
			$zbp->Config('sf_skin2')->version=1;
			$zbp->Config('sf_skin2')->keyword='';
			$zbp->Config('sf_skin2')->description='';
			$zbp->Config('sf_skin2')->showtitle1='欢迎各位光临本博客，(*^__^*) <br>想分享本主题请<b style="color: #000;font-weight:normal;"> @</b><strong style="color: #0088DD;"> shellingford</strong>：  <a href="http://www.iasuna.com" target="_blank">此处应有内容</a><br>———  <strong>-____-</strong> ———';
			$zbp->Config('sf_skin2')->icp='';
			$zbp->SaveConfig('sf_skin2');
		}
		if($zbp->Config('sf_skin2')->version<2){
			$zbp->Config('sf_skin2')->version=2;
			$zbp->Config('sf_skin2')->rancolors=0;
			$zbp->SaveConfig('sf_skin2');
		}
	}

	//保存函数
	public static function saveconfig(){
		global $zbp;
		$actiontype=GetVars('actiontype', 'POST');
		if($actiontype == "set1"){
			$zbp->Config('sf_skin2')->indextop=GetVars('indextop','POST');
			$zbp->Config('sf_skin2')->email=GetVars('email','POST');
			$zbp->Config('sf_skin2')->keyword=GetVars('keyword','POST');
			$zbp->Config('sf_skin2')->description=GetVars('description','POST');
			$zbp->Config('sf_skin2')->icp=GetVars('icp','POST');
			$zbp->Config('sf_skin2')->rancolors=GetVars('rancolors','POST');
			$zbp->SaveConfig('sf_skin2');
			$zbp->SetHint('good');
			Redirect($zbp->host.'zb_users/theme/sf_skin2/main.php');
		}else if($actiontype == "set2"){
			$zbp->Config('sf_skin2')->reed=GetVars('reed','POST');
			$zbp->Config('sf_skin2')->weibo=GetVars('weibo','POST');
			$zbp->Config('sf_skin2')->qqweibo=GetVars('qqweibo','POST');
			$zbp->Config('sf_skin2')->qq=GetVars('qq','POST');
			$zbp->Config('sf_skin2')->qqmail=GetVars('qqmail','POST');
			$zbp->Config('sf_skin2')->showtitle1=GetVars('showtitle1','POST');
			$zbp->SaveConfig('sf_skin2');
			$zbp->SetHint('good');
			Redirect($zbp->host.'zb_users/theme/sf_skin2/config/me.php');
		}else if($actiontype=="logo"){
			if(isset($_FILES["logo"])){
				$name=$_FILES["logo"]["tmp_name"];
				move_uploaded_file($_FILES["logo"]["tmp_name"], $zbp->usersdir . 'theme/sf_skin2/style/images/logo.png');
			}
			$zbp->SetHint('good');
			Redirect($zbp->host.'zb_users/theme/sf_skin2/main.php');
		}else{
			SF_skin2::checkconfig();
		}
	}

	public static function echoVip($comment){
		global $zbp;
		$email=$comment->Author->Email;
		$sf_skin2_vip='';
		if(empty($email)){
			$sf_skin2_vip='<a class="vip0" title="游客"></a>';
			echo  $sf_skin2_vip;
			return;
		}
		$adminemail=$zbp->Config('sf_skin2')->email;
		if(empty($adminemail)==false && trim($email)==trim($adminemail)){
			$sf_skin2_vip='<a class="vip10" title="博主"></a>';
			echo  $sf_skin2_vip;
			return;
		}
			$sql=$zbp->db->sql->Count($GLOBALS['table']['Comment'],array(array('count','*','cc')),array(array('=','comm_Email',$comment->Author->Email)),'');
			$array=$zbp->db->Query($sql);
			if(count($array)>0){
				foreach($array as $u){
					$cc=GetValueInArray($u,'cc');
					if($cc<5){
						$sf_skin2_vip='<a class="vip0" title="游客"></a>';
					}else if($cc<10){
						$sf_skin2_vip='<a class="vip1" title="vip1"></a>';
					}else if($cc<20){
						$sf_skin2_vip='<a class="vip2" title="vip2"></a>';
					}else if($cc<30){
						$sf_skin2_vip='<a class="vip3" title="vip3"></a>';
					}else if($cc<40){
						$sf_skin2_vip='<a class="vip4" title="vip4"></a>';
					}else if($cc<50){
						$sf_skin2_vip='<a class="vip5" title="vip5"></a>';
					}else if($cc<60){
						$sf_skin2_vip='<a class="vip6" title="vip6"></a>';
					}else if($cc<70){
						$sf_skin2_vip='<a class="vip7" title="vip7"></a>';
					}else if($cc<80){
						$sf_skin2_vip='<a class="vip8" title="vip8"></a>';
					}else{
						$sf_skin2_vip='<a class="vip9" title="vip9"></a>';
					}
					echo $sf_skin2_vip;
					return;
				}	
			}
	}

	public static function randompost($num){
		global $zbp;
		$select=array('log_ID');
		$where=array(array('=','log_Type',0),array('=','log_Status',0));
		$sql=$zbp->db->sql->Select($GLOBALS['table']['Post'],$select,$where,'','');
		$array=$zbp->db->Query($sql);
		$ids=array();
		if(count($array)>0){
			foreach($array as $u){
				$ids[]=GetValueInArray($u,'log_ID');
			}
		}
		$newids=array();
		if(count($ids)==0){
			return $newids;
		}else if(count($ids)<=$num){
			$newids=$ids;
		}else if($num==1){
			$newids[]=$ids[array_rand($ids)];
		}else{
			$rand_keys = array_rand($ids, $num);
			foreach($rand_keys as $key => $value)
			{
				$newids[] = $ids[$value];
			}
		}
		$or=array();
		foreach($newids as $key => $value){
			$or[]=array('log_ID',$value);
		}
		$where=array(array("ARRAY",$or));
		return	$zbp->GetArticleList(array('*'),$where,null,'','');
	}

	public static function echo3tag(){
		global $zbp;
		echo '<div id="tab_title"><h3><span class="off" id="content1" >随机文章</span><span class="on" id="content2" >热门文章</span><span class="off" id="content3" >热评文章</span></h3>';
		echo '<div id="tab_content"><ul class="hide" id="tab_content_1" style="display: none;">';
		$array=SF_skin2::randompost(10);
		foreach($array as $article){
			echo '<li><a href="'.$article->Url.'" rel="bookmark" title="'.$article->Title.'">'.$article->Title.'</a></li>';
		}
		echo '</ul><ul id="tab_content_2" style="display: block;">';
		$array = $zbp->GetArticleList(array('*'),null,array('log_ViewNums'=>'DESC'),array(10),'');
		foreach($array as $article){
			echo '<li><a href="'.$article->Url.'" rel="bookmark" title="'.$article->Title.'">'.$article->Title.'</a></li>';
		}
		echo '</ul><ul class="hide" id="tab_content_3" style="display: none;">';
		$array = $zbp->GetArticleList(array('*'),null,array('log_CommNums'=>'DESC'),array(10),'');
		foreach($array as $article){
			echo '<li><a href="'.$article->Url.'" rel="bookmark" title="'.$article->Title.'">'.$article->Title.'</a></li>';
		}
		echo '</ul></div>';
		echo '</div>';
	}

	public static function echoXingJi($article){
		$xingji=$xingji=$article->Metas->sf_skin2_xingji;
		if(empty($xingji)){
			$xingji=10;
		}else{
			$xingji=intval($xingji);
		}
		if($xingji<0 || $xingji>10){
			$xingji=10;
		}
		echo 'width:'.($xingji*10).'%;';

	}

	//最新评论的文章
	public static function findNewCommentsPost(){
		global $zbp;
		$sql=$zbp->db->sql->Select($GLOBALS['table']['Comment'],'DISTINCT comm_LogID',null,array('comm_PostTime'=>'desc'),array(0,10),'');
		$array=$zbp->db->Query($sql);
		if(count($array)>0){
			$or=array();
			foreach($array as $u){
				$id=GetValueInArray($u,'comm_LogID');
				if($id!=0){
					$or[]=array('log_ID',$id);
				}
			}
			if(count($or)==0){
				return array();
			}
			$where=array(array("ARRAY",$or));
			$result=$zbp->GetArticleList(array('*'),$where,null,'','');
			return $result;
		}else{
			return array();
		}
	}

	//过滤正文内容获取前N个字符
	public static function echoArticleInfo($article,$num=180){
		global $zbp;
		$info = preg_replace('/[\r\n\s]+/', '', trim(SubStrUTF8(TransferHTML($article->Content,'[nohtml]'),$num)).'...');
		echo $info;
	}

	//随机图片
	public static function echoRandomPicSrc(){
		global $zbp;
		$temp=mt_rand(1,15);
		$temp=$zbp->host."/zb_users/theme/sf_skin2/style/images/random/tb".$temp.".jpg";
		echo $temp;
	}
	
	//获取首页顶部幻灯片
	public static function findIndexTop(){
		global $zbp;
		$str=$zbp->Config('sf_skin2')->indextop;
		if(empty($str)){
			return array();
		}else{
			$ids=explode(',',$str);
		}
		$or=array();
		foreach($ids as $id){
			$id=intval($id);
			if($id!=0){
				$or[]=array('log_ID',$id);
			}
		}
		if(count($or)==0){
			return array();
		}
		$where=array(array("ARRAY",$or));
		$result=$zbp->GetArticleList(array('*'),$where,null,'','');
		return $result;
	}

	public static function startWith($str, $needle) {
		return strpos($str, $needle) === 0;
	}

	public static function endWith($str, $needle) {
		if(strlen($str)==0){
			return false;
		}
		$temp=substr($str,strlen($str)-1);
		return strpos($temp, $needle) === 0;
	}

	public static function indexbegin(){
		ob_start();
	}

	public static function removeHtml($str){
		$str = preg_replace('/<!--(.*?)-->/is',"",$str );
		$str = preg_replace ('/<\s*script[^>]*[^\/]>(.*?)<\s*\/\s*script\s*>/is',"" ,$str );
		$str = preg_replace ( '/<\s*script\s*>(.*?)<\s*\/\s*script\s*>/is',"" ,$str );
		$str = preg_replace ( '/<\s*style[^>]*[^\/]>(.*?)<\s*\/\s*style\s*>/is',"" ,$str  );
		$str = preg_replace ( '/<\s*style\s*>(.*?)<\s*\/\s*style\s*>/is',"" ,$str  );
		return $str;
	}

	public static function isMobile(){
		$agent = $_SERVER['HTTP_USER_AGENT']; 
		if(isset($agent)==false){
			return false;
		}
		if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,    "Windows CE") || strpos($agent,"SymbianOS")) {
			return true;
		}else{
			return false;
		}
	}

	public static function indexend(){
		global $zbp;
		$sf_html=ob_get_contents();
		$sf_html2=SF_skin2::removeHtml($sf_html);
		$flag=preg_match("/<a(.*?)href=\"http:\/\/www\.iasuna\.com\"(.*?)myid=\"shellingford\"/i",$sf_html2);
		if($flag<1 && $zbp->theme=='sf_skin2'){
			ob_end_clean();
			ob_start();
			$tmp=stripos($sf_html,'</head>');
			if($tmp===false){
				$tmp=stripos($sf_html,'</HEAD>');
			}
			$tmp1=substr($sf_html,0,$tmp);
			
			$tmp2=substr($sf_html,$tmp);
			echo $tmp1;
			echo base64_decode('PHNjcmlwdCB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiPmFsZXJ0KCLor7fli7/kv67mlLnniYjmnYPkv6Hmga/vvIzlpoLopoHmgaLlpI3vvIzor7fph43mlrDlronoo4XmnKzkuLvpopjvvIEiKTs8L3NjcmlwdD4=');
			echo $tmp2;
		}
		ob_end_flush();
	}
}
?>