<?php
class UserModel extends Model{
	function register(){
		if(!$_POST['submit']){
			exit();
		}
		header('Content-Type:text/html; charset=utf-8');
		if(!preg_match("/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.){1,2}[a-z]{2,4}$/i",$_POST['username'])){
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>您输入的邮箱格式有误，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/register.html'></div>";
			exit();
		}
		if($_POST['password'] == '' || $_POST['repassword'] == ''){
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>请填写用户密码，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/register.html'></div>";
			exit();
		}
		if($_POST['password'] != $_POST['repassword']){
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>两次输入的密码不一样，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/register.html'></div>";
			exit();
		}
		$u = M("user")->where("username = '".$_POST['username']."'")->count();
		if($u){
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>您输入的邮箱已被注册，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/register.html'></div>";
			exit();
		}
		$date = array(
			'username' => $_POST['username'],
			'password' => md5($_POST['password']),
		);
		M("user")->add($date);
		$uid = mysql_insert_id();
		$birth = strtotime($_POST['Year']."-".$_POST['Month']."-".$_POST['Day']);
		$udate = array(
			'uid'      => $uid,
			'rank'     => 1,
			'sex'      => $_POST['sex'],
			'birth'    => $birth,
			'from'     => $_POST['s']."-".$_POST['city'],
			'date'     => time(),
		);
		M("user_date")->add($udate);
		$lifeTime = 24 * 3600; 
		setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
		$_SESSION['NR_USER_ID']		= $uid;
		$_SESSION['NR_USER_KEY']    = $_POST['username'];
	}
	function login(){
		header('Content-Type:text/html; charset=utf-8');
		$username = $_POST['username'];
		$password = $_POST['password'];
		$u = M("user")->where("username = '".$username."'")->select();
		if(!$u){
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>您输入的邮箱没有注册，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/'></div>";
			exit();
		}
		if(md5($password) != $u[0]['password']){
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>用户密码输入错误，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/'></div>";
			exit();
		}
		$lifeTime = 24 * 3600; 
		setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
		$_SESSION['NR_USER_ID']		= $u[0]['id'];
		$_SESSION['NR_USER_KEY']    = $u[0]['username'];
	}
	function Logout(){
//		if($_GET['login'] == 'out'){
			header('Content-Type:text/html; charset=utf-8');
			unset($_SESSION['NR_USER_ID']);
			unset($_SESSION['NR_USER_KEY']);
			unset($_SESSION['NR_ADMIN_ID']);
			unset($_SESSION['NR_ADMIN_KEY']);
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>退出成功，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/'></div>";
			exit();
//		}
	}
	function add(){
		if(!$_POST['submit']){
			exit();
		}
		$title   = htmlspecialchars($_POST['title']);
		$content = htmlspecialchars($_POST['content']);
		$date = array(
			'title'   => $title,
			'memu'    => $_POST['memu'],
			'pid'     => $_SESSION['NR_USER_ID'],
			'content' => $content,
			'date'    => time(),
		);
		M("forum")->add($date);
		M("user_date")->where('uid= '.$_SESSION['NR_USER_ID'])->setInc('money',2);
	}
	function reply(){
		if(!$_POST['subreply']){
			exit();
		}
//		if($_POST['subreply']){
			$content = htmlspecialchars($_POST['content']);
			$date = array(
				'fid'     => $_GET['id'],
				'pid'     => $_SESSION['NR_USER_ID'],
				'content' => $content,
				'date'    => time(),
			);
			M("reply")->add($date);
			M("forum")->where('id= '.$_GET['id'])->setInc('reply');
			M("user_date")->where('uid= '.$_SESSION['NR_USER_ID'])->setInc('money');
//		}
	}
	function edu(){
		if(!$_POST['submit']){
			exit();
		}
		$date['uname'] = $_POST['uname'];
		M("user")->where("id = ".$_SESSION['NR_USER_ID'])->save($date);
		$birth = strtotime($_POST['Year']."-".$_POST['Month']."-".$_POST['Day']);
		$d = array(
			'sex'   => $_POST['sex'],
			'from'  => $_POST['s']."-".$_POST['city'],
			'birth' => $birth,
		);
		M("user_date")->where("uid = ".$_SESSION['NR_USER_ID'])->save($d);
	}
	function adminlogin(){
		$m = M("admin")->where("password = '".md5($_POST['password'])."' and uid = ".$_SESSION['NR_USER_ID'])->count();
		if($m){
			$lifeTime = 24 * 3600; 
			setcookie(session_name(), session_id(), time() + $lifeTime, "/"); 
			$_SESSION['NR_ADMIN_ID']		= $_SESSION['NR_USER_ID'];
			$_SESSION['NR_ADMIN_KEY']    = $_SESSION['NR_USER_KEY'];
		}else{
			header('Content-Type:text/html; charset=utf-8');
			echo "<div style='font-size:14px;text-align:center;padding-top:50px;'>用户密码输入错误，3秒钟后自动返回！<meta http-equiv='refresh' content='3;url=/Admin_login.html'></div>";
			exit();
		}
	}
	function strconf($key,$ycon,$ncon){
		if($ncon != $ycon){
			$file = "./conf/config.php";
			$str = file_get_contents($file);
			$str = preg_replace("/'$key'\s+=>\s+'([^']+)'/", "'$key'             => '$ncon'", $str);
			$w = fopen($file,"w");
			fwrite($w,$str);
		}
		fclose($w);
	}
}
?>