| // +---------------------------------------------------------------------------+ // $Id$ require_once 'Validate.php'; require_once 'DB/DataObject.php'; /** * Manages passwords. * * @package User * @author Demian Turner * @version $Revision: 1.26 $ */ class PasswordMgr extends SGL_Manager { function PasswordMgr() { SGL::logMessage(null, PEAR_LOG_DEBUG); parent::SGL_Manager(); $this->template = 'loginForgot.html'; $this->_aActionsMapping = array( 'retrieve' => array('retrieve', 'redirectToDefault'), 'forgot' => array('forgot'), ); } function validate($req, &$input) { SGL::logMessage(null, PEAR_LOG_DEBUG); $this->validated = true; $input->masterTemplate = $this->masterTemplate; $input->template = $this->template; $input->error = array(); $input->pageTitle = 'Retrieve password'; $input->action = ($req->get('action')) ? $req->get('action') : 'forgot'; $input->passwordOrig = $req->get('frmPasswordOrig'); $input->password = $req->get('frmPassword'); $input->passwordConfirm = $req->get('frmPasswordConfirm'); $input->question = $req->get('frmQuestion'); $input->answer = $req->get('frmAnswer'); $input->forgotEmail = $req->get('frmEmail'); $input->submitted = $req->get('submitted'); $aErrors = array(); // forgot password validation if ($input->submitted && ($input->action == 'forgot' || $input->action == 'retrieve')) { $v = & new Validate(); if (empty($input->forgotEmail)) { $aErrors['frmEmail'] = 'You must enter your email'; } else { if (!$v->email($input->forgotEmail)) { $aErrors['frmEmail'] = 'Your email is not correctly formatted'; } } if (empty($input->question)) { $aErrors['frmQuestion'] = 'You must choose a security question'; } if (empty($input->answer)) { $aErrors['frmAnswer'] = 'You must provide a security answer'; } // if errors have occured if (is_array($aErrors) && count($aErrors)) { SGL::raiseMsg('Please fill in the indicated fields'); $input->error = $aErrors; $this->validated = false; } unset($v); } } function display(&$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $output->aSecurityQuestions = SGL_String::translate('aSecurityQuestions'); } function _cmd_forgot(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); } function _cmd_retrieve(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $query = " SELECT * FROM " . $this->conf['table']['user'] ." WHERE email = " . $this->dbh->quote($input->forgotEmail) . " AND security_question = " . $input->question. " AND security_answer = '" . $input->answer . "'"; $userId = $this->dbh->getOne($query); if ($userId) { $aRet = $this->_resetPassword($userId); list($passwd, $oUser) = $aRet; $bEmailSent = $this->sendPassword($oUser, $passwd); if ($bEmailSent) { SGL::raiseMsg('password emailed out', true, SGL_MESSAGE_INFO); } else { SGL::raiseError('Problem sending email', SGL_ERROR_EMAILFAILURE); } // credentials not recognised } else { SGL::raiseMsg('email not in system'); } } function _cmd_redirectToEdit(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); // if no errors have occured, redirect if (!SGL_Error::count()) { SGL_HTTP::redirect(array('action' => 'edit')); // else display error with blank template } else { $output->template = 'error.html'; } } function _resetPassword($userId) { SGL::logMessage(null, PEAR_LOG_DEBUG); require_once 'Text/Password.php'; $oPassword = & new Text_Password(); $passwd = $oPassword->create(); $oUser = DB_DataObject::factory($this->conf['table']['user']); $oUser->get($userId); $oUser->passwd = md5($passwd); $oUser->update(); return array($passwd, $oUser); } /** * Wrapper for emailing password. * * @static * @param object $oUser * @param string $passwd * @return boolean */ function sendPassword($oUser, $passwd) { SGL::logMessage(null, PEAR_LOG_DEBUG); require_once SGL_CORE_DIR . '/Emailer.php'; $c = &SGL_Config::singleton(); $conf = $c->getAll(); $options = array( 'toEmail' => $oUser->email, 'fromEmail' => $conf['email']['admin'], 'replyTo' => $conf['email']['admin'], 'subject' => 'Password reminder from ' . $conf['site']['name'], 'template' => SGL_THEME_DIR . '/' . $_SESSION['aPrefs']['theme'] . '/user/email_forgot.php', 'username' => $oUser->username, 'password' => $passwd, ); $message = & new SGL_Emailer($options); $ok = $message->prepare(); return ($ok) ? $message->send() : $ok; } } ?>