1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Session
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 /**
 13  * Custom session storage handler for PHP
 14  *
 15  * @link        https://secure.php.net/manual/en/function.session-set-save-handler.php
 16  * @since       11.1
 17  * @deprecated  4.0  The CMS' Session classes will be replaced with the `joomla/session` package
 18  */
 19 abstract class JSessionStorage
 20 {
 21     /**
 22      * @var    JSessionStorage[]  JSessionStorage instances container.
 23      * @since  11.3
 24      */
 25     protected static $instances = array();
 26 
 27     /**
 28      * Constructor
 29      *
 30      * @param   array  $options  Optional parameters.
 31      *
 32      * @since   11.1
 33      */
 34     public function __construct($options = array())
 35     {
 36         $this->register($options);
 37     }
 38 
 39     /**
 40      * Returns a session storage handler object, only creating it if it doesn't already exist.
 41      *
 42      * @param   string  $name     The session store to instantiate
 43      * @param   array   $options  Array of options
 44      *
 45      * @return  JSessionStorage
 46      *
 47      * @since   11.1
 48      * @throws  JSessionExceptionUnsupported
 49      */
 50     public static function getInstance($name = 'none', $options = array())
 51     {
 52         $name = strtolower(JFilterInput::getInstance()->clean($name, 'word'));
 53 
 54         if (empty(self::$instances[$name]))
 55         {
 56             /** @var JSessionStorage $class */
 57             $class = 'JSessionStorage' . ucfirst($name);
 58 
 59             if (!class_exists($class))
 60             {
 61                 $path = __DIR__ . '/storage/' . $name . '.php';
 62 
 63                 if (!file_exists($path))
 64                 {
 65                     throw new JSessionExceptionUnsupported('Unable to load session storage class: ' . $name);
 66                 }
 67 
 68                 JLoader::register($class, $path);
 69 
 70                 // The class should now be loaded
 71                 if (!class_exists($class))
 72                 {
 73                     throw new JSessionExceptionUnsupported('Unable to load session storage class: ' . $name);
 74                 }
 75             }
 76 
 77             // Validate the session storage is supported on this platform
 78             if (!$class::isSupported())
 79             {
 80                 throw new JSessionExceptionUnsupported(sprintf('The %s Session Storage is not supported on this platform.', $name));
 81             }
 82 
 83             self::$instances[$name] = new $class($options);
 84         }
 85 
 86         return self::$instances[$name];
 87     }
 88 
 89     /**
 90      * Register the functions of this class with PHP's session handler
 91      *
 92      * @return  void
 93      *
 94      * @since   11.1
 95      */
 96     public function register()
 97     {
 98         // Use this object as the session handler
 99         session_set_save_handler(
100             array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'),
101             array($this, 'destroy'), array($this, 'gc')
102         );
103     }
104 
105     /**
106      * Open the SessionHandler backend.
107      *
108      * @param   string  $save_path     The path to the session object.
109      * @param   string  $session_name  The name of the session.
110      *
111      * @return  boolean  True on success, false otherwise.
112      *
113      * @since   11.1
114      */
115     public function open($save_path, $session_name)
116     {
117         return true;
118     }
119 
120     /**
121      * Close the SessionHandler backend.
122      *
123      * @return  boolean  True on success, false otherwise.
124      *
125      * @since   11.1
126      */
127     public function close()
128     {
129         return true;
130     }
131 
132     /**
133      * Read the data for a particular session identifier from the
134      * SessionHandler backend.
135      *
136      * @param   string  $id  The session identifier.
137      *
138      * @return  string  The session data.
139      *
140      * @since   11.1
141      */
142     public function read($id)
143     {
144         return;
145     }
146 
147     /**
148      * Write session data to the SessionHandler backend.
149      *
150      * @param   string  $id            The session identifier.
151      * @param   string  $session_data  The session data.
152      *
153      * @return  boolean  True on success, false otherwise.
154      *
155      * @since   11.1
156      */
157     public function write($id, $session_data)
158     {
159         return true;
160     }
161 
162     /**
163      * Destroy the data for a particular session identifier in the
164      * SessionHandler backend.
165      *
166      * @param   string  $id  The session identifier.
167      *
168      * @return  boolean  True on success, false otherwise.
169      *
170      * @since   11.1
171      */
172     public function destroy($id)
173     {
174         return true;
175     }
176 
177     /**
178      * Garbage collect stale sessions from the SessionHandler backend.
179      *
180      * @param   integer  $maxlifetime  The maximum age of a session.
181      *
182      * @return  boolean  True on success, false otherwise.
183      *
184      * @since   11.1
185      */
186     public function gc($maxlifetime = null)
187     {
188         return true;
189     }
190 
191     /**
192      * Test to see if the SessionHandler is available.
193      *
194      * @return  boolean  True on success, false otherwise.
195      *
196      * @since   12.1
197      */
198     public static function isSupported()
199     {
200         return true;
201     }
202 
203     /**
204      * Test to see if the SessionHandler is available.
205      *
206      * @return  boolean  True on success, false otherwise.
207      *
208      * @since   11.1
209      * @deprecated  12.3 (Platform) & 4.0 (CMS) - Use JSessionStorage::isSupported() instead.
210      */
211     public static function test()
212     {
213         JLog::add('JSessionStorage::test() is deprecated. Use JSessionStorage::isSupported() instead.', JLog::WARNING, 'deprecated');
214 
215         return static::isSupported();
216     }
217 }
218