<?php
/**
 * Implementation o action helper for all blog admin controllers.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Action helper for all blog admin controllers.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class Blog_Controller_Helper_Admin extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Action controller init hook.
     * - Makes simple auth via password file
     * - inits common view stuff for all blog admin controllers
     */
    public function init()
    {
        $this->_makeSimpleAuth();

        $view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
        
        //render right block into 'rightBlock' response segment
        $this->getResponse()->insert('rightBlock',$view->render('admin/menu.phtml'));

        //page title
        $view->pageTitle = "Administration";

        //some javascript files, not all are required for all controllers, but it is easy
        //and transparent to define all files in one place
        $view->headScript()->appendFile(MKLib_AppBaseUrl::getBaseUrl().'/js/calendar1.js');
        $view->headScript()->appendFile(MKLib_AppBaseUrl::getBaseUrl().'/js/selectbox.js');
    }

    /**
     * Makes simple HTTP digest auth via password file.
     * If auth fails, view password dialog and exit application.
     */
    private function _makeSimpleAuth()
    {
        //simple authorization code
        //create Zend_Auth objects
        $authConfig = array(
            'accept_schemes' => 'digest',
            'realm'          => 'Admin Access',
            'digest_domains' => Zend_Controller_Action_HelperBroker::getStaticHelper('url')
                ->url(array(),"blogAdmin"),
            'nonce_timeout'  => 3600,
        );
        $adapter = new Zend_Auth_Adapter_Http($authConfig);
        $basicResolver = new Zend_Auth_Adapter_Http_Resolver_File();
        $basicResolver->setFile(APPLICATION_PATH . "/configs/.htpasswd");
        $adapter->setDigestResolver($basicResolver);

        $adapter->setRequest( $this->getRequest() );
        $adapter->setResponse( $this->getResponse() );

        $result = $adapter->authenticate();

        if (!$result->isValid()) {
            //view auth dialog and exit
            $adapter->getResponse()->sendResponse();
            exit;
        }
    }
}
