3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Regex fragment for pulling an arbitrarily-formated nickname.
25 * Not guaranteed to be valid after normalization; run the string through
26 * Nickname::normalize() to get the canonical form, or Nickname::isValid()
27 * if you just need to check if it's properly formatted.
29 * This and CANONICAL_FMT replace the old NICKNAME_FMT, but be aware
30 * that these should not be enclosed in []s.
32 const DISPLAY_FMT = '[0-9a-zA-Z_]+';
35 * Regex fragment for checking a canonical nickname.
37 * Any non-matching string is not a valid canonical/normalized nickname.
38 * Matching strings are valid and canonical form, but may still be
39 * unavailable for registration due to blacklisting et.
41 * Only the canonical forms should be stored as keys in the database;
42 * there are multiple possible denormalized forms for each valid
43 * canonical-form name.
45 * This and DISPLAY_FMT replace the old NICKNAME_FMT, but be aware
46 * that these should not be enclosed in []s.
48 const CANONICAL_FMT = '[0-9a-z]{1,64}';
51 * Maximum number of characters in a canonical-form nickname.
56 * Nice simple check of whether the given string is a valid input nickname,
57 * which can be normalized into an internally canonical form.
59 * Note that valid nicknames may be in use or reserved.
64 public static function isValid($str)
67 self::normalize($str);
69 } catch (NicknameException $e) {
75 * Validate an input nickname string, and normalize it to its canonical form.
76 * The canonical form will be returned, or an exception thrown if invalid.
79 * @return string Normalized canonical form of $str
81 * @throws NicknameException (base class)
82 * @throws NicknameInvalidException
83 * @throws NicknameEmptyException
84 * @throws NicknameTooLongException
86 public static function normalize($str)
89 $str = str_replace('_', '', $str);
90 $str = mb_strtolower($str);
92 $len = mb_strlen($str);
94 throw new NicknameEmptyException();
95 } else if ($len > self::MAX_LEN) {
96 throw new NicknameTooLongException();
98 if (!self::isCanonical($str)) {
99 throw new NicknameInvalidException();
106 * Is the given string a valid canonical nickname form?
111 public static function isCanonical($str)
113 return preg_match('/^(?:' . self::CANONICAL_FMT . ')$/', $str);
117 class NicknameException extends ClientException
119 function __construct($msg=null, $code=400)
122 $msg = $this->defaultMessage();
124 parent::__construct($msg, $code);
128 * Default localized message for this type of exception.
131 protected function defaultMessage()
137 class NicknameInvalidException extends NicknameException {
139 * Default localized message for this type of exception.
142 protected function defaultMessage()
144 // TRANS: Validation error in form for registration, profile and group settings, etc.
145 return _('Nickname must have only lowercase letters and numbers and no spaces.');
149 class NicknameEmptyException extends NicknameException
152 * Default localized message for this type of exception.
155 protected function defaultMessage()
157 // TRANS: Validation error in form for registration, profile and group settings, etc.
158 return _('Nickname cannot be empty.');
162 class NicknameTooLongException extends NicknameInvalidException
165 * Default localized message for this type of exception.
168 protected function defaultMessage()
170 // TRANS: Validation error in form for registration, profile and group settings, etc.
171 return sprintf(_m('Nickname cannot be more than %d character long.',
172 'Nickname cannot be more than %d characters long.',