1: <?php
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: namespace Simpletools\Store;
40:
41: class Session
42: {
43: private static $default_return = 'exception';
44: private static $settings = array(
45: 'autostart_if_session_cookie_set' => false,
46: 'session_auto_start' => true
47: );
48: private static $_sessionStarted = false;
49: private static $_regenerateSessionIdEverySec = 600;
50:
51: public static function register($id,$data)
52: {
53: self::_autoStart();
54:
55: if(!isset($_SESSION[$id]))
56: {
57: $_SESSION[$id] = $data;
58: }
59: else
60: {
61: return false;
62: }
63: }
64:
65: public static function set($id,$data)
66: {
67: self::_autoStart();
68:
69: $_SESSION[$id] = $data;
70: }
71:
72:
73: public static function replace($id,$data)
74: {
75: return self::set($id,$data);
76: }
77:
78: public static function remove($id=false)
79: {
80: self::_autoStart();
81:
82: if($id)
83: {
84: unset($_SESSION[$id]);
85: }
86: else
87: {
88: self::destroy();
89: }
90: }
91:
92: public static function destroy($autoStart=true)
93: {
94: self::_autoStart();
95:
96: if(isset($_SESSION)) session_destroy();
97: if($autoStart) session_start();
98: }
99:
100: public static function get($id='')
101: {
102: self::_autoStart();
103:
104: if(!$id) return $_SESSION;
105:
106: if(isset($_SESSION[$id]))
107: {
108: return $_SESSION[$id];
109: }
110: else
111: {
112: if(self::$default_return == 'Exception')
113: {
114: throw new \Exception('Couldn\'t return id: '.$id.' because it hasn\'t been set',12345);
115: }
116: else
117: {
118: return self::$default_return;
119: }
120: }
121: }
122:
123: public static function is($key)
124: {
125: self::_autoStart();
126:
127: return isset($_SESSION[$key]);
128: }
129:
130:
131: public static function load($id='')
132: {
133: return self::get($id);
134: }
135:
136: public static function settings(array $options)
137: {
138: self::$settings['autostart_if_session_cookie_set'] = isset($options['autostartIfSessionCookieSet']) ? (boolean) $options['autostartIfSessionCookieSet'] : self::$settings['autostart_if_session_cookie_set'];
139:
140: self::$settings['session_auto_start'] = isset($options['session_auto_start']) ? (boolean) $options['sessionAutoStart'] : self::$settings['session_auto_start'];
141: self::$_regenerateSessionIdEverySec = isset($options['regenerateSessionIdEverySec']) ? (int) $options['regenerateSessionIdEverySec'] : self::$_regenerateSessionIdEverySec;
142:
143: 144: 145: 146: 147: 148:
149:
150: self::$default_return = (array_key_exists('defaultReturn',$options)) ? $options['defaultReturn'] : ((array_key_exists('default_return',$options)) ? $options['default_return'] : 'Exception');
151: }
152:
153: protected static function _autoStart()
154: {
155: if(self::$_sessionStarted)
156: {
157: return;
158: }
159:
160: if(!self::$_sessionStarted && session_id() == '')
161: {
162: if(self::$settings['session_auto_start']){@session_start();self::$_sessionStarted = true;}
163: elseif(self::$settings['autostart_if_session_cookie_set'] && isset($_COOKIE[session_name()]) && session_id() == ''){@session_start();self::$_sessionStarted = true;}
164: elseif(!self::$settings['autostart_if_session_cookie_set']){throw new \Exception('Please start session before using \Simpletools\Store\Session or set sessionAutoStart under ::settings() method.',11111);}
165: else return;
166: }
167: else
168: {
169: self::$_sessionStarted = true;
170: }
171:
172: $now = time();
173: if(!isset($_SESSION['__regenerateSessionIdEverySec']) OR $_SESSION['__regenerateSessionIdEverySec']<$now)
174: {
175: if(isset($_SESSION['__regenerateSessionIdEverySec']) && $_SESSION['__regenerateSessionIdEverySec']<$now)
176: {
177: @session_regenerate_id(true);
178: }
179:
180: $_SESSION['__regenerateSessionIdEverySec'] = time()+self::$_regenerateSessionIdEverySec;
181: }
182: }
183: }
184:
185: ?>