<?php
class Admin_AuthController extends Zend_Controller_Action 
{
	public function init()
    {
		$this->smarty=Zend_Registry::get('smarty');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
    }
    
  	public function imgcodeAction()
    {
    	Zend_Loader::loadClass('Custom_Controller_Plugin_ImgCode');
    	
    	$imagecode=new Custom_Controller_Plugin_ImgCode();
    	
    	$imagecode->image2();
    }
    
    public function loginAction()
    {
    	require_once 'Zend/Session/Namespace.php';
        if($this->_request->isPost()){
    		Zend_Loader::loadClass('Zend_Filter_StripTags');
    		$filter=new Zend_Filter_StripTags();
    		$username=$filter->filter($this->_request->getPost('username'));
    		$password=md5($filter->filter($this->_request->getPost('password')));
    		$imgcode=$filter->filter($this->_request->getPost('imgcode'));
    		$authCode = new Zend_Session_Namespace('Auth_Code');
        	//$authCode->imagecode;
    		if($authCode->imagecode==$imgcode){
	    		if($username!=''&&$password!=''){
	    			Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
	    			$dbAdapter=Zend_Registry::get('dbAdapter');
	    			$authAdapter=new Zend_Auth_Adapter_DbTable($dbAdapter);
	    			$authAdapter->setTableName(TABLE_PRE.'user');
	    			$authAdapter->setIdentityColumn('username');
	    			$authAdapter->setCredentialColumn('password');
	    
	    			$authAdapter->setIdentity($username);
	    			$authAdapter->setCredential($password);
	    			$auth=Zend_Auth::getInstance();
	    			$result=$auth->authenticate($authAdapter);
	    			if($result->isValid()){
	    				$data=$authAdapter->getResultRowObject(null,'password');
	    				$auth->getStorage()->write($data);
	    				$this->_redirect('admin/index/index');
	    				$request=$this->getRequest();
	    			}else{
	    				$this->smarty->assign('message','失败');
	    			}
	    		}else{
	    			$this->smarty->assign('message','失败');
	    		}
	    		
    		}else{
    			echo "验证码错误!";
    		}
        }
       	$this->smarty->display('login.phtml');
    
    }
    
    public function	logoutAction()
    {
    	Zend_Auth::getInstance()->clearIdentity();
    	
    	echo "<script language='JavaScript' type='text/javascript'>
		top.location.href='auth/login';
		</script>";
    } 
    
    
    public function __call($method, $args)    
    {        
    	if ('Action' == substr($method, -6)) 
    	{
    		// If the action method was not found, forward to the index action         
    		return $this->_forward('index');
    	}
    	// all other methods throw an exception 
    	throw new Exception('Invalid method "' . $method . '" called');    
    }
	
	
}
?>