]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #4382 from MrPetovan/task/3878-move-session-to-src
authorMichael Vogel <icarus@dabo.de>
Sat, 3 Feb 2018 20:02:09 +0000 (21:02 +0100)
committerGitHub <noreply@github.com>
Sat, 3 Feb 2018 20:02:09 +0000 (21:02 +0100)
Move include/session to src/

boot.php
include/session.php [deleted file]
index.php
src/Core/Cache.php
src/Core/Session.php [new file with mode: 0644]
src/Core/Session/DatabaseSessionHandler.php [new file with mode: 0644]
src/Core/Session/MemcacheSessionHandler.php [new file with mode: 0644]

index a699a66f95d29cc5b0c17c3624584808959c687a..b3743ab9916c47cd040ec000d7f0803ef7ad4ab4 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -871,13 +871,10 @@ function get_guid($size = 16, $prefix = "")
 
 /**
  * @brief Used to end the current process, after saving session state.
+ * @deprecated
  */
 function killme()
 {
-       if (!get_app()->is_backend()) {
-               session_write_close();
-       }
-
        exit();
 }
 
diff --git a/include/session.php b/include/session.php
deleted file mode 100644 (file)
index 0e8fe83..0000000
+++ /dev/null
@@ -1,134 +0,0 @@
-<?php
-/**
- * Session management functions. These provide database storage of PHP session info.
- */
-use Friendica\Core\Cache;
-use Friendica\Core\Config;
-use Friendica\Database\DBM;
-
-$session_exists = 0;
-$session_expire = 180000;
-
-function ref_session_open()
-{
-       return true;
-}
-
-function ref_session_read($id)
-{
-       global $session_exists;
-
-       if (!x($id)) {
-               return '';
-       }
-
-       $memcache = Cache::memcache();
-       if (is_object($memcache)) {
-               $data = $memcache->get(get_app()->get_hostname().":session:".$id);
-               if (!is_bool($data)) {
-                       $session_exists = true;
-                       return $data;
-               }
-               logger("no data for session $id", LOGGER_TRACE);
-               return '';
-       }
-
-       $session = dba::selectFirst('session', ['data'], ['sid' => $id]);
-       if (DBM::is_result($session)) {
-               $session_exists = true;
-               return $session['data'];
-       } else {
-               logger("no data for session $id", LOGGER_TRACE);
-       }
-
-       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.
- *
- * @global bool   $session_exists Whether a session with the given id already exists
- * @global int    $session_expire Session expiration delay in seconds
- * @param  string $id   Session ID with format: [a-z0-9]{26}
- * @param  string $data Serialized session data
- * @return boolean Returns false if parameters are missing, true otherwise
- */
-function ref_session_write($id, $data)
-{
-       global $session_exists, $session_expire;
-
-       if (!$id) {
-               return false;
-       }
-
-       if (!$data) {
-               return true;
-       }
-
-       $expire = time() + $session_expire;
-       $default_expire = time() + 300;
-
-       $memcache = Cache::memcache();
-       $a = get_app();
-       if (is_object($memcache) && is_object($a)) {
-               $memcache->set($a->get_hostname().":session:".$id, $data, MEMCACHE_COMPRESSED, $expire);
-               return true;
-       }
-
-       if ($session_exists) {
-               $fields = ['data' => $data, 'expire' => $expire];
-               $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire];
-               dba::update('session', $fields, $condition);
-       } else {
-               $fields = ['sid' => $id, 'expire' => $default_expire, 'data' => $data];
-               dba::insert('session', $fields);
-       }
-
-       return true;
-}
-
-function ref_session_close()
-{
-       return true;
-}
-
-function ref_session_destroy($id)
-{
-       $memcache = Cache::memcache();
-
-       if (is_object($memcache)) {
-               $memcache->delete(get_app()->get_hostname().":session:".$id);
-               return true;
-       }
-
-       dba::delete('session', ['sid' => $id]);
-       return true;
-}
-
-function ref_session_gc()
-{
-       dba::delete('session', ["`expire` < ?", time()]);
-       return true;
-}
-
-$gc_probability = 50;
-
-ini_set('session.gc_probability', $gc_probability);
-ini_set('session.use_only_cookies', 1);
-ini_set('session.cookie_httponly', 1);
-
-if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL) {
-       ini_set('session.cookie_secure', 1);
-}
-
-if (!Config::get('system', 'disable_database_session')) {
-       session_set_save_handler(
-               'ref_session_open', 'ref_session_close',
-               'ref_session_read', 'ref_session_write',
-               'ref_session_destroy', 'ref_session_gc'
-       );
-}
index 2f07fa9af98b6fc3ad2c2590582a9af9ff3598ee..ef84400990198b36bfe6c43ce0c530fdeaa22219 100644 (file)
--- a/index.php
+++ b/index.php
@@ -12,10 +12,11 @@ use Friendica\App;
 use Friendica\BaseObject;
 use Friendica\Content\Nav;
 use Friendica\Core\Addon;
-use Friendica\Core\System;
-use Friendica\Core\Theme;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Session;
+use Friendica\Core\System;
+use Friendica\Core\Theme;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
 use Friendica\Model\Profile;
@@ -77,7 +78,7 @@ if (!$install) {
                exit();
        }
 
-       require_once 'include/session.php';
+       Session::init();
        Addon::loadHooks();
        Addon::callHooks('init_1');
 
@@ -165,19 +166,10 @@ if (! x($_SESSION, 'authenticated')) {
 $a->page['htmlhead'] = '';
 $a->page['end'] = '';
 
+$_SESSION['sysmsg']       = defaults($_SESSION, 'sysmsg'      , []);
+$_SESSION['sysmsg_info']  = defaults($_SESSION, 'sysmsg_info' , []);
+$_SESSION['last_updated'] = defaults($_SESSION, 'last_updated', []);
 
-if (! x($_SESSION, 'sysmsg')) {
-       $_SESSION['sysmsg'] = [];
-}
-
-if (! x($_SESSION, 'sysmsg_info')) {
-       $_SESSION['sysmsg_info'] = [];
-}
-
-// Array for informations about last received items
-if (! x($_SESSION, 'last_updated')) {
-       $_SESSION['last_updated'] = [];
-}
 /*
  * check_config() is responsible for running update scripts. These automatically
  * update the DB schema whenever we push a new one out. It also checks to see if
@@ -474,7 +466,7 @@ if (isset($_GET["mode"]) && (($_GET["mode"] == "raw") || ($_GET["mode"] == "mini
        /// @TODO one day, kill those error-surpressing @ stuff, or PHP should ban it
        @$doc->loadHTML($content);
 
-       $xpath = new DomXPath($doc);
+       $xpath = new DOMXPath($doc);
 
        $list = $xpath->query("//*[contains(@id,'tread-wrapper-')]");  /* */
 
index 36db52b0e054170a73480ddf31825e2db53db680..70a9b9f292607489948a55c8f58a7c267bd4b6f8 100644 (file)
@@ -18,13 +18,13 @@ require_once 'include/dba.php';
 class Cache
 {
        /**
-        * @brief Check for memcache and open a connection if configured
+        * @brief Check for Memcache and open a connection if configured
         *
-        * @return object|boolean The memcache object - or "false" if not successful
+        * @return Memcache|boolean The Memcache object - or "false" if not successful
         */
        public static function memcache()
        {
-               if (!function_exists('memcache_connect')) {
+               if (!class_exists('Memcache', false)) {
                        return false;
                }
 
@@ -35,7 +35,7 @@ class Cache
                $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
                $memcache_port = Config::get('system', 'memcache_port', 11211);
 
-               $memcache = new Memcache;
+               $memcache = new Memcache();
 
                if (!$memcache->connect($memcache_host, $memcache_port)) {
                        return false;
diff --git a/src/Core/Session.php b/src/Core/Session.php
new file mode 100644 (file)
index 0000000..b5c09f7
--- /dev/null
@@ -0,0 +1,57 @@
+<?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
diff --git a/src/Core/Session/DatabaseSessionHandler.php b/src/Core/Session/DatabaseSessionHandler.php
new file mode 100644 (file)
index 0000000..d017e2e
--- /dev/null
@@ -0,0 +1,95 @@
+<?php\r
+\r
+namespace Friendica\Core\Session;\r
+\r
+use Friendica\BaseObject;\r
+use Friendica\Core\Session;\r
+use Friendica\Database\DBM;\r
+use SessionHandlerInterface;\r
+use dba;\r
+\r
+require_once 'boot.php';\r
+require_once 'include/dba.php';\r
+require_once 'include/text.php';\r
+\r
+/**\r
+ * SessionHandler using database\r
+ *\r
+ * @author Hypolite Petovan <mrpetovan@gmail.com>\r
+ */\r
+class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface\r
+{\r
+       public function open($save_path, $session_name)\r
+       {\r
+               return true;\r
+       }\r
+\r
+       public function read($session_id)\r
+       {\r
+               if (!x($session_id)) {\r
+                       return '';\r
+               }\r
+\r
+               $session = dba::selectFirst('session', ['data'], ['sid' => $session_id]);\r
+               if (DBM::is_result($session)) {\r
+                       Session::$exists = true;\r
+                       return $session['data'];\r
+               }\r
+               logger("no data for session $session_id", LOGGER_TRACE);\r
+\r
+               return '';\r
+       }\r
+\r
+       /**\r
+        * @brief Standard PHP session write callback\r
+        *\r
+        * This callback updates the DB-stored session data and/or the expiration depending\r
+        * on the case. Uses the Session::expire global for existing session, 5 minutes\r
+        * for newly created session.\r
+        *\r
+        * @param  string $session_id   Session ID with format: [a-z0-9]{26}\r
+        * @param  string $session_data Serialized session data\r
+        * @return boolean Returns false if parameters are missing, true otherwise\r
+        */\r
+       public function write($session_id, $session_data)\r
+       {\r
+               if (!$session_id) {\r
+                       return false;\r
+               }\r
+\r
+               if (!$session_data) {\r
+                       return true;\r
+               }\r
+\r
+               $expire = time() + Session::$expire;\r
+               $default_expire = time() + 300;\r
+\r
+               if (Session::$exists) {\r
+                       $fields = ['data' => $session_data, 'expire' => $expire];\r
+                       $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];\r
+                       dba::update('session', $fields, $condition);\r
+               } else {\r
+                       $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];\r
+                       dba::insert('session', $fields);\r
+               }\r
+\r
+               return true;\r
+       }\r
+\r
+       public function close()\r
+       {\r
+               return true;\r
+       }\r
+\r
+       public function destroy($id)\r
+       {\r
+               dba::delete('session', ['sid' => $id]);\r
+               return true;\r
+       }\r
+\r
+       public function gc($maxlifetime)\r
+       {\r
+               dba::delete('session', ["`expire` < ?", time()]);\r
+               return true;\r
+       }\r
+}\r
diff --git a/src/Core/Session/MemcacheSessionHandler.php b/src/Core/Session/MemcacheSessionHandler.php
new file mode 100644 (file)
index 0000000..dce206e
--- /dev/null
@@ -0,0 +1,102 @@
+<?php\r
+\r
+namespace Friendica\Core\Session;\r
+\r
+use Friendica\BaseObject;\r
+use Friendica\Core\Session;\r
+use SessionHandlerInterface;\r
+use Memcache;\r
+\r
+require_once 'boot.php';\r
+require_once 'include/text.php';\r
+\r
+/**\r
+ * SessionHandler using Memcache\r
+ *\r
+ * @author Hypolite Petovan <mrpetovan@gmail.com>\r
+ */\r
+class MemcacheSessionHandler extends BaseObject implements SessionHandlerInterface\r
+{\r
+       /**\r
+        * @var Memcache\r
+        */\r
+       private $memcache = null;\r
+\r
+       /**\r
+        *\r
+        * @param Memcache $memcache\r
+        */\r
+       public function __construct(Memcache $memcache)\r
+       {\r
+               $this->memcache = $memcache;\r
+       }\r
+\r
+       public function open($save_path, $session_name)\r
+       {\r
+               return true;\r
+       }\r
+\r
+       public function read($session_id)\r
+       {\r
+               if (!x($session_id)) {\r
+                       return '';\r
+               }\r
+\r
+               $data = $this->memcache->get(self::getApp()->get_hostname() . ":session:" . $session_id);\r
+               if (!is_bool($data)) {\r
+                       Session::$exists = true;\r
+                       return $data;\r
+               }\r
+               logger("no data for session $session_id", LOGGER_TRACE);\r
+               return '';\r
+       }\r
+\r
+       /**\r
+        * @brief Standard PHP session write callback\r
+        *\r
+        * This callback updates the stored session data and/or the expiration depending\r
+        * on the case. Uses the Session::expire for existing session, 5 minutes\r
+        * for newly created session.\r
+        *\r
+        * @param  string $session_id   Session ID with format: [a-z0-9]{26}\r
+        * @param  string $session_data Serialized session data\r
+        * @return boolean Returns false if parameters are missing, true otherwise\r
+        */\r
+       public function write($session_id, $session_data)\r
+       {\r
+               if (!$session_id) {\r
+                       return false;\r
+               }\r
+\r
+               if (!$session_data) {\r
+                       return true;\r
+               }\r
+\r
+               $expire = time() + Session::$expire;\r
+\r
+               $this->memcache->set(\r
+                       self::getApp()->get_hostname() . ":session:" . $session_id,\r
+                       $session_data,\r
+                       MEMCACHE_COMPRESSED,\r
+                       $expire\r
+               );\r
+\r
+               return true;\r
+       }\r
+\r
+       public function close()\r
+       {\r
+               return true;\r
+       }\r
+\r
+       public function destroy($id)\r
+       {\r
+               $this->memcache->delete(self::getApp()->get_hostname() . ":session:" . $id);\r
+               return true;\r
+       }\r
+\r
+       public function gc($maxlifetime)\r
+       {\r
+               return true;\r
+       }\r
+}\r