<commit_before><?php

class AdminLoginForm extends MemberLoginForm
{

    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
    {
        parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);

        if ($this->Actions()->fieldByName('forgotPassword')) {
            // replaceField won't work, since it's a dataless field
            $this->Actions()->removeByName('forgotPassword');
            $this->Actions()->push(new LiteralField(
                'forgotPassword',
                '<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">'
                . _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>'
            ));
        }

        Requirements::customScript(<<<JS
			(function() {
				var el = document.getElementById("AdminLoginForm_LoginForm_Email");
				if(el && el.focus) el.focus();
			})();
JS
        );
    }

    /**
     * @param array $data
     */
    public function forgotPassword($data)
    {
        $email = Convert::raw2sql($data['Email']);

        /* @var $member Member */
        $member = Member::get()->where("Email = '{$email}'")->first();

        if ($member) {
            $token = $member->generateAutologinTokenAndStoreHash();

            /* @var $email Member_ForgotPasswordEmail */
            $email = Member_ForgotPasswordEmail::create();
            $email->populateTemplate($member);
            $email->populateTemplate(array(
                'PasswordResetLink' => AdminSecurity::getPasswordResetLink($member, $token)
            ));
            $email->setTo($member->Email);
            $email->send();

            $this->controller->redirect('AdminSecurity/passwordsent/' . urlencode($data['Email']));
        } elseif ($data['Email']) {
            // Avoid information disclosure by displaying the same status,
            // regardless wether the email address actually exists
            $this->controller->redirect('AdminSecurity/passwordsent/' . urlencode($data['Email']));
        } else {
            $this->sessionMessage(
                _t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'),
                'bad'
            );

            $this->controller->redirect('AdminSecurity/lostpassword');
        }
    }
}<commit_msg>Move email sending to separate method<commit_after><?php

class AdminLoginForm extends MemberLoginForm
{

    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
    {
        parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);

        if ($this->Actions()->fieldByName('forgotPassword')) {
            // replaceField won't work, since it's a dataless field
            $this->Actions()->removeByName('forgotPassword');
            $this->Actions()->push(new LiteralField(
                'forgotPassword',
                '<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">'
                . _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>'
            ));
        }

        Requirements::customScript(<<<JS
			(function() {
				var el = document.getElementById("AdminLoginForm_LoginForm_Email");
				if(el && el.focus) el.focus();
			})();
JS
        );
    }

    /**
     * @param array $data
     */
    public function forgotPassword($data)
    {
        $email = Convert::raw2sql($data['Email']);

        /* @var $member Member */
        $member = Member::get()->where("Email = '{$email}'")->first();

        if ($member) {
            $token = $member->generateAutologinTokenAndStoreHash();

            $this->sendPasswordResetLinkEmail($member, $token);

            $this->controller->redirect('AdminSecurity/passwordsent/' . urlencode($data['Email']));
        } elseif ($data['Email']) {
            // Avoid information disclosure by displaying the same status,
            // regardless wether the email address actually exists
            $this->controller->redirect('AdminSecurity/passwordsent/' . urlencode($data['Email']));
        } else {
            $this->sessionMessage(
                _t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'),
                'bad'
            );

            $this->controller->redirect('AdminSecurity/lostpassword');
        }
    }

    /**
     * @param $member
     * @param $token
     */
    protected function sendPasswordResetLinkEmail($member, $token)
    {
        /* @var $email Member_ForgotPasswordEmail */
        $email = Member_ForgotPasswordEmail::create();
        $email->populateTemplate($member);
        $email->populateTemplate(array(
            'PasswordResetLink' => AdminSecurity::getPasswordResetLink($member, $token)
        ));
        $email->setTo($member->Email);
        $email->send();
    }

}
{'fraction_non_alphanumeric': 0.15096272019664073, 'fraction_numerical': 0.00040966816878328555, 'mean_word_length': 2.4146853146853147, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3589568', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1270, 'n_words': 253}