app->sts->contactUsLink)) { $contactUs = 'contact us'; } $this->_messageCodeGen = str_replace('%contactUs%', $contactUs, self::MESSAGE_CODE_GEN_DENNY); $adminAuth = App_Admin_Auth::getInstance(); $this->_identity = $adminAuth->getIdentity(); $this->dataObj->setPrimaryKey($this->_identity['data']['id']); $this->_stsUrlDomain = Zend_Registry::get('config')->app->sts->stsUrlDomain; return $this; } public function exec() { if ('n' == $this->dataObj->getStsLoginEnabled()) { $this->redirect(BASE_URL_LANGUAGE . '/admin'); } $this->doc->setHeader(''); $this->_addTemplatePath('_sys'); $this->doc->addItem(array('tpl' => $this->getTemplate('redirect-to-sts-item.tpl'))); parent::exec(); return $this; } public function getStsUrlDomain() { if (null === $this->_stsUrlDomain) { throw new Qs_Exception('Online Support System Url is not defined'); } return $this->_stsUrlDomain; } public function getLoginForm() { $form = $this->_getBaseForm(); $form->addElement('text', 'sts_login', array('label' => 'STS Login:', 'required' => true)); $form->addElement('password', 'sts_password', array('label' => 'STS Password:', 'required' => true)); $form->addElement('hidden', 'action', array('value' => 'login')); $form->addElement('submit', 'btnSubmit', array('label' => 'Log In', 'class' => 'btn')); $form->setAjaxValidation(false); return $form; } protected function _doView() { if (null == ($this->_code = $this->dataObj->getStsAuthCode())) { $form = $this->getLoginForm(); $this->_renderMainForm($form); } else { if (self::CODE_GEN_OK == ($resultCodeGen = $this->_doStsToGenCode())) { $this->_redirectToSts(); } elseif ($resultCodeGen == self::CODE_GEN_EMPTY) { $form = $this->getLoginForm(); $this->_renderMainForm($form); } elseif ($resultCodeGen == self::CODE_GEN_DENNY) { $this->doc->displayMessage($this->_messageCodeGen); } elseif (!$resultCodeGen) { $this->doc->displayMessage(self::FAILED_STS_RESPONSE); } } return $this; } protected function _doLogin() { $form = $this->getLoginForm(); if ($form->validate()) { if (self::CODE_GEN_OK == ($resultCodeGen = $this->_doStsToGenCode($form->getValue('sts_login'), $form->getValue('sts_password')))) { $form->clearDecorators(); $this->_redirectToSts(); } elseif ($resultCodeGen == self::CODE_GEN_EMPTY) { $decorators = $form->getDecorators(); $form->addDecorator('Errors', array('placement' => 'prepend')); $form->addError('Invalid Login or Password'); } elseif ($resultCodeGen == self::CODE_GEN_DENNY) { $this->doc->displayMessage($this->_messageCodeGen); return $this; } elseif (!$resultCodeGen) { $this->doc->displayMessage(self::FAILED_STS_RESPONSE); } } $this->_renderMainForm($form); } protected function _renderMainForm(Qs_Form $form) { parent::_renderMainForm($form); $this->_doc->addInlineScript('initLoginForm', " function initLoginForm(idForm) { if ($('#' + idForm).size()) { document.getElementById(idForm).sts_login.focus(); document.getElementById(idForm).sts_login.select(); } } "); $this->_doc->addInitFunction('initLoginForm', array($form->getAttrib('id'))); } protected function _renderRedirectForm($form) { parent::_renderMainForm($form); $this->_doc->addInlineScript('initRedirectForm', " function initRedirectForm(idForm) { if ($('#' + idForm).size()) { document.getElementById(idForm).submit(); } } "); $this->_doc->addInitFunction('initRedirectForm', array($form->getAttrib('id'))); } protected function _doStsToGenCode($login = null, $passwd = null) { $dataSend = array('action' => 'getRemoteAuthCode'); if (!empty($this->_code)) { $dataSend['code'] = $this->_code; } elseif (!empty($login) && !empty($passwd)) { $dataSend['login'] = $login; $dataSend['password'] = md5($passwd); } $config = array( 'keepalive' => true, 'strictredirects' => true ); $client = new Zend_Http_Client($this->getStsUrlDomain() . '/__generate-auth-code', $config); $client->setMethod('POST'); $client->setCookieJar(false); $client->setParameterPost($dataSend); $response = $client->request('POST'); if (200 != $response->getStatus()) { return false; } $headers = Zend_Http_Response::extractHeaders($response); if (isset($headers['transfer-encoding']) && $headers['transfer-encoding'] == 'gzip') { $body = Zend_Http_Response::extractBody($response); $dataCode = Zend_Http_Response::decodeGzip($body); } else { $dataCode = $response->getBody(); } preg_match('/\(code\[\/([a-z0-9]{32}|empty|denny)\/\]/', $dataCode, $matches); if (!empty($matches[1]) && $matches[1] != 'denny' && $matches[1] != 'empty') { $this->dataObj->updateStsLoginCode($matches[1]); $this->_code = $matches[1]; return self::CODE_GEN_OK; } elseif ($matches[1] == 'empty') { return self::CODE_GEN_EMPTY; } elseif ($matches[1] == 'denny') { return self::CODE_GEN_DENNY; } return false; } protected function _redirectToSts() { $this->doc->addItem(array('tpl' => $this->getTemplate('loader.tpl'))); $redirectForm = $this->_getBaseForm(); $redirectForm->setAttrib('id', $redirectForm->getAttrib('id') . '-redirect'); $redirectForm->setAction($this->_stsUrlDomain . '/login'); $redirectForm->setMethod('POST'); $redirectForm->addElement('hidden', 'action', array('value' => 'remoteLogin')); $redirectForm->addElement('hidden', 'code', array('value' => $this->_code)); $redirectForm->setAjaxValidation(false); $this->_renderRedirectForm($redirectForm); return $this; } }