1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Session |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Session\Driver; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Session\Interfaces\DriverInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* CookieDriver |
22
|
|
|
* |
23
|
|
|
* Use cookie mechanism to set session id over to the client |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Session |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see ObjectAbstract |
28
|
|
|
* @see DriverInterface |
29
|
|
|
* @version 2.1.0 |
30
|
|
|
* @since 2.1.0 added |
31
|
|
|
*/ |
32
|
|
|
class CookieDriver extends ObjectAbstract implements DriverInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
* @access protected |
37
|
|
|
*/ |
38
|
|
|
protected $cookies = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* cookie settings |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
* @access protected |
45
|
|
|
*/ |
46
|
|
|
protected $domain = null; |
47
|
|
|
protected $path = '/'; |
48
|
|
|
protected $ttl = 0; |
49
|
|
|
protected $secure = false; |
50
|
|
|
protected $httponly = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param array $settings cookie settings |
56
|
|
|
* @access public |
57
|
|
|
*/ |
58
|
|
|
public function __construct(array $settings = []) |
59
|
|
|
{ |
60
|
|
|
$this->setProperties($settings); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Destructor |
65
|
|
|
* |
66
|
|
|
* Set cookies right before script finishes |
67
|
|
|
* |
68
|
|
|
* @access public |
69
|
|
|
*/ |
70
|
|
|
public function __destruct() |
71
|
|
|
{ |
72
|
|
|
if (php_sapi_name() === 'cli') { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
$this->syncCookies(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function get(/*# string */ $sessionName)/*# : string */ |
82
|
|
|
{ |
83
|
|
|
// get from cookie |
84
|
|
|
if (isset($_COOKIE[$sessionName])) { |
85
|
|
|
return $_COOKIE[$sessionName]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// returns empty |
89
|
|
|
return ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function set( |
96
|
|
|
/*# string */ $sessionName, |
97
|
|
|
/*# string */ $sessionId |
98
|
|
|
)/*# : bool */ { |
99
|
|
|
$this->cookies[$sessionName] = $sessionId; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
public function del(/*# string */ $sessionName)/*# : bool */ |
106
|
|
|
{ |
107
|
|
|
if (isset($_COOKIE[$sessionName])) { |
108
|
|
|
unset($_COOKIE[$sessionName]); |
109
|
|
|
} |
110
|
|
|
$this->cookies[$sessionName] = null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sync session cookies with the client |
115
|
|
|
* |
116
|
|
|
* @access protected |
117
|
|
|
*/ |
118
|
|
|
protected function syncCookies() |
119
|
|
|
{ |
120
|
|
|
foreach ($this->cookies as $name => $id) { |
121
|
|
|
if (null === $id) { |
122
|
|
|
setcookie($name, '', time() - 3600); |
123
|
|
|
} else { |
124
|
|
|
setcookie( |
125
|
|
|
$name, |
126
|
|
|
$id, |
127
|
|
|
$this->ttl ? (time() + $this->ttl) : 0, |
128
|
|
|
$this->path, |
129
|
|
|
$this->domain, |
130
|
|
|
$this->secure, |
131
|
|
|
$this->httponly |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|