Completed
Push — 3.5 ( 975d46...a84659 )
by Damian
07:52
created

Session::expireIfInvalid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Handles all manipulation of the session.
4
 *
5
 * The static methods are used to manipulate the currently active controller's session.
6
 * The instance methods are used to manipulate a particular session.  There can be more than one of these created.
7
 *
8
 * In order to support things like testing, the session is associated with a particular Controller.  In normal usage,
9
 * this is loaded from and saved to the regular PHP session, but for things like static-page-generation and
10
 * unit-testing, you can create multiple Controllers, each with their own session.
11
 *
12
 * The instance object is basically just a way of manipulating a set of nested maps, and isn't specific to session
13
 * data.
14
 *
15
 * <b>Saving Data</b>
16
 *
17
 * You can write a value to a users session from your PHP code using the static function {@link Session::set()}. You
18
 * can add this line in any function or file you wish to save the value.
19
 *
20
 * <code>
21
 * 	Session::set('MyValue', 6);
22
 * </code>
23
 *
24
 * Saves the value of "6" to the MyValue session data. You can also save arrays or serialized objects in session (but
25
 * note there may be size restrictions as to how much you can save)
26
 *
27
 * <code>
28
 * 	// save a variable
29
 * 	$var = 1;
30
 * 	Session::set('MyVar', $var);
31
 *
32
 * 	// saves an array
33
 * 	Session::set('MyArrayOfValues', array('1','2','3'));
34
 *
35
 * 	// saves an object (you'll have to unserialize it back)
36
 * 	$object = new Object();
37
 *
38
 * 	Session::set('MyObject', serialize($object));
39
 * </code>
40
 *
41
 * <b>Accessing Data</b>
42
 *
43
 * Once you have saved a value to the Session you can access it by using the {@link Session::get()} function.
44
 * Like the {@link Session::set()} function you can use this anywhere in your PHP files.
45
 *
46
 * The values in the comments are the values stored from the previous example.
47
 *
48
 * <code>
49
 * public function bar() {
50
 * 	$value = Session::get('MyValue'); // $value = 6
51
 * 	$var   = Session::get('MyVar'); // $var = 1
52
 * 	$array = Session::get('MyArrayOfValues'); // $array = array(1,2,3)
53
 * 	$object = Session::get('MyObject', unserialize($object)); // $object = Object()
54
 * }
55
 * </code>
56
 *
57
 * You can also get all the values in the session at once. This is useful for debugging.
58
 *
59
 * <code>
60
 * Session::get_all(); // returns an array of all the session values.
61
 * </code>
62
 *
63
 * <b>Clearing Data</b>
64
 *
65
 * Once you have accessed a value from the Session it doesn't automatically wipe the value from the Session, you have
66
 * to specifically remove it. To clear a value you can either delete 1 session value by the name that you saved it
67
 *
68
 * <code>
69
 * Session::clear('MyValue'); // MyValue is no longer 6.
70
 * </code>
71
 *
72
 * Or you can clear every single value in the session at once. Note SilverStripe stores some of its own session data
73
 * including form and page comment information. None of this is vital but clear_all will clear everything.
74
 *
75
 * <code>
76
 * 	Session::clear_all();
77
 * </code>
78
 *
79
 * @see Cookie
80
 * @todo This class is currently really basic and could do with a more well-thought-out implementation.
81
 *
82
 * @package framework
83
 * @subpackage control
84
 */
85
86
class Session {
87
88
	/**
89
	 * @var $timeout Set session timeout in seconds.
90
	 * @config
91
	 */
92
	private static $timeout = 0;
93
94
	/**
95
	 * @config
96
	 * @var array
97
	 */
98
	private static $session_ips = array();
99
100
	/**
101
	 * @config
102
	 * @var string
103
	 */
104
	private static $cookie_domain;
105
106
	/**
107
	 * @config
108
	 * @var string
109
	 */
110
	private static $cookie_path;
111
112
	/**
113
	 * @config
114
	 * @var string
115
	 */
116
	private static $session_store_path;
117
118
	/**
119
	 * @config
120
	 * @var boolean
121
	 */
122
	private static $cookie_secure = false;
123
124
	/**
125
	 * Session data
126
	 */
127
	protected $data = array();
128
129
	protected $changedData = array();
130
131
	protected function userAgent() {
0 ignored issues
show
Coding Style introduced by
userAgent uses the super-global variable $_SERVER 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...
132
		if (isset($_SERVER['HTTP_USER_AGENT'])) {
133
			return $_SERVER['HTTP_USER_AGENT'];
134
		} else {
135
			return '';
136
		}
137
	}
138
139
	/**
140
	 * Start PHP session, then create a new Session object with the given start data.
141
	 *
142
	 * @param $data array|Session Can be an array of data (such as $_SESSION) or another Session object to clone.
143
	 */
144
	public function __construct($data) {
145
		if($data instanceof Session) $data = $data->inst_getAll();
146
147
		$this->data = $data;
148
		$this->expireIfInvalid();
0 ignored issues
show
Deprecated Code introduced by
The method Session::expireIfInvalid() has been deprecated with message: 3.0..4.0 Removed in 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
149
	}
150
151
	/**
152
	 * Cookie domain, for example 'www.php.net'.
153
	 *
154
	 * To make cookies visible on all subdomains then the domain
155
	 * must be prefixed with a dot like '.php.net'.
156
	 *
157
	 * @deprecated 4.0 Use the "Session.cookie_domain" config setting instead
158
	 *
159
	 * @param string $domain The domain to set
160
	 */
161
	public static function set_cookie_domain($domain) {
162
		Deprecation::notice('4.0', 'Use the "Session.cookie_domain" config setting instead');
163
		Config::inst()->update('Session', 'cookie_domain', $domain);
164
	}
165
166
	/**
167
	 * Get the cookie domain.
168
	 *
169
	 * @deprecated 4.0 Use the "Session.cookie_domain" config setting instead
170
	 *
171
	 * @return string
172
	 */
173
	public static function get_cookie_domain() {
174
		Deprecation::notice('4.0', 'Use the "Session.cookie_domain" config setting instead');
175
		return Config::inst()->get('Session', 'cookie_domain');
176
	}
177
178
	/**
179
	 * Path to set on the domain where the session cookie will work.
180
	 * Use a single slash ('/') for all paths on the domain.
181
	 *
182
	 * @deprecated 4.0 Use the "Session.cookie_path" config setting instead
183
	 *
184
	 * @param string $path The path to set
185
	 */
186
	public static function set_cookie_path($path) {
187
		Deprecation::notice('4.0', 'Use the "Session.cookie_path" config setting instead');
188
		Config::inst()->update('Session', 'cookie_path', $path);
189
	}
190
191
	/**
192
	 * Get the path on the domain where the session cookie will work.
193
	 *
194
	 * @deprecated 4.0 Use the "Session.cookie_path" config setting instead
195
	 *
196
	 * @return string
197
	 */
198
	public static function get_cookie_path() {
199
		Deprecation::notice('4.0', 'Use the "Session.cookie_path" config setting instead');
200
		if(Config::inst()->get('Session', 'cookie_path')) {
201
			return Config::inst()->get('Session', 'cookie_path');
202
		} else {
203
			return Director::baseURL();
204
		}
205
	}
206
207
	/**
208
	 * Secure cookie, tells the browser to only send it over SSL.
209
	 *
210
	 * @deprecated 4.0 Use the "Session.cookie_secure" config setting instead
211
	 *
212
	 * @param boolean $secure
213
	 */
214
	public static function set_cookie_secure($secure) {
215
		Deprecation::notice('4.0', 'Use the "Session.cookie_secure" config setting instead');
216
		Config::inst()->update('Session', 'cookie_secure', (bool)$secure);
217
	}
218
219
	/**
220
	 * Get if the cookie is secure
221
	 *
222
	 * @deprecated 4.0 Use the "Session.cookie_secure" config setting instead
223
	 *
224
	 * @return boolean
225
	 */
226
	public static function get_cookie_secure() {
227
		Deprecation::notice('4.0', 'Use the "Session.cookie_secure" config setting instead');
228
		return Config::inst()->get('Session', 'cookie_secure');
229
	}
230
231
	/**
232
	 * Set the session store path
233
	 *
234
	 * @deprecated 4.0 Use the "Session.session_store_path" config setting instead
235
	 *
236
	 * @param string $path Filesystem path to the session store
237
	 */
238
	public static function set_session_store_path($path) {
239
		Deprecation::notice('4.0', 'Use the "Session.session_store_path" config setting instead');
240
		Config::inst()->update('Session', 'session_store_path', $path);
241
	}
242
243
	/**
244
	 * Get the session store path
245
	 * @return string
246
	 * @deprecated since version 4.0
247
	 */
248
	public static function get_session_store_path() {
249
		Deprecation::notice('4.0', 'Use the "Session.session_store_path" config setting instead');
250
		return Config::inst()->get('Session', 'session_store_path');
251
	}
252
253
	/**
254
	 * Provide an <code>array</code> of rules specifing timeouts for IPv4 address ranges or
255
	 * individual IPv4 addresses. The key is an IP address or range and the value is the time
256
	 * until the session expires in seconds. For example:
257
	 *
258
	 * Session::set_timeout_ips(array(
259
	 * 		'127.0.0.1' => 36000
260
	 * ));
261
	 *
262
	 * Any user connecting from 127.0.0.1 (localhost) will have their session expired after 10 hours.
263
	 *
264
	 * Session::set_timeout is used to set the timeout value for any users whose address is not in the given IP range.
265
	 *
266
	 * @deprecated 4.0 Use the "Session.timeout_ips" config setting instead
267
	 *
268
	 * @param array $session_ips Array of IPv4 rules.
0 ignored issues
show
Bug introduced by
There is no parameter named $session_ips. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
269
	 */
270
	public static function set_timeout_ips($ips) {
271
		Deprecation::notice('4.0', 'Use the "Session.timeout_ips" config setting instead');
272
		Config::inst()->update('Session', 'timeout_ips', $ips);
273
	}
274
275
	/**
276
	 * Add a value to a specific key in the session array
277
	 */
278
	public static function add_to_array($name, $val) {
279
		return self::current_session()->inst_addToArray($name, $val);
280
	}
281
282
	/**
283
	 * Set a key/value pair in the session
284
	 *
285
	 * @param string $name Key
286
	 * @param string $val Value
287
	 */
288
	public static function set($name, $val) {
289
		return self::current_session()->inst_set($name, $val);
290
	}
291
292
	/**
293
	 * Return a specific value by session key
294
	 *
295
	 * @param string $name Key to lookup
296
	 */
297
	public static function get($name) {
298
		return self::current_session()->inst_get($name);
299
	}
300
301
	/**
302
	 * Return all the values in session
303
	 *
304
	 * @return Array
305
	 */
306
	public static function get_all() {
307
		return self::current_session()->inst_getAll();
308
	}
309
310
	/**
311
	 * Clear a given session key, value pair.
312
	 *
313
	 * @param string $name Key to lookup
314
	 */
315
	public static function clear($name) {
316
		return self::current_session()->inst_clear($name);
317
	}
318
319
	/**
320
	 * Clear all the values
321
	 *
322
	 * @return void
323
	 */
324
	public static function clear_all() {
325
		self::current_session()->inst_clearAll();
326
		self::$default_session = null;
327
	}
328
329
	/**
330
	 * Save all the values in our session to $_SESSION
331
	 */
332
	public static function save() {
333
		return self::current_session()->inst_save();
334
	}
335
336
	protected static $default_session = null;
337
338
	protected static function current_session() {
0 ignored issues
show
Coding Style introduced by
current_session 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...
339
		if(Controller::has_curr()) {
340
			return Controller::curr()->getSession();
341
		} else {
342
			if(!self::$default_session) {
343
				self::$default_session = Injector::inst()->create('Session', isset($_SESSION) ? $_SESSION : array());
344
			}
345
346
			return self::$default_session;
347
		}
348
	}
349
350
	public function inst_start($sid = null) {
0 ignored issues
show
Coding Style introduced by
inst_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...
351
		$path = Config::inst()->get('Session', 'cookie_path');
352
		if(!$path) $path = Director::baseURL();
353
		$domain = Config::inst()->get('Session', 'cookie_domain');
354
		// Director::baseURL can return absolute domain names - this extracts the relevant parts
355
		// for the session otherwise we can get broken session cookies
356
		if (Director::is_absolute_url($path)) {
357
			$urlParts = parse_url($path);
358
			$path = $urlParts['path'];
359
			if (!$domain) {
360
				$domain = $urlParts['host'];
361
			}
362
		}
363
		$secure = Director::is_https() && Config::inst()->get('Session', 'cookie_secure');
364
		$session_path = Config::inst()->get('Session', 'session_store_path');
365
		$timeout = Config::inst()->get('Session', 'timeout');
366
367
		if(!session_id() && !headers_sent()) {
368
			if($domain) {
369
				session_set_cookie_params($timeout, $path, $domain, $secure, true);
370
			} else {
371
				session_set_cookie_params($timeout, $path, null, $secure, true);
372
			}
373
374
			// Allow storing the session in a non standard location
375
			if($session_path) session_save_path($session_path);
376
377
			// If we want a secure cookie for HTTPS, use a seperate session name. This lets us have a
378
			// seperate (less secure) session for non-HTTPS requests
379
			if($secure) session_name('SECSESSID');
380
381
			if($sid) session_id($sid);
382
			session_start();
383
384
			$this->data = isset($_SESSION) ? $_SESSION : array();
385
		}
386
387
		// Ensure session is validated on start
388
		$this->expireIfInvalid();
0 ignored issues
show
Deprecated Code introduced by
The method Session::expireIfInvalid() has been deprecated with message: 3.0..4.0 Removed in 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
389
390
		// Modify the timeout behaviour so it's the *inactive* time before the session expires.
391
		// By default it's the total session lifetime
392
		if($timeout && !headers_sent()) {
393
			Cookie::set(session_name(), session_id(), $timeout/86400, $path, $domain ? $domain
394
				: null, $secure, true);
395
		}
396
	}
397
398
	public function inst_destroy($removeCookie = true) {
0 ignored issues
show
Coding Style introduced by
inst_destroy 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...
399
		if(session_id()) {
400
			if($removeCookie) {
401
				$path = Config::inst()->get('Session', 'cookie_path') ?: Director::baseURL();
402
				$domain = Config::inst()->get('Session', 'cookie_domain');
403
				$secure = Config::inst()->get('Session', 'cookie_secure');
404
405
				Cookie::force_expiry(session_name(), $path, $domain, $secure, true);
406
			}
407
408
			session_destroy();
409
410
			// Clean up the superglobal - session_destroy does not do it.
411
			// http://nz1.php.net/manual/en/function.session-destroy.php
412
			unset($_SESSION);
413
			$this->data = array();
414
		}
415
	}
416
417
	public function inst_set($name, $val) {
418
		// Quicker execution path for "."-free names
419
		if(strpos($name,'.') === false) {
420
			$this->data[$name] = $val;
421
			$this->changedData[$name] = $val;
422
423
		} else {
424
			$names = explode('.', $name);
425
426
			// We still want to do this even if we have strict path checking for legacy code
427
			$var = &$this->data;
428
			$diffVar = &$this->changedData;
429
430
			// Iterate twice over the names - once to see if the value needs to be changed,
431
			// and secondly to get the changed data value. This is done to solve a problem
432
			// where iterating over the diff var would create empty arrays, and the value
433
			// would then not be set, inadvertently clearing session values.
434
			foreach($names as $n) {
435
				$var = &$var[$n];
436
			}
437
438
			if($var !== $val) {
439
				foreach($names as $n) {
440
					$diffVar = &$diffVar[$n];
441
				}
442
443
				$var = $val;
444
				$diffVar = $val;
445
			}
446
		}
447
	}
448
449
	public function inst_addToArray($name, $val) {
450
		$names = explode('.', $name);
451
452
		// We still want to do this even if we have strict path checking for legacy code
453
		$var = &$this->data;
454
		$diffVar = &$this->changedData;
455
456
		foreach($names as $n) {
457
			$var = &$var[$n];
458
			$diffVar = &$diffVar[$n];
459
		}
460
461
		$var[] = $val;
462
		$diffVar[sizeof($var)-1] = $val;
463
	}
464
465
	public function inst_get($name) {
466
		// Quicker execution path for "."-free names
467
		if(strpos($name,'.') === false) {
468
			if(isset($this->data[$name])) return $this->data[$name];
469
470
		} else {
471
			$names = explode('.', $name);
472
473
			if(!isset($this->data)) {
474
				return null;
475
			}
476
477
			$var = $this->data;
478
479
			foreach($names as $n) {
480
				if(!isset($var[$n])) {
481
					return null;
482
				}
483
				$var = $var[$n];
484
			}
485
486
			return $var;
487
		}
488
	}
489
490
	public function inst_clear($name) {
491
		$names = explode('.', $name);
492
493
		// We still want to do this even if we have strict path checking for legacy code
494
		$var = &$this->data;
495
		$diffVar = &$this->changedData;
496
497
		foreach($names as $n) {
498
			// don't clear a record that doesn't exist
499
			if(!isset($var[$n])) return;
500
			$var = &$var[$n];
501
		}
502
503
		// only loop to find data within diffVar if var is proven to exist in the above loop
504
		foreach($names as $n) {
505
			$diffVar = &$diffVar[$n];
506
		}
507
508
		if($var !== null) {
509
			$var = null;
510
			$diffVar = null;
511
		}
512
	}
513
514
	public function inst_clearAll() {
515
		if($this->data && is_array($this->data)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
516
			foreach(array_keys($this->data) as $key) {
517
				$this->inst_clear($key);
518
			}
519
		}
520
	}
521
522
	public function inst_getAll() {
523
		return $this->data;
524
	}
525
526
	public function inst_finalize() {
527
		$this->inst_set('HTTP_USER_AGENT', $this->userAgent());
528
	}
529
530
	/**
531
	 * Save data to session
532
	 * Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
533
	 */
534
	public function inst_save() {
0 ignored issues
show
Coding Style introduced by
inst_save 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...
535
		if($this->changedData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->changedData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
536
			$this->inst_finalize();
537
538
			if(!isset($_SESSION)) {
539
				$this->inst_start();
540
			}
541
542
			$this->recursivelyApply($this->changedData, $_SESSION);
543
		}
544
	}
545
546
	/**
547
	 * Recursively apply the changes represented in $data to $dest.
548
	 * Used to update $_SESSION
549
	 */
550
	protected function recursivelyApply($data, &$dest) {
551
		foreach($data as $k => $v) {
552
			if(is_array($v)) {
553
				if(!isset($dest[$k]) || !is_array($dest[$k])) $dest[$k] = array();
554
				$this->recursivelyApply($v, $dest[$k]);
555
			} else {
556
				$dest[$k] = $v;
557
			}
558
		}
559
	}
560
561
	/**
562
	 * Return the changed data, for debugging purposes.
563
	 * @return array
564
	 */
565
	public function inst_changedData() {
566
		return $this->changedData;
567
	}
568
569
	/**
570
	* Sets the appropriate form message in session, with type. This will be shown once,
571
	* for the form specified.
572
	*
573
	* @param string $formname the form name you wish to use ( usually $form->FormName() )
574
	* @param string $message the message you wish to add to it
575
	* @param string $type the type of message
576
	*/
577
	public static function setFormMessage($formname, $message, $type){
578
		Session::set("FormInfo.$formname.formError.message", $message);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
579
		Session::set("FormInfo.$formname.formError.type", $type);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
580
	}
581
582
	/**
583
	 * Is there a session ID in the request?
584
	 * @return bool
585
	 */
586
	public static function request_contains_session_id() {
0 ignored issues
show
Coding Style introduced by
request_contains_session_id uses the super-global variable $_COOKIE 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...
Coding Style introduced by
request_contains_session_id uses the super-global variable $_REQUEST 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...
587
		$secure = Director::is_https() && Config::inst()->get('Session', 'cookie_secure');
588
		$name = $secure ? 'SECSESSID' : session_name();
589
		return isset($_COOKIE[$name]) || isset($_REQUEST[$name]);
590
	}
591
592
	/**
593
	 * Initialize session.
594
	 *
595
	 * @param string $sid Start the session with a specific ID
596
	 */
597
	public static function start($sid = null) {
598
		self::current_session()->inst_start($sid);
599
	}
600
601
	/**
602
	 * Destroy the active session.
603
	 *
604
	 * @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove
605
	 */
606
	public static function destroy($removeCookie = true) {
607
		self::current_session()->inst_destroy($removeCookie);
608
	}
609
610
	/**
611
	 * Set the timeout of a Session value
612
	 *
613
	 * @deprecated 4.0 Use the "Session.timeout" config setting instead
614
	 *
615
	 * @param int $timeout Time until a session expires in seconds. Defaults to expire when browser is closed.
616
	 */
617
	public static function set_timeout($timeout) {
618
		Deprecation::notice('4.0', 'Use the "Session.timeout" config setting instead');
619
		Config::inst()->update('Session', 'timeout', (int) $timeout);
620
	}
621
622
	/**
623
	 * @deprecated 4.0 Use the "Session.timeout" config setting instead
624
	 */
625
	public static function get_timeout() {
626
		Deprecation::notice('4.0', 'Use the "Session.timeout" config setting instead');
627
		return Config::inst()->get('Session', 'timeout');
628
	}
629
630
	/**
631
	 * Validate the user agent against the current data, resetting the
632
	 * current session if a mismatch is detected.
633
	 *
634
	 * @deprecated 3.0..4.0 Removed in 4.0
635
	 * @return bool If user agent has been set against this session, returns
636
	 * the valid state of this session as either true or false. If the agent
637
	 * isn't set it is assumed valid and returns true.
638
	 */
639
	private function expireIfInvalid() {
640
		// If not set, indeterminable; Assume true as safe default
641
		if (!isset($this->data['HTTP_USER_AGENT'])) {
642
			return true;
643
		}
644
645
		// Agents match, deterministically true
646
		if ($this->data['HTTP_USER_AGENT'] === $this->userAgent()) {
647
			return true;
648
		}
649
650
		// Funny business detected!
651
		$this->inst_clearAll();
652
		$this->inst_destroy();
653
		$this->inst_start();
654
		return false;
655
	}
656
}
657