]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/User.php
Merge pull request #9032 from annando/local-access
[friendica.git] / src / Model / User.php
index 9980c48d5aeeaaa2af4d5455da6305e343b2763d..990df65dbd30f585bf83625cff5eaa3efd35ffcb 100644 (file)
@@ -97,6 +97,8 @@ class User
         * @}
         */
 
+       private static $owner;
+
        /**
         * Returns true if a user record exists with the provided id
         *
@@ -160,14 +162,29 @@ class User
         * @return integer user id
         * @throws Exception
         */
-       public static function getIdForURL($url)
+       public static function getIdForURL(string $url)
        {
-               $self = DBA::selectFirst('contact', ['uid'], ['nurl' => Strings::normaliseLink($url), 'self' => true]);
-               if (!DBA::isResult($self)) {
-                       return false;
-               } else {
+               // Avoid any database requests when the hostname isn't even part of the url.
+               if (!strpos($url, DI::baseUrl()->getHostname())) {
+                       return 0;
+               }
+
+               $self = Contact::selectFirst(['uid'], ['self' => true, 'nurl' => Strings::normaliseLink($url)]);
+               if (!empty($self['uid'])) {
+                       return $self['uid'];
+               }
+
+               $self = Contact::selectFirst(['uid'], ['self' => true, 'addr' => $url]);
+               if (!empty($self['uid'])) {
+                       return $self['uid'];
+               }
+
+               $self = Contact::selectFirst(['uid'], ['self' => true, 'alias' => [$url, Strings::normaliseLink($url)]]);
+               if (!empty($self['uid'])) {
                        return $self['uid'];
                }
+
+               return 0;
        }
 
        /**
@@ -185,6 +202,24 @@ class User
                return DBA::selectFirst('user', $fields, ['email' => $email]);
        }
 
+       /**
+        * Fetch the user array of the administrator. The first one if there are several.
+        *
+        * @param array $fields
+        * @return array user
+        */
+       public static function getFirstAdmin(array $fields = [])
+       {
+               if (!empty(DI::config()->get('config', 'admin_nickname'))) {
+                       return self::getByNickname(DI::config()->get('config', 'admin_nickname'), $fields);
+               } elseif (!empty(DI::config()->get('config', 'admin_email'))) {
+                       $adminList = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
+                       return self::getByEmail($adminList[0], $fields);
+               } else {
+                       return [];
+               }
+       }
+
        /**
         * Get owner data by user id
         *
@@ -193,8 +228,12 @@ class User
         * @return boolean|array
         * @throws Exception
         */
-       public static function getOwnerDataById($uid, $check_valid = true)
+       public static function getOwnerDataById(int $uid, bool $check_valid = true)
        {
+               if (!empty(self::$owner[$uid])) {
+                       return self::$owner[$uid];
+               }
+
                $owner = DBA::selectFirst('owner-view', [], ['uid' => $uid]);
                if (!DBA::isResult($owner)) {
                        if (!DBA::exists('user', ['uid' => $uid]) || !$check_valid) {
@@ -238,6 +277,7 @@ class User
                        $owner = self::getOwnerDataById($uid, false);
                }
 
+               self::$owner[$uid] = $owner;
                return $owner;
        }
 
@@ -823,7 +863,7 @@ class User
                        $photo_failure = false;
 
                        $filename = basename($photo);
-                       $curlResult = DI::httpRequest()->curl($photo, true);
+                       $curlResult = DI::httpRequest()->get($photo, true);
                        if ($curlResult->isSuccess()) {
                                $img_str = $curlResult->getBody();
                                $type = $curlResult->getContentType();
@@ -1162,7 +1202,7 @@ class User
                // unique), so it cannot be re-registered in the future.
                DBA::insert('userd', ['username' => $user['nickname']]);
 
-               // The user and related data will be deleted in Friendica\Worker\CronJobs::expireAndRemoveUsers()
+               // The user and related data will be deleted in Friendica\Worker\ExpireAndRemoveUsers
                DBA::update('user', ['account_removed' => true, 'account_expires_on' => DateTimeFormat::utc('now + 7 day')], ['uid' => $uid]);
                Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::REMOVAL, $uid);