_usersFile = APPLICATION_PATH . DS . 'configs' . DS . 'users.ini'; $this->_authStorage = new Zend_Auth_Storage_Session(); $auth = $this->_authStorage->read(); if (!empty($auth)) { if (isset($auth['name'])) { $this->_userName = $auth['name']; } if (isset($auth['id'])) { $this->_userId = $auth['id']; } if ($this->_userName !== null && $this->_userId !== null) { $this->_isLoggedIn = true; } } else { $this->_loginByCookie(); } } /** * Returns the messages which comes from Zend_Auth_Result. * * @return array */ public function getAuthMessages() { return $this->_authMessages; } /** * Return the loggedin status. * * @return boolean */ public function isLoggedIn() { return $this->_isLoggedIn; } /** * Login the user with the given identity and credentials. * Set cookie if automatic login is wanted. * * Returns true if login was successful, otherwise false. * * @param string $username Identity for login process. * @param string $password Credentials for login procress. * @param boolean $autoLogin Set cookie for automatic login? * * @return int */ public function login($username, $password, $autoLogin = false) { if (!file_exists($this->_usersFile)) { return self::NO_USER_FILE; } $usersIni = new Msd_Ini($this->_usersFile); $users = $usersIni->getAll(); $hasValidUser = false; foreach ($users as $user) { if (isset($user['name']) && isset($user['pass'])) { $hasValidUser = true; break; } } if (!$hasValidUser) { return self::NO_VALID_USER; } $authAdapter = new Msd_Auth_Adapter_Ini($this->_usersFile); $authAdapter->setUsername($username); $authAdapter->setPassword($password); $auth = Zend_Auth::getInstance(); $authResult = $auth->authenticate($authAdapter); $this->_authMessages = $authResult->getMessages(); if ($authResult->isValid()) { $this->_isLoggedIn = true; if ($autoLogin) { Zend_Session::regenerateId(); $crypt = Msd_Crypt::getInstance('MySQLDumper27112010'); $identity = $crypt->encrypt( $username . ':' . $password ); if (PHP_SAPI != 'cli') { setcookie( 'msd_autologin', $identity . ':' . md5($identity), time() + 365 * 24 * 60 * 60, '/' ); } } $this->setDefaultConfiguration(); return self::SUCCESS; } return self::UNKNOWN_IDENTITY; } private function _loginByCookie() { $request = Zend_Controller_Front::getInstance()->getRequest(); $cookie = $request->get('msd_autologin'); if ($cookie === null || $cookie == '') { // no cookie found return false; } list($authInfo, $checksum) = explode(':', $cookie); if (md5($authInfo) != $checksum) { // autologin not valid - return return false; } $crypt = Msd_Crypt::getInstance('MySQLDumper27112010'); list($username, $pass) = explode(':', $crypt->decrypt($authInfo)); // Try to login the user and refresh the cookie. Because you want // to stay logged in until you logout. $this->login($username, $pass, true); } /** * Clear the user identity and logout the user. * * @return void */ public function logout() { Zend_Auth::getInstance()->clearIdentity(); $this->_isLoggedIn = false; $this->setDefaultConfiguration(); } /** * Set default configuration for user * * @return void */ public function setDefaultConfiguration() { $configFile = 'defaultConfig'; if ($this->_isLoggedIn) { $files = Msd_File::getConfigNames(); if (isset($files[0])) { $configFile = $files[0]; } } Msd_Configuration::getInstance($configFile, true); } }