<?php
/*
用户控制面板控制器
*/
class MemberController extends Easy_Controller_Action {
	public function Init(){
		$this->_View->setViewRoot('templates');
		$this->_View->skin = $this->config['template'];
		$this->_View->Template = new Template();	//
		$this->_User = new UsersModel();
		$this->uid = (int)$this->_Request->getCookie('uid');
		$this->_View->temporary['config'] = $this->config;
		$filter = array('login', 'logoff','register', 'check', 'lostpass', 'help', 'validate','online','exit');
		if (in_array($this->_Request->getParam('action'), $filter)===false){
			$this->receive_url = $this->config['domain'].INSTALLDIR.'member/receive/';
			$this->_View->user = $this->_User->login(array('uid'=>(int)$this->uid));
			if (!$this->_View->user['uid']){
				exit($this->_View->render('public/validate'));
			}
			
		}
	}
	public function registerAction(){
		if ($this->_Request->isPost()){//注册成功
			$filename = ROOT.'Config/user.xml';
			$xml = new Easy_Config();
			$xml->set($filename);
			$POST = $this->_Request->getPost();
			$POST['username'] = trim($this->_Request->getPost('username'));
			$POST['password'] = $this->_Request->getPost('password');
			$POST['passwd'] = $this->_Request->getPost('passwd');
			$POST['usergroup'] = (int)$xml->usergroup;
			$POST['logins'] = 1;
			$POST['playtime'] = time();
			$POST['locked'] = 0;
			$POST['gender'] = $this->_Request->getPost('gender');
			$POST['email'] = $this->_Request->getPost('email');
			$POST['usertype'] = (int)$xml->usertype;
			$POST['point'] = (int)$xml->point;
			if (strtotime((string)$xml->days)===false){
				$POST['lifetime'] = time()+(86400*(int)$xml->days);
			}else{
				$POST['lifetime'] = strtotime((string)$xml->days);
			}
			$POST['lifetime'] = date('Y-m-d H:i:s', $POST['lifetime']);
			unset($POST['sub'], $POST['sub_x'], $POST['sub_y']);
			$uid = $this->_User->add($POST);
			if ($uid===false){
				echo '<script>alert("'.$this->_User->error.'");history.back();</script>';
				exit;
			}
			$this->_Request->setCookie('username', $POST['username']);
			$this->_Request->setCookie('uid', $uid);
			$this->_Request->setCookie('usergroup', $POST['usergroup']);
			$this->_View->body = $this->_View->render('public/register_2');
		}else{
			$this->_View->body = $this->_View->render('public/register_1');
		}
		exit($this->_View->render('public/register'));
	}
	public function indexAction(){
		$this->editmyselfAction();
	}
	public function CheckAction(){
		header('Content-type: application/x-javascript');
		$username = $this->_Request->getParam('username');
		$useremail = $this->_Request->getParam('email');
		if($username){
			if ($this->config['ucenter'] == 1){
				$ucenter = new Ucenter();
				if ($ucenter->checkusername('username', $username)===false){
					exit('false');
				}
			}
			if ($this->_User->exists(array('username'=>
				$this->_Request->getParam('username')))===false){
				exit('false');
			}else{
				exit('true');
			}
		}elseif ($useremail){	//验证email
			if ($this->config['ucenter'] == 1){
				$ucenter = new Ucenter();
				if ($ucenter->checkusername('email', $useremail)===false){
					exit('false');
				}
			}
			if ($this->_User->exists(array('email'=>
			$this->_Request->getParam('email')))===false){
				exit('false');
			}else{
				exit('true');
			}
		}elseif ($this->_Request->getParam('pass')){
			$where['uid'] = $this->uid;
			$where['password'] = md5(strtolower($this->_Request->getParam('pass')));
			if ($this->_User->exists($where)===false){
				exit('true');
			}else{
				exit('false');
			}
		}
		exit;
	}
	/*登陆窗口*/
	public function loginAction(){
		$bar = new NetbarModel();
		$uid = intval($this->_Request->getCookie('uid'));
		$userip = $this->_Request->strUserIp();
		//echo $uid;
		if ($this->_Request->isPost()){
			$playlog = new PlaylogModel();
			$array = null;
			$array['username'] = trim($this->_Request->getPost('username'));
			$array['password'] = md5(strtolower($this->_Request->getPost('password')));
			$referer = $this->_Request->getPost('uri');
			$row = $this->_User->login($array);
			$tmp = $playlog->isOnline($array['username']);
			if ($row['uid'] ){
				//print_r($this->_Request->getCookie());
				//exit;
				if ($tmp && ($this->_Request->getCookie('online') != $tmp['sid'])){
					message('您的登陆名已经是登陆状态,请不要多人使用同一账户!\n如果是您上次未正常退出,请在'.(int)($this->config['refreshonline']/60).'分钟后在登陆','history.back()');
					exit;
				}
				$this->_Request->setCookie('username', $row['username']);
				$this->_Request->setCookie('uid', $row['uid']);
				$this->_Request->setCookie('usergroup', $row['usergroup']);
				$this->_Request->setCookie('point', $row['point']);
				/*写入用户到在线用户列表*/
				$array = array();
				$array['username'] = $row['username'];
				$array['sid'] = session_id();
				$array['userip'] = $this->_Request->strUserip();
				$array['firstime'] = time();
				$array['lastime'] = time();
				$this->_User->setLoginTime($row['uid']);////更新用户最后登录时间
				$playlog->setSession($array);	//write online session
				$this->_Request->setCookie('online', $array['sid'], 86400);
				unset($array);
				if ($referer){
					header('location:'.base64_decode($referer));
				}else{
					echo '<script>history.back();</script>';
					exit;
					
				}
			}else{
				message('用户名或者密码不正确!','history.back()');
				exit;
			}
		}else {
			$row = $this->_User->login(array('uid'=>$uid), 0);
			if ($row['uid']){
				$row['groupname'] = $row['groupname'] ? $row['groupname'] : _('游客');
				$playlog = new PlaylogModel();
				$log = $playlog->getRow("a.username='".$row['username']."'");	//获取用户上次观看
				$row['movie'] = '<a href="'.INSTALLDIR.'content/view/movid/'.$log['movid'].'/" target="_blank">'.truncate($log['movie'],10).'</a>';
				if ($row['usergroup'] == $this->config['vip']){
					$vip = '<a href="'.INSTALLDIR.'content/vip/" target="_blank">秘密通道</a>';
					//echo 'd';
				}
				$this->_View->user = $row;
				$this->_View->user['vip'] = $vip;
				if ($row['usertype'] == 1){
					if ($row['lifetime']<=time()){	//包月到期时
						if ($row['point']>0){	//到期但是有影币
							$this->_View->user['state'] = _($row['point'].'点');
						}else{
							$this->_View->user['state'] = _('已到期');
						}
					}else{
						$this->_View->user['state'] = ceil(($row['lifetime'] - time())/86400)._('天');
					}
				}else{
					if ($row['point']<1){
						if ($row['lifetime'] > time()){
							$this->_View->user['state'] = ceil(($row['lifetime'] - time())/86400)._('天');
						}else{
							$this->_View->user['state'] = intval($row['point'])._('点').',<a href="'.INSTALLDIR.'member/payment/" target="_blank">'._('请充值').'</a>';
						}
					}else{
						$this->_View->user['state'] = _($row['point'].'点');
					}
				}
				exit($this->_View->render($this->_View->skin.'/login_succeed'));
			}else{	//非登录模式
				$this->_View->netbar =$bar->isAllow($userip);
				$this->_View->netbar['groupname'] = $group[$this->_View->netbar['usergroup']]['title'];
				if ($this->_View->netbar['id']){
					$this->_View->netbar['days'] = ceil(($this->_View->netbar['lifetime']-time())/86400).'天';
					$group = unserialize(Easy_Cache::get('usergroup'));
					$this->_Request->setCookie('username', $this->_View->netbar['name']);
					exit($this->_View->render($this->_View->skin.'/login_netbar'));
				}elseif($this->config['auth'] !=1){
					if ($this->config['auth'] == 2){	//时间段免费
						$freetime = explode('/', $this->config['freetime']);
						if (date('H')>$freetime[0] || date('H')<$freetime[1]){	//在时间段内
							$this->_View->free['freetime'] = $freetime[0].'点至'.$freetime[1].'点';
							exit($this->_View->render($this->_View->skin.'/login_free'));
						}else{
							exit($this->_View->render($this->_View->skin.'/login'));
						}
					}else{	//免登录模式
						exit($this->_View->render($this->_View->skin.'/login_free'));
					}
				}else{
					exit($this->_View->render($this->_View->skin.'/login'));
				}
			}
		}
	}
	/*注销用户*/
	public function logoffAction(){
		if ($this->config['ucenter'] == 1 && $row['uid']){
				$ucenter = new Ucenter();
				echo $ucenter->synlogout();
		}
		$this->_Request->setCookie('username', null);
		$this->_Request->setCookie('uid', null);
		$this->_Request->setCookie('group', null);
		$playlog = new PlaylogModel();
		$playlog->delSession($this->_Request->getCookie('online'));
		$referer = $_SERVER['HTTP_REFERER'];
		$this->_View->redirect('member/login');
		exit;
	}
	/**/
	public function editmyselfAction(){
		$member = new MemberModel();
		if ($this->_Request->isPost()){
			$POST = $this->_Request->getPost();
			$POST['uid'] = $this->uid;
			$member->editmyself($POST);
			message(_('修改资料成功!'), 'history.back()');
			exit;
		}else{
			$row = $this->_View->user;
			$this->_view->group = unserialize(Easy_Cache::get('usergroup'));
			$row['maxdown'] = $this->_view->group[$row['usergroup']]['maxdown'];
			$row['maxip'] = $this->_view->group[$row['usergroup']]['maxip'];
			$row['maxdata'] = $this->_view->group[$row['usergroup']]['maxdata'];
			$this->_View->modify = $row;
			unset($row);
			$this->_View->rightBody = $this->_View->render('member/editmyself');
			exit($this->_View->render('member/index'));
		}
	}
	/*
	修改登陆密码
	*/
	public function  modifypassAction(){
		if ($this->_Request->isPost()){
			$member = new MemberModel();
			$POST = $this->_Request->getPOST();
			$POST['uid'] = $this->uid;
			if ($member->modfypass($POST)===false){
				message($member->error, 'history.back()');
			}else{
				message(_('修改成功!'), 'history.back()');
			}
			exit;
		}
		$this->_View->rightBody = $this->_View->render('member/modifypass');
		exit($this->_View->render('member/index'));
	}
	//**/
	public function paymentAction(){
		$case = new CaseModel();
		$step = (int)$this->_Request->getParam('step');
		$this->_View->payvia = (int)$this->_Request->getParam('payvia') ? (int)$this->_Request->getParam('payvia'): 1;
		$this->_View->payvias = array(
		'1' => '银行卡支付',
		'2' => '声讯电话支付',
		'3' => '互联星空支付',
		'4' => '手机短信注册',
		'5' => '腾讯财付通支付',
		'6' => '腾讯Q币支付',
		'7' => '神州行充值卡支付',
		'8' => '盛大游戏点卡支付',
		'9' => '支付宝支付',
		'10' => '手机银行支付',
		'11' => '北京宽带支付',
		'12' => '联通充值卡支付',
		);	//支付的全部途径
		if ($step == 2){	//提交支付页面
			$id = (int)$this->_Request->getPost('id');	//购买商品ID
			$this->_View->shopinfo = $case->getRow($id);
			$group = unserialize(Easy_Cache::get('usergroup'));
			$this->_View->shopinfo['groupname'] = $group[$this->_View->shopinfo['usergroup']]['title'];
			//生成订单
			$this->_View->shopinfo['orderno'] = date('YmdHis');
			$this->_View->shopinfo['username'] = $this->_View->user['username'];
			$numeric = $case->makeOrder($this->_View->shopinfo);	//生成订单返回订单号码
			$this->_View->shopinfo['inserid'] = $numeric;
			$this->_View->receive_url = $this->receive_url;
			$this->_View->payvia = $this->_Request->getPost('payvia') ? $this->_Request->getPost('payvia') : 1;
			$this->_View->paymode='testmode';
			$this->_View->ymd = date('Ymd');
			$this->_View->reg_key = $this->config['reg_key'];
			$this->_View->custom1 = $numeric;
			$this->_View->custom2 = md5($numeric.$this->_View->reg_key);
			//拼凑串里尽量不要使用中文字符，目前MD5在ASP、asp.net、java等环境下对中文处理可能有偏差现象
			//拼凑串为：商户编号+amount+ymd+receive_url+billno+custom1+custom2+payvia+商户密钥+paymode
			$post_key=$this->config['reg_userid'].($this->_View->shopinfo['money']*100).
			$this->_View->ymd.$this->_View->receive_url.$this->_View->shopinfo['orderno'].
			$this->_View->custom1.$this->_View->custom2.$this->_View->payvia.
			$this->_View->reg_key.$this->_View->paymode;
			//strtoupper函数是将字符转换为大写
			//贺喜支付平台支付接口对MD5值只认大写字符串，所以小写的MD5值得转换为大写
			//echo $post_key;
			$this->_View->post_md5info=strtoupper(md5($post_key));
			if ($this->_View->shopinfo['usertype'] == 1){
				$this->_View->shopinfo['services'] = '购买'.$this->_View->shopinfo['days'].'天'.$this->_View->shopinfo['groupname'].'会员';
			}else{
				$this->_View->shopinfo['services'] = '购买'.$this->_View->shopinfo['point'].'点'.$this->_View->shopinfo['groupname'].'会员';
			}
			$this->_View->rightBody = $this->_View->render('member/sendpay');
			exit($this->_View->render('member/index'));
		}
		
		$this->_View->shop = $case->getAll();
		$this->_View->rightBody = $this->_View->render('member/payment');
		exit($this->_View->render('member/index'));
	}
	/*支付成功后验证接受开通*/
	public function receiveAction(){
		$v_orderid=$_REQUEST['v_orderid'];   //贺喜支付平台反馈的定单号
		$v_orderdate=$_REQUEST['v_orderdate'];   //定单成交时间
		$v_price=$_REQUEST['v_price'];   //定单成交价格，以分为单位
		$v_billno=$_REQUEST['v_billno'];   //商户订单号
		$v_custom1=$_REQUEST['v_custom1'];   //自定义字段1
		$v_custom2=$_REQUEST['v_custom2'];   //自定义字段2
		$v_mobileno=$_REQUEST['v_mobileno'];   //用户支付的手机号码,仅手机短信注册时有效
		$v_servicename=$_REQUEST['v_servicename'];   //支付订单的服务名称
		$v_payvia=$_REQUEST['v_payvia'];   //用户选择的支付途径，1：银行卡在线支付；2：声讯电话支付；3：互联星空支付；4：手机短信注册；5：腾讯财付通支付；6：腾讯Q币支付；7：神州行充值卡支付；8，盛大游戏点卡支付；9，支付宝支付；10，手机银行支付；11，北京宽带支付；12，联通充值卡支付
		$v_payviastr=$_REQUEST['v_payviastr'];   //用户选择的支付途径中文名称
		$v_md5=$_REQUEST['v_md5'];   //返回的加密字符串
		$v_pstatus=$_REQUEST['v_pstatus'];   //定单成功反馈的字符串，有1和0两种情况。主要用于验证是否属正常反馈。1为成功。
		$v_pstring=$_REQUEST['v_pstring'];   //定单支付状态说明。
		$v_paymode=$_REQUEST['v_paymode'];   //当前定单运行模式，仅启用测试模式时有效，值为testmode。
		$v_server=$_REQUEST['v_server'];   //当前定单通知的模式，1为服务器端通知。
		$v_custom2 = md5($v_custom1.$this->config['reg_key']);
		$encrypt_string=strtoupper(md5($v_orderid.$v_billno.$v_custom1.$v_custom2.$v_price.$v_payvia.$this->config['reg_userid'].$v_pstatus.$this->config['reg_key'].$v_paymode));
		//echo $post_key;
		if ((int)$v_pstatus == 1){	//返回状态为成功时
			if ($encrypt_string != $v_md5){
				//echo '<br>'.$v_md5;
				exit('签名验证失败!');
			}else{
				$case = new CaseModel();
				$case->v_payvia = $v_payviastr;
				$case->changeState($v_custom1);
				echo '制服超过!';
			}
		}
		exit();
	}
	/*影卡*/
	public function carAction(){
		if ($this->_Request->isPost()){
			$car = new CarModel();
			if ($car->changeState($this->_Request->getPost())===false){
				$this->_View->message = '<script>alert("'.$car->error.'");</script>';
			}else{
				$this->_View->message = '<script>alert("'._('充值成功!').'");</script>';
			}
		}
		$this->_View->rightBody = $this->_View->render('member/car');
		exit($this->_View->render('member/index'));
	}
	/*orderfromAction*/
	public function orderfromAction(){
		$case = new CaseModel();
		$orderby = $this->_Request->getParam('by') ?  $this->_Request->getParam('by') : 'a.id';
		$state = $this->_Request->getParam('state');
		$where = "a.username='".$this->_View->user['username']."'";
		if (isset($state)){
			$where.= ' AND a.state='.$state;
		}
		$this->_View->rows = $case->getCase($where, $orderby);
		$this->_View->printPage = $case->printPage;
		$this->_View->rightBody = $this->_View->render('member/orderfrom');
		exit($this->_View->render('member/index'));
	}
	/*playlogAction*/
	public function playlogAction(){
		$play = new PlaylogModel();
		$where = "a.username='".$this->_View->user['username']."'";
		$this->_View->rows = $play->getAll($where);
		$this->_View->printpage = $play->printPage;
		$this->_View->rightBody = $this->_View->render('member/playlog');
		exit($this->_View->render('member/index'));
	}
	public function validateAction(){
		if (base64_decode($this->_Request->getParam('uri'))){
			$this->_View->uri = $this->_Request->getParam('uri');
		}else{
			$uri = $_SERVER['HTTP_REFERER'];
			if (stripos($uri, 'login')!==false){
				$uri = INSTALLDIR;
			}
			$this->_View->uri = base64_encode($uri);
		}
		exit($this->_View->render('public/validate'));
	}
	public function lostpassAction(){
		if($this->_Request->isPost()){
			$userid = $this->_Request->getPost('userid');
			if (stripos($userid,'@') !==false){
				$row = $this->_User->getRow(array('email'=> $userid));
			}else{
				$row = $this->_User->getRow(array('username'=> $userid));
			}
			if (!$row['uid']){
				$this->_View->message = _('用户名或者邮箱地址不存在!');
			}else{
				$message = file_get_contents(ROOT.'Config/lostpass.htm');
				$message  = str_replace('[email]', $row['email'] , $message);
				$message  = str_replace('[username]', $row['username'], $message);
				$message  = str_replace('[lastime]', date('Y-m-d H:i:s', $row['lastime']), $message);
				$uri = 'http://'.$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$this->config['basedir'].'member/lostpass/do/reset/uid/'.$row['uid'].'/pkey/'.md5($row['uid'].date('Y-m-d').$this->config['sitename']).'/';
				$message = str_replace('[resetpass]', $uri, $message);
				$subject = _('密码找回服务');
				$email = $row['email'];
				$mail = new Mail();
				$mail->sendMail($email, $subject, $message);
				$this->_View->message = _('密码重设信息已经发送到您的邮箱'.$email.'请进入邮箱后立即重设密码!');
			}
			$this->_View->rightBody = $this->_View->render('public/lostpass2');
		}else{
			$this->_View->rightBody = $this->_View->render('public/lostpass1');
		}
		exit($this->_View->render('public/lostpass'));
	}
	/*更新用户在线*/
	public function onlineAction(){
		header('Content-type: application/x-javascript');
		$sid = $this->_Request->getCookie('online');
		if ($sid){
			$Session = new PlaylogModel();
			$Session->upSession($sid);
		}
		exit;
	}
	
}?>