]> 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 be71bcf64589a61c4d10fee982ae5e78fd6082d9..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 = Network::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 "cron_expire_and_remove_users" (cronjobs.php)
+               // 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);
 
@@ -1272,6 +1312,7 @@ class User
                        'total_users'           => 0,
                        'active_users_halfyear' => 0,
                        'active_users_monthly'  => 0,
+                       'active_users_weekly'   => 0,
                ];
 
                $userStmt = DBA::select('owner-view', ['uid', 'login_date', 'last-item'],
@@ -1284,6 +1325,7 @@ class User
 
                $halfyear = time() - (180 * 24 * 60 * 60);
                $month = time() - (30 * 24 * 60 * 60);
+               $week = time() - (7 * 24 * 60 * 60);
 
                while ($user = DBA::fetch($userStmt)) {
                        $statistics['total_users']++;
@@ -1297,7 +1339,13 @@ class User
                        ) {
                                $statistics['active_users_monthly']++;
                        }
+
+                       if ((strtotime($user['login_date']) > $week) || (strtotime($user['last-item']) > $week)
+                       ) {
+                               $statistics['active_users_weekly']++;
+                       }
                }
+               DBA::close($userStmt);
 
                return $statistics;
        }
@@ -1320,6 +1368,7 @@ class User
                $condition = [];
                switch ($type) {
                        case 'active':
+                               $condition['account_removed'] = false;
                                $condition['blocked'] = false;
                                break;
                        case 'blocked':