Ubiquity  2.0.3
php rapid development framework
USession.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
7 
14 class USession {
15  private static $name;
16 
24  public static function getArray($arrayKey) {
25  self::start ();
26  if (isset ( $_SESSION [$arrayKey] )) {
27  $array = $_SESSION [$arrayKey];
28  if (! is_array ( $array ))
29  $array = [ ];
30  } else
31  $array = [ ];
32  return $array;
33  }
34 
46  public static function addOrRemoveValueFromArray($arrayKey, $value, $add = true) {
47  $array = self::getArray ( $arrayKey );
48  $_SESSION [$arrayKey] = $array;
49  $search = array_search ( $value, $array );
50  if ($search === FALSE && $add) {
51  $_SESSION [$arrayKey] [] = $value;
52  return true;
53  } else {
54  unset ( $_SESSION [$arrayKey] [$search] );
55  $_SESSION [$arrayKey] = array_values ( $_SESSION [$arrayKey] );
56  return false;
57  }
58  }
59 
69  public static function removeValueFromArray($arrayKey, $value) {
70  return self::addOrRemoveValueFromArray ( $arrayKey, $value, false );
71  }
72 
82  public static function addValueToArray($arrayKey, $value) {
83  return self::addOrRemoveValueFromArray ( $arrayKey, $value, true );
84  }
85 
95  public static function setBoolean($key, $value) {
96  $_SESSION [$key] = UString::isBooleanTrue ( $value );
97  return $_SESSION [$key];
98  }
99 
107  public static function getBoolean($key) {
108  self::start ();
109  $ret = false;
110  if (isset ( $_SESSION [$key] )) {
111  $ret = UString::isBooleanTrue ( $_SESSION [$key] );
112  }
113  return $ret;
114  }
115 
125  public static function session($key, $default = NULL) {
126  self::start ();
127  return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
128  }
129 
139  public static function get($key, $default = NULL) {
140  self::start ();
141  return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
142  }
143 
151  public static function set($key, $value) {
152  $_SESSION [$key] = $value;
153  return $value;
154  }
155 
156  public static function setTmp($key,$value,$duration){
157  if(isset($_SESSION[$key])){
158  $object=$_SESSION[$key];
159  if($object instanceof SessionObject){
160  return $object->setValue($value);
161  }
162  }
163  $object=new SessionObject($value, $duration);
164  return $_SESSION[$key]=$object;
165  }
166 
167  public static function getTmp($key,$default=null){
168  if(isset($_SESSION[$key])){
169  $object=$_SESSION[$key];
170  if($object instanceof SessionObject){
171  $value=$object->getValue();
172  if(isset($value))
173  return $object->getValue();
174  else{
175  self::delete($key);
176  }
177  }
178  }
179  return $default;
180  }
181 
182  public static function getTimeout($key){
183  if(isset($_SESSION[$key])){
184  $object=$_SESSION[$key];
185  if($object instanceof SessionObject){
186  $value=$object->getTimeout();
187  if($value<0){
188  return 0;
189  }else{
190  return $value;
191  }
192  }
193  }
194  return;
195  }
196 
203  public static function delete($key) {
204  self::start ();
205  unset ( $_SESSION [$key] );
206  }
207 
215  public static function inc($key, $inc = 1) {
216  return self::set ( $key, self::get ( $key, 0 ) + $inc );
217  }
218 
226  public static function dec($key, $dec = 1) {
227  return self::set ( $key, self::get ( $key, 0 ) - $dec );
228  }
229 
237  public static function concat($key, $str, $default = NULL) {
238  return self::set ( $key, self::get ( $key, $default ) . $str );
239  }
240 
248  public static function apply($key, $callback, $default = NULL) {
249  $value = self::get ( $key, $default );
250  if (is_string ( $callback ) && function_exists ( $callback )) {
251  $value = call_user_func ( $callback, $value );
252  } elseif (is_callable ( $callback )) {
253  $value = $callback ( $value );
254  } else {
255  return $value;
256  }
257  return self::set ( $key, $value );
258  }
259 
267  public static function Walk($callback, $userData = null) {
268  self::start ();
269  array_walk ( $_SESSION, $callback, $userData );
270  return $_SESSION;
271  }
272 
279  public static function replace($keyAndValues) {
280  self::start ();
281  $_SESSION = array_replace ( $_SESSION, $keyAndValues );
282  return $_SESSION;
283  }
284 
290  public static function getAll() {
291  self::start ();
292  return $_SESSION;
293  }
294 
301  public static function start($name = null) {
302  if (! isset ( $_SESSION )) {
303  if (isset ( $name ) && $name !== "") {
304  self::$name = $name;
305  }
306  if (isset ( self::$name )) {
307  \session_name ( self::$name );
308  }
309  \session_start ();
310  }
311  }
312 
318  public static function isStarted() {
319  return isset ( $_SESSION );
320  }
321 
329  public static function exists($key) {
330  self::start ();
331  return isset ( $_SESSION [$key] );
332  }
333 
340  public static function init($key,$value){
341  if(!isset($_SESSION[$key])){
342  $_SESSION[$key]=$value;
343  }
344  return $_SESSION[$key];
345  }
346 
350  public static function terminate() {
351  if (! self::isStarted ())
352  return;
353  self::start ();
354  $_SESSION = array ();
355 
356  if (\ini_get ( "session.use_cookies" )) {
357  $params = \session_get_cookie_params ();
358  \setcookie ( \session_name (), '', \time () - 42000, $params ["path"], $params ["domain"], $params ["secure"], $params ["httponly"] );
359  }
360  \session_destroy ();
361  }
362 }
Http Session utilities.
Definition: USession.php:14
static dec($key, $dec=1)
Decrement the value at the key index in session.
Definition: USession.php:226
static init($key, $value)
Initialize the key in Session if key does not exists.
Definition: USession.php:340
static exists($key)
Returns true if the key exists in Session.
Definition: USession.php:329
static session($key, $default=NULL)
Returns the value stored at the key position in session.
Definition: USession.php:125
static setTmp($key, $value, $duration)
Definition: USession.php:156
static isStarted()
Returns true if the session is started.
Definition: USession.php:318
static apply($key, $callback, $default=NULL)
Applies a callback function to the value at the key index in session.
Definition: USession.php:248
static Walk($callback, $userData=null)
Apply a user supplied function to every member of Session array.
Definition: USession.php:267
static concat($key, $str, $default=NULL)
Adds a string at the end of the value at the key index in session.
Definition: USession.php:237
static getBoolean($key)
Returns a boolean stored at the key position in session.
Definition: USession.php:107
static replace($keyAndValues)
Replaces elements from Session array with $keyAndValues.
Definition: USession.php:279
static terminate()
Terminates the active session.
Definition: USession.php:350
static setBoolean($key, $value)
Sets a boolean value at key position in session.
Definition: USession.php:95
static getAll()
Returns the associative array of session vars.
Definition: USession.php:290
static start($name=null)
Start new or resume existing session.
Definition: USession.php:301
static inc($key, $inc=1)
Increment the value at the key index in session.
Definition: USession.php:215
static removeValueFromArray($arrayKey, $value)
Removes a value from an array in session.
Definition: USession.php:69
static getArray($arrayKey)
Returns an array stored in session variable as $arrayKey.
Definition: USession.php:24
static addValueToArray($arrayKey, $value)
Adds a value from an array in session.
Definition: USession.php:82
static addOrRemoveValueFromArray($arrayKey, $value, $add=true)
Adds or removes a value from an array in session.
Definition: USession.php:46
static getTmp($key, $default=null)
Definition: USession.php:167