Daniele Bonini 5 years ago
commit
5e62ee3e78
2 changed files with 241 additions and 0 deletions
  1. 15 0
      README.md
  2. 226 0
      class.fastsession.inc

+ 15 - 0
README.md

@@ -0,0 +1,15 @@
+
+ FastSession    
+ 
+ FastSession class    
+ 
+ @package  OpenGallery http://github.com/par7133    
+ @author   Daniele Bonini <my25mb@aol.com>    
+ @version  1.0    
+ @access   public    
+ @note You have to declare in your "config.inc" file - or whatever file you    
+ use for the purpose, the following global constant:    
+ define('SESSION_NAME', "PHPSESSION");    
+ 
+
+

+ 226 - 0
class.fastsession.inc

@@ -0,0 +1,226 @@
+<?php
+
+/**
+ * Copyright (c) 2016, 2022, the Open Gallery's contributors
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither Open Gallery nor the names of its contributors 
+ *       may be used to endorse or promote products derived from this software 
+ *       without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * class.fastsession.inc
+ * 
+ * FastSession class.
+ *
+ * @author Daniele Bonini <my25mb@aol.com>
+ * @copyrights (c) 2016, 2022, the Open Gallery's contributors     
+ * @license https://opensource.org/licenses/BSD-3-Clause 
+ */
+
+namespace OpenGallery\OpenGallery;
+
+/**
+ * FastSession
+ *
+ * FastSession class
+ *
+ * @package  OpenGallery
+ * @author   Daniele Bonini <my25mb@aol.com>
+ * @version  1.0
+ * @access   public
+ * @note You have to declare in your "config.inc" file - or whatever file you
+ * use for the purpose, the following global constant:
+ * define('SESSION_NAME', "PHPSESSION");
+ */
+final class FastSession 
+{
+  /**
+      * Default constructor
+      * 
+      * @return void
+      */
+  private function __construct()
+  {
+  }
+
+  /**
+      * Check if the given session variable exists
+      * 
+      * @param string  $key the key of the variable
+      * @return bool  if the session variable exists, true/false
+      * @access public
+      */
+  public static function exists($key) 
+  {
+    return isset($_SESSION[$key]) ? true : false;
+      
+  }
+  
+  /**
+      * Check if the session is started
+      * 
+      * @return bool  if the session is started, true/false
+      * @access public
+      */
+  public static function isStarted()
+  {
+    return isset($_SESSION) ? true : false;
+  }
+
+  /**
+      * Check if the session is active
+      * 
+      * @return bool  if the session is active, true/false
+      * @access public
+      */
+  public static function isActive()
+  {
+    return session_status() === PHP_SESSION_ACTIVE ? true : false;
+  }
+  
+  /**
+      * Start the session
+      * 
+      * @return void
+      * @access public
+      */
+  public static function start()
+  {
+    if (!self::isStarted()) {
+      session_name(SESSION_NAME);
+    }
+    if (!self::isActive()) {
+      session_start();
+    }
+  }
+
+  /**
+      * Start the session with a new context
+      * 
+      * @return void
+      * @access public
+      */
+  public static function startWithNewContext()
+  {
+    if (!self::isStarted()) {
+      session_name(SESSION_NAME);
+    }
+    if (!self::isActive()) {
+      session_start();
+    }
+    session_regenerate_id(true);    
+  }
+  
+  /**
+      * Start the session in editing mode
+      * 
+      * @return void
+      * @access public
+      */
+  public static function startEditing()
+  {
+    if (!self::isActive()) {
+      session_start();
+    }
+  }
+
+  /**
+      * Retrieve the value of the given session variable
+      * 
+      * @param string  $key the key of session variable to retrieve
+      * @param string  $default a default value
+      * @return mixed  the value
+      * @access public
+      */
+  public static function get($key, $default=false) 
+  {
+    if (self::exists($key)) {
+      return $_SESSION[$key];
+    } else {
+      return $default;
+    }
+  }
+  
+  /**
+      * Set the value of the given session variable
+      * 
+      * @param string  $key the key of the session variable
+      * @param mixed  $value the new value to assign
+      * @return void
+      * @access public
+      */
+  public static function set($key, $value) 
+  {
+    $_SESSION[$key] = $value;
+  }
+
+  /**
+      * Unset the given session variable
+      * 
+      * @param string  $key the key of the session variable
+      * @return void
+      * @access public
+      */
+  public static function unset1($key) 
+  {
+    if (self::exists($key)) {
+      unset($_SESSION[$key]);
+    }
+  }
+
+  /**
+      * Release the write handle of the session
+      * 
+      * @return void
+      * @access public
+      */
+  public static function closeEditing()
+  {
+    self::close();
+  }
+
+  /**
+      * Close the session
+      * 
+      * @return void
+      * @access public
+      */
+  public static function close()
+  {
+    if (self::isActive()) {
+      session_write_close();
+    }
+  }
+  
+  /**
+      * Destroy the session
+      * 
+      * @return void
+      * @access public
+      */
+  public static function destory()
+  {
+    self::start();
+
+    session_unset();
+    session_destroy();
+  }
+}