]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Session.php
Merge pull request #7745 from MrPetovan/task/7190-remove-defaults-include-view
[friendica.git] / src / Core / Session.php
index e3768cbef135834f3a5c1187fd5a121e60511316..aaead868a0377cdff2999e7788a63e05c628d292 100644 (file)
@@ -11,8 +11,8 @@ use Friendica\Core\Session\DatabaseSessionHandler;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\User;
-use Friendica\Util\BaseURL;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Strings;
 
 /**
  * High-level Session service class
@@ -30,7 +30,7 @@ class Session
                ini_set('session.use_only_cookies', 1);
                ini_set('session.cookie_httponly', 1);
 
-               if (Config::get('system', 'ssl_policy') == BaseURL::SSL_POLICY_FULL) {
+               if (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL) {
                        ini_set('session.cookie_secure', 1);
                }
 
@@ -53,7 +53,7 @@ class Session
 
        /**
         * Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
-        * 
+        *
         * Handle the case where session_start() hasn't been called and the super global isn't available.
         *
         * @param string $name
@@ -62,13 +62,7 @@ class Session
         */
        public static function get($name, $defaults = null)
        {
-               if (isset($_SESSION)) {
-                       $return = defaults($_SESSION, $name, $defaults);
-               } else {
-                       $return = $defaults;
-               }
-
-               return $return;
+               return $_SESSION[$name] ?? $defaults;
        }
 
        /**
@@ -105,6 +99,14 @@ class Session
                unset($_SESSION[$name]);
        }
 
+       /**
+        * Clears the current session array
+        */
+       public static function clear()
+       {
+               $_SESSION = [];
+       }
+
        /**
         * @brief Sets the provided user's authenticated session
         *
@@ -113,6 +115,7 @@ class Session
         * @param bool  $login_initial
         * @param bool  $interactive
         * @param bool  $login_refresh
+        * @throws \Friendica\Network\HTTPException\ForbiddenException
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function setAuthenticatedForUser(App $a, array $user_record, $login_initial = false, $interactive = false, $login_refresh = false)
@@ -125,9 +128,11 @@ class Session
                        'page_flags'    => $user_record['page-flags'],
                        'my_url'        => $a->getBaseURL() . '/profile/' . $user_record['nickname'],
                        'my_address'    => $user_record['nickname'] . '@' . substr($a->getBaseURL(), strpos($a->getBaseURL(), '://') + 3),
-                       'addr'          => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0'),
+                       'addr'          => ($_SERVER['REMOTE_ADDR'] ?? '') ?: '0.0.0.0'
                ]);
 
+               self::setVisitorsContacts();
+
                $member_since = strtotime($user_record['register_date']);
                self::set('new_member', time() < ($member_since + ( 60 * 60 * 24 * 14)));
 
@@ -155,7 +160,7 @@ class Session
                        $a->getLogger()->info('auth_identities refresh: ' . print_r($a->identities, true));
                }
 
-               $contact = Contact::select([], ['uid' => $user_record['uid'], 'self' => true]);
+               $contact = DBA::selectFirst('contact', [], ['uid' => $user_record['uid'], 'self' => true]);
                if (DBA::isResult($contact)) {
                        $a->contact = $contact;
                        $a->cid = $contact['id'];
@@ -209,4 +214,68 @@ class Session
                        }
                }
        }
+
+       /**
+        * Returns contact ID for given user ID
+        *
+        * @param integer $uid User ID
+        * @return integer Contact ID of visitor for given user ID
+        */
+       public static function getRemoteContactID($uid)
+       {
+               if (empty($_SESSION['remote'][$uid])) {
+                       return false;
+               }
+
+               return $_SESSION['remote'][$uid];
+       }
+
+       /**
+        * Returns User ID for given contact ID of the visitor
+        *
+        * @param integer $cid Contact ID
+        * @return integer User ID for given contact ID of the visitor
+        */
+       public static function getUserIDForVisitorContactID($cid)
+       {
+               if (empty($_SESSION['remote'])) {
+                       return false;
+               }
+
+               return array_search($cid, $_SESSION['remote']);
+       }
+
+       /**
+        * Set the session variable that contains the contact IDs for the visitor's contact URL
+        *
+        * @param string $url Contact URL
+        */
+       public static function setVisitorsContacts()
+       {
+               $_SESSION['remote'] = [];
+
+               $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
+               while ($contact = DBA::fetch($remote_contacts)) {
+                       if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
+                               continue;
+                       }
+
+                       $_SESSION['remote'][$contact['uid']] = $contact['id'];
+               }
+               DBA::close($remote_contacts);
+       }
+
+       /**
+        * Returns if the current visitor is authenticated
+        *
+        * @return boolean "true" when visitor is either a local or remote user
+        */
+       public static function isAuthenticated()
+       {
+               if (empty($_SESSION['authenticated'])) {
+                       return false;
+               }
+
+               return $_SESSION['authenticated'];
+       }
 }