/** * Validate a username against all availability requirements. * * @param $username * @return array [ valid: true/false, message: 'Reason why invalid' ] */ protected function _checkUsername($username) { $username = trim($username); $lower = User::getLowerCase($username); // Username must exist. if (empty($username)) { return array('valid' => FALSE, 'message' => 'Username not specified.'); } // Username must only contain letters, numbers, -_~. if (!preg_match('/^([a-zA-Z0-9_.~-]+)$/', $username)) { return array('valid' => FALSE, 'message' => 'Username contains invalid characters. Only letters and numbers, dash, underscore, tilde and a period are allowed.'); } // Username must be at least 3 characters long. if (strlen($lower) < 3) { return array('valid' => FALSE, 'message' => 'Username must contain at least three alphanumeric characters.'); } // Username must not start with a period. if ($lower[0] == '.') { return array('valid' => FALSE, 'message' => 'Usernames must not start with a period.'); } // Username must not contain forbidden words. $fa_settings = $this->di->get('fa')->settings; $blocked_words = explode(' ', strtolower(str_replace(array("\n", "\r"), array(' ', ''), $fa_settings['Account_Name_Blocklist']))); $found = FALSE; $word = null; foreach ($blocked_words as $word) { if (trim($word) and strpos($username, $word) !== FALSE || strpos(strtolower($username), $word) !== FALSE) { $found = TRUE; break; } } if ($found) { return array('valid' => FALSE, 'message' => 'The word "' . $word . '" is forbidden in usernames.'); } // Check if account exists. $existing_user = User::getRepository()->findOneBy(array('lower' => $lower)); if ($existing_user instanceof User) { if ($existing_user->accesslevel == User::LEGACY_ACL_BANNED) { return array('valid' => FALSE, 'message' => 'This username already exists and is banned.'); } else { return array('valid' => FALSE, 'message' => 'This username already exists!'); } } // Check if reservation exists. $existing_reservation = RegistrationRequest::getRepository()->findOneBy(array('lower' => $lower)); if ($existing_reservation instanceof RegistrationRequest) { // Reservations more than 24 hours old are expired. if ($existing_reservation->created_at >= time() - 86400) { return array('valid' => FALSE, 'message' => 'A registration request already exists for this username. Check your e-mail for more information!'); } } // Return valid if none of the above checks failed! return array('valid' => TRUE, 'message' => ''); }
/** * Set the username and lower-case version together. * @param $username */ public function setUsername($username) { $this->username = $username; $this->lower = User::getLowerCase($username); }
/** * Adds support for generating user icons for the following shortcuts: * :iconusername:, :linkusername:, @username and @@username * (Note: this function assumes the text has been run through filter().) * * @param $string * @return mixed */ public function userIcons($string) { // :iconusername: $string = preg_replace_callback('|\\:icon([-\\w\\d_\\[\\]\\^`~.]+?)\\:|i', function ($matches) { $username = $matches[1]; $user_url = $this->url->get('user/' . User::getLowerCase($username)); $user_avatar = User::getUserAvatar($username, time()); return '<a href="' . $user_url . '" class="iconusername"><img src="' . $user_avatar . '" align="middle" title="' . $username . '" alt="' . $username . '"> ' . $username . '</a>'; }, $string); // :usernameicon: $string = preg_replace_callback('|\\:([-\\w\\d_\\[\\]\\^`~.]+?)icon\\:|i', function ($matches) { $username = $matches[1]; $user_url = $this->url->get('user/' . User::getLowerCase($username)); $user_avatar = User::getUserAvatar($username, time()); return '<a href="' . $user_url . '" class="iconusername"><img src="' . $user_avatar . '" align="middle" title="' . $username . '" alt="' . $username . '"></a>'; }, $string); // @@username $string = preg_replace_callback('!(^|\\s)@@([-\\w\\d_\\[\\]\\^`~.]{2,})(?=$|\\s|[<:])!mi', function ($matches) { $username = $matches[2]; $user_url = $this->url->get('user/' . User::getLowerCase($username)); $user_avatar = User::getUserAvatar($username, time()); return '<a href="' . $user_url . '" class="iconusername"><img src="' . $user_avatar . '" align="middle" title="' . $username . '" alt="' . $username . '" /> ' . $username . '</a>'; }, $string); // @username $string = preg_replace_callback('!(^|\\s)@([-\\w\\d_\\[\\]\\^`~.]{2,})(?=$|\\s|[<:])!mi', function ($matches) { $username = $matches[2]; $user_url = $this->url->get('user/' . User::getLowerCase($username)); return '<a href="' . $user_url . '" class="linkusername">' . $username . '</a>'; }, $string); // :linkusername: $string = preg_replace_callback('|\\:link([-\\w\\d_\\[\\]\\^`~.]+?)\\:|i', function ($matches) { $username = $matches[1]; $user_url = $this->url->get('user/' . User::getLowerCase($username)); return '<a href="' . $user_url . '" class="linkusername">' . $username . '</a>'; }, $string); return $string; }