Back to User class

Method check

public bool
check
()
Validation and filtering
Returns
  • bool True if satisfactory
Since
  • 1.7.0
Class: User
Project: Joomla

Method check - Source code

/**
 * Validation and filtering
 *
 * @return  boolean  True if satisfactory
 *
 * @since   1.7.0
 */
public function check()
{
    try {
        parent::check();
    } catch (\Exception $e) {
        $this->setError($e->getMessage());
        return false;
    }
    // Set user id to null instead of 0, if needed
    if ($this->id === 0) {
        $this->id = null;
    }
    $filterInput = InputFilter::getInstance();
    // Validate user information
    if ($filterInput->clean($this->name, 'TRIM') == '') {
        $this->setError(Text::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_YOUR_NAME'));
        return false;
    }
    if ($filterInput->clean($this->username, 'TRIM') == '') {
        $this->setError(Text::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_A_USER_NAME'));
        return false;
    }
    if (preg_match('#[<>"\'%;()&\\\\]|\\.\\./#', $this->username) || StringHelper::strlen($this->username) < 2 || $filterInput->clean($this->username, 'TRIM') !== $this->username || StringHelper::strlen($this->username) > 150) {
        $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_VALID_AZ09', 2));
        return false;
    }
    if ($filterInput->clean($this->email, 'TRIM') == '' || !MailHelper::isEmailAddress($this->email) || StringHelper::strlen($this->email) > 100) {
        $this->setError(Text::_('JLIB_DATABASE_ERROR_VALID_MAIL'));
        return false;
    }
    // Convert email to punycode for storage
    $this->email = PunycodeHelper::emailToPunycode($this->email);
    // Set the registration timestamp
    if (empty($this->registerDate)) {
        $this->registerDate = Factory::getDate()->toSql();
    }
    // Set the lastvisitDate timestamp
    if (empty($this->lastvisitDate)) {
        $this->lastvisitDate = null;
    }
    // Set the lastResetTime timestamp
    if (empty($this->lastResetTime)) {
        $this->lastResetTime = null;
    }
    $uid = (int) $this->id;
    // Check for existing username
    $query = $this->_db->getQuery(true)->select($this->_db->quoteName('id'))->from($this->_db->quoteName('#__users'))->where($this->_db->quoteName('username') . ' = :username')->where($this->_db->quoteName('id') . ' != :userid')->bind(':username', $this->username)->bind(':userid', $uid, ParameterType::INTEGER);
    $this->_db->setQuery($query);
    $xid = (int) $this->_db->loadResult();
    if ($xid && $xid != (int) $this->id) {
        $this->setError(Text::_('JLIB_DATABASE_ERROR_USERNAME_INUSE'));
        return false;
    }
    // Check for existing email
    $query->clear()->select($this->_db->quoteName('id'))->from($this->_db->quoteName('#__users'))->where('LOWER(' . $this->_db->quoteName('email') . ') = LOWER(:mail)')->where($this->_db->quoteName('id') . ' != :muserid')->bind(':mail', $this->email)->bind(':muserid', $uid, ParameterType::INTEGER);
    $this->_db->setQuery($query);
    $xid = (int) $this->_db->loadResult();
    if ($xid && $xid != (int) $this->id) {
        $this->setError(Text::_('JLIB_DATABASE_ERROR_EMAIL_INUSE'));
        return false;
    }
    // Check for root_user != username
    $rootUser = Factory::getApplication()->get('root_user');
    if (!is_numeric($rootUser)) {
        $query->clear()->select($this->_db->quoteName('id'))->from($this->_db->quoteName('#__users'))->where($this->_db->quoteName('username') . ' = :username')->bind(':username', $rootUser);
        $this->_db->setQuery($query);
        $xid = (int) $this->_db->loadResult();
        if ($rootUser == $this->username && (!$xid || $xid && $xid != (int) $this->id) || $xid && $xid == (int) $this->id && $rootUser != $this->username) {
            $this->setError(Text::_('JLIB_DATABASE_ERROR_USERNAME_CANNOT_CHANGE'));
            return false;
        }
    }
    return true;
}