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