]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/User.php
Language selector added, "channel" is now "channels"
[friendica.git] / src / Model / User.php
index cb769ce3fc809532221450cca772f8d18413260f..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;
@@ -88,7 +89,7 @@ class User
         * ACCOUNT_TYPE_NEWS - the account is a news reflector
         *      Associated page type: PAGE_FLAGS_SOAPBOX
         *
-        * ACCOUNT_TYPE_COMMUNITY - the account is community forum
+        * ACCOUNT_TYPE_COMMUNITY - the account is community group
         *      Associated page types: PAGE_COMMUNITY, PAGE_FLAGS_PRVGROUP
         *
         * ACCOUNT_TYPE_RELAY - the account is a relay
@@ -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;
@@ -483,7 +496,7 @@ class User
        }
 
        /**
-        * Returns the default circle for a given user and network
+        * Returns the default circle for a given user
         *
         * @param int $uid User id
         *
@@ -502,6 +515,46 @@ class User
                return $default_circle;
        }
 
+       /**
+        * Returns the default circle for groups for a given user
+        *
+        * @param int $uid User id
+        *
+        * @return int circle id
+        * @throws Exception
+        */
+       public static function getDefaultGroupCircle(int $uid): int
+       {
+               $default_circle = DI::pConfig()->get($uid, 'system', 'default-group-gid');
+               if (empty($default_circle)) {
+                       $default_circle = self::getDefaultCircle($uid);
+               }
+
+               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
         *
@@ -675,6 +728,10 @@ class User
         */
        public static function updateLastActivity(int $uid)
        {
+               if (!$uid) {
+                       return;
+               }
+
                $user = User::getById($uid, ['last-activity']);
                if (empty($user)) {
                        return;
@@ -847,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
         *
@@ -1204,6 +1275,11 @@ class User
 
                DBA::update('user', $fields, ['uid' => $uid]);
 
+               $def_gid_groups = Circle::create($uid, DI::l10n()->t('Groups'));
+               if ($def_gid_groups) {
+                       DI::pConfig()->set($uid, 'system', 'default-group-gid', $def_gid_groups);
+               }
+
                // if we have no OpenID photo try to look up an avatar
                if (!strlen($photo)) {
                        $photo = Network::lookupAvatarByEmail($email);
@@ -1276,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);
                }
 
@@ -1640,7 +1701,7 @@ class User
         */
        public static function identities(int $uid): array
        {
-               if (empty($uid)) {
+               if (!$uid) {
                        return [];
                }
 
@@ -1651,7 +1712,7 @@ class User
                        return $identities;
                }
 
-               if ($user['parent-uid'] == 0) {
+               if (!$user['parent-uid']) {
                        // First add our own entry
                        $identities = [[
                                'uid' => $user['uid'],
@@ -1712,7 +1773,7 @@ class User
         */
        public static function hasIdentities(int $uid): bool
        {
-               if (empty($uid)) {
+               if (!$uid) {
                        return false;
                }
 
@@ -1721,7 +1782,7 @@ class User
                        return false;
                }
 
-               if ($user['parent-uid'] != 0) {
+               if ($user['parent-uid']) {
                        return true;
                }
 
@@ -1848,8 +1909,8 @@ class User
        {
                $condition = [
                        'email'           => self::getAdminEmailList(),
-                       'parent-uid'      => 0,
-                       'blocked'         => 0,
+                       'parent-uid'      => null,
+                       'blocked'         => false,
                        'verified'        => true,
                        'account_removed' => false,
                        'account_expired' => false,