Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
storage.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Session
5  *
6  * @copyright Copyright (C) 2005 - 2013 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  * @package Joomla.Platform
16  * @subpackage Session
17  * @see http://www.php.net/manual/en/function.session-set-save-handler.php
18  * @todo When dropping compatibility with PHP 5.3 use the SessionHandlerInterface and the SessionHandler class
19  * @since 11.1
20  */
21 abstract class JSessionStorage
22 {
23  /**
24  * @var array JSessionStorage instances container.
25  * @since 11.3
26  */
27  protected static $instances = array();
28 
29  /**
30  * Constructor
31  *
32  * @param array $options Optional parameters.
33  *
34  * @since 11.1
35  */
36  public function __construct($options = array())
37  {
38  $this->register($options);
39  }
40 
41  /**
42  * Returns a session storage handler object, only creating it if it doesn't already exist.
43  *
44  * @param string $name The session store to instantiate
45  * @param array $options Array of options
46  *
47  * @return JSessionStorage
48  *
49  * @since 11.1
50  */
51  public static function getInstance($name = 'none', $options = array())
52  {
53  $name = strtolower(JFilterInput::getInstance()->clean($name, 'word'));
54 
55  if (empty(self::$instances[$name]))
56  {
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  require_once $path;
66  }
67  else
68  {
69  // No attempt to die gracefully here, as it tries to close the non-existing session
70  jexit('Unable to load session storage class: ' . $name);
71  }
72  }
73 
74  self::$instances[$name] = new $class($options);
75  }
76 
77  return self::$instances[$name];
78  }
79 
80  /**
81  * Register the functions of this class with PHP's session handler
82  *
83  * @return void
84  *
85  * @since 11.1
86  */
87  public function register()
88  {
89  // Use this object as the session handler
90  session_set_save_handler(
91  array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'),
92  array($this, 'destroy'), array($this, 'gc')
93  );
94  }
95 
96  /**
97  * Open the SessionHandler backend.
98  *
99  * @param string $save_path The path to the session object.
100  * @param string $session_name The name of the session.
101  *
102  * @return boolean True on success, false otherwise.
103  *
104  * @since 11.1
105  */
106  public function open($save_path, $session_name)
107  {
108  return true;
109  }
110 
111  /**
112  * Close the SessionHandler backend.
113  *
114  * @return boolean True on success, false otherwise.
115  *
116  * @since 11.1
117  */
118  public function close()
119  {
120  return true;
121  }
122 
123  /**
124  * Read the data for a particular session identifier from the
125  * SessionHandler backend.
126  *
127  * @param string $id The session identifier.
128  *
129  * @return string The session data.
130  *
131  * @since 11.1
132  */
133  public function read($id)
134  {
135  return;
136  }
137 
138  /**
139  * Write session data to the SessionHandler backend.
140  *
141  * @param string $id The session identifier.
142  * @param string $session_data The session data.
143  *
144  * @return boolean True on success, false otherwise.
145  *
146  * @since 11.1
147  */
148  public function write($id, $session_data)
149  {
150  return true;
151  }
152 
153  /**
154  * Destroy the data for a particular session identifier in the
155  * SessionHandler backend.
156  *
157  * @param string $id The session identifier.
158  *
159  * @return boolean True on success, false otherwise.
160  *
161  * @since 11.1
162  */
163  public function destroy($id)
164  {
165  return true;
166  }
167 
168  /**
169  * Garbage collect stale sessions from the SessionHandler backend.
170  *
171  * @param integer $maxlifetime The maximum age of a session.
172  *
173  * @return boolean True on success, false otherwise.
174  *
175  * @since 11.1
176  */
177  public function gc($maxlifetime = null)
178  {
179  return true;
180  }
181 
182  /**
183  * Test to see if the SessionHandler is available.
184  *
185  * @return boolean True on success, false otherwise.
186  *
187  * @since 12.1
188  */
189  public static function isSupported()
190  {
191  return true;
192  }
193 
194  /**
195  * Test to see if the SessionHandler is available.
196  *
197  * @return boolean True on success, false otherwise.
198  *
199  * @since 11.1
200  * @deprecated 12.3 (Platform) & 4.0 (CMS) - Use JSessionStorage::isSupported() instead.
201  */
202  public static function test()
203  {
204  JLog::add('JSessionStorage::test() is deprecated. Use JSessionStorage::isSupported() instead.', JLog::WARNING, 'deprecated');
205 
206  return static::isSupported();
207  }
208 }