Move busui to seperate repository
[bus.git] / state.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php 
 
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2008 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
 
/**
 * Service User Class
 * 
 * @author      Peter Adams <peter@openwebanalytics.com>
 * @copyright   Copyright &copy; 2008 Peter Adams <peter@openwebanalytics.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
 * @category    owa
 * @package     owa
 * @version             $Revision$            
 * @since               owa 1.0.0
 */
 
 
class owa_state {
 
        var $stores = array();
        var $stores_meta = array();
        var $is_dirty;
        var $dirty_stores;
        var $default_store_type = 'cookie';
        var $stores_with_cdh = array('c','v','s');
        var $store_formats = array ('v' => 'assoc', 's' => 'assoc');
        var $initial_state = array();
        
        function __construct() {
        
        }
        
        function __destruct() {
        
                $this->persistState();
        }
                
        function persistState() {
        
                return false;
        
        }
        
        function get($store, $name = '') {
                owa_coreAPI::debug("Getting state - store: ".$store.' key: '.$name);
                
                if ( ! isset($this->stores[$store] ) ) {
                        $this->loadState($store);
                }
                
                if (array_key_exists($store, $this->stores)) {
                
                        if (!empty($name)) {
                                // check to ensure this is an array, could be a string.
                                if (is_array($this->stores[$store]) && array_key_exists($name, $this->stores[$store])) {        
                                                
                                        return $this->stores[$store][$name];
                                } else {
                                        return false;
                                }
                        } else {
 
                                return $this->stores[$store];
                        }
                } else {
                        
                        return false;
                }
        }
        
        function setState($store, $name = '', $value, $store_type = '', $is_perminent = false) {
        
                owa_coreAPI::debug(sprintf('populating state for store: %s, name: %s, value: %s, store type: %s, is_perm: %s', $store, $name, print_r($value, true), $store_type, $is_perminent));
                
                // first call to set for a store sets the meta
                if (!array_key_exists($store, $this->stores)) {
                
                        if (empty($store_type)) {
                                $store_type = $this->default_store_type;
                        }
                        
                        $this->stores_meta[$store]['type'] = $store_type;
                        
                        if ($is_perminent === true) {
                                $this->stores_meta[$store]['is_perminent'] = true;
                        }
                        
                }
                
                // set values
                if (empty($name)) {
                        $this->stores[$store] = $value;
                        //owa_coreAPI::debug(print_r($this->stores, true));
                } else {
                        //just in case the store was set first as a string instead of as an array.
                        if ( array_key_exists($store, $this->stores)) {
                        
                                if ( ! is_array( $this->stores[$store] ) ) {
                                        $new_store = array();
                                        // check to see if we need ot ad a cdh
                                        if ( $this->isCdhRequired($store) ) {
                                                $new_store['cdh'] = $this->getCookieDomainHash();
                                        }
                                        
                                        $new_store[$name] = $value;
                                        $this->stores[$store] = $new_store;
                                
                                } else {
                                        $this->stores[$store][$name] = $value;  
                                }
                        // if the store does not exist then     maybe add a cdh and the value
                        } else {
                        
                                if ( $this->isCdhRequired($store) ) {
                                        $this->stores[$store]['cdh'] = $this->getCookieDomainHash();
                                }
                                
                                $this->stores[$store][$name] = $value;
                        }
                        
                }
                
                $this->dirty_stores[] = $store;
                //owa_coreAPI::debug(print_r($this->stores, true));
        }
        
        function isCdhRequired($store_name) {
                
                return in_array( $store_name, $this->stores_with_cdh );
        }
 
        function set($store, $name = '', $value, $store_type = '', $is_perminent = false) {
        
                if ( ! isset($this->stores[$store] ) ) {
                        $this->loadState($store);
                }
                
                $this->setState($store, $name, $value, $store_type, $is_perminent);
                
                // persist immeadiately if the store type is cookie
                if ($this->stores_meta[$store]['type'] === 'cookie') {
                        
                        $time = 0;
                        
                        // needed? i dont think so.
                        if (isset($this->stores_meta[$store]['is_perminent']) && $this->stores_meta[$store]['is_perminent'] === true) {
                                $time = $this->getPermExpiration();
                        } elseif (isset($this->stores_meta[$store]['is_perminent']) && $this->stores_meta[$store]['is_perminent'] > 0) {
                                $time = $this->stores_meta[$store]['is_perminent'] * 3600 * 24;
                        }
                        
                        if ($is_perminent === true) {
                                $time = $this->getPermExpiration();
                        }
                        
                        // transform state array into a string using proper format
                        if ( is_array( $this->stores[$store] ) ) {
                                
                                // check for old style assoc format
                                if (isset($this->store_formats[$store]) && $this->store_formats[$store] === 'assoc') {
                                        $cookie_value = owa_lib::implode_assoc('=>', '|||', $this->stores[$store] );
                                } else {
                                        $cookie_value = json_encode( $this->stores[$store] );
                                }
                        }
                        
                        
                        owa_coreAPI::createCookie($store, $this->stores[$store], $time, "/", owa_coreAPI::getSetting('base', 'cookie_domain'));
                }       
        }
        
        function setInitialState($store, $value, $store_type) {
                
                if ($value) {
                        $this->initial_state[$store] = $value;
                }
        }
        
        function loadState($store, $name = '', $value = '', $store_type = 'cookie') {
        
                if ( ! $value && isset( $this->initial_state[$store] ) ) {
                        $value = $this->initial_state[$store];
                } else {
                        return;
                }
        
                // check format of value
                if (strpos($value, "|||")) {
                        $value = owa_lib::assocFromString($value);
                } else if (strpos($value, '{')) {
                        $value = json_decode($value);
                } else {
                        $value = $value;
                }
                
                if ( in_array( $store, $this->stores_with_cdh ) ) {
                        
                        if ( is_array( $value ) && isset( $value['cdh'] ) ) {
                                
                                $runtime_cdh = $this->getCookieDomainHash();
                                $cdh_from_state = $value['cdh'];
                                
                                // return as the cdh's do not match
                                if ( $cdh_from_state != $runtime_cdh ) {
                                        // cookie domains do not match so we need to delete the cookie in the offending domain
                                        // which is always likely to be a sub.domain.com and thus HTTP_HOST.
                                        // if ccokie is not deleted then new cookies set on .domain.com will never be seen by PHP
                                        // as only the sub domain cookies are available.
                                        owa_coreAPI::debug("Not loading state store: $store. Domain hashes do not match - runtime: $runtime_cdh, cookie: $cdh_from_state");
                                        owa_coreAPI::debug("deleting cookie: owa_$store");
                                        owa_coreAPI::deleteCookie($store,'/', $_SERVER['HTTP_HOST']);
                                        unset($this->initial_state[$store]);
                                        return;
                                }
                        } else {
                                
                                owa_coreAPI::debug("Not loading state store: $store. No domain hash found.");
                                return;
                                
                        }
                }
        
                return $this->setState($store, $name, $value, $store_type);
                
        }
                
        function clear($store) {
        
                if ( ! isset($this->stores[$store] ) ) {
                        $this->loadState($store);
                }
                
                if (array_key_exists($store, $this->stores)) {
                        
                        unset($this->stores[$store]);
                        
                        if ($this->stores_meta[$store]['type'] === 'cookie') {
                        
                                return owa_coreAPI::deleteCookie($store);       
                        }       
                }               
        }
        
        function getPermExpiration() {
        
                $time = time()+3600*24*365*15;
                return $time;
        }
        
        function addStores($array) {
                
                $this->stores = array_merge($this->stores,<