/** * Displays a form to enter in an email address; if this email address * is associated with a user then an email will be sent to them with * a reset code and details on how to reset their password (change it). * * @return string|bool */ public function forgotSection() { $this->setTitle(t('Forgotten your password?')); $form = new View_form('pwd/form_forgotten.html', 'session'); $form->addElement('session/email', null, t('Email'), new Validator_Email()); if ($form->hasInput() && $form->isValid()) { /** * Check users exists, get details and send email */ $pdoSt = $this->_sql->prepare('SELECT id FROM {PREFIX}users WHERE email = ?'); $pdoSt->execute(array($form->getValues('session/email'))); $uid = $pdoSt->fetchColumn(); $pdoSt->closeCursor(); try { $user = $this->_ugmanager->getUser($uid); // Generate a reset code that is unique $pdoSt = $this->_sql->prepare('SELECT COUNT(uid) FROM {PREFIX}users_meta WHERE name = "sessionResetCode" AND value = ?'); do { $resetCode = zula_create_key(); $pdoSt->execute(array($resetCode)); } while ($pdoSt->fetchColumn() >= 1); $pdoSt->closeCursor(); // Update user account and attempt to send the email $this->_ugmanager->editUser($user['id'], array('sessionResetCode' => $resetCode)); $msgView = $this->loadView('pwd/email_forgotten.txt'); $msgView->assign(array('code' => $resetCode, 'user' => $user)); $message = new Email_Message(t('Forgotten password'), $msgView->getOutput()); $message->setTo($user['email']); $email = new Email(); $email->send($message); $this->_event->success(t("An email has been sent to the users email address")); return zula_redirect($this->_router->makeUrl('session')); } catch (Ugmanager_UserNoExist $e) { $this->_event->error(t('The provided email does not exist')); } catch (Email_Exception $e) { $this->_event->error(t('An error occurred while sending the email. Please try again later')); } } return $form->getOutput(); }
/** * Displays and handles the form for new users to register an account * * @return string */ public function indexSection() { $this->setTitle(t('Register an account')); // Check that registrations are actually available if ($this->_config->get('session/allow_register') == false) { throw new Module_ControllerNoExist(); } else { if ($this->_config->get('session/force_https')) { $formUrl = $this->_router->makeUrl('session', 'register')->makeFull('&', null, true); if ($this->_router->getScheme() != 'https') { return zula_redirect($formUrl); } } else { $formUrl = $this->_router->makeUrl('session', 'register'); } } // Build the form and prepare validation $form = new View_Form('register/form.html', 'session'); $form->action($formUrl)->antispam(true); $form->addElement('session/username', null, t('Username'), array(new Validator_Alphanumeric('_()!:@.^-'), new Validator_Length(2, 32), array($this, 'validateUsername'))); $form->addElement('session/password', null, t('Password'), array(new Validator_Length(4, 64), new Validator_Confirm('session/password_confirm', Validator_Confirm::_POST))); $form->addElement('session/email', null, t('Email'), array(new Validator_Email(), new Validator_Confirm('session/email_confirm', Validator_Confirm::_POST), array($this, 'validateEmail'))); $form->addElement('session/terms_agree', null, t('Terms'), new Validator_Bool(), false); if ($form->hasInput()) { if ($this->_config->get('session/register_terms') && !$this->_input->has('post', 'session/terms')) { $this->_event->error(t('Please agree to the terms and conditions')); $hasTerms = false; } else { $hasTerms = true; } if ($form->isValid() && $hasTerms) { /** * Attempt to add the new user and send correct email */ $fd = $form->getValues('session'); $userDetails = array('status' => 'locked', 'username' => $fd['username'], 'password' => $fd['password'], 'email' => $fd['email'], 'group' => $this->_config->get('session/register_group'), 'activate_code' => zula_create_key()); $validationMethod = $this->_config->get('session/validation_method'); switch ($validationMethod) { case 'none': $userDetails['status'] = 'active'; $userDetails['activate_code'] = ''; $eventMsg = t('Successfully registered, you may now login.'); break; case 'admin': $eventMsg = t('Successfully registered, an admin will review your registration shortly.'); break; case 'user': default: $validationMethod = 'user'; # Ensure a known validation method. $eventMsg = t('Successfully registered, an email has been sent to confirm your registration.'); } // Add the new user and attempt to send the email. $uid = $this->_ugmanager->addUser($userDetails); try { $msgView = $this->loadView('register/validation_' . $validationMethod . '.txt'); $msgView->assign($userDetails); $message = new Email_Message(t('Account Details'), $msgView->getOutput()); $message->addTo($userDetails['email']); $email = new Email(); $email->send($message); // All done, redirect user $this->_event->success($eventMsg); return zula_redirect($this->_router->makeUrl('session')); } catch (Email_Exception $e) { $this->_ugmanager->deleteUser($uid); $this->_event->error(t('An error occurred while sending the email. Please try again later')); $this->_log->message('Unable to send registration email: ' . $e->getMessage(), Log::L_WARNING); } } } // Add T&Cs then output the form $form->assign(array('TERMS' => $this->_config->get('session/register_terms'))); return $form->getOutput(); }