<?php
define('THIS_SCRIPT', 'honor');
require_once 'global.php';
include_once $fsetting['manageurl'].'inc/page.class.php';
$lang = load_lang($lang, 'honor' );
$titlelink = array(
		0=>array('linktitle'=>$lang['title'],'linkurl'=>'honor.php')
);
$honorobj=new userinfo();
head(1,0,0,$titlelink);
$honorobj->userlogin();
LoadMain();
footer();
class userinfo{
	var $username;
	var $usergroupid;
	var $userclass;
	var $userid;
	var $action;
	var $usercurrhonor;
	function userinfo() {
		global $dv,$db,$userid;
		$sql=$db->query("select usergroupid,username from {$dv}user where userid={$userid}");
		$result=$db->fetch_array($sql);
		$this->usergroupid=$result['usergroupid'];
		$this->username=$result['username'];
		$this->usercurrhonor=$this->getuserhonor();
	}
	function disappearance_days($user_lastlogin) {//失踪天数	
		$disap_pearance=time()+getUserTimeZoneOffset();
		$disap_pearance=dateDiff('D',$user_lastlogin,$disap_pearance);
		return ($disap_pearance);
		exit;
	}
	function honorpiclist() {
		global $piclist,$lang;
		$piclist=array();
		if (is_dir($lang['honor.info2'])) {
			if ($handle = opendir($lang['honor.info2'])) {
				while (($file = readdir($handle)) !== false) {
					if($file=="." or $file==".."){
						continue;
					}
					else{
						$piclist[]=$file;
					}
				}
			}
		}
		closedir($dir_hand);
	}
	function honorlist() {
		global $dv,$db,$hlist;
		$hlist=array();
		$sql=$db->query("select id,honorname,honorpic,honorread from {$dv}honor_list");
		while ($arr=$db->fetch_array($sql)) {
			$hlist[]=$arr;
		}
	}
	function savehonor() {
		global $dv,$db,$lang;
		$honor_name=trim($_POST['honor_name']);
		$honor_pic=$_POST['honor_pic'];
		$honor_read=trim($_POST['honor_read']);
		if(!empty($honor_name) or !empty($honor_pic) or !empty($honor_read)){
			$db->query("insert into {$dv}honor_list (honorname,honorpic,honorread) values ('{$honor_name}','{$honor_pic}','{$honor_read}')");
			showmsg($lang['honor.info1']);
			exit;
		}
		else{
			showmsg($lang['honor.info13']);
			exit;
		}
	}
	function edithonor() {
		global $lang,$dv,$db,$id;
		$hid=$_POST['hid'];
		$hname=$_POST['hname'];
		$hpic=$_POST['hpic'];
		$hread=$_POST['hread'];
		$num=count($hid);
		for ($i=0;$i<$num;$i++) {
			$db->query("update {$dv}honor_list set honorname='{$hname[$i]}',honorpic='{$hpic[$i]}',honorread='{$hread[$i]}' where id={$hid[$i]}");			
		}
		showmsg($lang['honor.info4']);
		exit;
	}
	function honordel() {
		global $dv,$db,$lang;
		$hid=$_GET['id'];
		if(!is_numeric($hid) or empty($hid)){
			showmsg($lang['honor.info3']);
			exit;
		}
		$db->query("delete from {$dv}honor_list where id={$hid}");
		showmsg($lang['honor.info5']);
		exit;
	}
	function honoruserlist() {
		global $dv,$db,$lang;
		global $page_size,$page,$total,$page_count,$offset,$p,$userlist;
		$userlist=array();
		$total=$db->scalar("select count(userid) from {$dv}user where userhonor!=''");
		$page_size=20;
		if ($page=="" or $page<1)
			$page=1;
		$page_count=ceil($total/$page_size);
		if ($page>$page_count)
			$page=$page_count;
		$offset=$page;
		if( $offset < 1 ) $offset = 1;
		$sql=$db->query("select username,userhonor from {$dv}user where userhonor!=''",array('absolutePage'=>$offset, 'pageSize'=>$page_size));
		while ($arr=$db->fetch_array($sql)) {
			$userlist[]=$arr;
		}		
	}
	function usergroup() {
		global $dv,$db,$usergrouplist;
		$usergrouplist=array();
		$sql=$db->query("select usergroupid,title,usertitle from {$dv}usergroups");
		while ($arr=$db->fetch_array($sql)) {
			$usergrouplist[]=$arr;
		}
	}
	function getuserhonor() {//得到用户的勋章
		global $dv,$db;
		$user_honor=$db->scalar("select userhonor from {$dv}user where username='{$this->username}'");
		return $user_honor;
	}
	function alluserhonor() {//颁发情况
		global $dv,$db,$allhonorlist;
		$allhonorlist=array();
		$sql=$db->query("select id,username,honorname,honorpic,issueuser,reason,honortime from {$dv}honor_user");
		while ($arr=$db->fetch_array($sql)) {
			$allhonorlist[]=$arr;
		}
	}
	function issuehonor() {//颁发勋章
		global $dv,$db,$lang;
		$gethonoruser=$_GET['gethonoruser'];//获奖/摘除者
		$poshonor=$_POST['poshonor'];//勋章图片
		$reason=$_POST['cause'];//原因
		$pos=$_POST['pos'];
		$nowtime=time();
		$already_honor=$db->scalar("select userhonor from {$dv}user where username='{$gethonoruser}'");
		$honorname=$db->scalar("select honorname from {$dv}honor_list where honorpic='{$poshonor}'");//勋章名称
		if(intval($pos)==1){//颁发
			$db->query("insert into {$dv}honor_user (username,honorname,honorpic,issueuser,reason,honortime,pos) values ('{$gethonoruser}','{$honorname}','{$poshonor}','{$this->username}','{$reason}',{$nowtime},{$pos})");
			if(empty($already_honor)){
				$db->query("update {$dv}user set userhonor='{$poshonor}' where username='{$gethonoruser}'");
			}
			else{
				$poshonor=$already_honor.'|||'.$poshonor;
				$db->query("update {$dv}user set userhonor='{$poshonor}' where username='{$gethonoruser}'");
			}			
			showmsg($lang['honor.info12']);
			exit;
		}
		else{//摘除
			if(strpos($already_honor,'|||')!==false){
				$tmphonor=explode('|||',$already_honor);
				if(in_array($poshonor,$tmphonor)){
					$endhonor='';
					$db->query("delete from {$dv}honor_user where username='{$gethonoruser}' and honorpic='{$poshonor}'");
					foreach($tmphonor as $tmphonorarr){
						if($tmphonorarr==$poshonor){
							continue;
						}
						else{
							$endhonor.=$tmphonorarr.'|||';
						}
					}
					$endhonor=substr($endhonor,0,-3);
					$db->query("update {$dv}user set userhonor='{$endhonor}' where username='{$gethonoruser}'");
					showmsg($lang['honor.info11']);
					exit;
				}
				else{
					showmsg($lang['honor.info10']);
					exit;
				}
			}
			else{
				if($already_honor==$poshonor){
					$db->query("delete from {$dv}honor_user where username='{$gethonoruser}' and honorpic='{$poshonor}'");
					$db->query("update {$dv}user set userhonor='' where username='{$gethonoruser}'");
					showmsg($lang['honor.info11']);
					exit;
				}
				else{
					showmsg($lang['honor.info10']);
					exit;					
				}
			}
		}
	}
	function userlogin() {//用户登录;
		global $lang;
		if(empty($this->usergroupid)){
			header("location:./index.php");
			exit;
		}
		else{
			if($this->usergroupid==1){
				$this->action='manager';
			}
			else{
				$this->action='common';
			}
		}
	}
}
function LoadMain() {
	global $dv,$db,$lang,$action,$doaction,$honorobj,$piclist,$hlist,$userhonorlist,$usergrouplist,$searchuserlist;
	global $allhonorlist;
	global $page_size,$page,$total,$page_count,$offset,$p,$userlist;
	switch ($doaction) {
		case 'savenew':
			$honorobj->savehonor();
			break;
		case 'honormanager':
		case '':
			$honorobj->honorpiclist();
			$honorobj->honorlist();
			break;
		case 'saveedit':
			$honorobj->edithonor();
			break;
		case 'del':
			$honorobj->honordel();
			break;
		case 'honorreadlist':
		case '':
			$honorobj->honorlist();
			break;
		case 'honoruser':
			$honorobj->honoruserlist();
			break;
		case 'honorsearch':
			$honorobj->usergroup();
			break;
		case 'user_search':
			usersearch();
			break;
		case 'honorlog':
			$honorobj->alluserhonor();
			break;
		case 'issueok':
			$honorobj->issuehonor();
			break;

	}
require_once printout('honor');
}
function usersearch() {
	global $lang,$dv,$db,$searchuserlist;
	global $page_size,$page,$total,$page_count,$offset,$p,$userlist;
	$searchuserlist=array();
	//快速查询
	$searchmax=$_POST['searchmax'];//最多显示记录数
	$username=trim($_POST['username']);//用户名
	$usergroups=$_POST['usergroups'];//用户组
	$useremail=trim($_POST['useremail']);//Email
	$userim=trim($_POST['userim']);//用户IM
	$lastip=trim($_POST['lastip']);//登录IP
	$usertitle=trim($_POST['usertitle']);//头衔
	$sign=trim($_POST['sign']);//签名
	$userinfo=trim($_POST['userinfo']);//详细资料
	$usernamepreg=$_POST['usernamepreg'];//完整匹配
	//特殊查询
	$loginr=$_POST['loginr'];//登录次数
	$logint=$_POST['logint'];
	$vanishr=$_POST['vanishr'];//消失天数
	$vanisht=$_POST['vanisht'];
	$regr=$_POST['regr'];//注册天数
	$regt=$_POST['regt'];
	$artcler=$_POST['artcler'];//发表帖数
	$artclet=$_POST['artclet'];
	$uwealth=$_POST['uwealth'];//用户金钱
	$uwealthvalue=$_POST['uwealthvalue'];
	$uep=$_POST['uep'];//用户经验
	$uepvalue=$_POST['uepvalue'];
	$ucp=$_POST['ucp'];//用户魅力
	$ucpvalue=$_POST['ucpvalue'];
	$upower=$_POST['upower'];//用户威望
	$upowervalue=$_POST['upowervalue'];
	$umoney=$_POST['umoney'];//用户金币
	$umoneyvalue=$_POST['umoneyvalue'];
	$uticket=$_POST['uticket'];//用户点券
	$uticketvalue=$_POST['uticketvalue'];
	$page_size=intval($searchmax);//显示记录数
	$username=trim($username);
	$fldList='userid,username,useremail,lastlogin,userlastip,userisbest,userlogins,userpost,usergroupid,vip_startime,vip_endtime,userhonor';
	if($username!=''){
		if(intval($usernamepreg)==1){
			$condstr.="username='{$username}' and ";
		}
		else{
			$condstr.="username like '%{$username}%' and ";			
		}
	}
	//用户组
	if(!empty($usergroups)){
		$condstr.="usergroupid={$usergroups} and ";
	}
	//Email
	if($useremail!=''){
		$condstr.="useremail like '%{$useremail}%' and ";		
	}
	//用户IM
	if($userim!=''){
		$condstr.="userim like '%{$userim}%' and ";
	}
	//登录IP
	if($lastip!=''){
		$condstr.="userlastip like '%{$lastip}%' and ";
	}
	//头衔
	if($usertitle!=''){
		$condstr.="usertitle like '%{$usertitle}%' and ";
	}
	//签名
	if($sign!=''){
		$condstr.="usersign like '%{$sign}%' and ";
	}
	//详细资料
	if($userinfo!=''){
		$condstr.="userinfo like '%{$userinfo}%' and ";
	}
	//登录次数
	$logint=trim($logint);
	if(is_numeric($logint) ){
		if( $loginr == 'more' ){
			$condstr .= "userlogins>='{$logint}' and ";
		} else{
			$condstr .= "userlogins<='{$logint}' and ";
		}
	}
	//消失天数
	$vanisht = trim($vanisht);
	if( is_numeric($vanisht) ){
		if( $vanishr == 'more' ){
			$condstr.= "lastlogin<='".dateAdd('d',-$vanisht,time(),'')."' and ";
		} else{
			$condstr.= "lastlogin>='".dateAdd('d',-$vanisht,time(),'')."' and ";
		}
	}
	//注册天数
	$regt = trim($regt);
	if( is_numeric($regt) ){
		if( $regr == 'more' ){
			$condstr .= "joindate<='".dateAdd('d',-$regt,time(),'')."' and ";
		} else{
			$condstr .= "joindate>='".dateAdd('d',-$regt,time(),'')."' and ";
		}
	}
	//发表帖数
	$artclet = trim($artclet);
	if( is_numeric($artclet) ){
		if( $artcler == 'more' ){
			$condstr .= "userpost>='{$artclet}' and ";
		} else{
			$condstr .= "userpost<='{$artclet}' and ";
		}
	}
	//用户金钱
	$uwealthvalue = trim($uwealthvalue);
	if( is_numeric($uwealthvalue) ){
		if( $uwealth == 'more' ){
			$condstr .= "userwealth>='{$uwealthvalue}' and ";
		} else{
			$condstr .= "userwealth<='{$uwealthvalue}' and ";
		}
	}
	//用户经验
	$uepvalue = trim($uepvalue);
	if( is_numeric($uepvalue) ){
		if( $uep == 'more' ){
			$condstr .= "userep>='{$uepvalue}' and ";
		} else{
			$condstr .= "userep<='{$uepvalue}' and ";
		}
	}
	//用户魅力
	$ucpvalue = trim($ucpvalue);
	if( is_numeric($ucpvalue) ){
		if( $ucp == 'more' ){
			$condstr .= "usercp>='{$ucpvalue}' and ";
		} else{
			$condstr .= "usercp<='{$ucpvalue}' and ";
		}
	}
	//用户威望
	$upowervalue = trim($upowervalue);
	if( is_numeric($upowervalue) ){
		if( $upower== 'more' ){
			$condstr .= "userpower>='{$upowervalue}' and ";
		} else{
			$condstr .= "userpower<='{$upowervalue}' and ";
		}
	}
	//用户金币
	$umoneyvalue = trim($umoneyvalue);
	if( is_numeric($umoneyvalue) ){
		if( $umoney == 'more' ){
			$condstr .= "usermoney>='{$umoneyvalue}' and ";
		} else{
			$condstr .= "usermoney<='{$umoneyvalue}' and ";
		}
	}
	//用户点券
	$uticketvalue = trim($uticketvalue);
	if( is_numeric($uticketvalue) ){
		if( $uticket == 'more' ){
			$condstr .= "userticket>='{$uticketvalue}' and ";
		} else{
			$condstr .= "userticket<='{$uticketvalue}' and ";
		}
	}
	if($condstr!=''){
		$condstr=substr($condstr,0,-5);
		$condstr="where 1=1 and {$condstr}";
	}
	else{
		showmsg($lang['honor.info8']);
		exit;
	}
	$order="order by userid limit {$page_size}";
	$count=$db->scalar("select count(1) from {$dv}user {$condstr} {$order}");
	if($count<=0){
		showmsg($lang['honor.info9']);
		exit;
	}
	$sql=$db->query("select {$fldList} from {$dv}user {$condstr} {$order}");
	while ($arr=$db->fetch_array($sql)) {
		$searchuserlist[]=$arr;
	}
}
?>