-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.class.php
159 lines (145 loc) · 3.73 KB
/
session.class.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
<?php
/**
* Class session
* kevinpark@webace.co.kr
*/
class session
{
private static $session_id; // 세션 아이디
/**
* @var Redis
*/
private static $cache;
/**
* 토큰생성
*
* @param int $length
*
* @return string token
*/
public static function gen_token($length=40)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.!@#$%^&()-_*=+';
$token = '';
for ($i = 0; $i < $length; $i++) {
$token .= $characters[rand(0, strlen($characters))];
}
return $token;
}
/**
* @throws Exception
*/
protected static function connect_cache()
{
if(self::$cache) return;
$redis_cfg = cfg::session();
if(!($redis_cfg['host'] && $redis_cfg['port']))
throw new Exception("Require cache config to use auth.", rester_response::code_config);
self::$cache = new Redis();
if(self::$cache->connect($redis_cfg['host'], $redis_cfg['port'], 1.0))
{
if ($redis_cfg['auth']) self::$cache->auth($redis_cfg['auth']);
}
else
{
throw new Exception("Can not access redis server.", rester_response::code_cache_server);
}
}
/**
* @param string $token
*
* @return bool|string
* @throws Exception
*/
public static function get_token($token)
{
self::connect_cache();
if(self::$session_id = self::$cache->get('token_'.$token))
{
if(json_decode(self::$session_id,true))
{
self::$session_id = json_decode(self::$session_id,true);
}
return self::$session_id;
}
}
/**
* @param mixed $data
*
* @return string
* @throws Exception
*/
public static function set_token($data)
{
if(!$data) throw new Exception("Require first parameter.", rester_response::code_parameter);
if(is_array($data)) $data = json_encode($data);
self::connect_cache();
$timeout = intval(cfg::Get('session','timeout'));
do {
$token = self::gen_token();
} while(self::$cache->get('token_'.$token));
self::$cache->set('token_'.$token,$data,$timeout);
self::$session_id = $data;
return $token;
}
/**
* @param string $key
* @param mixed $data
* @param int $timeout
*
* @throws Exception
*/
public static function set($key,$data,$timeout)
{
if(!$key) throw new Exception("Require first parameter.", rester_response::code_parameter);
if(!$data) throw new Exception("Require second parameter.", rester_response::code_parameter);
if(is_array($data)) $data = json_encode($data);
self::connect_cache();
self::$cache->set($key,$data,$timeout);
}
/**
* @param string $key
*
* @return bool|string
* @throws Exception
*/
public static function get($key)
{
self::connect_cache();
if($ret = self::$cache->get($key))
{
if(json_decode($ret,true))
{
$ret = json_decode($ret,true);
}
}
return $ret;
}
/**
* @param string $token
*
* @throws Exception
*/
public static function del_token($token)
{
self::connect_cache();
self::$cache->delete('token_'.$token);
}
/**
* @param string $key
*
* @throws Exception
*/
public static function del($key)
{
self::connect_cache();
self::$cache->delete($key);
}
/**
* @return string
*/
public static function id()
{
return self::$session_id;
}
}