<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
include_once("include/config.php");
include_once("include/label.php");
@header("Cache-control: private"); 
$keyword=SafeRequest("keyword","post");
if($keyword==""){
	$keyword=ReplaceStr(SafeRequest("keyword","get"),"　","");
	if(is_utf8($keyword)) $keyword=convert_encoding($keyword, 'GBK' ,'UTF-8');
	
}
if($keyword==""){
	die("<script>alert('请输入查询的关键字');history.back();</script>");
	}
		
$Mark_Text=@file_get_contents(_SYC_ROOT_.S_Templateurl."search.html");
$Mark_Text=topandbottom($Mark_Text);
$Mark_Text=ReplaceStr($Mark_Text,"{syccms:search}",$keyword);
$data_content='';
	//预先除了分页
	$pagenum=getpagenum($Mark_Text);
	preg_match_all('/{syccms:video(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/syccms:video}/',$Mark_Text,$page_arr);//判断是否有分页标识
	if(!empty($page_arr) && !empty($page_arr[2])){
		$zimu_arr=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
		$zimu_arr1=array(-20319,-20283,-19775,-19218,-18710,-18526,-18239,-17922,-17417,-16474,-16212,-15640,-15165,-14922,-14914,-14630,-14149,-14090,-13318,-12838,-12556,-11847,-11055);
		$zimu_arr2=array(-20284,-19776,-19219,-18711  ,-18527,-18240,-17923,-17418,-16475,-16213,-15641,-15166,-14923,-14915,-14631,-14150,-14091,-13319,-12839,-12557,-11848,-11056,-2050);

		if(in_array(strtoupper($keyword),$zimu_arr)){
			$posarr=array_keys($zimu_arr,strtoupper($keyword));
			$pos=$posarr[0];
			$sqlstr="SELECT * FROM s_video
where ((ord( substring( S_Name, 1, 1 ) ) -65536>=".($zimu_arr1[$pos])." and  ord( substring( S_Name, 1, 1 ) ) -65536<=".($zimu_arr2[$pos]).")) or UPPER(substring( S_Name, 1, 1 ))='".$zimu_arr[$pos]."'";
		}else{
			$sqlstr="select * from s_video where S_IsHide=0 and S_ClassID<>0 and S_Name like '%".$keyword."%' or S_Author like '%".$keyword."%' or S_Year like '%".$keyword."%' or S_Direct like '%".$keyword."%' or S_Area like '%".$keyword."%' order by S_AddTime desc";
		}	
	//$sqlstr="select * from s_video where S_IsHide=0 and S_ClassID<>0 and S_Name like '%".$keyword."%' or S_Author like '%".$keyword."%' or S_Year like '%".$keyword."%' or S_Direct like '%".$keyword."%' or S_Area like '%".$keyword."%' order by S_AddTime desc";
		$Arr=searchpage($sqlstr,$page_arr[2][0],$keyword);//sql,每页显示条数
		$result=$db->query($Arr[2]);
		$recount=$db->num_rows($result);
		if($recount==0){
			$mmc=urlencode(convert_encoding("管理员您好，我想看《".$keyword."》请您添加一下，谢谢！",'UTF-8','GBK'));
			$data_content="<div align=center><a href=gbook.php?content=".$mmc."><strong><font color=#FF0000>对不起,没有找到《".$keyword."》相关视频,请给管理员留言!</font></strong></a><br/><br/></div>";
		}else{
			if($result){
				$sorti=1;
				while ($row2 = $db ->fetch_array($result)){
					$datatmp=datavideo($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
					$sorti=$sorti+1;
					$data_content.=$datatmp;
					}
				}
			}// end if recount
		$Mark_Text=Page_Mark($Mark_Text,$Arr);	
		$Mark_Text=ReplaceStr($Mark_Text,$page_arr[0][0],$data_content);
		unset($Arr);
		}
	unset($page_arr);
	  $Mark_Text=	Common_Mark($Mark_Text,0);
	  $p=SafeRequest("pages","get");
	  $cache_id ='s_'.$keyword.$p;
	  if(!($cache_opt->start($cache_id))){
		 echo($Mark_Text);
		 $cache_opt->end();
		  }

function searchpage($mysql,$pagesize,$keyword){
	global $db;
	if(!is_utf8($keyword)) $keyword=urlencode(convert_encoding($keyword ,'UTF-8', 'GBK'));
	$url="keyword=".$keyword."&";
	$pages=SafeRequest("pages","get");
	$pagesok=$pagesize;//每页显示记录数
    if (!isset($pages)||$pages==""||!is_numeric($pages)||$pages<=0){$pages=1;}
  	$sqlstr=$mysql;
  	$res=$db -> query($sqlstr);
 	$nums= $db -> num_rows($res);
	if($nums==0){$nums=1;}
    $pagejs=ceil($nums/$pagesok);//总页数
    if($pages>$pagejs){ $pages=$pagejs;}
  
    $sql=$sqlstr." LIMIT ".$pagesok*($pages-1).",".$pagesok;
	$result = $db -> query($sql);
	$str="";
	$first="?".$url."pages=1";
	$pageup="?".$url."pages=".($pages-1)."";
	$pagenext="?".$url."pages=".($pages+1)."";
	$last="?".$url."pages=".$pagejs."";
		$pagelist="<script>
		function gotopage(url,page){
			window.location=url+page;
			}
		</script>";
	$pagelist.="<select onchange=\"gotopage('?".$url."pages=',this.value)\">\r\n<option value='0'>跳转</option>\r\n";
	for($k=1;$k<=$pagejs;$k++){
		$pagelist.="<option value='".$k."'>第".$k."页</option>\r\n";
		}
	$pagelist.="</select>";	
	if($pagejs<=10){
  		for($i=1;$i<=$pagejs;$i++){
   			$str.="<a href='?".$url."pages=".$i."'>".$i."</a>&nbsp;";
 	 	}
	}
	else{
 		if($pages>=12){
 			for($i=$pages-5;$i<=$pages+6;$i++){
   				if($i<=$pagejs){
   					$str.="<a href='?".$url."pages=".$i."'>".$i."</a>&nbsp;";
    			}
  			}
  			if($i<=$pagejs){ 
    			$str.="....";
	    		$str.="<a href='?".$url."pages=".$pagejs." '>".$pagejs."</a>&nbsp;";
   			}
   		}
  		else{
  			for($i=1;$i<=12;$i++){
    			$str.="<a href='?".$url."pages=".$i." '>".$i."</a>&nbsp;";
 		} 
 				if($i<=$pagejs){ 
      				$str.="....";
	  				$str.="<a href='?".$url."pages=".$pagejs." '>".$pagejs."</a>&nbsp;";
    			}
 		 }
	}
	 while ($row = $db -> fetch_array($result) ){
	 }
	 $arr=array($str,$result,$sql,$nums,$pagelist,$pages,$pagejs,$first,$pageup,$pagenext,$last,$pagesok);
	 return $arr;
}

function is_utf8($string) {
// From http://w3.org/International/questions/qa-forms-utf-8.html
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string);

} // function is_utf8 
?>