<?php
class Api_DomainController extends Zend_Controller_Action 
{
	
	public function init()
	{
		//TODO: extend authentication to support multiple user levels/roles/groups 
		require_once 'application/models/api.php';
		$this->api = new api();
		
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->appBaseUrl = $this->_request->getBaseUrl() . (strpos($this->_request->getBaseUrl(),'index.php')?'':'/index.php');
		$this->view->siteBaseUrl = Zend_Registry::get('siteBaseUrl');
		$this->view->moduleBaseUrl = $this->view->appBaseUrl . ($this->_request->module == 'default'?'':'/' . $this->_request->module);
		
		$this->_helper->viewRenderer->setNoRender();
		
		$this->restserver = new Zend_Rest_Server();
		$this->restserver->setClass('api');
		
		if( !Atmail_HttpAuth::authenticated(3, null, 'admin', null) )
			$this->_forward('authenticationfailed');
		
	} 
	
	public function authenticationfailedAction() {
	   
	   	//just an empty deadend so failed XML responses are uniform
		$this->restserver->handle( array('method' => 'authenticationFailed') ); 
	
	}
	
	public function indexAction() {
		$this->restserver->handle( array('method' => 'index') );
	}
	
	public function createAction() {
		$params = $this->getRequest()->getParams();
		$params['method'] = 'domainCreate';		
		$this->restserver->handle($params);
	}
	
	public function checkAction() {
		$params = $this->getRequest()->getParams();
		$params['method'] = 'domainCheck';		
		$this->restserver->handle($params);
	}
	
	public function listAction() {
		
		$this->restserver->handle( array('method' => 'domainList') );
	}	

	public function deleteAction() {
		
		$params = $this->getRequest()->getParams();
		$params['method'] = 'domainDelete';		
		$this->restserver->handle($params);
	
	}
	
	public function status()
	{
		$params = $this->getRequest()->getParams();
		$params['method'] = 'getDomainStatus';		
		$this->restserver->handle($params);
    }
    
	public function lock()
	{
		$params = $this->getRequest()->getParams();
		$params['method'] = 'domainLock';		
		$this->restserver->handle($params);
    }
    
    
    public function unlock()
	{
		$params = $this->getRequest()->getParams();
		$params['method'] = 'domainUnlock';		
		$this->restserver->handle($params);
    }

	public function getdomainandgroupusersAction()
	{
	
		$params = $this->getRequest()->getParams();
		$params['method'] = 'getDomainAndGroupUsers';		
		$this->restserver->handle($params);
	
	}
    
}
