]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/nickname.php
common_to_alphanumeric added, filtering Notice->source in classic layout
[quix0rs-gnu-social.git] / lib / nickname.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 class Nickname
21 {
22     /**
23      * Regex fragment for pulling a formated nickname *OR* ID number.
24      * Suitable for router def of 'id' parameters on API actions.
25      *
26      * Not guaranteed to be valid after normalization; run the string through
27      * Nickname::normalize() to get the canonical form, or Nickname::isValid()
28      * if you just need to check if it's properly formatted.
29      *
30      * This, DISPLAY_FMT, and CANONICAL_FMT should not be enclosed in []s.
31      *
32      * @fixme would prefer to define in reference to the other constants
33      */
34     const INPUT_FMT = '(?:[0-9]+|[0-9a-zA-Z_]{1,64})';
35
36     /**
37      * Regex fragment for acceptable user-formatted variant of a nickname.
38      *
39      * This includes some chars such as underscore which will be removed
40      * from the normalized canonical form, but still must fit within
41      * field length limits.
42      *
43      * Not guaranteed to be valid after normalization; run the string through
44      * Nickname::normalize() to get the canonical form, or Nickname::isValid()
45      * if you just need to check if it's properly formatted.
46      *
47      * This, INPUT_FMT and CANONICAL_FMT should not be enclosed in []s.
48      */
49     const DISPLAY_FMT = '[0-9a-zA-Z_]{1,64}';
50
51     /**
52      * Simplified regex fragment for acceptable full WebFinger ID of a user
53      *
54      * We could probably use an email regex here, but mainly we are interested
55      * in matching it in our URLs, like https://social.example/user@example.com
56      */
57     const WEBFINGER_FMT = '[0-9a-zA-Z_]{1,64}\@[0-9a-zA-Z_-.]{3,255}';
58
59     /**
60      * Regex fragment for checking a canonical nickname.
61      *
62      * Any non-matching string is not a valid canonical/normalized nickname.
63      * Matching strings are valid and canonical form, but may still be
64      * unavailable for registration due to blacklisting et.
65      *
66      * Only the canonical forms should be stored as keys in the database;
67      * there are multiple possible denormalized forms for each valid
68      * canonical-form name.
69      *
70      * This, INPUT_FMT and DISPLAY_FMT should not be enclosed in []s.
71      */
72     const CANONICAL_FMT = '[0-9a-z]{1,64}';
73
74     /**
75      * Maximum number of characters in a canonical-form nickname.
76      */
77     const MAX_LEN = 64;
78
79     /**
80      * Nice simple check of whether the given string is a valid input nickname,
81      * which can be normalized into an internally canonical form.
82      *
83      * Note that valid nicknames may be in use or reserved.
84      *
85      * @param string    $str       The nickname string to test
86      * @param boolean   $checkuse  Check if it's in use (return false if it is)
87      *
88      * @return boolean  True if nickname is valid. False if invalid (or taken if checkuse==true).
89      */
90     public static function isValid($str, $checkuse=false)
91     {
92         try {
93             self::normalize($str, $checkuse);
94         } catch (NicknameException $e) {
95             return false;
96         }
97
98         return true;
99     }
100
101     /**
102      * Validate an input nickname string, and normalize it to its canonical form.
103      * The canonical form will be returned, or an exception thrown if invalid.
104      *
105      * @param string    $str       The nickname string to test
106      * @param boolean   $checkuse  Check if it's in use (return false if it is)
107      * @return string Normalized canonical form of $str
108      *
109      * @throws NicknameException (base class)
110      * @throws   NicknameBlacklistedException
111      * @throws   NicknameEmptyException
112      * @throws   NicknameInvalidException
113      * @throws   NicknamePathCollisionException
114      * @throws   NicknameTakenException
115      * @throws   NicknameTooLongException
116      */
117     public static function normalize($str, $checkuse=false)
118     {
119         // We should also have UTF-8 normalization (å to a etc.)
120         $str = trim($str);
121         $str = str_replace('_', '', $str);
122         $str = mb_strtolower($str);
123
124         if (mb_strlen($str) > self::MAX_LEN) {
125             // Display forms must also fit!
126             throw new NicknameTooLongException();
127         } elseif (mb_strlen($str) < 1) {
128             throw new NicknameEmptyException();
129         } elseif (!self::isCanonical($str)) {
130             throw new NicknameInvalidException();
131         } elseif (self::isBlacklisted($str)) {
132             throw new NicknameBlacklistedException();
133         } elseif (self::isSystemPath($str)) {
134             throw new NicknamePathCollisionException();
135         } elseif ($checkuse) {
136             $profile = self::isTaken($str);
137             if ($profile instanceof Profile) {
138                 throw new NicknameTakenException($profile);
139             }
140         }
141
142         return $str;
143     }
144
145     /**
146      * Is the given string a valid canonical nickname form?
147      *
148      * @param string $str
149      * @return boolean
150      */
151     public static function isCanonical($str)
152     {
153         return preg_match('/^(?:' . self::CANONICAL_FMT . ')$/', $str);
154     }
155
156     /**
157      * Is the given string in our nickname blacklist?
158      *
159      * @param string $str
160      * @return boolean
161      */
162      public static function isBlacklisted($str)
163      {
164          $blacklist = common_config('nickname', 'blacklist');
165          return in_array($str, $blacklist);
166      }
167
168     /**
169      * Is the given string identical to a system path or route?
170      * This could probably be put in some other class, but at
171      * at the moment, only Nickname requires this functionality.
172      *
173      * @param string $str
174      * @return boolean
175      */
176      public static function isSystemPath($str)
177      {
178         $paths = array();
179
180         // All directory and file names in site root should be blacklisted
181         $d = dir(INSTALLDIR);
182         while (false !== ($entry = $d->read())) {
183             $paths[$entry] = true;
184         }
185         $d->close();
186
187         // All top level names in the router should be blacklisted
188         $router = Router::get();
189         foreach ($router->m->getPaths() as $path) {
190             if (preg_match('/^([^\/\?]+)[\/\?]/',$path,$matches) && isset($matches[1])) {
191                 $paths[$matches[1]] = true;
192             }
193         }
194
195         // FIXME: this assumes the 'path' is in the first-level directory, though common it's not certain
196         foreach (['avatar', 'attachments'] as $cat) {
197             $paths[basename(common_config($cat, 'path'))] = true;
198         }
199
200         return in_array($str, array_keys($paths));
201     }
202
203     /**
204      * Is the nickname already in use locally? Checks the User table.
205      *
206      * @param   string $str
207      * @return  Profile|null   Returns Profile if nickname found, otherwise null
208      */
209     public static function isTaken($str)
210     {
211         $found = User::getKV('nickname', $str);
212         if ($found instanceof User) {
213             return $found->getProfile();
214         }
215
216         $found = Local_group::getKV('nickname', $str);
217         if ($found instanceof Local_group) {
218             return $found->getProfile();
219         }
220
221         $found = Group_alias::getKV('alias', $str);
222         if ($found instanceof Group_alias) {
223             return $found->getProfile();
224         }
225
226         return null;
227     }
228 }
229
230 class NicknameException extends ClientException
231 {
232     function __construct($msg=null, $code=400)
233     {
234         if ($msg === null) {
235             $msg = $this->defaultMessage();
236         }
237         parent::__construct($msg, $code);
238     }
239
240     /**
241      * Default localized message for this type of exception.
242      * @return string
243      */
244     protected function defaultMessage()
245     {
246         return null;
247     }
248 }
249
250 class NicknameInvalidException extends NicknameException {
251     /**
252      * Default localized message for this type of exception.
253      * @return string
254      */
255     protected function defaultMessage()
256     {
257         // TRANS: Validation error in form for registration, profile and group settings, etc.
258         return _('Nickname must have only lowercase letters and numbers and no spaces.');
259     }
260 }
261
262 class NicknameEmptyException extends NicknameInvalidException
263 {
264     /**
265      * Default localized message for this type of exception.
266      * @return string
267      */
268     protected function defaultMessage()
269     {
270         // TRANS: Validation error in form for registration, profile and group settings, etc.
271         return _('Nickname cannot be empty.');
272     }
273 }
274
275 class NicknameTooLongException extends NicknameInvalidException
276 {
277     /**
278      * Default localized message for this type of exception.
279      * @return string
280      */
281     protected function defaultMessage()
282     {
283         // TRANS: Validation error in form for registration, profile and group settings, etc.
284         return sprintf(_m('Nickname cannot be more than %d character long.',
285                           'Nickname cannot be more than %d characters long.',
286                           Nickname::MAX_LEN),
287                        Nickname::MAX_LEN);
288     }
289 }
290
291 class NicknameBlacklistedException extends NicknameException
292 {
293     protected function defaultMessage()
294     {
295         // TRANS: Validation error in form for registration, profile and group settings, etc.
296         return _('Nickname is disallowed through blacklist.');
297     }
298 }
299
300 class NicknamePathCollisionException extends NicknameException
301 {
302     protected function defaultMessage()
303     {
304         // TRANS: Validation error in form for registration, profile and group settings, etc.
305         return _('Nickname is identical to system path names.');
306     }
307 }
308
309 class NicknameTakenException extends NicknameException
310 {
311     public $profile = null;    // the Profile which occupies the nickname
312
313     public function __construct(Profile $profile, $msg=null, $code=400)
314     {
315         $this->profile = $profile;
316
317         if ($msg === null) {
318             $msg = $this->defaultMessage();
319         }
320
321         parent::__construct($msg, $code);
322     }
323
324     protected function defaultMessage()
325     {
326         // TRANS: Validation error in form for registration, profile and group settings, etc.
327         return _('Nickname is already in use on this server.');
328     }
329 }