<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：phpwms.member.php
 * 摘　　要：会员
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS')) {exit('phpwms');}
class member_WMS extends base{
	protected function init(){
		parent::init();
	}
	function wmsAction(){
		$this->landingAction();
	}
	function ajaxLoginAction(){
		if($this->isPost()){
		$post = html_encode($_POST);
		$flag = $post['flag'];
		if($flag!='no_check'){
			$code = $post['codenumber'];
			if(empty($code) || !$this->check_code($code))exit('ERROR_CODE');
		}
		$name = $post['landingusername'];
			if (strlen ( $name ) < 4 || ! is_chekChar ( $name )) {
				exit('ERROR_UNAME');
			}
			// 验证密码            得到密码
			$password = $post['password'];
			// 查询 语句
			$password = md5 ( $password );
			$sql = "SELECT * FROM ##__user_info WHERE `login_name` = '$name' AND `login_pwd` = '$password'";
			$result = $this->db->fetch($sql, true);
			if(empty($result))exit('ERROR_U_OR_PASS');
			unset($result['login_pwd']);
			$_SESSION['phpwmscuruser'] = $result;
			$_SESSION['landing']  = '';
			// 更新用户信息
			$time = time();
			$login_ip = get_ip();
			$sql = "UPDATE ##__user_info SET `login_ip` = '$login_ip', `total_login` = total_login + 1, `login_time` = '$time'";
			$this->db->query($sql);
			exit('OK');
		}else{
			@header("Content-type: text/html;charset=gb2312");
			$this->display('member/member_login_panel.php');
		}
	}
	function registerAction(){
		return $this->loginAction();
	}
	private function _check(){
		$url = building_url('member@membermanage','manage');
		if(!empty($_SESSION['phpwmscuruser']))header("location:$url");
	}
	public function loginAction(){
		$this->_check();
		// 跳转到注册页面;
		$this->setSeo(array('keywords'=>'会员注册', 'title'=>'会员注册'));
		if($this->isPost()){
			$post = html_encode($_POST);
			$_SESSION['login'] = $post;//放到  session里
			$lgoin_url = building_url('member', 'login', array(), true, false);
			if(empty($post['agreement'])){
				$this->bgmsg('您必须同意本公司条款才可以注册',$lgoin_url);
			}
			// 得到判断验证码是否正确
			if(empty($post['codenumber']) || !$this->check_code($post['codenumber'])){
				$this->bgmsg('验证码不正确，请重新填写',$lgoin_url);
			}
			$membername = $post['username'];
			if(strlen($membername) < 4 || !is_chekChar($membername)){
				$this->bgmsg('用户名不符合要求，请重新填写',$lgoin_url);
			}
			$num = $this->_check_member_get_num($membername);
			if(!empty($num)){
				$this->bgmsg('用户名已被占用，请重新填写',$lgoin_url);
			}
			$password = $post['password'];
			if(strlen($password) < 6){
				$this->bgmsg('密码不正确，请重新填写',$lgoin_url);
			}
			$repassword = $post['repass'];
			if($password != $repassword){
				$this->bgmsg('确认密码和密码不正确，请重新填写',$lgoin_url);
			}
			$email = $post['email'];
			if(empty($email) || !is_email($email)){
				$this->bgmsg('电子邮箱不正确，请重新填写',$lgoin_url);
			}
			$num = $this->_check_member_get_num($email, 'email');
			if(!empty($num)){
				$this->bgmsg('电子邮箱以被人使用，请重新填写',$lgoin_url);
			}
			//开始保存到 数据库
			$time = time();
			$data = array(
				'login_name' => $membername,
				'login_pwd'=> md5($password),
				'email'=>$email,
				'group_id'=>'1',
				'login_time'=>$time,
				'login_ip'=>get_ip(),
				'reg_time'=>$time,
			);
			$this->db->insert($data, 'user_info');
			unset($data['login_pwd']);
			$sql = "SELECT * FROM ##__user_info where `login_name` = '$membername'";
			$data = $this->db->fetch($sql, true);
			$_SESSION['phpwmscuruser'] = $data;
			$_SESSION['login'] = '';
			// 发信
			$site_link ='http://'.$_SERVER['SERVER_NAME'].get_self(true);
			$cache = get_cache(array('siteBase'));
			$senarray = array('user_name'=>$membername, 'site_link'=>$site_link, 'password'=>$password, 'site_name'=>$cache['sys_name']);
			sendSetMail('reg_notice', $senarray, $email);
			$this->bgmsg('恭喜您注册成功', building_url('member@membermanage','manage', array(), true, false));
		}else{
			$this->display('member/member.php');
		}
	}
	/**
	 * 用户登录
	 *
	 */
	public function landingAction(){
		$this->_check();
		$this->setSeo(array('keywords'=>'会员登陆', 'title'=>'会员登陆'));
		if($this->isPost()){
			$landling_url = building_url('member', 'landing', array(), true, false);
			// 得到 用户名
			$post = html_encode($_POST);
			$_SESSION['landing'] = $post;
			// 验证码
			if(empty($post['codenumber']) || !$this->check_code($post['codenumber'])){
				$this->bgmsg('验证码不正确，请重新填写',$landling_url);
			}
			$name = $post['landingusername'];
			if (strlen ( $name ) < 4 || ! is_chekChar ( $name )) {
				$this->bgmsg('用户名不符合要求，请重新填写',$landling_url);
			}
			// 验证密码            得到密码
			$password = $post['password'];
			if (strlen ( $password ) < 6) {
				$this->bgmsg('密码不符合要求，请重新填写',$landling_url);
			}
			// 查询 语句
			$password = md5 ( $password );
			$sql = "SELECT * FROM ##__user_info WHERE `login_name` = '$name' AND `login_pwd` = '$password'";
			$result = $this->db->fetch($sql, true);
			if(empty($result)){
				$this->bgmsg('用户名或密码错误，请重新填写',$landling_url);
			}
			unset($result['login_pwd']);
			$_SESSION['phpwmscuruser'] = $result;
			$_SESSION['landing']  = '';
			// 更新用户信息
			$time = time();
			$login_ip = get_ip();
			$sql = "UPDATE ##__user_info SET `login_ip` = '$login_ip', `total_login` = total_login + 1, `login_time` = '$time'";
			$this->db->query($sql);
			$this->bgmsg('恭喜您登陆成功', building_url('member@membermanage','manage', array(), true, false));
		}else{
			$this->display('member/member.php');
		}
	}
	/**
	 * 找回密码
	 *
	 */
	public function findpassAction(){
		$this->setSeo(array('title'=>'-找回密码'));
		$find_url = building_url('member', 'findpass');
		if($this->isPost()){
			$post = html_encode($_POST);
			// 验证码
			if(empty($post['code']) || !$this->check_code($post['code'])){
				$this->bgmsg('验证码不正确，请重新填写',$find_url);
			}
			$username = $post['username'];
			if (strlen ( $username ) < 4 || ! is_chekChar ( $username )) {
				$this->bgmsg('用户名不符合要求，请重新填写',$find_url);
			}
			$email = $post['email'];
			if(empty($email) || !is_email($email)){
				$this->bgmsg('电子邮箱不正确，请重新填写',$find_url);
			}
			$sql = "SELECT `email` FROM ##__user_info WHERE `login_name` = '$username' AND `email` = '$email'";
			$result = $this->db->fetch($sql, true);
			if(empty($result) || count($result) == 0){
				$this->bgmsg('您输入的用户名或邮箱不正确,请您在重试。',$find_url);
			}
			// 随机生成 一个 字符串 并发送 密码
			$sendpassword = strtolower(random(8));
			$newpass = md5($sendpassword);
			$sql = "UPDATE ##__user_info SET `login_pwd` = '$newpass' WHERE `login_name` = '$username'";
			if($this->db->query($sql)){
				$senarray = array('user_name'=>$username, 'newpassword'=>$sendpassword);
				if(sendSetMail('reset_pwd', $senarray, $email)){
					$this->bgmsg('已将您的新密码发送到邮箱里，请您及时查收',building_url('member', 'landing'));
				}else{
					$this->bgmsg('发送失败！请您及时与管理员联系',$find_url);
				}
			}else{
				$this->bgmsg('系统修改您的密码失败，请您重试，给您带来不便，请谅解！。',$find_url);
			}
		}else{
			$this->display('member/findpass.php');
		}
	}
	/**
	 * 注册协议
	 *
	 */
	public function registeragreeAction(){
		$this->setSeo(array('title'=>'-用户注册'));
		$this->tpl->assign('agreement', get_cache(array('siteBase', 'sys_agreement')));
		$this->display('member/agreement.php');
	}
	/**
	 * 检查数据库中会员表是否含有此名称并返回含有的个数
	 * @param string 值
	 * @param string 列名 默认为 查看用户名
	 */
	private function _check_member_get_num($val, $col_type = 'login_name'){
		if(empty($val)){return false;}
		$col_type = empty($col_type) ? 'login_name' : $col_type;
		$sql = "SELECT COUNT(*) as count FROM ##__user_info WHERE `$col_type` = '$val' limit 0,1 ";
		$result = $this->db->fetch($sql, true);
		return $result['count'];
	}
}
?>