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