Grav\Plugin\Admin\AdminController::taskForgot PHP Method

taskForgot() protected method

Handle the email password recovery procedure.
protected taskForgot ( ) : boolean
return boolean True if the action was performed.
    protected function taskForgot()
    {
        $param_sep = $this->grav['config']->get('system.param_sep', ':');
        $post = $this->post;
        $data = $this->data;
        $username = isset($data['username']) ? strip_tags(strtolower($data['username'])) : '';
        $user = !empty($username) ? User::load($username) : null;
        if (!isset($this->grav['Email'])) {
            $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
            $this->setRedirect($post['redirect']);
            return true;
        }
        if (!$user || !$user->exists()) {
            $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL'), 'info');
            $this->setRedirect($post['redirect']);
            return true;
        }
        if (empty($user->email)) {
            $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL'), 'info');
            $this->setRedirect($post['redirect']);
            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 = rtrim($this->grav['uri']->rootUrl(true), '/') . '/' . trim($this->admin->base, '/') . '/reset/task' . $param_sep . 'reset/user' . $param_sep . $username . '/token' . $param_sep . $token . '/admin-nonce' . $param_sep . Utils::getNonce('admin-form');
        $sitename = $this->grav['config']->get('site.title', 'Website');
        $from = $this->grav['config']->get('plugins.email.from');
        if (empty($from)) {
            $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
            $this->setRedirect($post['redirect']);
            return true;
        }
        $to = $user->email;
        $subject = $this->admin->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_SUBJECT', $sitename]);
        $content = $this->admin->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]);
        $body = $this->grav['twig']->processTemplate('email/base.html.twig', ['content' => $content]);
        $message = $this->grav['Email']->message($subject, $body, 'text/html')->setFrom($from)->setTo($to);
        $sent = $this->grav['Email']->send($message);
        if ($sent < 1) {
            $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_FAILED_TO_EMAIL'), 'error');
        } else {
            $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL'), 'info');
        }
        $this->setRedirect('/');
        return true;
    }