<?php
$nowpath = "<a href='{$pe['host_root']}'>首页</a> > 会员中心";
switch($act) {
	//#####################@ 用户登录sql @#####################//
	case 'loginsql':
		
		$_p_login['user_name'] = pe_dbhold($_p_login['user_name']);
		$_p_login['user_pw'] = md5($_p_login['user_pw']);
		$sqlwhere = " and (`user_name` = '{$_p_login['user_name']}' or `user_email` = '{$_p_login['user_name']}') and `user_pw` = '{$_p_login['user_pw']}'";

		if ($info = $db->pe_select('user', $sqlwhere)) {
			if ($info['user_isactive'] == 0) {
				pe_goto("user.php?mod=do&act=registersql&type=emailactive");
			}
			//自动升级vip
			$ips=10;//自动升级vip的推广ip数
			$usernum=2;//自动升级vip的推广注册用户数
			if($info['vip']==0){
				if($info['user_tjnum']>=$ips){
					if($info['tj_user_num']>=$usernum){
						$db->pe_update('user', array('user_id'=>$info['user_id']), array('vip'=>1));
						echo "<script>alert('您的推广的ip和注册用户下线达到要求，自动升级为vip用户！')</script>";
					}
					else{
						echo('<script>alert("你推广的ip质量不高，系统监测，怀疑有作弊嫌疑，请继续努力！");</script>');
					}
				}
			}
			$db->pe_update('user', array('user_id'=>$info['user_id']), array('user_ltime'=>time()));
			$_SESSION['user_id'] = $info['user_id'];
			$_SESSION['user_name'] = $info['user_name'];
			$_SESSION['user_tjlevel'] = $info['user_tjlevel'];
			$_SESSION['user_tjnum'] = $info['user_tjnum'];
			$_SESSION['package_id'] = array();
			$packagelog = $db->pe_selectall('packagelog', array('user_id'=>$info['user_id'], 'packagelog_state'=>1));
			foreach ($packagelog as $v) {
				$_SESSION['package_id'][] = $v['package_id'];
			}
			pe_success('用户登录成功！', "{$pe['host_root']}user.php");
		}
		else {
			pe_error('用户名或密码错误...');
		}
	break;
	//#####################@ 用户退出 @#####################//
	case 'logout':
		unset($_SESSION['user_id'], $_SESSION['user_name'], $_SESSION['user_tjlevel'], $_SESSION['user_tjnum'], $_SESSION['package_id']);
		pe_success('用户退出成功！', $pe['host_root']);
	break;
	//#####################@ 用户取消订阅 @#####################//
	case 'email_stop':
		$db->pe_delete('emailtask', array('user_email'=>pe_dbhold($_g_user_email)));
		if ($db->pe_update('user', array('user_email'=>pe_dbhold($_g_user_email)), array('user_emailget'=>2))) {
			pe_success('取消邮件订阅成功！', $pe['host_root']);
		}
		else {
			pe_success('取消邮件订阅失败...', $pe['host_root']);
		}
	break;
	case 'sign':
	    $user_id=$_POST['user_id'];
	    if($user_id){
		$user = $db->pe_select('user', array('user_id'=>$user_id));
		$money=$user['user_money'];
		$sign=$user['user_sign'];
		$sign_time=$user['user_sign_time'];
		if(time()-$sign_time >24*60*60){
		    $sign_now['user_sign']=1;
			$sign_now['user_sign_time']=time();
			$sign_now['user_money']=$money + 0.01;
		    if($db->pe_update('user', array('user_id'=>$user_id), $sign_now)){
		    echo "<script>alert('签到成功！+0.01元');window.location.href='user.php';</script>";
			}else{
			echo "<script>alert('签到失败！...');window.location.href='user.php';</script>";
			}
		}elseif(date('Y-m-d',time())!=date('Y-m-d',$sign_time)){
		    $sign_now['user_sign']=$sign + 1;
			$sign_now['user_sign_time']=time();
			$sign_now['user_money']=$money + 0.01;
		    if($db->pe_update('user', array('user_id'=>$user_id), $sign_now)){
		    echo "<script>alert('签到成功！+0.01元');window.location.href='user.php';</script>";
			}else{
			echo "<script>alert('签到失败！...');window.location.href='user.php';</script>";
			}
		}else{
		    echo "<script>alert('请勿重复签到！');window.location.href='user.php';</script>";
		}
		}
	break;
	//#####################@ 用户注册 @#####################//
	case 'register':
		$seo = pe_seo('用户注册');
		$action = "{$pe['host_root']}user.php?mod=do&act=registersql&dialog=1";
 		include(pe_tpl('do_register.html'));
	break;
	//#####################@ 用户注册sql @#####################//
	case 'registersql':
		if ($_g_type == 'checkname') {
			$result = $db->pe_num('user', "and (`user_name` ='".pe_dbhold($_g_user_name)."' or `user_subdomain` ='".pe_dbhold($_g_user_name)."')") > 0 ? false : true;
			echo json_encode(array('result'=>$result));
			die();
		}
		if ($_g_type == 'checkemail') {
			$result = $db->pe_num('user', array('user_email'=>pe_dbhold($_g_user_email))) > 0 ? false : true;
			echo json_encode(array('result'=>$result));
			die();
		}
		//取消dialog注册弹层
		if ($_g_type == 'regdialog') {
			$db->pe_insert('regdialog', array('regdialog_ip'=>pe_ip(), 'regdialog_cookie'=>$_COOKIE['regdialog_cookie']));
			pe_goto($pe['host_root'], 'dialog');
		}
		//邮件需要激活提示
		if ($_g_type == 'emailactive') {
			include(pe_tpl('do_emailactive.html'));
			die();
		}
		//取回密码
		if ($_g_type == 'getpwsql') {
			if (!$_p_info[user_email])pe_error('邮箱必须填写...', pe_url('page-getpw'));
			pe_lead('hook/qunfa.hook.php');
			    if($info = $db->pe_select('user', pe_dbhold($_p_info))){
			     $linshi_pw = substr(md5($pe['host_root'].$info['user_pw'].time()), 5, 8);
			     $email['qunfa_name'] = "您好：{$info['user_name']}，您的密码找回信息！";
			     $email['qunfa_text'] = "您好：{$info['user_name']}，您的临时密码是“{$linshi_pw}”，请尽快登录网站修改密码！";
			     qunfa_emaildiy($email, $info['user_id']);
			     if ($db->pe_update('user', pe_dbhold($_p_info), array('user_pw'=>md5($linshi_pw)))) {
				    pe_success('您的临时密码已发送至注册邮箱，请注意查收！', "{$pe['host_root']}index.php?mod=page&act=login",'dialog');
			      }
			       else {
				     pe_error('取回密码失败...', "{$pe['host_root']}index.php?mod=page&act=getpw",'dialog');
			       }
			   }else{
			       pe_error('邮箱不存在！！...', "{$pe['host_root']}index.php?mod=page&act=getpw",'dialog');
			   }
		}
		//激活账户
		if ($_g_type == 'activeemail') {
			$user_active['user_id'] = $_g_user_id;
			$user_active['user_email'] = $_g_user_email;
			$user_active['user_atime'] = $_g_user_atime;
			$user_active['user_isactive'] = 0;
			$info = $db->pe_select('user', pe_dbhold($user_active));
			if ($info['user_id']) {
				$db->pe_update('user', array('user_id'=>$info['user_id']), array('user_ltime'=>time(),'user_isactive'=>1));
				$_SESSION['user_id'] = $info['user_id'];
				$_SESSION['user_name'] = $info['user_name'];
				$_SESSION['user_tjlevel'] = $info['user_tjlevel'];
				$_SESSION['user_tjnum'] = $info['user_tjnum'];
				$_SESSION['package_id'] = array();
				$packagelog = $db->pe_selectall('packagelog', array('user_id'=>$info['user_id'], 'packagelog_state'=>1));
				foreach ($packagelog as $v) {
					$_SESSION['package_id'][] = $v['package_id'];
				}
				pe_success('用户登录成功！', "{$pe['host_root']}user.php");		
			}
			else {
				pe_error('该用户已激活或非法链接！', "{$pe['host_root']}");
			}
		}
		//防跨站提交
		if (!$_g_reg['user_name'] || !$_g_reg['user_pw'] || !$_g_reg['user_email']) {
			echo json_encode(array('result'=>false));
			die();	
		}
		
		//防重复注册
		if ($db->pe_num('user', "and (`user_name` ='".pe_dbhold($_g_reg['user_name'])."' or `user_subdomain` ='".pe_dbhold($_g_reg['user_email'])."')")) {
			echo json_encode(array('result'=>'false_name'));
			die();		
		}
		if ($db->pe_num('user', array('user_email'=>pe_dbhold($_g_reg['user_email'])))) {
			echo json_encode(array('result'=>'false_email'));
			die();		
		}
		if ($db->pe_num('user', array('user_join_ip'=>pe_ip()))) {
			echo json_encode(array('result'=>'false_ip'));
			die();		
		}
		
		//下面开始准备注册信息
		$_g_reg['user_level'] = 1;
		$_g_reg['user_tjlevel'] = 1;
		$_g_reg['user_join_ip'] = pe_ip();
		$_g_reg['user_pw'] = md5($_g_reg['user_pw']);
		$_g_reg['user_subdomain'] = $_g_reg['user_name'];
		$_g_reg['user_atime'] = $_g_reg['user_ltime']=time();
		//选出当前注册的推荐人
		if ($_COOKIE['user_name']) {
			$user_tj = $db->pe_select('user', array('user_name'=>$_COOKIE['user_name']));
			if ($user_tj['user_id']) {
				$_g_reg['user_id_tj'] = $user_tj['user_id'];
				$_g_reg['user_name_tj'] = $user_tj['user_name'];
				$user_tj['user_money']= $user_tj['user_money'] +0.01;
				$user_tj['tj_user_num']++;		
			}		
		}
		if ($user_id = $db->pe_insert('user', pe_dbhold($_g_reg))) {
			if ($user_tj['user_id']) {
				$db->pe_update('user',array('user_id'=>$user_tj['user_id']), pe_dbhold($user_tj));
			}
			//注册成功发送激活邮件
			$info = $db->pe_select('user', array('user_id'=>$user_id));
			pe_lead('hook/qunfa.hook.php');
			$email['qunfa_name'] = "恭喜您注册为{$cache_setting['web_title']['setting_value']}";
			$email['qunfa_text'] = "请<a href='{$pe['host_root']}user.php?mod=do&act=registersql&type=activeemail&user_id={$info['user_id']}&user_email={$info['user_email']}&user_atime={$info['user_atime']}'>点此激活您的会员帐号</a>";
			qunfa_emaildiy($email, $user_id);
			//注册成功发送第一封注册邮件模版，并把序列信邮件模版放入计划任务
			$emailtpl_list = $db->pe_selectall('emailtpl', array('emailtpl_utype'=>'user', 'emailtpl_type'=>'reg', 'order by'=>'emailtpl_id asc'));
			$emailtpl_info['user_id'] = $user_id;
			$emailtpl_info['user_name'] = pe_dbhold($_g_reg['user_name']);
			$emailtpl_info['user_email'] = pe_dbhold($_g_reg['user_email']);
			$emailtpl_info['emailtask_date'] = date('Y-m-d');
			foreach ($emailtpl_list as $v) {
				//$emailtpl_info['emailtask_date'] = $v['emailtpl_type'] == 'reg' ? date('Y-m-d') : ($emailtask_date = date('Y-m-d', strtotime($emailtask_date) + 86400));
				$emailtpl_info['emailtpl_id'] = $v['emailtpl_id'];
				$db->pe_insert('emailtask', $emailtpl_info);
				$emailtpl_info['emailtask_date'] = date('Y-m-d', strtotime($emailtpl_info['emailtask_date']) + 86400);
			}
			//pe_lead('hook/qunfa.hook.php');
			//qunfa_emailtpl();
			//pe_goto("user.php?mod=do&act=registersql&type=emailactive", $_g_dialog==1 ? 'dialog' : '');*/
			$db->pe_update('user', array('user_id'=>$user_id), array('user_isactive'=>1));
			$info = $db->pe_select('user', array('user_id'=>$user_id));
			$_SESSION['user_id'] = $info['user_id'];
			$_SESSION['user_name'] = $info['user_name'];
			$_SESSION['user_tjlevel'] = $info['user_tjlevel'];
			$_SESSION['package_id'] = array();
			$packagelog = $db->pe_selectall('packagelog', array('user_id'=>$info['user_id'], 'packagelog_state'=>1));
			foreach ($packagelog as $v) {
				$_SESSION['package_id'][] = $v['package_id'];
			}
			//pe_success('用户注册成功！', "{$pe['host_root']}user.php", 'dialog');
			echo json_encode(array('result'=>true));
			/*$info = $db->pe_select('user', array('user_id'=>$user_id));
			$_SESSION['user_id'] = $info['user_id'];
			$_SESSION['user_name'] = $info['user_name'];
			$_SESSION['user_tjlevel'] = $info['user_tjlevel'];
			$_SESSION['package_id'] = array();
			$packagelog = $db->pe_selectall('packagelog', array('user_id'=>$info['user_id']));
			foreach ($packagelog as $v) {
				$_SESSION['package_id'][] = $v['package_id'];
			}
			pe_success('用户注册成功！', "{$pe['host_root']}user.php");*/
		}
		else {
			//pe_error('用户注册失败...');
			echo json_encode(array('result'=>false));
		}
	break;
	//#####################@ 用户注册sql @#####################//
	case 'rssuser_add':
		if ($_g_type == 'checkemail') {
			$result = $db->pe_num('rssuser', array('rssuser_email'=>pe_dbhold($_g_rssuser_email))) > 0 ? false : true;
			echo json_encode(array('result'=>$result));
			die();
		}
		else if ($_g_type == 'submit') {
			//防跨站提交
			//if (!$_p_info['rssuser_name'] || !$_p_info['rssuser_email']) pe_error('用户订阅失败...');
			//防重复提交
			if ($db->pe_num('rssuser', array('rssuser_name'=>pe_dbhold($_g_rssuser_name), 'rssuser_email'=>pe_dbhold($_g_rssuser_email)))) {
				echo json_encode(array('result'=>'haveone'));
				die();			
			}
			$rssuser['rssuser_name'] = $_g_rssuser_name;
			$rssuser['rssuser_email'] = $_g_rssuser_email;
			$rssuser['rssuser_atime'] = time();
			$rssuser['package_id'] = intval($_g_package_id);
			if ($user_id = $db->pe_insert('rssuser', pe_dbhold($rssuser))) {
				//订阅成功发送第一封注册邮件模版，并把序列信邮件模版放入计划任务
				$emailtpl_list = $db->pe_selectall('emailtpl', array('emailtpl_utype'=>'rssuser', 'emailtpl_type'=>'reg', 'package_id'=>intval($_g_package_id), 'order by'=>'emailtpl_id asc'));
				//$emailtpl_info['user_id'] = 0;
				$emailtpl_info['user_name'] = pe_dbhold($rssuser['rssuser_name']);
				$emailtpl_info['user_email'] = pe_dbhold($rssuser['rssuser_email']);
				$emailtpl_info['emailtask_date'] = date('Y-m-d');
				foreach ($emailtpl_list as $v) {
					//$emailtpl_info['emailtask_date'] = $v['emailtpl_type'] == 'reg' ? date('Y-m-d') : ($emailtask_date = date('Y-m-d', strtotime($emailtask_date) + 86400));
					$emailtpl_info['emailtpl_id'] = $v['emailtpl_id'];
					$db->pe_insert('emailtask', $emailtpl_info);
					$emailtpl_info['emailtask_date'] = date('Y-m-d', strtotime($emailtpl_info['emailtask_date']) + 86400);
				}
				//pe_lead('hook/qunfa.hook.php');
				//qunfa_emailtpl();
				echo json_encode(array('result'=>true));
			}
			else {
				echo json_encode(array('result'=>false));
			}
		}
	break;
}
pe_result();
?>