1: <?php
2: /*
3: * Simpletools Framework.
4: * Copyright (c) 2009, Marcin Rosinski. (http://www.getsimpletools.com/)
5: * All rights reserved.
6: *
7: * LICENCE
8: *
9: * Redistribution and use in source and binary forms, with or without modification,
10: * are permitted provided that the following conditions are met:
11: *
12: * - Redistributions of source code must retain the above copyright notice,
13: * this list of conditions and the following disclaimer.
14: *
15: * - Redistributions in binary form must reproduce the above copyright notice,
16: * this list of conditions and the following disclaimer in the documentation and/or other
17: * materials provided with the distribution.
18: *
19: * - Neither the name of the Simpletools nor the names of its contributors may be used to
20: * endorse or promote products derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: *
31: * @framework Simpletools
32: * @packages Session
33: * @description Flash Message Storage
34: * @copyright Copyright (c) 2011 Marcin Rosinski. (http://www.getsimpletools.com/)
35: * @license BSD
36: *
37: */
38:
39: namespace Simpletools\Store;
40:
41: /**
42: * Flash Message Storage
43: */
44: class Flash
45: {
46: /**
47: * Simpletools\Store\Flash Session key internal namespace to prevent direct session overwrite
48: */
49: protected static $_sessionKey = 'ST_FLASH_32896hdiuqwgd6723gdy23g.';
50:
51: /**
52: * Cleaning process controller
53: */
54: protected static $_cleanedPrev = 0;
55:
56:
57: /**
58: * Setting clearing message, when set to true clears the message straight after 1st get, useful when flash message is accessed within same request that it was set, defaults to false
59: */
60: protected static $_readOnce = false;
61:
62: /**
63: * Setting what to return if get can't find a message with a key, ST__EXCEPTION will throw an exception;
64: */
65: protected static $_defaultReturn = 'ST__EXCEPTION';
66:
67: /**
68: * Global settings setter
69: *
70: * @param array $settings Settings to be used as default
71: * @return null
72: */
73: public static function settings($settings=array())
74: {
75: self::$_readOnce = isset($settings['readOnce']) ? (boolean) $settings['readOnce'] : false;
76: self::$_defaultReturn = (array_key_exists('defaultReturn',$settings)) ? $settings['defaultReturn'] : 'ST__EXCEPTION';
77: }
78:
79: /**
80: * Flash old messages cleaner
81: *
82: * @return boolean
83: */
84: protected static function _cleanPrevious()
85: {
86: if(self::$_cleanedPrev) return true;
87: self::$_cleanedPrev = 1;
88:
89: foreach($_SESSION as $key => $value)
90: {
91: if(strpos($key,self::$_sessionKey)===0)
92: {
93: if($_SESSION[$key][0]<1)
94: {
95: unset($_SESSION[$key]);
96: }
97: else
98: {
99: $_SESSION[$key][0] = 0;
100: }
101: }
102: }
103: }
104:
105: /**
106: * Existing messages reflusher allowing them to last till next session
107: *
108: * @param mixed $keys Settings to be used as default
109: * @return null
110: */
111: public static function reflush($keys=null)
112: {
113: self::_cleanPrevious();
114:
115: if($keys)
116: {
117: if(!is_array($keys))
118: {
119: $keys = array($keys);
120: }
121:
122: $keys = array_flip($keys);
123: }
124:
125: foreach($_SESSION as $key => $value)
126: {
127: if(strpos($key,self::$_sessionKey)===0)
128: {
129: if($keys)
130: {
131: $k = str_replace(self::$_sessionKey,'',$key);
132: if(isset($keys[$k]))
133: {
134: $_SESSION[$key][0] = 1;
135: }
136: }
137: else
138: {
139: $_SESSION[$key][0] = 1;
140: }
141: }
142: }
143: }
144:
145: /**
146: * Message setter
147: *
148: * @param string $key Message key
149: * @param mixed $value Message value
150: * @param array $settings Message additional settings e.g. readOnce
151: * @return null
152: */
153: public static function set($key,$value,$settings=array())
154: {
155: self::_cleanPrevious();
156:
157: $settings['readOnce'] = isset($settings['readOnce']) ? (boolean) $settings['readOnce'] : self::$_readOnce;
158:
159: $_SESSION[self::$_sessionKey.$key] = array(1,$value,$settings);
160: }
161:
162: /**
163: * Message getter
164: *
165: * @param string $key Message key
166: * @return mixed Returns message value
167: */
168: public static function get($key)
169: {
170: self::_cleanPrevious();
171:
172: if(!isset($_SESSION[self::$_sessionKey.$key]) && self::$_defaultReturn=="ST__EXCEPTION")
173: {
174: throw new \Exception("Message with key: ".$key." doesn't exist",404);
175: }
176:
177: $message = isset($_SESSION[self::$_sessionKey.$key]) ? $_SESSION[self::$_sessionKey.$key] : null;
178: if(isset($message[2]['readOnce']) && $message[2]['readOnce'])
179: {
180: self::remove($key);
181: }
182:
183: return $message ? $message[1] : self::$_defaultReturn;
184: }
185:
186: /**
187: * Message existence checker
188: *
189: * @param string $key Message key
190: * @return boolean
191: */
192: public static function is($key)
193: {
194: self::_cleanPrevious();
195:
196: return isset($_SESSION[self::$_sessionKey.$key]);
197: }
198:
199: /**
200: * Message unsetter
201: *
202: * @param string $key Message key
203: * @return boolean Returns true on successful unset, false otherwise
204: */
205: public static function remove($key)
206: {
207: self::_cleanPrevious();
208:
209: if(self::is($key))
210: {
211: unset($_SESSION[self::$_sessionKey.$key]);
212: return true;
213: }
214: else
215: {
216: return false;
217: }
218: }
219: }
220:
221: ?>