<?php
require_once 'Zend/Session/Namespace.php'; 
class AuthController extends Zend_Controller_Action
{
    function init()
    {
		$this->smarty=Zend_Registry::get('smarty');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
    }

    function	loginAction()
    {
    	$this->smarty->assign('message','');
    	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')));
    		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->smarty->assign('message','成功');
    				$this->_redirect('/');
    				$request=$this->getRequest();
    			}else{
    				$this->smarty->assign('message','失败');
    			}
    		}else{
    			$this->smarty->assign('message','失败');
    		}
    	}
    	
    	
    	$this->smarty->assign('title','用户登陆');
    	
    	//$this->smarty->display('header.phtml');  
		$this->smarty->display('login.phtml');
		//$this->smarty->display('footer.phtml');
    }
    
    public function	logoutAction()
    {
    	Zend_Auth::getInstance()->clearIdentity();
    	$this->_redirect('/');
    } 
}