]> git.mxchange.org Git - friendica.git/commitdiff
Renaming class
authornupplaPhil <admin@philipp.info>
Tue, 10 Dec 2019 07:49:33 +0000 (08:49 +0100)
committernupplaPhil <admin@philipp.info>
Tue, 10 Dec 2019 07:49:33 +0000 (08:49 +0100)
src/Core/Session/Cache.php [new file with mode: 0644]
src/Core/Session/CacheSession.php [deleted file]
src/Core/Session/Database.php [new file with mode: 0644]
src/Core/Session/DatabaseSession.php [deleted file]
src/Core/Session/Memory.php [new file with mode: 0644]
src/Core/Session/MemorySession.php [deleted file]
src/Core/Session/Native.php [new file with mode: 0644]
src/Core/Session/NativeSession.php [deleted file]
src/Factory/SessionFactory.php

diff --git a/src/Core/Session/Cache.php b/src/Core/Session/Cache.php
new file mode 100644 (file)
index 0000000..f5dee0f
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+
+namespace Friendica\Core\Session;
+
+use Friendica\Core\Cache\ICache;
+use Friendica\Core\Config\Configuration;
+use Friendica\Core\Session;
+use Friendica\Model\User\Cookie;
+use Psr\Log\LoggerInterface;
+use SessionHandlerInterface;
+
+/**
+ * SessionHandler using Friendica Cache
+ *
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
+ */
+final class Cache extends Native implements SessionHandlerInterface
+{
+       /** @var ICache */
+       private $cache;
+       /** @var LoggerInterface */
+       private $logger;
+       /** @var array The $_SERVER array */
+       private $server;
+
+       public function __construct(Configuration $config, Cookie $cookie, ICache $cache, LoggerInterface $logger, array $server)
+       {
+               parent::__construct($config, $cookie);
+
+               $this->cache  = $cache;
+               $this->logger = $logger;
+               $this->server = $server;
+
+               session_set_save_handler($this);
+       }
+
+       public function open($save_path, $session_name)
+       {
+               return true;
+       }
+
+       public function read($session_id)
+       {
+               if (empty($session_id)) {
+                       return '';
+               }
+
+               $data = $this->cache->get('session:' . $session_id);
+               if (!empty($data)) {
+                       Session::$exists = true;
+                       return $data;
+               }
+
+               $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
+
+               return '';
+       }
+
+       /**
+        * @brief Standard PHP session write callback
+        *
+        * This callback updates the stored session data and/or the expiration depending
+        * on the case. Uses the Session::expire for existing session, 5 minutes
+        * for newly created session.
+        *
+        * @param string $session_id   Session ID with format: [a-z0-9]{26}
+        * @param string $session_data Serialized session data
+        *
+        * @return boolean Returns false if parameters are missing, true otherwise
+        * @throws \Exception
+        */
+       public function write($session_id, $session_data)
+       {
+               if (!$session_id) {
+                       return false;
+               }
+
+               if (!$session_data) {
+                       return true;
+               }
+
+               return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
+       }
+
+       public function close()
+       {
+               return true;
+       }
+
+       public function destroy($id)
+       {
+               return $this->cache->delete('session:' . $id);
+       }
+
+       public function gc($maxlifetime)
+       {
+               return true;
+       }
+}
diff --git a/src/Core/Session/CacheSession.php b/src/Core/Session/CacheSession.php
deleted file mode 100644 (file)
index fdf9aa6..0000000
+++ /dev/null
@@ -1,99 +0,0 @@
-<?php
-
-namespace Friendica\Core\Session;
-
-use Friendica\Core\Cache\ICache;
-use Friendica\Core\Config\Configuration;
-use Friendica\Core\Session;
-use Friendica\Model\User\Cookie;
-use Psr\Log\LoggerInterface;
-use SessionHandlerInterface;
-
-/**
- * SessionHandler using Friendica Cache
- *
- * @author Hypolite Petovan <hypolite@mrpetovan.com>
- */
-final class CacheSession extends NativeSession implements SessionHandlerInterface
-{
-       /** @var ICache */
-       private $cache;
-       /** @var LoggerInterface */
-       private $logger;
-       /** @var array The $_SERVER array */
-       private $server;
-
-       public function __construct(Configuration $config, Cookie $cookie, ICache $cache, LoggerInterface $logger, array $server)
-       {
-               parent::__construct($config, $cookie);
-
-               $this->cache  = $cache;
-               $this->logger = $logger;
-               $this->server = $server;
-
-               session_set_save_handler($this);
-       }
-
-       public function open($save_path, $session_name)
-       {
-               return true;
-       }
-
-       public function read($session_id)
-       {
-               if (empty($session_id)) {
-                       return '';
-               }
-
-               $data = $this->cache->get('session:' . $session_id);
-               if (!empty($data)) {
-                       Session::$exists = true;
-                       return $data;
-               }
-
-               $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
-
-               return '';
-       }
-
-       /**
-        * @brief Standard PHP session write callback
-        *
-        * This callback updates the stored session data and/or the expiration depending
-        * on the case. Uses the Session::expire for existing session, 5 minutes
-        * for newly created session.
-        *
-        * @param string $session_id   Session ID with format: [a-z0-9]{26}
-        * @param string $session_data Serialized session data
-        *
-        * @return boolean Returns false if parameters are missing, true otherwise
-        * @throws \Exception
-        */
-       public function write($session_id, $session_data)
-       {
-               if (!$session_id) {
-                       return false;
-               }
-
-               if (!$session_data) {
-                       return true;
-               }
-
-               return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
-       }
-
-       public function close()
-       {
-               return true;
-       }
-
-       public function destroy($id)
-       {
-               return $this->cache->delete('session:' . $id);
-       }
-
-       public function gc($maxlifetime)
-       {
-               return true;
-       }
-}
diff --git a/src/Core/Session/Database.php b/src/Core/Session/Database.php
new file mode 100644 (file)
index 0000000..5874c5d
--- /dev/null
@@ -0,0 +1,118 @@
+<?php
+
+namespace Friendica\Core\Session;
+
+use Friendica\Core\Config\Configuration;
+use Friendica\Core\Session;
+use Friendica\Database\Database;
+use Friendica\Model\User\Cookie;
+use Psr\Log\LoggerInterface;
+use SessionHandlerInterface;
+
+/**
+ * SessionHandler using database
+ *
+ * @author Hypolite Petovan <hypolite@mrpetovan.com>
+ */
+final class Database extends Native implements SessionHandlerInterface
+{
+       /** @var Database */
+       private $dba;
+       /** @var LoggerInterface */
+       private $logger;
+       /** @var array The $_SERVER variable */
+       private $server;
+
+       /**
+        * DatabaseSessionHandler constructor.
+        *
+        * @param Database        $dba
+        * @param LoggerInterface $logger
+        * @param array           $server
+        */
+       public function __construct(Configuration $config, Cookie $cookie, Database $dba, LoggerInterface $logger, array $server)
+       {
+               parent::__construct($config, $cookie);
+
+               $this->dba    = $dba;
+               $this->logger = $logger;
+               $this->server = $server;
+
+               session_set_save_handler($this);
+       }
+
+       public function open($save_path, $session_name)
+       {
+               return true;
+       }
+
+       public function read($session_id)
+       {
+               if (empty($session_id)) {
+                       return '';
+               }
+
+               $session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
+               if ($this->dba->isResult($session)) {
+                       Session::$exists = true;
+                       return $session['data'];
+               }
+
+               $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
+
+               return '';
+       }
+
+       /**
+        * @brief Standard PHP session write callback
+        *
+        * This callback updates the DB-stored session data and/or the expiration depending
+        * on the case. Uses the Session::expire global for existing session, 5 minutes
+        * for newly created session.
+        *
+        * @param string $session_id   Session ID with format: [a-z0-9]{26}
+        * @param string $session_data Serialized session data
+        *
+        * @return boolean Returns false if parameters are missing, true otherwise
+        * @throws \Exception
+        */
+       public function write($session_id, $session_data)
+       {
+               if (!$session_id) {
+                       return false;
+               }
+
+               if (!$session_data) {
+                       return true;
+               }
+
+               $expire         = time() + Session::$expire;
+               $default_expire = time() + 300;
+
+               if (Session::$exists) {
+                       $fields    = ['data' => $session_data, 'expire' => $expire];
+                       $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
+                       $this->dba->update('session', $fields, $condition);
+               } else {
+                       $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
+                       $this->dba->insert('session', $fields);
+               }
+
+               return true;
+       }
+
+       public function close()
+       {
+               return true;
+       }
+
+       public function destroy($id)
+       {
+               return $this->dba->delete('session', ['sid' => $id]);
+       }
+
+       public function gc($maxlifetime)
+       {
+               return $this->dba->delete('session', ["`expire` < ?", time()]);
+       }
+}
diff --git a/src/Core/Session/DatabaseSession.php b/src/Core/Session/DatabaseSession.php
deleted file mode 100644 (file)
index c431be9..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-<?php
-
-namespace Friendica\Core\Session;
-
-use Friendica\Core\Config\Configuration;
-use Friendica\Core\Session;
-use Friendica\Database\Database;
-use Friendica\Model\User\Cookie;
-use Psr\Log\LoggerInterface;
-use SessionHandlerInterface;
-
-/**
- * SessionHandler using database
- *
- * @author Hypolite Petovan <hypolite@mrpetovan.com>
- */
-final class DatabaseSession extends NativeSession implements SessionHandlerInterface
-{
-       /** @var Database */
-       private $dba;
-       /** @var LoggerInterface */
-       private $logger;
-       /** @var array The $_SERVER variable */
-       private $server;
-
-       /**
-        * DatabaseSessionHandler constructor.
-        *
-        * @param Database        $dba
-        * @param LoggerInterface $logger
-        * @param array           $server
-        */
-       public function __construct(Configuration $config, Cookie $cookie, Database $dba, LoggerInterface $logger, array $server)
-       {
-               parent::__construct($config, $cookie);
-
-               $this->dba    = $dba;
-               $this->logger = $logger;
-               $this->server = $server;
-
-               session_set_save_handler($this);
-       }
-
-       public function open($save_path, $session_name)
-       {
-               return true;
-       }
-
-       public function read($session_id)
-       {
-               if (empty($session_id)) {
-                       return '';
-               }
-
-               $session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
-               if ($this->dba->isResult($session)) {
-                       Session::$exists = true;
-                       return $session['data'];
-               }
-
-               $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
-
-               return '';
-       }
-
-       /**
-        * @brief Standard PHP session write callback
-        *
-        * This callback updates the DB-stored session data and/or the expiration depending
-        * on the case. Uses the Session::expire global for existing session, 5 minutes
-        * for newly created session.
-        *
-        * @param string $session_id   Session ID with format: [a-z0-9]{26}
-        * @param string $session_data Serialized session data
-        *
-        * @return boolean Returns false if parameters are missing, true otherwise
-        * @throws \Exception
-        */
-       public function write($session_id, $session_data)
-       {
-               if (!$session_id) {
-                       return false;
-               }
-
-               if (!$session_data) {
-                       return true;
-               }
-
-               $expire         = time() + Session::$expire;
-               $default_expire = time() + 300;
-
-               if (Session::$exists) {
-                       $fields    = ['data' => $session_data, 'expire' => $expire];
-                       $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
-                       $this->dba->update('session', $fields, $condition);
-               } else {
-                       $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
-                       $this->dba->insert('session', $fields);
-               }
-
-               return true;
-       }
-
-       public function close()
-       {
-               return true;
-       }
-
-       public function destroy($id)
-       {
-               return $this->dba->delete('session', ['sid' => $id]);
-       }
-
-       public function gc($maxlifetime)
-       {
-               return $this->dba->delete('session', ["`expire` < ?", time()]);
-       }
-}
diff --git a/src/Core/Session/Memory.php b/src/Core/Session/Memory.php
new file mode 100644 (file)
index 0000000..a7e3366
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+
+namespace Friendica\Core\Session;
+
+/**
+ * Usable for backend processes (daemon/worker) and testing
+ */
+final class Memory implements ISession
+{
+       private $data = [];
+
+       public function start()
+       {
+               // Backward compatibility until all Session variables are replaced
+               // with the Session class
+               $_SESSION = [];
+               $this->clear();
+               return $this;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function exists(string $name)
+       {
+               return isset($this->data[$name]);
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function get(string $name, $defaults = null)
+       {
+               return $this->data[$name] ?? $defaults;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function set(string $name, $value)
+       {
+               $this->data[$name] = $value;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function setMultiple(array $values)
+       {
+               foreach ($values as $key => $value) {
+                       $this->data[$key] = $value;
+               }
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function remove(string $name)
+       {
+               if ($this->exists($name)) {
+                       unset($this->data[$name]);
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function clear()
+       {
+               $this->data = [];
+               return true;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function delete()
+       {
+               $this->data = [];
+               return true;
+       }
+}
\ No newline at end of file
diff --git a/src/Core/Session/MemorySession.php b/src/Core/Session/MemorySession.php
deleted file mode 100644 (file)
index eee481d..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-<?php
-
-namespace Friendica\Core\Session;
-
-/**
- * Usable for backend processes (daemon/worker) and testing
- */
-final class MemorySession implements ISession
-{
-       private $data = [];
-
-       public function start()
-       {
-               $this->clear();
-               return $this;
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function exists(string $name)
-       {
-               return isset($this->data[$name]);
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function get(string $name, $defaults = null)
-       {
-               return $this->data[$name] ?? $defaults;
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function set(string $name, $value)
-       {
-               $this->data[$name] = $value;
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function setMultiple(array $values)
-       {
-               foreach ($values as $key => $value) {
-                       $this->data[$key] = $value;
-               }
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function remove(string $name)
-       {
-               if ($this->exists($name)) {
-                       unset($this->data[$name]);
-                       return true;
-               }
-
-               return false;
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function clear()
-       {
-               $this->data = [];
-               return true;
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function delete()
-       {
-               $this->data = [];
-               return true;
-       }
-}
\ No newline at end of file
diff --git a/src/Core/Session/Native.php b/src/Core/Session/Native.php
new file mode 100644 (file)
index 0000000..b7f992b
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+namespace Friendica\Core\Session;
+
+use Friendica\Core\Config\Configuration;
+use Friendica\App;
+use Friendica\Model\User\Cookie;
+
+/**
+ * The native Session class which uses the PHP internal Session function
+ */
+class Native implements ISession
+{
+       /** @var Cookie */
+       protected $cookie;
+
+       public function __construct(Configuration $config, Cookie $cookie)
+       {
+               ini_set('session.gc_probability', 50);
+               ini_set('session.use_only_cookies', 1);
+               ini_set('session.cookie_httponly', 1);
+
+               if ($config->get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL) {
+                       ini_set('session.cookie_secure', 1);
+               }
+
+               $this->cookie = $cookie;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function start()
+       {
+               session_start();
+               return $this;
+       }
+
+       /**
+        * {@inheritDoc}}
+        */
+       public function exists(string $name)
+       {
+               return isset($_SESSION[$name]);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function get(string $name, $defaults = null)
+       {
+               return $_SESSION[$name] ?? $defaults;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function set(string $name, $value)
+       {
+               $_SESSION[$name] = $value;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function setMultiple(array $values)
+       {
+               $_SESSION = $values + $_SESSION;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function remove(string $name)
+       {
+               unset($_SESSION[$name]);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function clear()
+       {
+               $_SESSION = [];
+       }
+
+       /**
+        * @brief Kills the "Friendica" cookie and all session data
+        */
+       public function delete()
+       {
+               $this->cookie->clear();
+               $_SESSION = [];
+       }
+}
diff --git a/src/Core/Session/NativeSession.php b/src/Core/Session/NativeSession.php
deleted file mode 100644 (file)
index 49278ed..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-<?php
-
-namespace Friendica\Core\Session;
-
-use Friendica\Core\Config\Configuration;
-use Friendica\App;
-use Friendica\Model\User\Cookie;
-
-/**
- * The native Session class which uses the PHP internal Session function
- */
-class NativeSession implements ISession
-{
-       /** @var Cookie */
-       protected $cookie;
-
-       public function __construct(Configuration $config, Cookie $cookie)
-       {
-               ini_set('session.gc_probability', 50);
-               ini_set('session.use_only_cookies', 1);
-               ini_set('session.cookie_httponly', 1);
-
-               if ($config->get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL) {
-                       ini_set('session.cookie_secure', 1);
-               }
-
-               $this->cookie = $cookie;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function start()
-       {
-               session_start();
-               return $this;
-       }
-
-       /**
-        * {@inheritDoc}}
-        */
-       public function exists(string $name)
-       {
-               return isset($_SESSION[$name]);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function get(string $name, $defaults = null)
-       {
-               return $_SESSION[$name] ?? $defaults;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function set(string $name, $value)
-       {
-               $_SESSION[$name] = $value;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function setMultiple(array $values)
-       {
-               $_SESSION = $values + $_SESSION;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function remove(string $name)
-       {
-               unset($_SESSION[$name]);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function clear()
-       {
-               $_SESSION = [];
-       }
-
-       /**
-        * @brief Kills the "Friendica" cookie and all session data
-        */
-       public function delete()
-       {
-               $this->cookie->clear();
-               $_SESSION = [];
-       }
-}
index 999c424c9954578c213aa55e7fe8857e1e4fee54..47baa74708664f4f6e0a19bddecd56596964eae3 100644 (file)
@@ -6,12 +6,12 @@ use Friendica\App;
 use Friendica\Core\Cache\Cache;
 use Friendica\Core\Cache\ICache;
 use Friendica\Core\Config\Configuration;
-use Friendica\Core\Session\CacheSession;
-use Friendica\Core\Session\DatabaseSession;
+use Friendica\Core\Session\Cache;
+use Friendica\Core\Session\Database;
 use Friendica\Core\Session\ISession;
 use Friendica\Core\Session\Memory;
-use Friendica\Core\Session\MemorySession;
-use Friendica\Core\Session\NativeSession;
+use Friendica\Core\Session\Memory;
+use Friendica\Core\Session\Native;
 use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Model\User\Cookie;
@@ -52,24 +52,24 @@ class SessionFactory
 
                try {
                        if ($mode->isInstall() || $mode->isBackend()) {
-                               $session = new MemorySession();
+                               $session = new Memory();
                        } else {
                                $session_handler = $config->get('system', 'session_handler', self::DEFAULT);
 
                                switch ($session_handler) {
                                        case self::INTERNAL:
-                                               $session = new NativeSession($config, $cookie);
+                                               $session = new Native($config, $cookie);
                                                break;
                                        case self::DATABASE:
                                        default:
-                                               $session = new DatabaseSession($config, $cookie, $dba, $logger, $server);
+                                               $session = new Database($config, $cookie, $dba, $logger, $server);
                                                break;
                                        case self::CACHE:
                                                // In case we're using the db as cache driver, use the native db session, not the cache
                                                if ($config->get('system', 'cache_driver') === Cache::TYPE_DATABASE) {
-                                                       $session = new DatabaseSession($config, $cookie, $dba, $logger, $server);
+                                                       $session = new Database($config, $cookie, $dba, $logger, $server);
                                                } else {
-                                                       $session = new CacheSession($config, $cookie, $cache, $logger, $server);
+                                                       $session = new Cache($config, $cookie, $cache, $logger, $server);
                                                }
                                                break;
                                }