]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Authentication.php
Merge pull request #6637 from annando/fix-jpm
[friendica.git] / src / Core / Authentication.php
index 76d078341f74c0658391f8a53e20fb1950e300a7..106ba2a6021ccd542325b4c4bff87d6c477818ae 100644 (file)
@@ -5,17 +5,14 @@
 
 namespace Friendica\Core;
 
-use Friendica\Core\Addon;
 use Friendica\BaseObject;
-use Friendica\Core\Config;
-use Friendica\Util\DateTimeFormat;
 use Friendica\Database\DBA;
-use Friendica\Core\L10n;
-use Friendica\Core\PConfig;
+use Friendica\Model\User;
+use Friendica\Util\DateTimeFormat;
 
 /**
 * Handle Authentification, Session and Cookies
-*/    
+*/
 class Authentication extends BaseObject
 {
        /**
@@ -24,6 +21,7 @@ class Authentication extends BaseObject
         * @param array $user Record from "user" table
         *
         * @return string Hashed data
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function getCookieHashForUser($user)
        {
@@ -35,8 +33,9 @@ class Authentication extends BaseObject
        /**
         * @brief Set the "Friendica" cookie
         *
-        * @param int $time
+        * @param int   $time
         * @param array $user Record from "user" table
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static  function setCookie($time, $user = [])
        {
@@ -58,12 +57,13 @@ class Authentication extends BaseObject
        /**
         * @brief Sets the provided user's authenticated session
         *
-        * @todo Should be moved to Friendica\Core\Session once it's created
+        * @todo  Should be moved to Friendica\Core\Session once it's created
         *
-        * @param type $user_record
-        * @param type $login_initial
-        * @param type $interactive
-        * @param type $login_refresh
+        * @param array $user_record
+        * @param bool  $login_initial
+        * @param bool  $interactive
+        * @param bool  $login_refresh
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function setAuthenticatedSessionForUser($user_record, $login_initial = false, $interactive = false, $login_refresh = false)
        {
@@ -81,7 +81,7 @@ class Authentication extends BaseObject
                $a->user = $user_record;
 
                if ($interactive) {
-                       if ($a->user['login_date'] <= NULL_DATE) {
+                       if ($a->user['login_date'] <= DBA::NULL_DATETIME) {
                                $_SESSION['return_path'] = 'profile_photo/new';
                                $a->module = 'profile_photo';
                                info(L10n::t("Welcome ") . $a->user['username'] . EOL);
@@ -102,61 +102,22 @@ class Authentication extends BaseObject
                        $a->timezone = $a->user['timezone'];
                }
 
-               $master_record = $a->user;
+               $masterUid = $user_record['uid'];
 
-               if ((x($_SESSION, 'submanage')) && intval($_SESSION['submanage'])) {
-                       $user = DBA::selectFirst('user', [], ['uid' => $_SESSION['submanage']]);
+               if (!empty($_SESSION['submanage'])) {
+                       $user = DBA::selectFirst('user', ['uid'], ['uid' => $_SESSION['submanage']]);
                        if (DBA::isResult($user)) {
-                               $master_record = $user;
-                       }
-               }
-
-               if ($master_record['parent-uid'] == 0) {
-                       // First add our own entry
-                       $a->identities = [['uid' => $master_record['uid'],
-                                       'username' => $master_record['username'],
-                                       'nickname' => $master_record['nickname']]];
-
-                       // Then add all the children
-                       $r = DBA::select('user', ['uid', 'username', 'nickname'],
-                               ['parent-uid' => $master_record['uid'], 'account_removed' => false]);
-                       if (DBA::isResult($r)) {
-                               $a->identities = array_merge($a->identities, DBA::toArray($r));
-                       }
-               } else {
-                       // Just ensure that the array is always defined
-                       $a->identities = [];
-
-                       // First entry is our parent
-                       $r = DBA::select('user', ['uid', 'username', 'nickname'],
-                               ['uid' => $master_record['parent-uid'], 'account_removed' => false]);
-                       if (DBA::isResult($r)) {
-                               $a->identities = DBA::toArray($r);
-                       }
-
-                       // Then add all siblings
-                       $r = DBA::select('user', ['uid', 'username', 'nickname'],
-                               ['parent-uid' => $master_record['parent-uid'], 'account_removed' => false]);
-                       if (DBA::isResult($r)) {
-                               $a->identities = array_merge($a->identities, DBA::toArray($r));
+                               $masterUid = $user['uid'];
                        }
                }
 
-               $r = DBA::p("SELECT `user`.`uid`, `user`.`username`, `user`.`nickname`
-                       FROM `manage`
-                       INNER JOIN `user` ON `manage`.`mid` = `user`.`uid`
-                       WHERE `user`.`account_removed` = 0 AND `manage`.`uid` = ?",
-                       $master_record['uid']
-               );
-               if (DBA::isResult($r)) {
-                       $a->identities = array_merge($a->identities, DBA::toArray($r));
-               }
+               $a->identities = User::identities($masterUid);
 
                if ($login_initial) {
-                       logger('auth_identities: ' . print_r($a->identities, true), LOGGER_DEBUG);
+                       Logger::log('auth_identities: ' . print_r($a->identities, true), Logger::DEBUG);
                }
                if ($login_refresh) {
-                       logger('auth_identities refresh: ' . print_r($a->identities, true), LOGGER_DEBUG);
+                       Logger::log('auth_identities refresh: ' . print_r($a->identities, true), Logger::DEBUG);
                }
 
                $contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]);
@@ -173,7 +134,7 @@ class Authentication extends BaseObject
 
                        // Set the login date for all identities of the user
                        DBA::update('user', ['login_date' => DateTimeFormat::utcNow()],
-                               ['parent-uid' => $master_record['uid'], 'account_removed' => false]);
+                               ['parent-uid' => $masterUid, 'account_removed' => false]);
                }
 
                if ($login_initial) {
@@ -184,14 +145,14 @@ class Authentication extends BaseObject
                         * The week ensures that sessions will expire after some inactivity.
                         */
                        if (!empty($_SESSION['remember'])) {
-                               logger('Injecting cookie for remembered user ' . $a->user['nickname']);
+                               Logger::log('Injecting cookie for remembered user ' . $a->user['nickname']);
                                self::setCookie(604800, $user_record);
                                unset($_SESSION['remember']);
                        }
                }
 
                if ($login_initial) {
-                       Addon::callHooks('logged_in', $a->user);
+                       Hook::callAll('logged_in', $a->user);
 
                        if (($a->module !== 'home') && isset($_SESSION['return_path'])) {
                                $a->internalRedirect($_SESSION['return_path']);