<?php
//@*--


class Theme_Designer_Controller_AdminAction extends Mage_Adminhtml_Controller_Action
{
    protected $coreHelper;
    protected $adminHelper;

    protected $currentTheme;

    protected $allowLogin = array(
        'adminhtml_theme' => array('export', 'download', 'publish', 'clear', 'updatePreview')
    );

    protected function _construct() {
        $this->coreHelper = Mage::helper('core');
        $this->adminHelper = Mage::helper('adminhtml');
        $this->currentTheme = $this->getTheme();
    }

    protected function getAdminUrl($path, $params = array()) {
        return Mage::helper('core/url')->addRequestParam(
            $this->adminHelper->getUrl($path),
            $params
        );
    }

    protected function setAjaxResponse($data = array()) {
        $this->getResponse()->setBody(
            $this->coreHelper->jsonEncode($data)
        );
    }

    protected function adminRedirect($path, $params = array()) {
        $this->getResponse()->setRedirect($this->getAdminUrl($path, $params));
    }

    private function getTheme() {
        $theme = $this->getRequest()->getParam('theme');
        $theme = substr(preg_replace('/[^a-z0-9_]/i', '', $theme), 0, 20);
        return $theme;
    }

    public function preDispatch()
    {
        Mage::getSingleton('core/session', array('name' => 'adminhtml'));

        $domain = $this->getRequest()->getParam('domain', '');
        $theme = $this->getRequest()->getParam('theme', '');

        if ($this->getRequest()->getParam('auto_login', false)) {
            $login = $this->getRequest()->getParam('login', '');
            $pwd = $this->getRequest()->getParam('pwd', '');

            if (!Mage::getSingleton('admin/session')->isLoggedIn()) {
                $session = Mage::getSingleton('admin/session');
                $this->getRequest()->setPost('login', array('username' => $login));

                try {
                    /** @var $user Mage_Admin_Model_User */
                    $user = Mage::getModel('admin/user');
                    $user->login($login, $pwd);
                    if ($user->getId()) {
                        $session->renewSession();

                        if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
                            Mage::getSingleton('adminhtml/url')->renewSecretUrls();
                        }
                        $session->setIsFirstPageAfterLogin(true);
                        $session->setUser($user);
                        $session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());

                        $this->_forceRedirect(
                            '*/*/*',
                            array('domain' => $domain, 'theme' => $theme)
                        );
                    } else {
                        Mage::throwException(Mage::helper('adminhtml')->__('Invalid User Name or Password.'));
                    }
                } catch (Mage_Core_Exception $e) {
                    Mage::dispatchEvent('admin_session_user_login_failed',
                        array('user_name' => $login, 'exception' => $e));
                }
            }
        }

        if ($this->getRequest()->getParam('forceLogout', '')) {
            if (!is_array($this->getRequest()->getPost('login'))) {
                $session = Mage::getSingleton('admin/session');
                if (($user = $session->getUser()) && $user->getId()) {
                    $user->setId(null);
                    $user->reload();
                }
            }

            $this->_forceRedirect(
                'designer/adminhtml_theme/index',
                array('domain' => $domain, 'theme' => $theme)
            );
        }

        return parent::preDispatch();
    }

    private function _forceRedirect($route, $params = array())
    {
        $url = Mage::helper('adminhtml')->getUrl($route);
        $url = Mage::helper('core/url')->addRequestParam(
            $url, array_map('urlencode', $params)
        );
        $this->getResponse()->setRedirect($url);
        $this->getResponse()->sendHeaders();
        $this->getResponse()->sendResponse();
        exit;
    }
}

//--*@