/**
  * Handle the email password recovery procedure.
  *
  * @return bool True if the action was performed.
  */
 protected function taskForgot()
 {
     $param_sep = $this->grav['config']->get('system.param_sep', ':');
     $data = $this->post;
     $username = isset($data['username']) ? $data['username'] : '';
     $user = !empty($username) ? User::load($username) : null;
     /** @var Language $l */
     $language = $this->grav['language'];
     $messages = $this->grav['messages'];
     if (!isset($this->grav['Email'])) {
         $messages->add($language->translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
         $this->setRedirect('/');
         return true;
     }
     if (!$user || !$user->exists()) {
         $messages->add($language->translate(['PLUGIN_ADMIN.FORGOT_USERNAME_DOES_NOT_EXIST', $username]), 'error');
         $this->setRedirect('/forgot');
         return true;
     }
     if (empty($user->email)) {
         $messages->add($language->translate(['PLUGIN_ADMIN.FORGOT_CANNOT_RESET_EMAIL_NO_EMAIL', $username]), 'error');
         $this->setRedirect('/forgot');
         return true;
     }
     $token = md5(uniqid(mt_rand(), true));
     $expire = time() + 604800;
     // next week
     $user->reset = $token . '::' . $expire;
     $user->save();
     $author = $this->grav['config']->get('site.author.name', '');
     $fullname = $user->fullname ?: $username;
     $reset_link = $this->grav['base_url_absolute'] . $this->grav['config']->get('plugins.login.route_reset') . '/task:login.reset/token' . $param_sep . $token . '/user' . $param_sep . $username . '/nonce' . $param_sep . Utils::getNonce('reset-form');
     $sitename = $this->grav['config']->get('site.title', 'Website');
     $from = $this->grav['config']->get('plugins.email.from');
     if (empty($from)) {
         $messages->add($language->translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
         $this->setRedirect('/forgot');
         return true;
     }
     $to = $user->email;
     $subject = $language->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_SUBJECT', $sitename]);
     $content = $language->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]);
     $sent = LoginUtils::sendEmail($subject, $content, $to);
     if ($sent < 1) {
         $messages->add($language->translate('PLUGIN_ADMIN.FORGOT_FAILED_TO_EMAIL'), 'error');
     } else {
         $messages->add($language->translate(['PLUGIN_ADMIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL', $to]), 'info');
     }
     $this->setRedirect('/');
     return true;
 }
Exemple #2
0
 /**
  * Handle the email to activate the user account.
  *
  * @return bool True if the action was performed.
  */
 protected function sendActivationEmail($user)
 {
     if (empty($user->email)) {
         throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.USER_NEEDS_EMAIL_FIELD'));
     }
     $token = md5(uniqid(mt_rand(), true));
     $expire = time() + 604800;
     // next week
     $user->activation_token = $token . '::' . $expire;
     $user->save();
     $param_sep = $this->grav['config']->get('system.param_sep', ':');
     $activation_link = $this->grav['base_url_absolute'] . $this->config->get('plugins.login.route_activate') . '/token' . $param_sep . $token . '/username' . $param_sep . $user->username . '/nonce' . $param_sep . Utils::getNonce('user-activation');
     $sitename = $this->grav['config']->get('site.title', 'Website');
     $subject = $this->grav['language']->translate(['PLUGIN_LOGIN.ACTIVATION_EMAIL_SUBJECT', $sitename]);
     $content = $this->grav['language']->translate(['PLUGIN_LOGIN.ACTIVATION_EMAIL_BODY', $user->username, $activation_link, $sitename]);
     $to = $user->email;
     $sent = LoginUtils::sendEmail($subject, $content, $to);
     if ($sent < 1) {
         throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.EMAIL_SENDING_FAILURE'));
     }
     return true;
 }