123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- class Nickname
- {
-
- const INPUT_FMT = '(?:[0-9]+|[0-9a-zA-Z_]{1,64})';
-
- const DISPLAY_FMT = '[0-9a-zA-Z_]{1,64}';
-
- const WEBFINGER_FMT = '(?:\w+[\w\-\_\.]*)?\w+\@'.URL_REGEX_DOMAIN_NAME;
-
-
-
- const CANONICAL_FMT = '[0-9a-z]{1,64}';
-
- const MAX_LEN = 64;
-
- const BEFORE_MENTIONS = '(?:^|[\s\.\,\:\;\[\(]+)';
-
- public static function isValid($str, $checkuse=false)
- {
- try {
- self::normalize($str, $checkuse);
- } catch (NicknameException $e) {
- return false;
- }
- return true;
- }
-
- public static function normalize($str, $checkuse=false)
- {
- if (mb_strlen($str) > self::MAX_LEN) {
-
- throw new NicknameTooLongException();
- }
-
- $str = trim($str);
- $str = str_replace('_', '', $str);
- $str = mb_strtolower($str);
- if (mb_strlen($str) < 1) {
- throw new NicknameEmptyException();
- } elseif (!self::isCanonical($str) && !filter_var($str, FILTER_VALIDATE_EMAIL)) {
- throw new NicknameInvalidException();
- } elseif (self::isBlacklisted($str)) {
- throw new NicknameBlacklistedException();
- } elseif (self::isSystemPath($str)) {
- throw new NicknamePathCollisionException();
- } elseif ($checkuse) {
- $profile = self::isTaken($str);
- if ($profile instanceof Profile) {
- throw new NicknameTakenException($profile);
- }
- }
- return $str;
- }
-
- public static function isCanonical($str)
- {
- return preg_match('/^(?:' . self::CANONICAL_FMT . ')$/', $str);
- }
-
- public static function isBlacklisted($str)
- {
- $blacklist = common_config('nickname', 'blacklist');
- if(!$blacklist)
- return false;
- return in_array($str, $blacklist);
- }
-
- public static function isSystemPath($str)
- {
- $paths = array();
-
- $d = dir(INSTALLDIR);
- while (false !== ($entry = $d->read())) {
- $paths[$entry] = true;
- }
- $d->close();
-
- $router = Router::get();
- foreach ($router->m->getPaths() as $path) {
- if (preg_match('/^([^\/\?]+)[\/\?]/',$path,$matches) && isset($matches[1])) {
- $paths[$matches[1]] = true;
- }
- }
-
- foreach (['avatar', 'attachments'] as $cat) {
- $paths[basename(common_config($cat, 'path'))] = true;
- }
- return in_array($str, array_keys($paths));
- }
-
- public static function isTaken($str)
- {
- $found = User::getKV('nickname', $str);
- if ($found instanceof User) {
- return $found->getProfile();
- }
- $found = Local_group::getKV('nickname', $str);
- if ($found instanceof Local_group) {
- return $found->getProfile();
- }
- $found = Group_alias::getKV('alias', $str);
- if ($found instanceof Group_alias) {
- return $found->getProfile();
- }
- return null;
- }
- }
- class NicknameException extends ClientException
- {
- function __construct($msg=null, $code=400)
- {
- if ($msg === null) {
- $msg = $this->defaultMessage();
- }
- parent::__construct($msg, $code);
- }
-
- protected function defaultMessage()
- {
- return null;
- }
- }
- class NicknameInvalidException extends NicknameException {
-
- protected function defaultMessage()
- {
-
- return _('Nickname must have only lowercase letters and numbers and no spaces.');
- }
- }
- class NicknameEmptyException extends NicknameInvalidException
- {
-
- protected function defaultMessage()
- {
-
- return _('Nickname cannot be empty.');
- }
- }
- class NicknameTooLongException extends NicknameInvalidException
- {
-
- protected function defaultMessage()
- {
-
- return sprintf(_m('Nickname cannot be more than %d character long.',
- 'Nickname cannot be more than %d characters long.',
- Nickname::MAX_LEN),
- Nickname::MAX_LEN);
- }
- }
- class NicknameBlacklistedException extends NicknameException
- {
- protected function defaultMessage()
- {
-
- return _('Nickname is disallowed through blacklist.');
- }
- }
- class NicknamePathCollisionException extends NicknameException
- {
- protected function defaultMessage()
- {
-
- return _('Nickname is identical to system path names.');
- }
- }
- class NicknameTakenException extends NicknameException
- {
- public $profile = null;
- public function __construct(Profile $profile, $msg=null, $code=400)
- {
- $this->profile = $profile;
- if ($msg === null) {
- $msg = $this->defaultMessage();
- }
- parent::__construct($msg, $code);
- }
- protected function defaultMessage()
- {
-
- return _('Nickname is already in use on this server.');
- }
- }
|