1: <?php
2: /**
3: * Magic session class.
4: * This class is intended to extend the $_SESSION magic variable by providing an API hook
5: * to plug in other values.
6: *
7: * Primarily this is intended to provide a way of supplying "logged in user"
8: * details without touching the session (which can cause problems when
9: * accessed server side).
10: *
11: * If a value is present in the session then that value is returned, otherwise
12: * a plugin hook 'session:get', '$var' is called, where $var is the variable
13: * being requested.
14: *
15: * Setting values will store variables in the session in the normal way.
16: *
17: * LIMITATIONS: You can not access multidimensional arrays
18: *
19: * @package Elgg.Core
20: * @subpackage Sessions
21: */
22: class ElggSession implements ArrayAccess {
23: /** Local cache of trigger retrieved variables */
24: private static $__localcache;
25:
26: /**
27: * Test if property is set either as an attribute or metadata.
28: *
29: * @param string $key The name of the attribute or metadata.
30: *
31: * @return bool
32: */
33: function __isset($key) {
34: return $this->offsetExists($key);
35: }
36:
37: /**
38: * Set a value, go straight to session.
39: *
40: * @param string $key Name
41: * @param mixed $value Value
42: *
43: * @return void
44: */
45: function offsetSet($key, $value) {
46: $_SESSION[$key] = $value;
47: }
48:
49: /**
50: * Get a variable from either the session, or if its not in the session
51: * attempt to get it from an api call.
52: *
53: * @see ArrayAccess::offsetGet()
54: *
55: * @param mixed $key Name
56: *
57: * @return mixed
58: */
59: function offsetGet($key) {
60: if (!ElggSession::$__localcache) {
61: ElggSession::$__localcache = array();
62: }
63:
64: if (isset($_SESSION[$key])) {
65: return $_SESSION[$key];
66: }
67:
68: if (isset(ElggSession::$__localcache[$key])) {
69: return ElggSession::$__localcache[$key];
70: }
71:
72: $value = NULL;
73: $value = elgg_trigger_plugin_hook('session:get', $key, NULL, $value);
74:
75: ElggSession::$__localcache[$key] = $value;
76:
77: return ElggSession::$__localcache[$key];
78: }
79:
80: /**
81: * Unset a value from the cache and the session.
82: *
83: * @see ArrayAccess::offsetUnset()
84: *
85: * @param mixed $key Name
86: *
87: * @return void
88: */
89: function offsetUnset($key) {
90: unset(ElggSession::$__localcache[$key]);
91: unset($_SESSION[$key]);
92: }
93:
94: /**
95: * Return whether the value is set in either the session or the cache.
96: *
97: * @see ArrayAccess::offsetExists()
98: *
99: * @param int $offset Offset
100: *
101: * @return bool
102: */
103: function offsetExists($offset) {
104: if (isset(ElggSession::$__localcache[$offset])) {
105: return true;
106: }
107:
108: if (isset($_SESSION[$offset])) {
109: return true;
110: }
111:
112: if ($this->offsetGet($offset)) {
113: return true;
114: }
115:
116: return false;
117: }
118:
119:
120: /**
121: * Alias to ::offsetGet()
122: *
123: * @param string $key Name
124: *
125: * @return mixed
126: */
127: function get($key) {
128: return $this->offsetGet($key);
129: }
130:
131: /**
132: * Alias to ::offsetSet()
133: *
134: * @param string $key Name
135: * @param mixed $value Value
136: *
137: * @return void
138: */
139: function set($key, $value) {
140: $this->offsetSet($key, $value);
141: }
142:
143: /**
144: * Alias to offsetUnset()
145: *
146: * @param string $key Name
147: *
148: * @return void
149: */
150: function del($key) {
151: $this->offsetUnset($key);
152: }
153: }
154: