<?php
class IndexAction extends Action{
    public function checkUser(){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
	}
    public function index(){
		$this->checkUser();
		$data = M('Config');
		$list = $data->field('title')->find();
		$this->assign('title',$list['title']);
		$data = M('Page');
		$page = $data->field('name,id')->limit(10)->select();
		$this->assign('page',$page);
		$level = $_SESSION['levels'];
		$this->assign('level',$level);
		$this->display();
	}
	public function stat(){
		$this->checkUser();
		$info['name'] = $_SERVER['SERVER_NAME'];	//服务器域名
		$info['addr'] = $_SERVER['SERVER_ADDR'];	//服务器IP
		$info['port'] = $_SERVER['SERVER_PORT'];	//服务器端口号
		$info['soft'] = $_SERVER['SERVER_SOFTWARE']; //服务器类型/版本
		$info['phpversion'] = PHP_OS;				//服务器操作系统
		$info['root'] = $_SERVER['DOCUMENT_ROOT'];	//网站根目录
		$info['mysql']  = mysql_get_client_info();  //Mysql版本
		$info['version']  = C('YICMS_VERSION').'&nbsp;&nbsp;'.C('YICMS_RELEASE');  //template版本
		$info['license']  = @file_get_contents('http://url.siji99.com/license.php?url='.$info['name']);  //授权信息
		$list = M()->query("SHOW TABLE STATUS LIKE '".C('DB_PREFIX')."%'");
        foreach ($list as $row){
            $info['dataSize'] += $row['Data_length'];
        }
		$year = date('Y',$_SERVER['REQUEST_TIME']);
		$month = date('m',$_SERVER['REQUEST_TIME']);
		$day = date('d',$_SERVER['REQUEST_TIME']);
		$time = mktime(0,0,0,$month,$day,$year);
		$data = M('Buy');
		$info['todayBuy'] = $data->where("time>{$time}")->count('id');
		$data = M('Reply');
		$info['todayReply'] = $data->where("artime>{$time}")->count('id');
		$this->assign('info',$info);
		$this->display();
	}
    public function login(){
		if(empty($_SESSION['aid'])){
			$data = M('Config');
			$list = $data->field('title')->find();
			$this->assign('title',$list['title']);
			$this->display();
		}else{
			$this->redirect('Index/index');
		}
	}
	function checkLogin(){
		if(empty($_POST['account']))  $this->error('帐号必须填写！');
		if(empty($_POST['password'])) $this->error('密码必须填写！');
		if(empty($_POST['verify']))   $this->error('验证码必须填写！');
		if($_SESSION['verify'] != md5($_POST['verify']))  $this->error('验证码错误!');
		$account = trim($_POST['account']);
		$password = md5(trim($_POST['password']));
		$data = M('Admin');
		$user = $data->where("`username` = '$account'")->find();
		if(is_array($user)){
			if($user['password'] == $password){
				if($user['state'] == '0') $this->error('您已被禁止登录,');
				unset($user['password']);
				$_SESSION = array_merge($user,$_SESSION);
				$_SESSION['lasttime'] = switchtime($user['lasttime']);
				$_SESSION['lastip'] = long2ip($user['lastip']);
				$_SESSION['aid'] = $user['id'];
				$_SESSION['levels'] = explode(',',$_SESSION['level']);
				unset($_SESSION['level']);
				//保存登录信息
				$upUser = M('Admin');
				$time = $_SERVER['REQUEST_TIME'];
				$ip = ip2long(get_client_ip());
				$data = array();
				$data['id']	=	$user['id'];
				$data['lasttime']	=	$time;
				$data['lastip']	= $ip;
				$upUser->save($data);
				$this->success('登录成功！现在将返回主页!');
			}else{
				$this->error('密码错误!');
			}
		}else{
			$this->error('用户名不存在!');
		}
	}
	public function amend(){
		$this->display();
	}
	public function checkPass(){
		$data = M('Admin');
		$oldpass = md5(trim($_POST['oldpass']));
		$newpass = trim($_POST['newpass']);
		$password = trim($_POST['password']);
		$username = trim($_POST['username']);
		$user = $data->where("`password` = '$oldpass'")->field('id')->find();
		if(!is_array($user))	$this->error('原密码输入错误，请重新输入!');
		if($newpass != $password)	$this->error('两次密码不一致，请重新输入!');
		if(empty($newpass))		$this->error('新密码不能为空!');
		$list['id'] = $_SESSION['aid'];
		$list['password'] = md5($password);
		if(!empty($username)){
			$user = $data->where("`username` = '$username'")->field('id')->find();
			if(is_array($user))	$this->error('用户名已存在,换一个吧!');
			$list['username'] = $username;
		}	
		if(!$data->save($list)) $this->error('密码修改失败，重试吧！');
		$this->success('修改成功!^_^下次登录记得用新密码!');
	}
	public function logout(){
		unset($_SESSION);
		session_destroy();
		$this->redirect('/Admin/Index/login');
	}
	public function verify(){
		import("@.ORG.Image"); 
		Image::buildImageVerify(4,1,'gif',53,25); 
	}
}
?>
