yf_register user module

Users registration.

Fileplugins/user/modules/yf_register.class.php
Usemodule('register'), URL ?object=register

Public properties

A site sets them in its config with $PROJECT_CONF['register']['NAME'] = ...;

PropertyDefaultDescription
$CONFIRM_REGISTER null
$CONFIRM_TTL null
$_account_types null

Public methods 11

confirm

public function confirm()

Confirm registration for common users.

    public function confirm()
    {
        $body = $replace3 = $replace4 = null;
        // Send registration confirmation email
        if ( ! $this->CONFIRM_REGISTER) {
            return tpl()->parse('@object/confirm_messages', ['msg' => 'confirm_not_needed']);
        }
        // Check confirmation code
        if ( ! strlen(($_GET['id'] ?? ''))) {
            return _e('Confirmation ID is required!');
        }
        // Decode confirmation number
        list($user_id, $member_date) = array_pad(explode('wvcn', trim(base64_decode(($_GET['id'] ?? '')))), 2, null);
        $user_id = (int) $user_id;
        $member_date = (int) $member_date;
        // Get target user info
        if ( ! empty($user_id)) {
            $target_user_info = user($user_id);
        }
        // User id is required
        if (empty($target_user_info['id'])) {
            return _e('Wrong user ID');
        }
        // Check if user already confirmed
        if ($target_user_info['active']) {
            return tpl()->parse('@object/confirm_messages', ['msg' => 'already_confirmed']);
        }
        // Check if code is expired
        if ( ! common()->_error_exists()) {
            if ( ! empty($member_date) && (time() - $member_date) > $this->CONFIRM_TTL) {
                _re('Confirmation code has expired.');
            }
        }
        if ( ! common()->_error_exists()) {
            if (($_GET['id'] ?? null) != $target_user_info['verify_code']) {
                _re('Wrong confirmation code');
            }
        }
        if ( ! common()->_error_exists()) {
            db()->update('user', ['active' => 1], $user_id);
            return tpl()->parse('@object/confirm_messages', ['msg' => 'confirm_success']);
        }
        $body = null;
        $replace3 = null;
        $replace4 = null;
        $body .= _e();
        $body .= tpl()->parse('@object/enter_code', $replace3);
        $body .= tpl()->parse('@object/resend_code', $replace4);
        return $body;
    }

enter_code

public function enter_code()

Manually enter code.

    public function enter_code()
    {
        // Do activate
        if (isset($_POST['confirm_code'])) {
            $_GET['id'] = ($_POST['confirm_code'] ?? null);
            return $this->confirm();
        }
        // Display form
        $replace = [
        ];
        return tpl()->parse('@object/enter_code', $replace);
    }

resend_code

public function resend_code()

Re-send confirmation code.

    public function resend_code()
    {
        $replace = null;
        if ( ! empty($_POST['email'])) {
            $user_info = db()->query_fetch('SELECT * FROM ' . db('user') . ' WHERE email="' . _es($_POST['email'] ?? null) . '"');
            if (empty($user_info)) {
                return _e('No such user');
            }
            if ($user_info['active']) {
                return 'Your account is already activated.';
            }
            if ( ! common()->_error_exists()) {
                $code = base64_encode($user_info['id'] . 'wvcn' . time());
                db()->update('user', ['verify_code' => $code], $user_info['id']);
                $replace = [
                    'nick' => $user_info['nick'],
                    'confirm_code' => $code,
                    'conf_link' => url('/@object/confirm&id=' . $code),
                    'conf_form_url' => url('/login_form/account_inactive'),
                    'admin_name' => SITE_ADVERT_NAME,
                    'advert_url' => SITE_ADVERT_URL,
                ];
                $text = $this->_email_text('@object/email_confirm_' . $this->_account_types[$user_info['group']], $replace, ['nick']);
                // Prepare email
                $email_from = SITE_ADMIN_EMAIL;
                $name_from = SITE_ADVERT_NAME;
                $email_to = $user_info['email'];
                $name_to = $user_info['nick'];
                $subject = t('Membership confirmation required!');
                $send_result = common()->send_mail($email_from, $name_from, $email_to, $name_to, $subject, $text, nl2br($text));
                if ($send_result) {
                    return 'Code sent. Please check your email.';
                }
                return 'Error sending mail. Please contact site admin.';
            }
        }
        return tpl()->parse('@object/resend_code', $replace);
    }

show

public function show()
    public function show()
    {
        $validate_rules = [
            '__form_id__' => 'register_form',
            'login' => ['trim|required|min_length[2]|max_length[12]|ajax_is_unique[user.login]|xss_clean', function ($in) {
                return module('register')->_login_not_exists($in);
            }],
            'email' => ['trim|required|valid_email|ajax_is_unique[user.email]', function ($in) {
                return module('register')->_email_not_exists($in);
            }],
            'emailconf' => 'trim|required|valid_email|matches[email]',
            'password' => 'trim|required|password_store',
            'pswdconf' => 'trim|required|matches[password]', // |md5
            'captcha' => 'trim|captcha',
        ];
        $a = $_POST;
        $a['redirect_link'] = url('/@object/success');
        // TODO: generate confirmation code and send emails
        return form($a, ['legend' => 'Registration', 'class_add' => 'col-md-6'])
            ->validate($validate_rules)
            ->db_insert_if_ok('user', ['login', 'email', 'password'], null, ['on_success_text' => 'Your account was created successfully!'])
            ->login(['pattern' => '^[a-zA-Z0-9]{4,32}$', 'title' => 'Only letters and numbers allowed, min 4, max 32 symbols'])
            ->email()
            ->email('emailconf')
            ->password()
            ->password('pswdconf')
            ->captcha()
            ->save()
            ->container(module('login_form')->oauth(['only_icons' => 1]), ['wide' => 0]);
    }

success

public function success()
    public function success()
    {
        return common()->show_notices();
    }

Methods starting with _ hooks and helpers; not routable as page actions

public function _account_type_suffix()

'_<type>' of the posted account type for an email template name: a known type (_account_types, else the account_types data), or with no known types a plain name; '' otherwise, as the value is part of a template path.

    public function _account_type_suffix()
    {
        $type = $_POST['account_type'] ?? '';
        if (empty($type) || ! is_string($type)) {
            return '';
        }
        $known = (array) ($this->_account_types ?: main()->get_data('account_types'));
        $ok = $known ? in_array($type, $known, true) : (bool) preg_match('/^[a-z0-9_]+\z/i', $type);
        return $ok ? '_' . $type : '';
    }
public function _email_not_exists($in = '')
    public function _email_not_exists($in = '')
    {
        return ! db()->get_one('SELECT id FROM ' . db('user') . ' WHERE email="' . db()->es($in) . '"');
    }

_email_text

public function _email_text($stpl, array $replace, array $user_keys)

Email text from a template. parse() runs tags over the {var} values it put in too, so the request / user values ($user_keys) go in with escaped braces; a mail is no HTML page, so they turn back into braces after the parse.

  • $replace array
  • $user_keys array
    public function _email_text($stpl, array $replace, array $user_keys)
    {
        foreach ($user_keys as $key) {
            $replace[$key] = _escape_tpl_braces($replace[$key] ?? '');
        }
        return str_replace(['{', '}'], ['{', '}'], tpl()->parse($stpl, $replace));
    }
public function _login_not_exists($in = '')
    public function _login_not_exists($in = '')
    {
        return ! db()->get_one('SELECT id FROM ' . db('user') . ' WHERE login="' . db()->es($in) . '"');
    }
public function _send_email_with_code($code = '', $extra = false)

Send email with verification code.

    public function _send_email_with_code($code = '', $extra = false)
    {
        $identify = ! empty($extra['identify']) ? $extra['identify'] : ($_POST['email'] ?? null);
        $replace = [
            'nick' => $identify,
            'confirm_code' => $code,
            'conf_link' => url('/@object/confirm/' . $code),
            'conf_form_url' => url('/login_form/account_inactive'),
            'admin_name' => SITE_ADVERT_NAME,
            'advert_url' => SITE_ADVERT_URL,
        ];
        if (isset($extra['add_replace']) && is_array($extra['add_replace'])) {
            $replace = array_merge($replace, $extra['add_replace']);
        }
        $text = $this->_email_text('@object/email_confirm' . $this->_account_type_suffix(), $replace, ['nick']);
        // prepare email data
        $email_from = SITE_ADMIN_EMAIL;
        $name_from = SITE_ADVERT_NAME;
        $email_to = ! empty($extra['email']) ? $extra['email'] : ($_POST['email'] ?? null);
        $name_to = $identify;
        $subject = ! empty($extra['subject']) ? $extra['subject'] : t('Membership confirmation required!');
        return common()->send_mail($email_from, $name_from, $email_to, $name_to, $subject, $text, nl2br($text));
    }
public function _send_success_email($extra = false)

Send success email.

    public function _send_success_email($extra = false)
    {
        $code = null;
        $identify = ! empty($extra['identify']) ? $extra['identify'] : ($_POST['email'] ?? null);
        $replace = [
            'code' => null,
            'nick' => $identify,
            'password' => ($_POST['password'] ?? null),
            'advert_name' => SITE_ADVERT_NAME,
            'advert_url' => SITE_ADVERT_URL,
        ];
        $text = $this->_email_text('@object/email_success' . $this->_account_type_suffix(), $replace, ['nick', 'password']);
        // prepare email data
        $email_from = SITE_ADMIN_EMAIL;
        $name_from = SITE_ADVERT_NAME;
        $email_to = ! empty($extra['email']) ? $extra['email'] : ($_POST['email'] ?? null);
        $name_to = $identify;
        $subject = ! empty($extra['subject']) ? $extra['subject'] : t('Membership confirmation required!');
        return common()->send_mail($email_from, $name_from, $email_to, $name_to, $subject, $text, nl2br($text));
    }