]> git.mxchange.org Git - friendica.git/blobdiff - src/Security/Authentication.php
Remove most calls to date_default_timezone_* calls
[friendica.git] / src / Security / Authentication.php
index a36341a1d8724a386d0a3898d31e2ee4b9d6b304..c0dc1e9aa7402144a632b2a65a8c22cc062fd531 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -141,9 +141,9 @@ class Authentication
 
                if ($this->session->get('authenticated')) {
                        if ($this->session->get('visitor_id') && !$this->session->get('uid')) {
-                               $contact = $this->dba->selectFirst('contact', [], ['id' => $this->session->get('visitor_id')]);
+                               $contact = $this->dba->selectFirst('contact', ['id'], ['id' => $this->session->get('visitor_id')]);
                                if ($this->dba->isResult($contact)) {
-                                       $a->contact = $contact;
+                                       $a->setContactId($contact['id']);
                                }
                        }
 
@@ -239,34 +239,12 @@ class Authentication
        {
                $record = null;
 
-               $addon_auth = [
-                       'username'      => $username,
-                       'password'      => $password,
-                       'authenticated' => 0,
-                       'user_record'   => null
-               ];
-
-               /*
-                * An addon indicates successful login by setting 'authenticated' to non-zero value and returning a user record
-                * Addons should never set 'authenticated' except to indicate success - as hooks may be chained
-                * and later addons should not interfere with an earlier one that succeeded.
-                */
-               Hook::callAll('authenticate', $addon_auth);
-
                try {
-                       if ($addon_auth['authenticated']) {
-                               $record = $addon_auth['user_record'];
-
-                               if (empty($record)) {
-                                       throw new Exception($this->l10n->t('Login failed.'));
-                               }
-                       } else {
-                               $record = $this->dba->selectFirst(
-                                       'user',
-                                       [],
-                                       ['uid' => User::getIdFromPasswordAuthentication($username, $password)]
-                               );
-                       }
+                       $record = $this->dba->selectFirst(
+                               'user',
+                               [],
+                               ['uid' => User::getIdFromPasswordAuthentication($username, $password)]
+                       );
                } catch (Exception $e) {
                        $this->logger->warning('authenticate: failed login attempt', ['action' => 'login', 'username' => Strings::escapeTags($username), 'ip' => $_SERVER['REMOTE_ADDR']]);
                        notice($this->l10n->t('Login failed. Please check your credentials.'));
@@ -327,34 +305,13 @@ class Authentication
                $this->session->set('new_member', time() < ($member_since + (60 * 60 * 24 * 14)));
 
                if (strlen($user_record['timezone'])) {
-                       date_default_timezone_set($user_record['timezone']);
-                       $a->timezone = $user_record['timezone'];
-               }
-
-               $masterUid = $user_record['uid'];
-
-               if ($this->session->get('submanage')) {
-                       $user = $this->dba->selectFirst('user', ['uid'], ['uid' => $this->session->get('submanage')]);
-                       if ($this->dba->isResult($user)) {
-                               $masterUid = $user['uid'];
-                       }
-               }
-
-               $a->identities = User::identities($masterUid);
-
-               if ($login_initial) {
-                       $this->logger->info('auth_identities: ' . print_r($a->identities, true));
-               }
-
-               if ($login_refresh) {
-                       $this->logger->info('auth_identities refresh: ' . print_r($a->identities, true));
+                       $a->setTimeZone($user_record['timezone']);
                }
 
-               $contact = $this->dba->selectFirst('contact', [], ['uid' => $user_record['uid'], 'self' => true]);
+               $contact = $this->dba->selectFirst('contact', ['id'], ['uid' => $user_record['uid'], 'self' => true]);
                if ($this->dba->isResult($contact)) {
-                       $a->contact = $contact;
-                       $a->cid     = $contact['id'];
-                       $this->session->set('cid', $a->cid);
+                       $a->setContactId($contact['id']);
+                       $this->session->set('cid', $contact['id']);
                }
 
                header('X-Account-Management-Status: active; name="' . $user_record['username'] . '"; id="' . $user_record['nickname'] . '"');
@@ -364,7 +321,7 @@ class Authentication
 
                        // Set the login date for all identities of the user
                        $this->dba->update('user', ['login_date' => DateTimeFormat::utcNow()],
-                               ['parent-uid' => $masterUid, 'account_removed' => false]);
+                               ['parent-uid' => $user_record['uid'], 'account_removed' => false]);
                }
 
                if ($login_initial) {
@@ -384,7 +341,7 @@ class Authentication
                        }
                }
 
-               $this->redirectForTwoFactorAuthentication($user_record['uid'], $a);
+               $this->redirectForTwoFactorAuthentication($user_record['uid']);
 
                if ($interactive) {
                        if ($user_record['login_date'] <= DBA::NULL_DATETIME) {
@@ -394,10 +351,11 @@ class Authentication
                        }
                }
 
-               $a->user = $user_record;
+               $a->setLoggedInUserId($user_record['uid']);
+               $a->setLoggedInUserNickname($user_record['nickname']);
 
                if ($login_initial) {
-                       Hook::callAll('logged_in', $a->user);
+                       Hook::callAll('logged_in', $user_record);
 
                        if (DI::module()->getName() !== 'home' && $this->session->exists('return_path')) {
                                $this->baseUrl->redirect($this->session->get('return_path'));
@@ -410,12 +368,11 @@ class Authentication
         * All return calls in this method skip two-factor authentication
         *
         * @param int $uid The User Identified
-        * @param App $a   The Friendica Application context
         *
         * @throws HTTPException\ForbiddenException In case the two factor authentication is forbidden (e.g. for AJAX calls)
         * @throws HTTPException\InternalServerErrorException
         */
-       private function redirectForTwoFactorAuthentication(int $uid, App $a)
+       private function redirectForTwoFactorAuthentication(int $uid)
        {
                // Check user setting, if 2FA disabled return
                if (!$this->pConfig->get($uid, '2fa', 'verified')) {
@@ -423,7 +380,7 @@ class Authentication
                }
 
                // Check current path, if public or 2fa module return
-               if ($a->argc > 0 && in_array($a->argv[0], ['2fa', 'view', 'help', 'api', 'proxy', 'logout'])) {
+               if (DI::args()->getArgc() > 0 && in_array(DI::args()->getArgv()[0], ['2fa', 'view', 'help', 'api', 'proxy', 'logout'])) {
                        return;
                }