wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.SessionData.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.SessionData.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 require_once(BASE."wcmf/lib/util/class.ObjectFactory.php");
21 function __autoload($className)
22 {
23  $objectFactory = &ObjectFactory::getInstance();
24  $classFile = $objectFactory->getClassfileFromConfig($className);
25  if ($classFile !== false) {
26  require_once(BASE.$classFile);
27  }
28 }
29 
31 /**
32  * @class SessionData
33  * @ingroup Util
34  * @brief This class provides a unified access to session data.
35  *
36  * @author ingo herwig <ingo@wemove.com>
37  */
39 {
40  var $ERROR_VARNAME = 'SessionData.errors';
41  var $_id = null;
42 
43  /**
44  * Get the file where the class definitions are stored.
45  * @return filename
46  */
48  {
49  $path = array_pop(split(";", session_save_path()));
50  return $path."/sess_cd_".session_id().'_'.Application::getId();
51  }
52 
53  /**
54  * Created a SessionData instance with given session id.
55  * @param sessionID The session id to use (maybe null).
56  * @note If session id is null an automatically generated session id will be used.
57  */
58  function init($sessionID)
59  {
60  global $gSessionData;
61  $gSessionData = new SessionData();
62 
63  if ($sessionID != null)
64  {
65  // We have a custom session id so we can automatically restore class definitions.
66 
67  // Set custom session id
68  session_id($sessionID);
69 
70  // Restore class definitions before session start
72  if (file_exists($filename))
73  {
74  $fp = fopen($filename, "r");
75  $classDefs = fread($fp, filesize ($filename));
76  fclose($fp);
77  foreach (split("\n", $classDefs) as $classDef)
78  if ($classDef != '')
79  require_once($classDef);
80  }
81  }
82  session_start();
83  if (strlen($_COOKIE[ini_get('session.name')]) == 0) {
84  session_regenerate_id();
85  }
86  $this->_id = session_id();
87  session_write_close();
88  }
89  /**
90  * Returns an instance of the class.
91  * @note If called before init an automatically generated session id will be used.
92  * @return A reference to the only instance of the Singleton object
93  */
94  function &getInstance()
95  {
96  global $gSessionData;
97 
98  if ($gSessionData == null)
99  SessionData::init(null);
100 
101  return $gSessionData;
102  }
103  /**
104  * Get the id of the session.
105  * @return The id of the current session.
106  */
107  function getID()
108  {
109  return $this->_id;
110  }
111  /**
112  * Returns the value of an session variable
113  * @param key The key (name) of the session vaiable.
114  * @return A reference to the session var or null if it doesn't exist.
115  */
116  function &get($key)
117  {
118  session_start();
119  $val = null;
120  if (array_key_exists($key, $_SESSION))
121  {
122  $val = &$_SESSION[$key];
123  if (is_object($val))
124  {
125  $classMethods = array_map("strtolower", get_class_methods($val));
126  if (in_array('loadFromSession', $classMethods))
127  $val->loadFromSession();
128  }
129  }
130  session_write_close();
131  return $val;
132  }
133  /**
134  * Sets the value of an session variable. If the value is an object it must eiter implement the Storable interface
135  * or the classFiles parameter must not be null.
136  * @param key The key (name) of the session vaiable.
137  * @param val The value of the session variable.
138  * @param classFiles An array of definition files needed to be included for rebuilding val from the session [default: null].
139  * @return A reference to the session var
140  */
141  function set($key, &$val, $classFiles=null)
142  {
143  // try to store class definition
144 
145  if (is_object($val))
146  {
147  if ($classFiles == null)
148  {
149  // no class files given -> check for Storable interface
150  $classMethods = array_map(strtolower, get_class_methods($val));
151  if (!in_array('getclassdefinitionfiles', $classMethods))
152  {
153  WCMFException::throwEx("Class ".get_class($val)." does not implement the Storable interface.", __FILE__, __LINE__);
154  }
155  else
156  {
157  // Store class definitions of session object
158  $classFiles = $val->getClassDefinitionFiles();
159 
160  if (in_array('saveToSession', $classMethods))
161  $val->saveToSession();
162  }
163  }
164  // Store class definitions of session object
165  $this->addClassDefinitions($classFiles);
166  }
167  session_start();
168  $_SESSION[$key] = &$val;
169  session_write_close();
170  }
171  /**
172  * Add an array of class definitions to the session's class definitions list.
173  * @param classFiles An array of class definition files.
174  */
175  function addClassDefinitions($classFiles)
176  {
178  $classDefsStr = '';
179  if (file_exists($filename))
180  {
181  $fp = fopen($filename, "r");
182  $classDefsStr = fread($fp, filesize ($filename));
183  fclose($fp);
184  }
185  $classDefs = preg_split("/\n/", $classDefsStr);
186  $fp = fopen($filename, "a");
187  foreach ($classFiles as $classFile)
188  if (!in_array($classFile, $classDefs))
189  fwrite($fp, $classFile."\n");
190  fclose($fp);
191  }
192  /**
193  * Remove a session variable.
194  * @param key The key (name) of the session variable.
195  */
196  function remove($key)
197  {
198  session_start();
199  unset($_SESSION[$key]);
200  session_write_close();
201  }
202  /**
203  * Tests, if a certain session variable is defined.
204  * @param key The key (name) of the session variable.
205  * @return A boolean flag. true if the session variable is set, false if not.
206  */
207  function exist($key)
208  {
209  session_start();
210  return array_key_exists($key, $_SESSION);
211  session_write_close();
212  }
213  /**
214  * Clear the session data.
215  */
216  function clear()
217  {
218  session_start();
219  session_unset();
220  session_write_close();
221  }
222  /**
223  * Add an error to the session data.
224  * @param key The identifier of the error
225  * @param error The error message
226  */
227  function addError($key, $error)
228  {
229  session_start();
230  if (!is_array($_SESSION[$this->ERROR_VARNAME]))
231  $_SESSION[$this->ERROR_VARNAME] = array();
232  $_SESSION[$this->ERROR_VARNAME][$key] = $error;
233  session_write_close();
234  }
235  /**
236  * Get an error stored in the session data.
237  * @param key The identifier of the error
238  * @return The error message
239  */
240  function getError($key)
241  {
242  session_start();
243  return $_SESSION[$this->ERROR_VARNAME][$key];
244  session_write_close();
245  }
246  /**
247  * Get all errors stored in the session data.
248  * @return The error message
249  */
250  function getErrors()
251  {
252  session_start();
253  return $_SESSION[$this->ERROR_VARNAME];
254  session_write_close();
255  }
256  /**
257  * Clear the session error data.
258  */
259  function clearErrors()
260  {
261  session_start();
262  unset($_SESSION[$this->ERROR_VARNAME]);
263  session_write_close();
264  }
265  /**
266  * Destroy the session.
267  */
268  function destroy()
269  {
270  // Delete class definition file
271  session_start();
273  if (file_exists($filename))
274  unlink($filename);
275  $_SESSION = array();
276  session_destroy();
277  }
278 }
__autoload($className)
$gSessionData
throwEx($message, $file='', $line='')
addClassDefinitions($classFiles)
set($key, &$val, $classFiles=null)
init($sessionID)
This class provides a unified access to session data.
addError($key, $error)