]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/nickname.php
Type-hint is array here.
[quix0rs-gnu-social.git] / lib / nickname.php
index 2b8249d59ab4b6ab7ff01099386cf7c94b55d491..1ed0abbe78dbfcec46365c97f73684cb2ff34b83 100644 (file)
@@ -27,8 +27,7 @@ class Nickname
      * Nickname::normalize() to get the canonical form, or Nickname::isValid()
      * if you just need to check if it's properly formatted.
      *
-     * This, DISPLAY_FMT, and CANONICAL_FMT replace the old NICKNAME_FMT,
-     * but be aware that these should not be enclosed in []s.
+     * This, DISPLAY_FMT, and CANONICAL_FMT should not be enclosed in []s.
      *
      * @fixme would prefer to define in reference to the other constants
      */
@@ -36,6 +35,7 @@ class Nickname
 
     /**
      * Regex fragment for acceptable user-formatted variant of a nickname.
+     *
      * This includes some chars such as underscore which will be removed
      * from the normalized canonical form, but still must fit within
      * field length limits.
@@ -44,11 +44,18 @@ class Nickname
      * Nickname::normalize() to get the canonical form, or Nickname::isValid()
      * if you just need to check if it's properly formatted.
      *
-     * This and CANONICAL_FMT replace the old NICKNAME_FMT, but be aware
-     * that these should not be enclosed in []s.
+     * This, INPUT_FMT and CANONICAL_FMT should not be enclosed in []s.
      */
     const DISPLAY_FMT = '[0-9a-zA-Z_]{1,64}';
 
+    /**
+     * Simplified regex fragment for acceptable full WebFinger ID of a user
+     *
+     * We could probably use an email regex here, but mainly we are interested
+     * in matching it in our URLs, like https://social.example/user@example.com
+     */
+    const WEBFINGER_FMT = '[0-9a-zA-Z_]{1,64}\@[0-9a-zA-Z_-.]{3,255}';
+
     /**
      * Regex fragment for checking a canonical nickname.
      *
@@ -60,8 +67,7 @@ class Nickname
      * there are multiple possible denormalized forms for each valid
      * canonical-form name.
      *
-     * This and DISPLAY_FMT replace the old NICKNAME_FMT, but be aware
-     * that these should not be enclosed in []s.
+     * This, INPUT_FMT and DISPLAY_FMT should not be enclosed in []s.
      */
     const CANONICAL_FMT = '[0-9a-z]{1,64}';
 
@@ -110,30 +116,26 @@ class Nickname
      */
     public static function normalize($str, $checkuse=false)
     {
-        if (mb_strlen($str) > self::MAX_LEN) {
-            // Display forms must also fit!
-            throw new NicknameTooLongException();
-        }
-
+        // We should also have UTF-8 normalization (å to a etc.)
         $str = trim($str);
         $str = str_replace('_', '', $str);
         $str = mb_strtolower($str);
 
-        if (mb_strlen($str) < 1) {
+        if (mb_strlen($str) > self::MAX_LEN) {
+            // Display forms must also fit!
+            throw new NicknameTooLongException();
+        } elseif (mb_strlen($str) < 1) {
             throw new NicknameEmptyException();
-        }
-        if (!self::isCanonical($str)) {
+        } elseif (!self::isCanonical($str)) {
             throw new NicknameInvalidException();
-        }
-        if (self::isBlacklisted($str)) {
+        } elseif (self::isBlacklisted($str)) {
             throw new NicknameBlacklistedException();
-        }
-        if (self::isSystemPath($str)) {
+        } elseif (self::isSystemPath($str)) {
             throw new NicknamePathCollisionException();
-        }
-        if ($checkuse && $user = self::isTaken($str)) {
-            if ($user instanceof User) {
-                throw new NicknameTakenException();
+        } elseif ($checkuse) {
+            $profile = self::isTaken($str);
+            if ($profile instanceof Profile) {
+                throw new NicknameTakenException($profile);
             }
         }
 
@@ -196,12 +198,26 @@ class Nickname
      * Is the nickname already in use locally? Checks the User table.
      *
      * @param   string $str
-     * @return  User|null   Returns null if no such user, otherwise a User object
+     * @return  Profile|null   Returns Profile if nickname found, otherwise null
      */
     public static function isTaken($str)
     {
-        $user = User::getKV('nickname', $str);
-        return $user;   // null if no such User entry
+        $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;
     }
 }
 
@@ -286,11 +302,11 @@ class NicknamePathCollisionException extends NicknameException
 
 class NicknameTakenException extends NicknameException
 {
-    public $user = null;    // the User which occupies the nickname
+    public $profile = null;    // the Profile which occupies the nickname
 
-    public function __construct(User $user, $msg=null, $code=400)
+    public function __construct(Profile $profile, $msg=null, $code=400)
     {
-        $this->byuser = $user;
+        $this->profile = $profile;
 
         if ($msg === null) {
             $msg = $this->defaultMessage();