]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Session.php
Remove deprecated App::getHostName() - process methods to DI::baseUrl()->getHostName()
[friendica.git] / src / Core / Session.php
index b5c09f745b27a549f637f9793ce849deac64f8e4..d0c8581d31e4a07560b261d77039c6684c4d9454 100644 (file)
-<?php\r
-\r
-/**\r
- * @file src/Core/Session.php\r
- */\r
-namespace Friendica\Core;\r
-\r
-use Friendica\Core\Session\DatabaseSessionHandler;\r
-use Friendica\Core\Session\MemcacheSessionHandler;\r
-\r
-/**\r
- * High-level Session service class\r
- *\r
- * @author Hypolite Petovan <mrpetovan@gmail.com>\r
- */\r
-class Session\r
-{\r
-       public static $exists = false;\r
-       public static $expire = 180000;\r
-\r
-       public static function init()\r
-       {\r
-               ini_set('session.gc_probability', 50);\r
-               ini_set('session.use_only_cookies', 1);\r
-               ini_set('session.cookie_httponly', 1);\r
-\r
-               if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL) {\r
-                       ini_set('session.cookie_secure', 1);\r
-               }\r
-\r
-               if (!Config::get('system', 'disable_database_session')) {\r
-                       $memcache = Cache::memcache();\r
-                       if (is_object($memcache)) {\r
-                               $SessionHandler = new MemcacheSessionHandler($memcache);\r
-                       } else {\r
-                               $SessionHandler = new DatabaseSessionHandler();\r
-                       }\r
-\r
-                       session_set_save_handler($SessionHandler);\r
-               }\r
-       }\r
-\r
-       public static function exists($name)\r
-       {\r
-               return isset($_SESSION[$name]);\r
-       }\r
-\r
-       public static function get($name)\r
-       {\r
-               return defaults($_SESSION, $name, null);\r
-       }\r
-\r
-       public static function set($name, $value)\r
-       {\r
-               $_SESSION[$name] = $value;\r
-       }\r
-}\r
+<?php
+
+/**
+ * @file src/Core/Session.php
+ */
+namespace Friendica\Core;
+
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Model\Contact;
+use Friendica\Util\Strings;
+
+/**
+ * High-level Session service class
+ *
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
+ */
+class Session
+{
+       public static $exists = false;
+       public static $expire = 180000;
+
+       public static function exists($name)
+       {
+               return DI::session()->exists($name);
+       }
+
+       public static function get($name, $defaults = null)
+       {
+               return DI::session()->get($name, $defaults);
+       }
+
+       public static function set($name, $value)
+       {
+               DI::session()->set($name, $value);
+       }
+
+       public static function setMultiple(array $values)
+       {
+               DI::session()->setMultiple($values);
+       }
+
+       public static function remove($name)
+       {
+               DI::session()->remove($name);
+       }
+
+       public static function clear()
+       {
+               DI::session()->clear();
+       }
+
+       /**
+        * 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)
+       {
+               $session = DI::session();
+
+               if (empty($session->get('remote')[$uid])) {
+                       return false;
+               }
+
+               return $session->get('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)
+       {
+               $session = DI::session();
+
+               if (empty($session->get('remote'))) {
+                       return false;
+               }
+
+               return array_search($cid, $session->get('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 = DI::session();
+
+               $session->set('remote', []);
+
+               $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($session->get('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->set('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()
+       {
+               $session = DI::session();
+
+               return $session->get('authenticated', false);
+       }
+}