1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class SessionSection extends Object implements IteratorAggregate, ArrayAccess
22: {
23:
24: private $session;
25:
26:
27: private $name;
28:
29:
30: private $data;
31:
32:
33: private $meta = FALSE;
34:
35:
36: public $warnOnUndefined = FALSE;
37:
38:
39:
40: 41: 42:
43: public function __construct(Session $session, $name)
44: {
45: if (!is_string($name)) {
46: throw new InvalidArgumentException("Session namespace must be a string, " . gettype($name) ." given.");
47: }
48:
49: $this->session = $session;
50: $this->name = $name;
51: }
52:
53:
54:
55: 56: 57:
58: private function start()
59: {
60: if ($this->meta === FALSE) {
61: $this->session->start();
62: $this->data = & $_SESSION['__NF']['DATA'][$this->name];
63: $this->meta = & $_SESSION['__NF']['META'][$this->name];
64: }
65: }
66:
67:
68:
69: 70: 71: 72:
73: public function getIterator()
74: {
75: $this->start();
76: if (isset($this->data)) {
77: return new ArrayIterator($this->data);
78: } else {
79: return new ArrayIterator;
80: }
81: }
82:
83:
84:
85: 86: 87: 88: 89: 90:
91: public function __set($name, $value)
92: {
93: $this->start();
94: $this->data[$name] = $value;
95: if (is_object($value)) {
96: $this->meta[$name]['V'] = ClassReflection::from($value)->getAnnotation('serializationVersion');
97: }
98: }
99:
100:
101:
102: 103: 104: 105: 106:
107: public function &__get($name)
108: {
109: $this->start();
110: if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
111: trigger_error("The variable '$name' does not exist in session section", E_USER_NOTICE);
112: }
113:
114: return $this->data[$name];
115: }
116:
117:
118:
119: 120: 121: 122: 123:
124: public function __isset($name)
125: {
126: if ($this->session->exists()) {
127: $this->start();
128: }
129: return isset($this->data[$name]);
130: }
131:
132:
133:
134: 135: 136: 137: 138:
139: public function __unset($name)
140: {
141: $this->start();
142: unset($this->data[$name], $this->meta[$name]);
143: }
144:
145:
146:
147: 148: 149: 150: 151: 152:
153: public function offsetSet($name, $value)
154: {
155: $this->__set($name, $value);
156: }
157:
158:
159:
160: 161: 162: 163: 164:
165: public function offsetGet($name)
166: {
167: return $this->__get($name);
168: }
169:
170:
171:
172: 173: 174: 175: 176:
177: public function offsetExists($name)
178: {
179: return $this->__isset($name);
180: }
181:
182:
183:
184: 185: 186: 187: 188:
189: public function offsetUnset($name)
190: {
191: $this->__unset($name);
192: }
193:
194:
195:
196: 197: 198: 199: 200: 201:
202: public function setExpiration($time, $variables = NULL)
203: {
204: $this->start();
205: if (empty($time)) {
206: $time = NULL;
207: $whenBrowserIsClosed = TRUE;
208: } else {
209: $time = DateTime53::from($time)->format('U');
210: $max = ini_get('session.gc_maxlifetime');
211: if ($time - time() > $max + 3) {
212: trigger_error("The expiration time is greater than the session expiration $max seconds", E_USER_NOTICE);
213: }
214: $whenBrowserIsClosed = FALSE;
215: }
216:
217: if ($variables === NULL) {
218: $this->meta['']['T'] = $time;
219: $this->meta['']['B'] = $whenBrowserIsClosed;
220:
221: } elseif (is_array($variables)) {
222: foreach ($variables as $variable) {
223: $this->meta[$variable]['T'] = $time;
224: $this->meta[$variable]['B'] = $whenBrowserIsClosed;
225: }
226:
227: } else {
228: $this->meta[$variables]['T'] = $time;
229: $this->meta[$variables]['B'] = $whenBrowserIsClosed;
230: }
231: return $this;
232: }
233:
234:
235:
236: 237: 238: 239: 240:
241: public function removeExpiration($variables = NULL)
242: {
243: $this->start();
244: if ($variables === NULL) {
245:
246: unset($this->meta['']['T'], $this->meta['']['B']);
247:
248: } elseif (is_array($variables)) {
249:
250: foreach ($variables as $variable) {
251: unset($this->meta[$variable]['T'], $this->meta[$variable]['B']);
252: }
253: } else {
254: unset($this->meta[$variables]['T'], $this->meta[$variable]['B']);
255: }
256: }
257:
258:
259:
260: 261: 262: 263:
264: public function remove()
265: {
266: $this->start();
267: $this->data = NULL;
268: $this->meta = NULL;
269: }
270:
271: }
272: