1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Beware of the cookie monster *nom nom* |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Core |
9
|
|
|
* @package HTTP |
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\http; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Provides tools to work with cookies |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package HTTP |
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
abstract class Cookies |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Retrieves the data stored in a cookie |
33
|
|
|
* |
34
|
|
|
* @param string $name Name of the cookie to retrieve |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
**/ |
|
|
|
|
38
|
|
|
|
39
|
|
|
public static function get($name) |
40
|
|
|
{ |
41
|
|
|
$string = \csphere\core\http\Input::get('cookie', $name); |
42
|
|
|
|
43
|
|
|
return $string; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sends a cookie limited to http(s) |
48
|
|
|
* |
49
|
|
|
* @param string $name Name of the cookie to set |
50
|
|
|
* @param string $value The value of the cookie |
51
|
|
|
* @param integer $expire Cookie expires at this unix timestamp |
52
|
|
|
* @param boolean $secure Only allow this cookie for https connections |
53
|
|
|
* |
54
|
|
|
* @return boolean |
55
|
|
|
**/ |
|
|
|
|
56
|
|
|
|
57
|
|
|
public static function set($name, $value, $expire = 0, $secure = false) |
58
|
|
|
{ |
59
|
|
|
$result = false; |
60
|
|
|
|
61
|
|
|
// Get request settings |
62
|
|
|
$request = \csphere\core\http\Request::get(); |
63
|
|
|
|
64
|
|
|
$path = $request['dirname']; |
65
|
|
|
|
66
|
|
|
$domain = $request['dns']; |
67
|
|
|
|
68
|
|
|
// Check if secure is enabled and valid for usage |
69
|
|
|
if (empty($secure) || $request['protocol'] == 'https') { |
70
|
|
|
|
71
|
|
|
setcookie($name, $value, $expire, $path, $domain, $secure, true); |
72
|
|
|
|
73
|
|
|
$result = true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|