]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/User/Cookie.php
Language selector added, "channel" is now "channels"
[friendica.git] / src / Model / User / Cookie.php
index c0e8ffc0e1b5181eb807cae6440c07337c997057..aa89ae227508d8fe214bb1b3dd445548cf297cb8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -52,21 +52,21 @@ class Cookie
        private $data;
 
        /**
+        * @param App\Request         $request The current http request
         * @param IManageConfigValues $config
         * @param App\BaseURL         $baseURL
-        * @param array               $SERVER The $_SERVER array
         * @param array               $COOKIE The $_COOKIE array
         */
-       public function __construct(IManageConfigValues $config, App\BaseURL $baseURL, array $SERVER = [], array $COOKIE = [])
+       public function __construct(App\Request $request, IManageConfigValues $config, App\BaseURL $baseURL, array $COOKIE = [])
        {
-               $this->sslEnabled     = $baseURL->getSSLPolicy() === App\BaseURL::SSL_POLICY_FULL;
+               $this->sslEnabled     = $baseURL->getScheme() === 'https';
                $this->sitePrivateKey = $config->get('system', 'site_prvkey');
 
                $authCookieDays = $config->get('system', 'auth_cookie_lifetime',
                        self::DEFAULT_EXPIRE);
                $this->lifetime = $authCookieDays * 24 * 60 * 60;
 
-               $this->remoteAddr = ($SERVER['REMOTE_ADDR'] ?? null) ?: '0.0.0.0';
+               $this->remoteAddr = $request->getRemoteAddress();
 
                $this->data = json_decode($COOKIE[self::NAME] ?? '[]', true) ?: [];
        }
@@ -124,6 +124,19 @@ class Cookie
                }
        }
 
+       /**
+        * Resets the cookie to a given data set
+        *
+        * @param array $data
+        *
+        * @return bool
+        */
+       public function reset(array $data): bool
+       {
+               return $this->clear() &&
+                          $this->setMultiple($data);
+       }
+
        /**
         * Clears the Friendica cookie
         */
@@ -131,7 +144,7 @@ class Cookie
        {
                $this->data = [];
                // make sure cookie is deleted on browser close, as a security measure
-               return $this->setCookie( '', -3600, $this->sslEnabled);
+               return $this->setCookie('', -3600, $this->sslEnabled);
        }
 
        /**
@@ -161,7 +174,7 @@ class Cookie
         *
         */
        protected function setCookie(string $value = null, int $expire = null,
-                                    bool $secure = null): bool
+                                                                bool $secure = null): bool
        {
                return setcookie(self::NAME, $value, $expire, self::PATH, self::DOMAIN, $secure, self::HTTPONLY);
        }