]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/User.php
Merge pull request #13390 from annando/channel
[friendica.git] / src / Model / User.php
index 854961154b5dfe56772dc11c70c7d4314a1eaae0..d6dfa3525361800e5174fdd40a44c139b2b35e22 100644 (file)
@@ -37,6 +37,7 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Module;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
 use Friendica\Network\HTTPException;
 use Friendica\Object\Image;
@@ -132,6 +133,17 @@ class User
                return null;
        }
 
+       /**
+        * Get the Uri-Id of the system account
+        *
+        * @return integer
+        */
+       public static function getSystemUriId(): int
+       {
+               $system = self::getSystemAccount();
+               return $system['uri-id'] ?? 0;
+       }
+
        /**
         * Fetch the system account
         *
@@ -171,7 +183,7 @@ class User
                $system['dob'] = '0000-00-00';
 
                // Ensure that the user contains data
-               $user = DBA::selectFirst('user', ['prvkey', 'guid'], ['uid' => 0]);
+               $user = DBA::selectFirst('user', ['prvkey', 'guid', 'language'], ['uid' => 0]);
                if (empty($user['prvkey']) || empty($user['guid'])) {
                        $fields = [
                                'username' => $system['name'],
@@ -191,7 +203,8 @@ class User
 
                        $system['guid'] = $fields['guid'];
                } else {
-                       $system['guid'] = $user['guid'];
+                       $system['guid']     = $user['guid'];
+                       $system['language'] = $user['language'];
                }
 
                return $system;
@@ -520,6 +533,28 @@ class User
                return $default_circle;
        }
 
+/**
+ * Fetch the language code from the given user. If the code is invalid, return the system language
+ *
+ * @param integer $uid   User-Id
+ * @param boolean $short If true, return the short form g.g. "en", otherwise the long form e.g. "en-gb"
+ * @return string
+ */
+       public static function getLanguageCode(int $uid, bool $short): string
+       {
+               $owner = self::getOwnerDataById($uid);
+               $languages = DI::l10n()->getAvailableLanguages();
+               if (in_array($owner['language'], array_keys($languages))) {
+                       $language = $owner['language'];
+               } else {
+                       $language = DI::config()->get('system', 'language');
+               }
+               if ($short) {
+                       return substr($language, 0, 2);
+               }
+               return $language;
+       }
+
        /**
         * Authenticate a user with a clear text password
         *
@@ -869,6 +904,20 @@ class User
                ]);
        }
 
+       /**
+        * Returns if the given uid is valid and a moderator
+        *
+        * @param int $uid
+        *
+        * @return bool
+        * @throws Exception
+        */
+       public static function isModerator(int $uid): bool
+       {
+               // @todo Replace with a moderator check in the future
+               return self::isSiteAdmin($uid);
+       }
+
        /**
         * Checks if a nickname is in the list of the forbidden nicknames
         *
@@ -1303,33 +1352,18 @@ class User
        /**
         * Update a user entry and distribute the changes if needed
         *
-        * @param array $fields
+        * @param array   $fields
         * @param integer $uid
         * @return boolean
+        * @throws Exception
         */
        public static function update(array $fields, int $uid): bool
        {
-               $old_owner = self::getOwnerDataById($uid);
-               if (empty($old_owner)) {
-                       return false;
-               }
-
                if (!DBA::update('user', $fields, ['uid' => $uid])) {
                        return false;
                }
 
-               $update = Contact::updateSelfFromUserID($uid);
-
-               $owner = self::getOwnerDataById($uid);
-               if (empty($owner)) {
-                       return false;
-               }
-
-               if ($old_owner['name'] != $owner['name']) {
-                       Profile::update(['name' => $owner['name']], $uid);
-               }
-
-               if ($update) {
+               if (Contact::updateSelfFromUserID($uid)) {
                        Profile::publishUpdate($uid);
                }