<?php

/*

'**************************************************************************************************

' 软件名称: 黑色幻想舞曲管理系统CMS2.0

' 版本编号: hshxsdj 2.0

' 官方网站: http://www.hshxs.com

' 官方论坛：http://bbs.hshxs.com

' 版权所有: 黑色幻想开发团队    噬己翼(QQ:532917920)

' 郑重声明:

'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；

'    2、黑色幻想网络事业部保留此软件的法律追究权利

'**************************************************************************************************

*/

include_once("include/label.php");

$keyword=SafeRequest("keyword","post","zr");

$type=SafeRequest("type","post");

if($keyword==""){

	$keyword=SafeRequest("keyword","get","zr");

	if(is_utf8($keyword)) $keyword=convert_encoding($keyword, 'GBK' ,'UTF-8');

}

if($type=="") $type=SafeRequest("type","get","zr");

if($keyword=="") die("<script>alert('请输入查询的关键字');history.back();</script>");

$Mark_Text=@file_get_contents(_CHSHDJ_ROOT_.S_Templateurl."search.html");

$Mark_Text=topandbottom($Mark_Text);

$Mark_Text=ReplaceStr($Mark_Text,"{chshdjcms:search}",$keyword);

$data_content='';

	//预先除了分页

	$pagenum=getpagenum($Mark_Text);

	preg_match_all('/{chshdjcms:video(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/chshdjcms: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,-1,-17417,-16474,-16212,-15640,-15165,-14922,-14914,-14630,-14149,-14090,-13318,-1,-1,-12838,-12556,-11847,-11055);

		$zimu_arr2=array(-20284,-19776,-19219,-18711  ,-18527,-18240,-17923,-17418,-1,-16475,-16213,-15641,-15166,-14923,-14915,-14631,-14150,-14091,-13319,-12839,-1,-1,-12557,-11848,-11056,-2050);



		if(in_array(strtoupper($keyword),$zimu_arr)){

			$posarr=array_keys($zimu_arr,strtoupper($keyword));

			$pos=$posarr[0];

                   if($type==1){ //字母开头音乐人歌曲

			$sqlstr="SELECT * FROM s_video where ".$class." ((ord( substring( S_Author, 1, 1 ) ) -65536>=".($zimu_arr1[$pos])." and  ord( substring( S_Author, 1, 1 ) ) -65536<=".($zimu_arr2[$pos]).")) or UPPER(substring( S_Author, 1, 1 ))='".$zimu_arr[$pos]."'";



                   }else{ //字母开头音乐歌曲

			$sqlstr="SELECT * FROM s_video where ".$class." ((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{

                   if($type==1){ //歌曲标签搜索

			$sqlstr="select * from s_video where ".$class." S_IsHide=0 and S_Keywords like '%".$keyword."%' order by S_AddTime desc";

                   }else{

			$sqlstr="select * from s_video where ".$class." S_IsHide=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,$type);//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></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,$type){

	global $db;

	if($type=="") $type=SafeRequest("type","get");

	if(!is_utf8($keyword)) $keyword=urlencode(convert_encoding($keyword ,'UTF-8', 'GBK'));

	$url="keyword=".$keyword."&tid=".$tid."&class=".$class."&type=".$type."&";

	$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."' class='kd'>".$i."</a></li>";

 	 	}

	}

	else{

 		if($pages>=12){

 			for($i=$pages-5;$i<=$pages+6;$i++){

   				if($i<=$pagejs){

   					$str.="<a href='?".$url."pages=".$i."'>".$i."</a>";

    			}

  			}

  			if($i<=$pagejs){ 

    			//$str.="....";

	    		$str.="<a href='?".$url."pages=".$pagejs." '>".$pagejs."</a>";

   			}

   		}

  		else{

  			for($i=1;$i<=12;$i++){

    			$str.="<a href='?".$url."pages=".$i." ' class='kd'>".$i."</a>";

 		} 

 				if($i<=$pagejs){ 

      				//$str.="....";

	  				$str.="<a href='?".$url."pages=".$pagejs." '>".$pagejs."</a>";

    			}

 		 }

	}

	 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 

?>