<?php
 /*
 * 74cms 会员中心函数
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
 if(!defined('IN_QISHI'))
 {
 	die('Access Denied!');
 }
//注册会员
function user_register($username,$password,$member_type=0,$email,$uc_reg=true)
{
	global $db,$timestamp,$_CFG,$online_ip,$QS_pwdhash;
	$member_type=intval($member_type);
	$ck_username=get_user_inusername($username,$member_type);
	$ck_email=get_user_inemail($email,$member_type);
	if ($member_type==0) 
	{
	return -1;
	}
	elseif (!empty($ck_username))
	{
	return -2;
	}
	elseif (!empty($ck_email))
	{
	return -3;
	}
	$pwd_hash=randstr();
	$password_hash=md5(md5($password).$pwd_hash.$QS_pwdhash);
	$setsqlarr['username']=$username;
	$setsqlarr['password']=$password_hash;
	$setsqlarr['pwd_hash']=$pwd_hash;
	$setsqlarr['email']=$email;
	$setsqlarr['utype']=intval($member_type);
	$setsqlarr['reg_time']=$timestamp;
	$setsqlarr['reg_ip']=$online_ip;
	if ($_SESSION["openid"] && $_CFG['qq_apiopen']=="1")
	{
	$setsqlarr['qq_openid']=$_SESSION["openid"];
	}	
	$insert_id=inserttable(table('members'),$setsqlarr,true);
	if($member_type=="1")
	{
		if(!$db->query("UPDATE ".table('members')." SET parentuid='{$insert_id}' where uid={$insert_id}"))  return false;
		if(!$db->query("INSERT INTO ".table('members_points')." (uid) VALUES ('{$insert_id}')"))  return false;
		if(!$db->query("INSERT INTO ".table('members_setmeal')." (uid) VALUES ('{$insert_id}')")) return false;
		include_once(QISHI_ROOT_PATH.'include/fun_company.php');
		// 为企业分配顾问
		set_consultant($insert_id);
		///企业注册为体验版
		set_members_setmeal($insert_id,1);
	}
	elseif($member_type=="3")
	{
		if(!$db->query("INSERT INTO ".table('members_points')." (uid) VALUES ('{$insert_id}')"))  return false;
		if(!$db->query("INSERT INTO ".table('hunter_profile')." (uid,last_login_time) VALUES ('{$insert_id}','{$setsqlarr['reg_time']}')"))  return false;
	}
	if(!$db->query("INSERT INTO ".table('account')." (uid,utype) VALUES ('{$insert_id}','{$member_type}')")) return false;
	write_memberslog($insert_id,$member_type,1000,$username,"/",0,"注册成为会员",0);
return $insert_id;
}
//会员登录
function user_login($account,$password,$utype,$account_type=1,$uc_login=true,$expire=NULL)
{
	global $timestamp,$online_ip,$QS_pwdhash;
	$usinfo = $login = array();
	$success = false;
	if ($account_type=="1")
	{
		$usinfo=get_user_inusername($account,$utype);
		$audit="1";
	}
	elseif ($account_type=="2")
	{
		$usinfo=get_user_inemail($account,$utype);
		$audit=$usinfo['email_audit'];
	}
	elseif ($account_type=="3")
	{
		$usinfo=get_user_inmobile($account);
		$audit=$usinfo['mobile_audit'];
	}
	if (!empty($usinfo))
	{
		$pwd_hash=$usinfo['pwd_hash'];
		$usname=$usinfo['username'];
		$pwd=md5(md5($password).$pwd_hash.$QS_pwdhash);
		if ($usinfo['password']==$pwd)
		{
		update_user_info($usinfo['uid'],true,true,$expire);
		$login['qs_login']=get_member_url($usinfo['utype']);
		$success=true;
		write_memberslog($usinfo['uid'],$usinfo['utype'],1001,$usinfo['username'],"/",0,"成功登录",0);
		}
		else
		{
		$usinfo='';
		$success=false;
		}
	}
	return $login;	
}
//检测COOKIE
function check_cookie($name,$pwd){
 	global $db;
 	$row = $db->getone("SELECT COUNT(*) AS num FROM ".table('members')." WHERE username='{$name}' and password = '{$pwd}'");
 	if($row['num'] > 0)
	{
 	return true;
 	}else{
 	return false;
 	}
 }
 /**
  *
  * 更新用户信息
  *
  *
  */
 function update_user_info($uid,$record=true,$setcookie=true,$cookie_expire=NULL)
 {
 	global $timestamp, $online_ip,$db,$QS_cookiepath,$QS_cookiedomain;
	$user = get_user_inid($uid);
	if (empty($user))
	{
	return false;
	}
	else
	{
 	$_SESSION['uid'] = intval($user['uid']);
 	$_SESSION['parentuid'] = intval($user['parentuid']);
 	$_SESSION['username'] = $user['username'];
	$_SESSION['utype']=intval($user['utype']);
	}
	if ($setcookie)
	{
		$expire=intval($cookie_expire)>0?time()+3600*24*$cookie_expire:0;
		setcookie('QS[uid]',$user['uid'],$expire,$QS_cookiepath,$QS_cookiedomain);
		setcookie('QS[username]',$user['username'],$expire,$QS_cookiepath,$QS_cookiedomain);
		setcookie('QS[password]',$user['password'],$expire,$QS_cookiepath,$QS_cookiedomain);
		setcookie('QS[utype]',$user['utype'], $expire,$QS_cookiepath,$QS_cookiedomain);
	}
	if ($record)
	{
		$last_login_time = $timestamp;
		$last_login_ip = $online_ip;
		$sql = "UPDATE ".table('members')." SET last_login_time = '$last_login_time', last_login_ip = '$last_login_ip' WHERE uid='{$_SESSION[uid]}'  LIMIT 1";
		$db->query($sql);
		if ($_SESSION['utype']=="1")
		{
			$rule=get_cache('points_rule');
			if ($rule['company_login']['value']>0 )
			{
				$time=time();
				$today=mktime(0, 0, 0,date('m'), date('d'), date('Y'));
				$info=$db->getone("SELECT uid FROM ".table('members_handsel')." WHERE uid ='{$_SESSION['parentuid']}' AND htype='userlogin' AND addtime>{$today}  LIMIT 1");
				if(empty($info))
				{				
				$db->query("INSERT INTO ".table('members_handsel')." (uid,htype,points,addtime) VALUES ('{$_SESSION['parentuid']}', '{$rule['company_login']['value']}','userlogin','{$time}')");
				require_once(QISHI_ROOT_PATH.'include/fun_company.php');
				report_deal($_SESSION['parentuid'],$rule['company_login']['type'],$rule['company_login']['value']);
				$user_points=get_user_points($_SESSION['parentuid']);
				$operator=$rule['company_login']['type']=="1"?"+":"-";
				$_SESSION['handsel_userlogin']=$operator.$rule['company_login']['value'];
				write_memberslog($_SESSION['uid'],1,9101,$_SESSION['username'],"积分：".$operator.$rule['company_login']['value'].",剩余：{$user_points}",$_SESSION['parentuid'],"每日首次登录",1);
				}
			}
		}
		elseif($_SESSION['utype']=="3")
		{
			//积分
			$rule=get_cache('points_rule');
			if ($rule['hunter_login']['value']>0 )
			{
				$time=time();
				$today=mktime(0, 0, 0,date('m'), date('d'), date('Y'));
				$info=$db->getone("SELECT uid FROM ".table('members_handsel')." WHERE uid ='{$_SESSION['uid']}' AND htype='userlogin' AND addtime>{$today}  LIMIT 1");
				if(empty($info))
				{				
				$db->query("INSERT INTO ".table('members_handsel')." (uid,htype,points,addtime) VALUES ('{$_SESSION['uid']}', 'userlogin',{$rule['hunter_login']['value']},'{$time}')");
				require_once(QISHI_ROOT_PATH.'include/fun_hunter.php');
				report_deal($_SESSION['uid'],$rule['hunter_login']['type'],$rule['hunter_login']['value']);
				$user_points=get_user_points($_SESSION['uid']);
				$operator=$rule['hunter_login']['type']=="1"?"+":"-";
				$_SESSION['handsel_userlogin']=$operator.$rule['hunter_login']['value'];
				write_memberslog($_SESSION['uid'],1,9101,$_SESSION['username'],"积分：".$operator.$rule['hunter_login']['value'].",剩余：{$user_points}",0,"每日首次登录",1);
				}
			}
			// 经验
			$levelRule=get_cache('hunter_level_rule');
			if ($levelRule['login_first']['value']>0 )
			{
				$time=time();
				$today=mktime(0, 0, 0,date('m'), date('d'), date('Y'));
				$info=$db->getone("SELECT uid FROM ".table('members_handsel_level')." WHERE uid ='{$_SESSION['uid']}' AND htype='userlogin' AND addtime>{$today}  LIMIT 1");
				if(empty($info))
				{				
				$db->query("INSERT INTO ".table('members_handsel_level')." (uid,htype,level,addtime) VALUES ('{$_SESSION['uid']}', 'userlogin',{$levelRule['login_first']['value']},'{$time}')");
				require_once(QISHI_ROOT_PATH.'include/fun_hunter.php');
				level_deal($_SESSION['uid'],$levelRule['login_first']['type'],$levelRule['login_first']['value']);
				level_meal_deal($_SESSION['uid']);
				$user_level=get_hunter($_SESSION['uid']);
				$operator=$levelRule['login_first']['type']=="1"?"+":"-";
				$_SESSION['handsel_level_userlogin']=$operator.$levelRule['login_first']['value'];
				write_memberslog($_SESSION['uid'],3,1402,$user['username'],"经验：".$operator.$levelRule['login_first']['value'].",剩余：{$user_level[level]}",0,"每日首次登录",1);
				}
			}
			$hunter_sql=" select * from ".table("hunter_profile")." where uid='{$_SESSION[uid]}' LIMIT 1 ";
			$hunter=$db->getone($hunter_sql);
			if($hunter)
			{
				$sql = "UPDATE ".table('hunter_profile')." SET last_login_time = '$last_login_time' WHERE uid='{$_SESSION[uid]}'  LIMIT 1";
				$db->query($sql);
			}
		}
	}
	return true;
 }
function get_user_inemail($email,$utype="")
{
	global $db;
	if($utype!=""){
		$wheresql=" and utype={$utype} ";
	}
	return $db->getone("select * from ".table('members')." where email = '{$email}'".$wheresql."LIMIT 1");
}
function get_user_inusername($username,$utype="")
{
	global $db;
	if($utype!=""){
		$wheresql=" and utype={$utype} ";
	}
	$sql = "select * from ".table('members')." where username = '{$username}'".$wheresql."LIMIT 1";
	return $db->getone($sql);
}
function get_user_inid($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('members')." where uid = '{$uid}' LIMIT 1";
	return $db->getone($sql);
}
function get_user_inmobile($mobile,$utype="")
{
	global $db;
	if($utype!=""){
		$wheresql=" and utype={$utype} ";
	}
	$sql = "select * from ".table('members')." where mobile = '{$mobile}'".$wheresql."LIMIT 1";
	return $db->getone($sql);
}
function get_user_inqqopenid($openid)
{
	global $db;
	if (empty($openid))
	{
	return false;
	}
	$sql = "select * from ".table('members')." where qq_openid = '{$openid}' LIMIT 1";
	return $db->getone($sql);
}
//获取随机字符串
function randstr($length=6)
{
$hash='';
$chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#!~?:-='; 
$max=strlen($chars)-1;   
mt_srand((double)microtime()*1000000);   
for($i=0;$i<$length;$i++)   {   
$hash.=$chars[mt_rand(0,$max)];   
}   
return $hash;   
}
//修改密码
function edit_password($arr,$check=true)
{
	global $db,$QS_pwdhash;
	if (!is_array($arr))return false;
	$user_info=get_user_inusername($arr['username']);
	$pwd_hash=$user_info['pwd_hash'];
	$password=md5(md5($arr['oldpassword']).$pwd_hash.$QS_pwdhash);
	if ($check)
	{
		$row = $db->getone("SELECT * FROM ".table('members')." WHERE username='{$arr['username']}' and password = '{$password}' LIMIT 1");
		if(empty($row))
		{
			return -1;
		}
	}
	$md5password=md5(md5($arr['password']).$pwd_hash.$QS_pwdhash);	
	if ($db->query( "UPDATE ".table('members')." SET password = '$md5password'  WHERE username='".$arr['username']."'")) return $arr['username'];
	write_memberslog($_SESSION['uid'],$_SESSION['utype'],1004,$_SESSION['username'],"修改了密码");
	return false;
}
function get_loginlog_one($uid,$type)
{
	global $db;
	$result = $db->getone("SELECT * FROM ".table('members_log')." WHERE log_uid={$uid} AND log_type={$type} ORDER BY log_id DESC LIMIT 1,1");
	return $result;
}
//得到会员信息
function get_user_info($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('members')." where uid = '{$uid}' LIMIT 1";
	return $db->getone($sql);
}
?>