Completed
Push — master ( 9aea22...ab0022 )
by Jean-Christophe
02:12
created

USession::getTimeout()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
use Ubiquity\utils\base\UString;
6
use Ubiquity\utils\http\session\SessionObject;
7
8
/**
9
 * Http Session utilities
10
 *
11
 * @author jc
12
 * @version 1.0.0.4
13
 */
14
class USession {
15
	private static $name;
16
17
	/**
18
	 * Returns an array stored in session variable as $arrayKey
19
	 *
20
	 * @param string $arrayKey
21
	 *        	the key of the array to return
22
	 * @return array
23
	 */
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
35
	/**
36
	 * Adds or removes a value from an array in session
37
	 *
38
	 * @param string $arrayKey
39
	 *        	the key of the array to add or remove in
40
	 * @param mixed $value
41
	 *        	the value to add
42
	 * @param boolean $add
43
	 *        	If true, adds otherwise removes
44
	 * @return boolean
45
	 */
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
60
	/**
61
	 * Removes a value from an array in session
62
	 *
63
	 * @param string $arrayKey
64
	 *        	the key of the array to remove in
65
	 * @param mixed $value
66
	 *        	the value to remove
67
	 * @return boolean
68
	 */
69
	public static function removeValueFromArray($arrayKey, $value) {
70
		return self::addOrRemoveValueFromArray ( $arrayKey, $value, false );
71
	}
72
73
	/**
74
	 * Adds a value from an array in session
75
	 *
76
	 * @param string $arrayKey
77
	 *        	the key of the array to add in
78
	 * @param mixed $value
79
	 *        	the value to add
80
	 * @return boolean
81
	 */
82
	public static function addValueToArray($arrayKey, $value) {
83
		return self::addOrRemoveValueFromArray ( $arrayKey, $value, true );
84
	}
85
86
	/**
87
	 * Sets a boolean value at key position in session
88
	 *
89
	 * @param string $key
90
	 *        	the key to add or set in
91
	 * @param mixed $value
92
	 *        	the value to set
93
	 * @return boolean
94
	 */
95
	public static function setBoolean($key, $value) {
96
		$_SESSION [$key] = UString::isBooleanTrue ( $value );
97
		return $_SESSION [$key];
98
	}
99
100
	/**
101
	 * Returns a boolean stored at the key position in session
102
	 *
103
	 * @param string $key
104
	 *        	the key to add or set
105
	 * @return boolean
106
	 */
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
116
	/**
117
	 * Returns the value stored at the key position in session
118
	 *
119
	 * @param string $key
120
	 *        	the key to retreive
121
	 * @param mixed $default
122
	 *        	the default value to return if the key does not exists in session
123
	 * @return mixed
124
	 */
125
	public static function session($key, $default = NULL) {
126
		self::start ();
127
		return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
128
	}
129
130
	/**
131
	 * Returns the value stored at the key position in session
132
	 *
133
	 * @param string $key
134
	 *        	the key to retreive
135
	 * @param mixed $default
136
	 *        	the default value to return if the key does not exists in session
137
	 * @return mixed
138
	 */
139
	public static function get($key, $default = NULL) {
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
140
		self::start ();
141
		return isset ( $_SESSION [$key] ) ? $_SESSION [$key] : $default;
142
	}
143
144
	/**
145
	 * Adds or sets a value to the Session at position $key
146
	 *
147
	 * @param string $key
148
	 *        	the key to add or set
149
	 * @param mixed $value
150
	 */
151
	public static function set($key, $value) {
0 ignored issues
show
Coding Style introduced by
set uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
152
		$_SESSION [$key] = $value;
153
		return $value;
154
	}
155
	
156
	public static function setTmp($key,$value,$duration){
1 ignored issue
show
Coding Style introduced by
setTmp uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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){
1 ignored issue
show
Coding Style introduced by
getTmp uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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){
1 ignored issue
show
Coding Style introduced by
getTimeout uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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
197
	/**
198
	 * Deletes the key in Session
199
	 *
200
	 * @param string $key
201
	 *        	the key to delete
202
	 */
203
	public static function delete($key) {
204
		self::start ();
205
		unset ( $_SESSION [$key] );
206
	}
207
208
	/**
209
	 * Increment the value at the key index in session
210
	 *
211
	 * @param string $key
212
	 * @param number $inc
213
	 * @return number
214
	 */
215
	public static function inc($key, $inc = 1) {
216
		return self::set ( $key, self::get ( $key, 0 ) + $inc );
217
	}
218
219
	/**
220
	 * Decrement the value at the key index in session
221
	 *
222
	 * @param string $key
223
	 * @param number $dec
224
	 * @return number
225
	 */
226
	public static function dec($key, $dec = 1) {
227
		return self::set ( $key, self::get ( $key, 0 ) - $dec );
228
	}
229
230
	/**
231
	 * Adds a string at the end of the value at the key index in session
232
	 *
233
	 * @param string $key
234
	 * @param string $str
235
	 * @return string
236
	 */
237
	public static function concat($key, $str, $default = NULL) {
238
		return self::set ( $key, self::get ( $key, $default ) . $str );
239
	}
240
241
	/**
242
	 * Applies a callback function to the value at the key index in session
243
	 *
244
	 * @param string $key
245
	 * @param string|callable $callback
246
	 * @return mixed
247
	 */
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
260
	/**
261
	 * Apply a user supplied function to every member of Session array
262
	 *
263
	 * @param callable $callback
264
	 * @param mixed $userData
265
	 * @return array
266
	 */
267
	public static function Walk($callback, $userData = null) {
268
		self::start ();
269
		array_walk ( $_SESSION, $callback, $userData );
270
		return $_SESSION;
271
	}
272
273
	/**
274
	 * Replaces elements from Session array with $keyAndValues
275
	 *
276
	 * @param array $keyAndValues
277
	 * @return array
278
	 */
279
	public static function replace($keyAndValues) {
280
		self::start ();
281
		$_SESSION = array_replace ( $_SESSION, $keyAndValues );
282
		return $_SESSION;
283
	}
284
285
	/**
286
	 * Returns the associative array of session vars
287
	 *
288
	 * @return array
289
	 */
290
	public static function getAll() {
0 ignored issues
show
Coding Style introduced by
getAll uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
291
		self::start ();
292
		return $_SESSION;
293
	}
294
295
	/**
296
	 * Start new or resume existing session
297
	 *
298
	 * @param string|null $name
299
	 *        	the name of the session
300
	 */
301
	public static function start($name = null) {
0 ignored issues
show
Coding Style introduced by
start uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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
313
	/**
314
	 * Returns true if the session is started
315
	 *
316
	 * @return boolean
317
	 */
318
	public static function isStarted() {
0 ignored issues
show
Coding Style introduced by
isStarted uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
319
		return isset ( $_SESSION );
320
	}
321
322
	/**
323
	 * Returns true if the key exists in Session
324
	 *
325
	 * @param string $key
326
	 *        	the key to test
327
	 * @return boolean
328
	 */
329
	public static function exists($key) {
0 ignored issues
show
Coding Style introduced by
exists uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
330
		self::start ();
331
		return isset ( $_SESSION [$key] );
332
	}
333
	
334
	/**
335
	 * Initialize the key in Session if key does not exists
336
	 * @param string $key
337
	 * @param mixed $value
338
	 * @return mixed
339
	 */
340
	public static function init($key,$value){
341
		if(!isset($_SESSION[$key])){
342
			$_SESSION[$key]=$value;
343
		}
344
		return $_SESSION[$key];
345
	}
346
347
	/**
348
	 * Terminates the active session
349
	 */
350
	public static function terminate() {
0 ignored issues
show
Coding Style introduced by
terminate uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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
}
363