<?php
/*
秀影EasyFramework框架
qq:24498936
msn:jdzcn_net@hotmail.com
website:http://www.vodcms.com
*/
class AdminController extends Easy_Controller_Action {
	public $_AdminModel;
	public function Init(){
		$this->_AdminModel = new AdminModel();
		$this->_ModuleModel = new ModuleModel();
		$this->_Deny = new DenyModel();
		$this->_Deny->CheckDeny();
		//$acl = new AclModel();
		//$acl->set('guest');
		if ($this->_Request->getParam('action') != 'index' && $this->_Request->getParam('action') != 'login' ){
			if ($this->_AdminModel->CheckLogin() != true){
				exit($this->_View->render('admin/admin_login'));
			}
		}
		
	}
	public function IndexAction(){
	//	$this->_AdminModel = new AdminModel();
		$this->_Request->setSession('failed', NULL);
		$this->_View->topModule = $this->_ModuleModel->listModule('parentid=0');
		$this->_View->title = '秀影系统管理后台';
		if ($this->_AdminModel->CheckLogin() != true){
			exit($this->_View->render('admin/admin_login'));
		}
	}
	public function LoginAction(){
		$maxTimeout = 5;
		$username = $this->_Request->getPost('username');
		$password = $this->_Request->getPost('password');
		$array['username'] = strtolower($username);
		$array['password'] = MD5(strtolower($password));
		$code = (int)$this->_Request->getSession('code');
		if ($code !== (int)$this->_Request->getPost('code')){
			$this->_View->error ='<script>alert("'._('验证码输入不正确!').'");</script>';
		}elseif ($result = $this->_AdminModel->Login($array)){
			$this->_View->Redirect('admin/');
			exit;
		}else{
			$this->_View->error =_('用户名或者密码不正确!');
			$this->failed = $this->_Request->getSession('failed');
			$this->_Request->setSession('failed', ($this->failed + 1));
			if ($this->failed >= $maxTimeout){
				if ($this->failed == $maxTimeout){
					$this->_Deny->deny($array['username'].'后台登陆密码错误次数太多!', 
					$this->_Request->StrUserIp(), $this->_Request->StrUserIp(), 5);	//写入封锁IP
					$this->_Request->sendHeader(403);
					echo '您的密码错误次数太多!诿15分钟后访问此页！';
					exit;
				}
			}
		}
	}
	public function AuthAction(){
		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		header("Content-type: image/png");
		$height = 19;
		$width = 47;
		$im = ImageCreateTrueColor($width, $height);
		$blue = ImageColorAllocate ($im, 255,255,255);
		$f1 = ImageColorAllocate ($im, 79,14,95);
		$f2 = ImagecolorAllocate($im,201,16,63);
		$f3 = ImagecolorAllocate($im,22,104,11);
		ImageFill($im, 0, 0, $blue);
		$str = substr(rand(), 0, 4);
		$this->_Request->setSession('code', $str);
		$yan=ImageString($im, 5, 6, 1, substr($str,0,1), $f2);  
		$yan=ImageString($im, 5, 15,1, substr($str,1,1), $fl);  
		$yan=ImageString($im, 5, 25, 1, substr($str,2,1), $f2);  
		$yan=ImageString($im, 5, 34,1, substr($str,3,1), $f3); 
		ImagePng ($im);
		ImageDestroy($im);
		exit;
	}
	public function TopAction(){
		
	}
	public function LeftAction(){
		$id = (int)$this->_Request->getParam('menu') ? (int)$this->_Request->getParam('menu') : 1;
		$this->_View->row = $this->_ModuleModel->getModule(array('id'=> $id));
		$this->_View->leftModule = $this->_ModuleModel->listModule(array('parentid'=>$id));
	}
	public function MainAction(){
		$DB = $GLOBALS['DB'];
		$this->_View->mysqlversion = $DB->version();
		$this->_View->information = $this->_AdminModel->information();
		$this->_View->license = $GLOBALS['license'];
		$this->_View->ver = base64_encode(implode(',', $GLOBALS['license']));
	}
	public function notieAction(){
		
	}
	public function CopyrightAction(){
		
	}
	public function logoutAction(){
		$this->_Request->setSession('id', '');
		$this->_Request->setSession('flag', '');
		$this->_Request->setSession('failed', '');
		$this->_Request->setCookie('admin', '');
		$this->_Request->getCookie('group', '');
		$this->_Request->getCookie('hash', '');
		$this->_View->redirect('admin/login/');
		exit;
	}
	public function reportAction(){
		if ($this->_Request->isPost()){
			$email = $this->_Request->getPost('email');
			$subject = $this->_Request->getPost('subject');
			$contents = $this->_Request->getPost('contents');
			if ($email && $subject && $contents){
				$Mail = new Mail();
				if ($Mail->sendMail(array('24498936@qq.com', 'jdzcn_net@hotmail.com'), $subject, $contents)===true){
					$this->_View->message = _('报告提交成功!我们将在48小时内给您回复');
				}else{
					$this->_View->message = _('报告发送失败!错误原因:'.$Mail->ErrorInfo);
				}
				exit($this->_View->render('message'));
			}else{
				$this->_View->message = _('提交失败!您的信息填写不完整');
				exit($this->_View->render('message'));
			}
		}
	}
}