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: | function __session_init() |
26: | { |
27: | $defaultTypes = array('default', 'database'); |
28: | $options = array( |
29: | 'name' => 'LCSESSID', |
30: | 'table' => 'lc_sessions', |
31: | 'gc_maxlifetime' => 240, |
32: | 'cookie_lifetime' => 180 |
33: | ); |
34: | |
35: | $userSettings = _cfg('session'); |
36: | $type = (isset($userSettings['type']) && in_array($userSettings['type'], $defaultTypes)) |
37: | ? $userSettings['type'] |
38: | : 'default'; |
39: | |
40: | if ($userSettings && isset($userSettings['options']) && is_array($userSettings['options'])) { |
41: | $options = array_merge($options, $userSettings['options']); |
42: | } |
43: | |
44: | |
45: | if ($type === 'database' && !$options['table']) { |
46: | $type = 'default'; |
47: | } |
48: | |
49: | if ($type === 'database') { |
50: | define('LC_SESSION_TABLE', db_table($options['table'])); |
51: | } |
52: | |
53: | if (isset($options['table'])) { |
54: | |
55: | unset($options['table']); |
56: | } |
57: | |
58: | |
59: | $options['use_cookies'] = true; |
60: | $options['use_only_cookies'] = true; |
61: | $options['use_trans_sid'] = false; |
62: | $options['cookie_secure'] = true; |
63: | $options['cookie_httponly'] = true; |
64: | |
65: | foreach ($options as $key => $value) { |
66: | if ($key == 'gc_maxlifetime' || $key == 'cookie_lifetime') { |
67: | $options[$key] = $value * 60; |
68: | } |
69: | } |
70: | |
71: | if ($type === 'database') { |
72: | session_set_save_handler( |
73: | '__session_open', |
74: | '__session_close', |
75: | '__session_read', |
76: | '__session_write', |
77: | '__session_destroy', |
78: | '__session_clean' |
79: | ); |
80: | register_shutdown_function('session_write_close'); |
81: | } |
82: | |
83: | if (function_exists('session_beforeStart')) { |
84: | call_user_func('session_beforeStart'); |
85: | } |
86: | |
87: | session_start($options); |
88: | } |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | function __session_open() |
99: | { |
100: | return true; |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | function __session_close() |
112: | { |
113: | global $lc_session; |
114: | |
115: | $probability = mt_rand(1, 100); |
116: | if ($probability <= 10) { |
117: | $maxlifetime = $lc_session['options']['gc_maxlifetime']; |
118: | __session_clean($maxlifetime); |
119: | } |
120: | |
121: | return true; |
122: | } |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | function __session_read($sessionId) |
135: | { |
136: | if (!$sessionId) { |
137: | return false; |
138: | } |
139: | |
140: | $sql = 'SELECT session FROM '.LC_SESSION_TABLE.' WHERE sid = ":id"'; |
141: | $data = db_fetch($sql, array('id' => $sessionId)); |
142: | |
143: | return $data ?: false; |
144: | } |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | function __session_write($sessionId, $data) |
158: | { |
159: | if (!$sessionId) { |
160: | return false; |
161: | } |
162: | |
163: | $record = array( |
164: | 'id' => $sessionId, |
165: | 'host' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '', |
166: | 'timestamp' => time(), |
167: | 'session' => $data, |
168: | 'useragent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '' |
169: | ); |
170: | |
171: | $sql = 'REPLACE INTO '.LC_SESSION_TABLE.' (sid, host, timestamp, session, useragent) |
172: | VALUES (":id", ":host", ":timestamp", ":session", ":useragent")'; |
173: | |
174: | return db_query($sql, $record) ? true : false; |
175: | } |
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | function __session_destroy($sessionId) |
188: | { |
189: | return db_delete(LC_SESSION_TABLE, array('sid' => $sessionId)) ? true : false; |
190: | } |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | function __session_clean($maxlifetime) |
203: | { |
204: | $backTime = time() - $maxlifetime; |
205: | $sql = 'DELETE FROM '.LC_SESSION_TABLE.' WHERE timestamp < :backTime'; |
206: | |
207: | return db_query($sql, array('backTime' => $backTime)) ? true : false; |
208: | } |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | function session_set($name, $value = '', $serialize = false) |
220: | { |
221: | __dotNotationToArray($name, 'session', $value, $serialize); |
222: | } |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | function session_get($name, $unserialize = false) |
233: | { |
234: | $value = __dotNotationToArray($name, 'session'); |
235: | |
236: | return ($unserialize && is_string($value)) ? unserialize($value) : $value; |
237: | } |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | function session_delete($name) |
245: | { |
246: | $name = S_PREFIX . $name; |
247: | if (isset($_SESSION[$name])) { |
248: | unset($_SESSION[$name]); |
249: | return true; |
250: | } |
251: | |
252: | $keys = explode('.', $name); |
253: | $firstKey = array_shift($keys); |
254: | |
255: | if (count($keys)) { |
256: | if (!isset($_SESSION[$firstKey])) { |
257: | return false; |
258: | } |
259: | |
260: | $array = &$_SESSION[$firstKey]; |
261: | $parent = &$_SESSION[$firstKey]; |
262: | foreach ($keys as $k) { |
263: | if (isset($array[$k])) { |
264: | $parent = &$array; |
265: | $array = &$array[$k]; |
266: | } else { |
267: | return false; |
268: | } |
269: | } |
270: | $array = null; |
271: | unset($array); |
272: | unset($parent[$k]); |
273: | } |
274: | |
275: | return true; |
276: | } |
277: | |
278: | if (!function_exists('flash_set')) { |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | function flash_set($msg, $name = '', $class = 'success') |
290: | { |
291: | $msgHTML = _msg($msg, $class, 'html'); |
292: | $name = $name ?: 'general'; |
293: | $_SESSION[S_PREFIX . 'flashMessage'][$name] = $msgHTML; |
294: | } |
295: | } |
296: | |
297: | if (!function_exists('flash_get')) { |
298: | |
299: | |
300: | |
301: | |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | function flash_get($name = '', $html = true) |
308: | { |
309: | $name = $name ?: 'general'; |
310: | $message = ''; |
311: | if (isset($_SESSION[S_PREFIX.'flashMessage'][$name])) { |
312: | $message = $_SESSION[S_PREFIX.'flashMessage'][$name]; |
313: | unset($_SESSION[S_PREFIX.'flashMessage'][$name]); |
314: | } |
315: | |
316: | return $html ? $message : strip_tags($message); |
317: | } |
318: | } |
319: | |
320: | |
321: | |
322: | |
323: | |
324: | |
325: | |
326: | |
327: | |
328: | |
329: | |
330: | |
331: | |
332: | |
333: | |
334: | |
335: | |
336: | |
337: | |
338: | |
339: | function cookie_set($name, $value, $expiry = 0, $path = '/', $domain = '', $secure = false, $httpOnly = false) |
340: | { |
341: | if (!$domain) { |
342: | $domain = _cfg('siteDomain'); |
343: | } |
344: | |
345: | $name = preg_replace('/^('.S_PREFIX.')/', '', $name); |
346: | $name = S_PREFIX . $name; |
347: | if ($expiry > 0) { |
348: | $expiry = time() + $expiry; |
349: | } |
350: | |
351: | setcookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly); |
352: | } |
353: | |
354: | |
355: | |
356: | |
357: | |
358: | |
359: | |
360: | |
361: | |
362: | |
363: | function cookie_get($name = '') |
364: | { |
365: | if (empty($name)) { |
366: | return $_COOKIE; |
367: | } |
368: | |
369: | $name = preg_replace('/^('.S_PREFIX.')/', '', $name); |
370: | $name = S_PREFIX . $name; |
371: | |
372: | return (isset($_COOKIE[$name])) ? $_COOKIE[$name] : null; |
373: | } |
374: | |
375: | |
376: | |
377: | |
378: | |
379: | |
380: | |
381: | |
382: | |
383: | function cookie_delete($name, $path = '/') |
384: | { |
385: | if (empty($name)) { |
386: | return $_COOKIE; |
387: | } |
388: | |
389: | $name = preg_replace('/^('.S_PREFIX.')/', '', $name); |
390: | $name = S_PREFIX . $name; |
391: | |
392: | if (isset($_COOKIE[$name])) { |
393: | unset($_COOKIE[$name]); |
394: | setcookie($name, null, -1, $path); |
395: | return true; |
396: | } |
397: | |
398: | return !isset($_COOKIE[$name]); |
399: | } |
400: | |