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