<?php
require_once "../includes/common.inc.php";
require_once "../includes/splitword.func.php";
	$sorts=$sortselect=array();
	$query = $db -> query("SELECT * FROM {$tablepre}picsort ORDER BY ordermode ");
	while($sort = $db -> fetch_array($query)){
		$sorts[$sort['id']]=$sort;
		$sortselect[$sort['upid']][$sort['id']]=$sort;
	}
	$id = ispage($id) ? $id : 0;
	$sql = 	$id ? "WHERE id='$id'" : '';

if($q) {
	  $keyword = ereg_replace("[\"\r\n\t\*\?\(\)\$%']"," ",trim($q));
		$urladd = '&keyword='.$keyword;
		$sql =  "WHERE pname LIKE '%$keyword%'" ;
		$navname = strip_tags($keyword).' - ';
}else{
mexit('出错了哦！！关键字不允许为空！！');
}
function GetKeywords($keyword){
global $tablepre,$db;
$time=date("Y-m-d");
	   $keyword = cn_substr($keyword,50);
	   $row = $db -> queryarray("SELECT tags FROM {$tablepre}tags where keyword='".addslashes($keyword)."'; ");
	   if(!is_array($row)){
		   if(strlen($keyword) > 16){
		      $sp = new SplitWord();
	        $keywords = $sp->SplitRMM($keyword);
	        $sp->Clear();
	        $keywords = ereg_replace("[ ]{1,}"," ",trim($keywords));
	     }else{
	        $keywords = $keyword;
	     }
	     $inquery = "INSERT INTO `{$tablepre}tags`(`keyword`,`tags`,`weekct`,`weekup`,`monthct`,`monthup`,`count`,`result`,`sotime`)
          VALUES ('".addslashes($keyword)."', '".addslashes($keywords)."', '1', '".$time."', '1', '".$time."', '1', '0', '".$time."');
       ";
	     $db -> query($inquery);
	  }else{
		   $db -> query("Update {$tablepre}tags set count=count+1,sotime='".$time."' where keyword='".addslashes($keyword)."'; ");
		   $keywords = $row['tags'];
	  }
	  return $keywords;
}

function GetLikeWords(){
global $tablepre,$db,$keywords,$keyword;
	  $keyword = cn_substr($keyword,50);
 		$ks = explode(" ",$keywords);
 		$lsql = "";
 		foreach($ks as $k){
 			$k = trim($k);
 	    if(strlen($k)<2) continue;
 			if(ord($k[0])>0x80 && strlen($k)<3) continue;
 			$k = addslashes($k);
 			if($lsql=="") $lsql = $lsql." CONCAT(tags,' ') like '%$k %' ";
 			else $lsql = $lsql." Or CONCAT(tags,' ') like '%$k %' ";
 		}
 		if($lsql=="") return "";
 		else{
 			$i=0;
 			$lsql = "(".$lsql.") And Not(keyword like '".addslashes($keyword)."') ";
 			$query = $db -> query("Select keyword,count From {$tablepre}tags where $lsql and result='0'order by sotime desc limit 0,8; ");
 			while($row = $db -> fetch_array($query)){
 			$i=$i+1;
 			if($i==1){
 			$likeword = "相关搜索：";
 			}
 				 if($row['count']>1000) $fstyle=" style='font-size:11pt;color:red'";
 				 else if($row['count']>300) $fstyle=" style='font-size:10pt;color:green'";
 				 else $style = "";
 				 $likeword .= '<a href="?q='.urlencode($row['keyword']).'"'.$style."><u>".$row['keyword']."</u></a>　";
 			}
 			return $likeword;
 		}
}
function Gettags(){
global $tablepre,$db,$keywords;
$time=date("Y-m-d");
 		$ks = explode(" ",$keywords);
 		$rstr = "";
 		$kwsql = "";
 		$words = array();
 		foreach($ks as $k){
 			$k = trim($k);
 			if($k=="") continue;
 			if(ord($k[0])>0x80 && strlen($k)<3) continue;
 			$row = $db -> queryarray("SELECT * FROM {$tablepre}tags where tags='".$k."' and result='1'; ");
 			if(!is_array($row)){
	     $inquery = "INSERT INTO `{$tablepre}tags`(`tags`,`weekct`,`weekup`,`monthct`,`monthup`,`count`,`result`,`sotime`)
          VALUES ('".$k."', '1', '".$time."', '1', '".$time."', '1', '1', '".$time."');
       ";
	     $db -> query($inquery);
	  }else{
		   $db -> query("Update {$tablepre}tags set weekct=weekct+1,monthct=monthct+1,count=count+1,sotime='".$time."' where id='".$row['id']."'; ");
		   //周统计
			  if($time-$row['weekup']>7){
        $db -> query("Update {$tablepre}tags set weekct=0,weekup='".$time."' where id='".$row['id']."'; ");
			  }
			  //月统计
			  if($time-$row['monthup']>30){
        $db -> query("Update {$tablepre}tags set monthct=0,monthup='".$time."' where id='".$row['id']."'; ");
			  }
	  }
 		}
 		return ;
}


	$page = ispage($page) ? $page : 1;
	$pagenum = 30;
	$start_limit = ($page-1) * $pagenum;	
	$pics = array();
	$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}pic $sql");
	$lines = $db -> result($query, 0);
	$query = $db -> query("SELECT * FROM {$tablepre}pic $sql  ORDER BY postime DESC LIMIT $start_limit, $pagenum");
	while($pic = $db -> fetch_array($query)){
	if($keyword){
	Gettags();
	$keywords=GetKeywords($keyword);
	$likeword=GetLikeWords();
	}
		$pic['folderdir']  = 	$sorts[$pic['id']]['folderdir'];
		$pic['sortname'] = 	$sorts[$pic['id']]['sortname'];
		$pic['postime'] = gmdate("$dateformat $timeformat", $pic['postime'] + ($timeoffset * 3600));
		$pic['pname'] = $pic['pname'];
		$pics[$pic['pid']] = $pic;
	}
	include theme('so');
?>