<?php
                    /**
                    * ###############################################
                    *
                    * SWIFT Framework
                    * _______________________________________________
                    *
                    * @package        SWIFT
                    * @copyright    Copyright (c) 2001-2014, Kayako
                    * @license        http://www.kayako.com/license
                    * @link        http://www.kayako.com
                    *
                    * ###############################################
                    */

                       abstract class Controller_StaffBase extends SWIFT_Controller { private $_controllerType = false;  const TYPE_ADMIN = 'admin'; const TYPE_STAFF = 'staff'; const TYPE_PDA = 'pda'; const TYPE_INTRANET = 'intranet'; const LOGOUTTYPE_SESSIONEXPIRED = 1;  public function __construct($_controllerType) { parent::__construct(); $_SWIFT = SWIFT::GetInstance(); if (!$this->SetControllerType($_controllerType)) { throw new SWIFT_Exception(SWIFT_INVALIDDATA); return false; } $this->Load->Library('Staff:StaffPasswordPolicy');  if (($_SWIFT->Router->GetController() == 'Default' && $_SWIFT->Router->GetAction() == 'CSS') || ($_SWIFT->Router->GetController() == 'Default' && $_SWIFT->Router->GetAction() == 'Login') || ($_SWIFT->Router->GetController() == 'Default' && $_SWIFT->Router->GetAction() == 'Logout') || ($_SWIFT->Router->GetController() == 'Default' && $_SWIFT->Router->GetAction() == 'Compressor') || ($_SWIFT->Router->GetController() == 'Default' && $_SWIFT->Router->GetAction() == 'RebuildCache')) { return true; } if (!SWIFT_Session::Start($this->Interface)) {  $this->_LoadLoginTemplateFinal(SWIFT::Get('errorstring')); exit; } if (!$_SWIFT->Staff instanceof SWIFT_Staff || !$_SWIFT->Staff->GetIsClassLoaded()) {  $this->_DisplayLoginTemplate(SWIFT::Get('errorstring')); exit; }  $_ipMatch = $_SWIFT->Staff->ValidateIP(); if ($_ipMatch != true) { $this->_DisplayLoginTemplate($_SWIFT->Language->Get('invaliduseripres')); exit; } return true; }  public function __destruct() { parent::__destruct(); return true; }  static public function IsValidControllerType($_controllerType) { if ($_controllerType == self::TYPE_ADMIN || $_controllerType == self::TYPE_STAFF || $_controllerType == self::TYPE_INTRANET || $_controllerType == self::TYPE_PDA) { return true; } return false; }  protected function SetControllerType($_controllerType) { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } else if (!self::IsValidControllerType($_controllerType)) { throw new SWIFT_Exception(SWIFT_INAVLIDDATA); return false; } $this->_controllerType = $_controllerType; return true; }  public function GetControllerType() { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } return $this->_controllerType; }  protected function _DisplayLoginTemplate($_errorString, $_displayNewPassword = false) { $_SWIFT = SWIFT::GetInstance();  $_languagecode = $this->Cookie->Get('languagecode'); require_once ('./'. SWIFT_BASEDIRECTORY .'/'. SWIFT_LOCALEDIRECTORY .'/'. LANGUAGE_ADMIN ."/languagecodes.php"); $_languages = array(); if ($handle = opendir('./'. SWIFT_BASEDIRECTORY .'/'. SWIFT_LOCALEDIRECTORY .'/')) { while (false !== ($file = readdir($handle))) { if (substr($file, 0, 1) != '.' && $file != '.' && $file != '..' && is_dir('./' . SWIFT_BASEDIRECTORY . '/' . SWIFT_LOCALEDIRECTORY . '/' . $file)) { $_languages[] = array($file, IIF(!empty($__LANG[$file]), $__LANG[$file], $file), IIF(($file == LANGUAGE_ADMIN && empty($_languagecode)) || $file == $_languagecode, true, false)); } } closedir($handle); } $this->Template->Assign("_languageList", $_languages); $this->Template->Assign('_redirectAction', $_SWIFT->Router->GetArgumentsAsString());  $this->Template->Assign('_area', $this->GetControllerType());  $_cookieOptionsContainer = @json_decode($_COOKIE['jqCookieJar_options'], true); if (!isset($_cookieOptionsContainer['loginoptions'])) { $_cookieOptionsContainer['loginoptions'] = false; } if ($_displayNewPassword) { $this->Template->Assign('_passwordExpiredMessage', $this->StaffPasswordPolicy->GetPasswordPolicyString()); $this->Template->Assign('_passwordExpired', true); } $this->Template->Assign('_displayLoginOptions', $_cookieOptionsContainer['loginoptions']); if (!empty($_SERVER['QUERY_STRING'])) { $this->Template->Assign('_queryString', IIF(!stristr(htmlspecialchars($_SERVER['QUERY_STRING']), 'logout'), htmlspecialchars($_SERVER['QUERY_STRING']))); } else if (!empty($_POST['querystring'])) { $this->Template->Assign("_queryString", IIF(!stristr(htmlspecialchars($_POST['querystring']), 'logout'), htmlspecialchars($_POST['querystring']))); } $this->Template->Assign('_errorString', $_errorString); if (isset($_POST['username'])) { $this->Template->Assign("_userName", htmlspecialchars($_POST['username'])); } else { $this->Template->Assign("_userName", ''); } if (isset($_POST['password'])) { $this->Template->Assign("_password", htmlspecialchars($_POST['password'])); } else { $this->Template->Assign("_password", ''); }  if ((isset($_POST['isajax']) && $_POST['isajax'] == 1) || (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) { $this->Template->Render('loginformajax'); return true; } $this->Template->Render('loginform'); SWIFT::RunSystemChecks(); return true; }  protected function _Redirect($_redirectURL) { $this->Template->Assign('_redirectURL', $_redirectURL); $this->Template->Render('redirect'); return true; }  protected function _ParseRememberMe() { $_userName = $this->Cookie->Get('rememberusername' . $this->Interface->GetInterface()); $_password = $this->Cookie->Get('rememberpassword' . $this->Interface->GetInterface(), true); return array($_userName, $_password); }  protected function _ProcessRememberMe() {  $_cookieUserName = $this->Cookie->Get('rememberusername' . $this->Interface->GetInterface()); if ($_POST['username'] != $_cookieUserName && !empty($_cookieUserName)) { $this->_DeleteRememberMeCookies(); } $_languageCode = $this->Cookie->Get('languagecode'); if (($_POST['languagecode'] != LANGUAGE_ADMIN && !empty($_POST['languagecode'])) || ($_POST['languagecode'] != $_languageCode && !empty($_languageCode))) { $this->Cookie->Set('languagecode', $_POST["languagecode"], true); } if ($_POST['remember'] == 1) { $this->Cookie->Set('rememberusername' . $this->Interface->GetInterface(), $_POST['username'], true); $this->Cookie->Set('rememberpassword' . $this->Interface->GetInterface(), SWIFT_Staff::GetComputedPassword($_POST['password']), true, true); } else { $this->_DeleteRememberMeCookies(); } return true; }  protected function _DeleteRememberMeCookies() { $this->Cookie->Delete('rememberusername' . $this->Interface->GetInterface()); $this->Cookie->Delete('rememberpassword' . $this->Interface->GetInterface()); return true; }  protected function _LoginIndex() { $_SWIFT = SWIFT::GetInstance(); if ($_SWIFT->Session instanceof SWIFT_Session && $_SWIFT->Session->GetIsClassLoaded() && $_SWIFT->Session->GetSessionID() && self::IsValidStaffInterface($_SWIFT->Session->GetProperty('sessiontype'))) { header('location: '. SWIFT::Get('swiftpath') . $this->GetControllerType() . '/' . SWIFT_BASENAME . '/Base/Home/Index'); } else { $this->_LoadLoginTemplateFinal(); } }  protected function _LoadLoginTemplateFinal($_errorString = '') { $_SWIFT = SWIFT::GetInstance(); if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $_rememberMeContainer = $this->_ParseRememberMe(); $_hasRememberMe = false; if (!empty($_rememberMeContainer[0]) && !empty($_rememberMeContainer[1])) { $_hasRememberMe = true; }  if ($_hasRememberMe) { $_POST['_redirectAction'] = $_SWIFT->Router->GetArgumentsAsString();   if (stripos($_POST['_redirectAction'], '/Ajax/')) { $_POST['_redirectAction'] = null; } $this->_Login(); } else { $this->_DisplayLoginTemplate($_errorString); } return true; }  static protected function IsValidStaffInterface($_interfaceType) { if ($_interfaceType == SWIFT_Interface::INTERFACE_ADMIN || $_interfaceType == SWIFT_Interface::INTERFACE_STAFF || $_interfaceType == SWIFT_Interface::INTERFACE_INTRANET) { return true; } return false; }  protected function GetLoginLogInterface() { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } switch ($this->GetControllerType()) { case self::TYPE_ADMIN: return SWIFT_StaffLoginLog::INTERFACE_ADMIN; break; case self::TYPE_STAFF: return SWIFT_StaffLoginLog::INTERFACE_STAFF; break; case self::TYPE_INTRANET: return SWIFT_StaffLoginLog::INTERFACE_INTRANET; break; default: return false; break; } return false; }  protected function _Login() { $_SWIFT = SWIFT::GetInstance(); $_staffCache = $_SWIFT->Cache->Get('staffcache'); $_rememberMeContainer = $this->_ParseRememberMe(); $_hasRememberMe = false; if (!empty($_rememberMeContainer[0]) && !empty($_rememberMeContainer[1])) { $_hasRememberMe = true; } if (!$_hasRememberMe && (!isset($_POST['username']) || !isset($_POST['password']) || empty($_POST['username']) || empty($_POST['password']))) { $this->_DisplayLoginTemplate($this->Language->Get('invaliduser')); return false; } $_staffUsername = $_staffPassword = false; if ($_hasRememberMe) { $_staffUsername = $_rememberMeContainer[0]; $_staffPassword = $_rememberMeContainer[1]; } else { $_staffUsername = $_POST['username']; $_staffPassword = $_POST['password']; }  if ($_SWIFT->Settings->Get('security_loginlocked') == '1') {  $_loginLogCheck = SWIFT_StaffLoginLog::CanStaffLogin($_staffUsername); if (!$_loginLogCheck[0]) {  $this->Template->Assign("_loginLogCheckError", true); $this->Template->Assign("_loginAttemptCount", $_loginLogCheck[1]); $_errorString = sprintf($this->Language->Get('loginlogerror'), round(SWIFT_StaffLoginLog::GetLoginTimeline() / 60), $_loginLogCheck[1], SWIFT_StaffLoginLog::GetLoginRetries()); $this->_DisplayLoginTemplate($_errorString); return false; } } $_SWIFT_StaffObject = false; $_shouldBeAdmin = false; if ($this->GetControllerType() == self::TYPE_ADMIN) { $_shouldBeAdmin = true; }  $_shouldComputeHash = true; if ($_hasRememberMe) { $_shouldComputeHash = false; } $_SWIFT_StaffObject = SWIFT_Staff::Authenticate($_staffUsername, $_staffPassword, $_shouldBeAdmin, $_shouldComputeHash); $_staffCache = $_SWIFT->Cache->Get('staffcache'); if (isset($_POST['remember']) && $_POST['remember'] != '1') { $this->_DeleteRememberMeCookies(); } if ($_SWIFT_StaffObject instanceof SWIFT_Staff && $_SWIFT_StaffObject->GetIsClassLoaded()) { if ($_SWIFT_StaffObject->HasPasswordExpired() && !SWIFT_LoginShareStaff::IsActive()) { if (isset($_POST['newpassword']) && isset($_POST['newpasswordagain'])) { if (empty($_POST['newpassword']) || $_POST['newpassword'] != $_POST['newpasswordagain']) { $this->_DisplayLoginTemplate($this->Language->Get('passworddontmatch'), true); return false; } else if (!$this->StaffPasswordPolicy->Check($_POST['newpassword']) || $_POST['password'] == $_POST['newpassword']) { $this->_DisplayLoginTemplate($this->Language->Get('msgpwpolicy'), true); return false; } else {  if (!$_SWIFT_StaffObject->ChangePassword($_POST['newpassword'])) { $this->_DisplayLoginTemplate($this->Language->Get('passwordexpired'), true); return false; } } } else { $this->_DisplayLoginTemplate($this->Language->Get('passwordexpired'), true); return false; } } else if (!isset($_staffCache[$_SWIFT_StaffObject->GetStaffID()])) { $this->_DisplayLoginTemplate('Invalid Staff. Contact Kayako Support', false); return false; }  SWIFT_Session::InsertAndStart($_SWIFT_StaffObject->GetStaffID());  SWIFT_StaffLoginLog::Success($_SWIFT_StaffObject, $this->GetLoginLogInterface()); if (!isset($_SWIFT->Session) || !$_SWIFT->Session instanceof SWIFT_Session || !$_SWIFT->Session->GetIsClassLoaded()) { $this->_DisplayLoginTemplate(SWIFT::Get('errorstring')); return false; } if (!$_hasRememberMe) { $this->_ProcessRememberMe(); } $_redirectAction = SWIFT::Get('basename') . '/Base/Home/Index'; if (isset($_POST['_redirectAction']) && !empty($_POST['_redirectAction'])) { $_redirectAction = SWIFT::Get('basename') . $_POST['_redirectAction']; } $this->_Redirect($_redirectAction); return true; } else {  SWIFT_StaffLoginLog::Failure($_staffUsername, $this->GetLoginLogInterface()); $this->_DeleteRememberMeCookies(); $_errorString = ''; if ($_SWIFT->Settings->Get('security_loginlocked') == '1') { $_loginLogCheck = SWIFT_StaffLoginLog::CanStaffLogin($_staffUsername); if ($_loginLogCheck[0] && $_loginLogCheck[1] > 0) {  $this->Template->Assign('_loginLogCheckWarning', true); $this->Template->Assign('_loginAttemptCount', $_loginLogCheck[1]); $_errorStringText = $this->Language->Get('loginlogwarning'); if (SWIFT::Get('errorstring')) { $_errorStringText = SWIFT::Get('errorstring'); } $_errorString = sprintf($_errorStringText, $_loginLogCheck[1], SWIFT_StaffLoginLog::GetLoginRetries()); } else { $_errorString = sprintf($this->Language->Get('loginlogerror'), round(SWIFT_StaffLoginLog::GetLoginTimeline()/60), $_loginLogCheck[1], SWIFT_StaffLoginLog::GetLoginRetries()); } } else { $_errorString = $this->Language->Get('invaliduser'); } $this->_DisplayLoginTemplate($_errorString); return false; } return false; }  protected function _Logout($_logoutType = false) { SWIFT_Session::Logout($this->Interface); $this->Cookie->Delete('documentheight'); if ($_logoutType == self::LOGOUTTYPE_SESSIONEXPIRED) { $_errorString = $this->Language->Get('invalid_sessionid'); } else { $_errorString = $this->Language->Get('loggedout'); } $this->_DeleteRememberMeCookies(); $this->_DisplayLoginTemplate($_errorString); return true; }  public function _CSS() { header('Content-Type: text/css'); $this->Template->Render('css'); return true;; }  public function _Compressor($_dispatchType, $_fileList) { if (!$this->GetIsClassLoaded()) { throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED); return false; } $this->Load->Library('Compressor:Compressor'); $this->Compressor->Dispatch($_dispatchType, $_fileList); return true; } } ?>