forked from avdg/ppi-framework-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.php
208 lines (182 loc) · 5.35 KB
/
Session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @package Core
* @link http://www.ppiframework.com/docs/session.html
*/
class PPI_Session {
/**
* The config object, optionally passed.
*
* @var null|object
*/
protected $_config = null;
/**
* The session defaults
*
* @var array
*/
protected $_defaults = array(
'userAuthKey' => 'userAuthInfo',
'sessionNamespace' => '__MYAPP',
'frameworkSessionNamespace' => '__PPI'
);
/**
* This detemines if this class auto collects data from $_SESSION or it has been given its own mock data directly.
*
* @var bool
*/
protected $_isCollected = true;
/**
* The actual data we're manipulating this is only used for mock data.
*
* @var array
*/
protected $_data = array();
/**
* Constructor to optionally pass in session default options
*
*/
public function __construct(array $p_aOptions = array()) {
$this->_defaults = ($p_aOptions + $this->_defaults);
$this->_defaults['sessionNamespace'] = $this->_defaults['frameworkSessionNamespace'] . '_' . $this->_defaults['sessionNamespace'];
if(isset($p_aOptions['data'])) {
$this->_isCollected = false;
if (!isset($this->_array[$this->_defaults['sessionNamespace']])) {
$this->_array[$this->_defaults['sessionNamespace']] = array();
}
$this->_array[$this->_defaults['sessionNamespace']] = $p_aOptions['data'];
} else {
if(session_id() == '') {
session_name($this->_defaults['sessionNamespace']);
session_start();
}
if (!isset($_SESSION[$this->_defaults['sessionNamespace']])) {
$_SESSION[$this->_defaults['sessionNamespace']] = array();
}
}
}
public function defaults(array $options) {
$this->_defaults = ($options + $this->_defaults);
$this->_defaults['sessionNamespace'] = $this->_defaults['frameworkSessionNamespace'] . '_' . $this->_defaults['sessionNamespace'];
if(isset($p_aOptions['data'])) {
$this->_isCollected = false;
$this->_array[$this->_defaults['sessionNamespace']] = $p_aOptions['data'];
}
}
/**
* Check if a key exists
*
* @param string $p_sKey The key
* @return boolean
*/
public function exists($p_sKey) {
if($this->_isCollected) {
return array_key_exists($p_sKey, $_SESSION[$this->_defaults['sessionNamespace']]);
}
return array_key_exists($p_sKey, $this->_array[$this->_defaults['sessionNamespace']]);
}
/**
* Remove all set keys from the session
*
* @return void
*/
public function removeAll() {
if($this->_isCollected) {
foreach($_SESSION[$this->_defaults['sessionNamespace']]as $key => $val) {
unset($_SESSION[$this->_defaults['sessionNamespace']][$key]);
}
} else {
foreach($this->_array[$this->_defaults['sessionNamespace']] as $key => $val) {
unset($this->_array[$this->_defaults['sessionNamespace']][$key]);
}
}
}
/**
* Remove a specific key, or just data within that key.
* @example
* $session->remove('userInfo');
* $session->remove('userInfo', 'email');
*
* @param string $p_sKey The initial key set
* @param string $p_sName A key within the initial key set.
* @return void
*/
public function remove($p_sKey, $p_sName = null) {
if (null === $p_sName) {
if($this->_isCollected) {
unset($_SESSION[$this->_defaults['sessionNamespace']][$p_sKey]);
} else {
unset($this->_array[$this->_defaults['sessionNamespace']][$p_sKey]);
}
} else {
if($this->_isCollected) {
unset($_SESSION[$this->_defaults['sessionNamespace']][$p_sKey][$p_sName]);
} else {
unset($this->_array[$this->_defaults['sessionNamespace']][$p_sKey][$p_sName]);
}
}
}
/**
* Get information from the session by key
*
* @param string $p_sNamespace
* @param mixed $p_mDefault Optional. Default is null
* @return mixed
*/
public function get($key, $default = null) {
if($this->_isCollected) {
if(isset($_SESSION[$this->_defaults['sessionNamespace']][$key])) {
return $_SESSION[$this->_defaults['sessionNamespace']][$key];
}
} else {
if(isset($this->_array[$this->_defaults['sessionNamespace']][$key])) {
return $this->_array[$this->_defaults['sessionNamespace']][$key];
}
}
return $default;
}
/**
* Set data into the session by key
*
* @param string $p_sNamespace
* @param mixed $p_mData
* @return void
*/
public function set($p_sKey, $p_mData = true) {
if($this->_isCollected) {
$_SESSION[$this->_defaults['sessionNamespace']][$p_sKey] = $p_mData;
} else {
$this->_array[$this->_defaults['sessionNamespace']][$p_sKey] = $p_mData;
}
}
/**
* Set the authentication information for the current user
*
* @param mixed $aData The data to be set
* @return void
*/
public function setAuthData($mData) {
$this->set($this->_defaults['userAuthKey'], $mData);
}
/**
* Clear the set authentication information
*
* @return void
*/
public function clearAuthData() {
$this->set($this->_defaults['userAuthKey'], null);
}
/**
* Get the auth data, if it doesn't exist we return a blank array
*
* @param boolean $p_bUseArray Default is true. If true returns array, else object.
* @return array
*/
public function getAuthData($p_bUseArray = true) {
$aAuthData = $this->get($this->_defaults['userAuthKey'], false);
$aAuthData = !empty($aAuthData) ? $aAuthData : array();
return true === $p_bUseArray ? $aAuthData : (object)$aAuthData;
}
}