@@ -4,11 +4,11 @@ |
||
4 | 4 | */ |
5 | 5 | interface iCache |
6 | 6 | { |
7 | - //获取缓存 |
|
8 | - public function getCache($key); |
|
7 | + //获取缓存 |
|
8 | + public function getCache($key); |
|
9 | 9 | |
10 | - //更新缓存 |
|
11 | - public function setCache($key,$var); |
|
10 | + //更新缓存 |
|
11 | + public function setCache($key,$var); |
|
12 | 12 | |
13 | 13 | } |
14 | 14 | ?> |
15 | 15 | \ No newline at end of file |
@@ -8,7 +8,7 @@ |
||
8 | 8 | public function getCache($key); |
9 | 9 | |
10 | 10 | //更新缓存 |
11 | - public function setCache($key,$var); |
|
11 | + public function setCache($key, $var); |
|
12 | 12 | |
13 | 13 | } |
14 | 14 | ?> |
15 | 15 | \ No newline at end of file |
@@ -4,35 +4,35 @@ |
||
4 | 4 | */ |
5 | 5 | class YacCache implements iCache |
6 | 6 | { |
7 | - public $isEnable = true; |
|
7 | + public $isEnable = true; |
|
8 | 8 | |
9 | - function __construct() |
|
10 | - { |
|
11 | - if (!extension_loaded("yac")) |
|
12 | - $this->isEnable = false; |
|
13 | - } |
|
9 | + function __construct() |
|
10 | + { |
|
11 | + if (!extension_loaded("yac")) |
|
12 | + $this->isEnable = false; |
|
13 | + } |
|
14 | 14 | |
15 | - public function getCache($key) |
|
16 | - { |
|
17 | - $key = md5($key); |
|
18 | - if($this->isEnable){ |
|
19 | - $yac = new Yac(); |
|
20 | - return $yac->get($key); |
|
21 | - }else{ |
|
22 | - echo "yac is not enable ,skip getCache"; |
|
23 | - } |
|
24 | - } |
|
15 | + public function getCache($key) |
|
16 | + { |
|
17 | + $key = md5($key); |
|
18 | + if($this->isEnable){ |
|
19 | + $yac = new Yac(); |
|
20 | + return $yac->get($key); |
|
21 | + }else{ |
|
22 | + echo "yac is not enable ,skip getCache"; |
|
23 | + } |
|
24 | + } |
|
25 | 25 | |
26 | - public function setCache($key,$var) |
|
27 | - { |
|
28 | - $key = md5($key); |
|
29 | - if($this->isEnable){ |
|
30 | - $yac = new Yac(); |
|
31 | - $yac->set($key, $var); |
|
32 | - }else{ |
|
33 | - echo "yac is not enable ,skip setCache"; |
|
34 | - } |
|
35 | - } |
|
26 | + public function setCache($key,$var) |
|
27 | + { |
|
28 | + $key = md5($key); |
|
29 | + if($this->isEnable){ |
|
30 | + $yac = new Yac(); |
|
31 | + $yac->set($key, $var); |
|
32 | + }else{ |
|
33 | + echo "yac is not enable ,skip setCache"; |
|
34 | + } |
|
35 | + } |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | ?> |
39 | 39 | \ No newline at end of file |
@@ -15,21 +15,21 @@ |
||
15 | 15 | public function getCache($key) |
16 | 16 | { |
17 | 17 | $key = md5($key); |
18 | - if($this->isEnable){ |
|
18 | + if ($this->isEnable) { |
|
19 | 19 | $yac = new Yac(); |
20 | 20 | return $yac->get($key); |
21 | - }else{ |
|
21 | + }else { |
|
22 | 22 | echo "yac is not enable ,skip getCache"; |
23 | 23 | } |
24 | 24 | } |
25 | 25 | |
26 | - public function setCache($key,$var) |
|
26 | + public function setCache($key, $var) |
|
27 | 27 | { |
28 | 28 | $key = md5($key); |
29 | - if($this->isEnable){ |
|
29 | + if ($this->isEnable) { |
|
30 | 30 | $yac = new Yac(); |
31 | 31 | $yac->set($key, $var); |
32 | - }else{ |
|
32 | + }else { |
|
33 | 33 | echo "yac is not enable ,skip setCache"; |
34 | 34 | } |
35 | 35 | } |
@@ -8,8 +8,9 @@ discard block |
||
8 | 8 | |
9 | 9 | function __construct() |
10 | 10 | { |
11 | - if (!extension_loaded("yac")) |
|
12 | - $this->isEnable = false; |
|
11 | + if (!extension_loaded("yac")) { |
|
12 | + $this->isEnable = false; |
|
13 | + } |
|
13 | 14 | } |
14 | 15 | |
15 | 16 | public function getCache($key) |
@@ -18,7 +19,7 @@ discard block |
||
18 | 19 | if($this->isEnable){ |
19 | 20 | $yac = new Yac(); |
20 | 21 | return $yac->get($key); |
21 | - }else{ |
|
22 | + } else{ |
|
22 | 23 | echo "yac is not enable ,skip getCache"; |
23 | 24 | } |
24 | 25 | } |
@@ -29,7 +30,7 @@ discard block |
||
29 | 30 | if($this->isEnable){ |
30 | 31 | $yac = new Yac(); |
31 | 32 | $yac->set($key, $var); |
32 | - }else{ |
|
33 | + } else{ |
|
33 | 34 | echo "yac is not enable ,skip setCache"; |
34 | 35 | } |
35 | 36 | } |
@@ -20,12 +20,12 @@ discard block |
||
20 | 20 | |
21 | 21 | public static function hmac_md5($input, $key) { |
22 | 22 | $key = base64_decode($key); |
23 | - return hash_hmac('md5', $input, $key,true); |
|
23 | + return hash_hmac('md5', $input, $key, true); |
|
24 | 24 | } |
25 | 25 | |
26 | - private static function pkcs5_pad ($text, $blocksize) { |
|
27 | - $pad = $blocksize - (strlen($text) % $blocksize); |
|
28 | - return $text . str_repeat(chr($pad), $pad); |
|
26 | + private static function pkcs5_pad($text, $blocksize) { |
|
27 | + $pad = $blocksize-(strlen($text)%$blocksize); |
|
28 | + return $text.str_repeat(chr($pad), $pad); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | public static function decrypt($sStr, $key) { |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | |
43 | 43 | mcrypt_generic_deinit($module); |
44 | 44 | mcrypt_module_close($module); |
45 | - if(!$decrypted){ |
|
45 | + if (!$decrypted) { |
|
46 | 46 | throw new Exception("Decrypt Error,Please Check SecretKey"); |
47 | 47 | } |
48 | 48 | return $decrypted; |
@@ -1,589 +1,589 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - include './SecretContext.php'; |
|
4 | - include './MagicCrypt.php'; |
|
5 | - |
|
6 | - class SecurityUtil |
|
7 | - { |
|
8 | - |
|
9 | - private $BASE64_ARRAY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
|
10 | - private $SEPARATOR_CHAR_MAP; |
|
11 | - |
|
12 | - function __construct() |
|
13 | - { |
|
14 | - if(!defined("PHONE_SEPARATOR_CHAR")) |
|
15 | - { |
|
16 | - define('PHONE_SEPARATOR_CHAR','$'); |
|
17 | - } |
|
18 | - if(!defined("NICK_SEPARATOR_CHAR")) |
|
19 | - { |
|
20 | - define('NICK_SEPARATOR_CHAR','~'); |
|
21 | - } |
|
22 | - if(!defined("NORMAL_SEPARATOR_CHAR")) |
|
23 | - { |
|
24 | - define('NORMAL_SEPARATOR_CHAR',chr(1)); |
|
25 | - } |
|
26 | - |
|
27 | - $this->SEPARATOR_CHAR_MAP['nick'] = NICK_SEPARATOR_CHAR; |
|
28 | - $this->SEPARATOR_CHAR_MAP['simple'] = NICK_SEPARATOR_CHAR; |
|
29 | - $this->SEPARATOR_CHAR_MAP['receiver_name'] = NICK_SEPARATOR_CHAR; |
|
30 | - $this->SEPARATOR_CHAR_MAP['search'] = NICK_SEPARATOR_CHAR; |
|
31 | - $this->SEPARATOR_CHAR_MAP['normal'] = NORMAL_SEPARATOR_CHAR; |
|
32 | - $this->SEPARATOR_CHAR_MAP['phone'] = PHONE_SEPARATOR_CHAR; |
|
33 | - |
|
34 | - } |
|
35 | - |
|
36 | - /* |
|
3 | + include './SecretContext.php'; |
|
4 | + include './MagicCrypt.php'; |
|
5 | + |
|
6 | + class SecurityUtil |
|
7 | + { |
|
8 | + |
|
9 | + private $BASE64_ARRAY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
|
10 | + private $SEPARATOR_CHAR_MAP; |
|
11 | + |
|
12 | + function __construct() |
|
13 | + { |
|
14 | + if(!defined("PHONE_SEPARATOR_CHAR")) |
|
15 | + { |
|
16 | + define('PHONE_SEPARATOR_CHAR','$'); |
|
17 | + } |
|
18 | + if(!defined("NICK_SEPARATOR_CHAR")) |
|
19 | + { |
|
20 | + define('NICK_SEPARATOR_CHAR','~'); |
|
21 | + } |
|
22 | + if(!defined("NORMAL_SEPARATOR_CHAR")) |
|
23 | + { |
|
24 | + define('NORMAL_SEPARATOR_CHAR',chr(1)); |
|
25 | + } |
|
26 | + |
|
27 | + $this->SEPARATOR_CHAR_MAP['nick'] = NICK_SEPARATOR_CHAR; |
|
28 | + $this->SEPARATOR_CHAR_MAP['simple'] = NICK_SEPARATOR_CHAR; |
|
29 | + $this->SEPARATOR_CHAR_MAP['receiver_name'] = NICK_SEPARATOR_CHAR; |
|
30 | + $this->SEPARATOR_CHAR_MAP['search'] = NICK_SEPARATOR_CHAR; |
|
31 | + $this->SEPARATOR_CHAR_MAP['normal'] = NORMAL_SEPARATOR_CHAR; |
|
32 | + $this->SEPARATOR_CHAR_MAP['phone'] = PHONE_SEPARATOR_CHAR; |
|
33 | + |
|
34 | + } |
|
35 | + |
|
36 | + /* |
|
37 | 37 | * 判断是否是base64格式的数据 |
38 | 38 | */ |
39 | - function isBase64Str($str) |
|
40 | - { |
|
41 | - $strLen = strlen($str); |
|
42 | - for($i = 0; $i < $strLen ; $i++) |
|
43 | - { |
|
44 | - if(!$this->isBase64Char($str[$i])) |
|
45 | - { |
|
46 | - return false; |
|
47 | - } |
|
48 | - } |
|
49 | - return true; |
|
50 | - } |
|
51 | - |
|
52 | - /* |
|
39 | + function isBase64Str($str) |
|
40 | + { |
|
41 | + $strLen = strlen($str); |
|
42 | + for($i = 0; $i < $strLen ; $i++) |
|
43 | + { |
|
44 | + if(!$this->isBase64Char($str[$i])) |
|
45 | + { |
|
46 | + return false; |
|
47 | + } |
|
48 | + } |
|
49 | + return true; |
|
50 | + } |
|
51 | + |
|
52 | + /* |
|
53 | 53 | * 判断是否是base64格式的字符 |
54 | 54 | */ |
55 | - function isBase64Char($char) |
|
56 | - { |
|
57 | - return strpos($this->BASE64_ARRAY,$char) !== false; |
|
58 | - } |
|
55 | + function isBase64Char($char) |
|
56 | + { |
|
57 | + return strpos($this->BASE64_ARRAY,$char) !== false; |
|
58 | + } |
|
59 | 59 | |
60 | - /* |
|
60 | + /* |
|
61 | 61 | * 使用sep字符进行trim |
62 | 62 | */ |
63 | - function trimBySep($str,$sep) |
|
64 | - { |
|
65 | - $start = 0; |
|
66 | - $end = strlen($str); |
|
67 | - for($i = 0; $i < $end; $i++) |
|
68 | - { |
|
69 | - if($str[$i] == $sep) |
|
70 | - { |
|
71 | - $start = $i + 1; |
|
72 | - } |
|
73 | - else |
|
74 | - { |
|
75 | - break; |
|
76 | - } |
|
77 | - } |
|
78 | - for($i = $end -1 ; $i >= 0; $i--) |
|
79 | - { |
|
80 | - if($str[$i] == $sep) |
|
81 | - { |
|
82 | - $end = $i - 1; |
|
83 | - } |
|
84 | - else |
|
85 | - { |
|
86 | - break; |
|
87 | - } |
|
88 | - } |
|
89 | - return substr($str,$start,$end); |
|
90 | - } |
|
91 | - |
|
92 | - function checkEncryptData($dataArray) |
|
93 | - { |
|
94 | - if(count($dataArray) == 2){ |
|
95 | - return $this->isBase64Str($dataArray[0]); |
|
96 | - }else{ |
|
97 | - return $this->isBase64Str($dataArray[0]) && $this->isBase64Str($dataArray[1]); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - /* |
|
63 | + function trimBySep($str,$sep) |
|
64 | + { |
|
65 | + $start = 0; |
|
66 | + $end = strlen($str); |
|
67 | + for($i = 0; $i < $end; $i++) |
|
68 | + { |
|
69 | + if($str[$i] == $sep) |
|
70 | + { |
|
71 | + $start = $i + 1; |
|
72 | + } |
|
73 | + else |
|
74 | + { |
|
75 | + break; |
|
76 | + } |
|
77 | + } |
|
78 | + for($i = $end -1 ; $i >= 0; $i--) |
|
79 | + { |
|
80 | + if($str[$i] == $sep) |
|
81 | + { |
|
82 | + $end = $i - 1; |
|
83 | + } |
|
84 | + else |
|
85 | + { |
|
86 | + break; |
|
87 | + } |
|
88 | + } |
|
89 | + return substr($str,$start,$end); |
|
90 | + } |
|
91 | + |
|
92 | + function checkEncryptData($dataArray) |
|
93 | + { |
|
94 | + if(count($dataArray) == 2){ |
|
95 | + return $this->isBase64Str($dataArray[0]); |
|
96 | + }else{ |
|
97 | + return $this->isBase64Str($dataArray[0]) && $this->isBase64Str($dataArray[1]); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + /* |
|
102 | 102 | * 判断是否是加密数据 |
103 | 103 | */ |
104 | - function isEncryptDataArray($array,$type) |
|
105 | - { |
|
106 | - foreach ($array as $value) { |
|
107 | - if(!$this->isEncryptData($value,$type)){ |
|
108 | - return false; |
|
109 | - } |
|
110 | - } |
|
111 | - return true; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * 判断是否是已加密的数据,数据必须是同一个类型 |
|
116 | - */ |
|
117 | - function isPartEncryptData($array,$type) |
|
118 | - { |
|
119 | - $result = false; |
|
120 | - foreach ($array as $value) { |
|
121 | - if($this->isEncryptData($value,$type)){ |
|
122 | - $result = true; |
|
123 | - break; |
|
124 | - } |
|
125 | - } |
|
126 | - return $result; |
|
127 | - } |
|
128 | - |
|
129 | - /* |
|
104 | + function isEncryptDataArray($array,$type) |
|
105 | + { |
|
106 | + foreach ($array as $value) { |
|
107 | + if(!$this->isEncryptData($value,$type)){ |
|
108 | + return false; |
|
109 | + } |
|
110 | + } |
|
111 | + return true; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * 判断是否是已加密的数据,数据必须是同一个类型 |
|
116 | + */ |
|
117 | + function isPartEncryptData($array,$type) |
|
118 | + { |
|
119 | + $result = false; |
|
120 | + foreach ($array as $value) { |
|
121 | + if($this->isEncryptData($value,$type)){ |
|
122 | + $result = true; |
|
123 | + break; |
|
124 | + } |
|
125 | + } |
|
126 | + return $result; |
|
127 | + } |
|
128 | + |
|
129 | + /* |
|
130 | 130 | * 判断是否是加密数据 |
131 | 131 | */ |
132 | - function isEncryptData($data,$type) |
|
133 | - { |
|
134 | - if(!is_string($data) || strlen($data) < 4) |
|
135 | - { |
|
136 | - return false; |
|
137 | - } |
|
138 | - |
|
139 | - $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
140 | - $strlen = strlen($data); |
|
141 | - if($data[0] != $separator || $data[$strlen -1] != $separator) |
|
142 | - { |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
147 | - $arrayLength = count($dataArray); |
|
148 | - |
|
149 | - if($separator == PHONE_SEPARATOR_CHAR) |
|
150 | - { |
|
151 | - if($arrayLength != 3) |
|
152 | - { |
|
153 | - return false; |
|
154 | - } |
|
155 | - if($data[$strlen - 2] == $separator) |
|
156 | - { |
|
157 | - return $this->checkEncryptData($dataArray); |
|
158 | - } |
|
159 | - else |
|
160 | - { |
|
161 | - $version = $dataArray[$arrayLength -1]; |
|
162 | - if(is_numeric($version)) |
|
163 | - { |
|
164 | - $base64Val = $dataArray[$arrayLength -2]; |
|
165 | - return $this->isBase64Str($base64Val); |
|
166 | - } |
|
167 | - } |
|
168 | - }else{ |
|
169 | - if($data[strlen($data) - 2] == $separator && $arrayLength == 3) |
|
170 | - { |
|
171 | - return $this->checkEncryptData($dataArray); |
|
172 | - } |
|
173 | - else if($arrayLength == 2) |
|
174 | - { |
|
175 | - return $this->checkEncryptData($dataArray); |
|
176 | - } |
|
177 | - else |
|
178 | - { |
|
179 | - return false; |
|
180 | - } |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - function search($data, $type,$secretContext) |
|
185 | - { |
|
186 | - $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
187 | - if('phone' == $type) { |
|
188 | - if (strlen($data) != 4 ) { |
|
189 | - throw new Exception("phoneNumber error"); |
|
190 | - } |
|
191 | - return $separator.$this->hmacMD5EncryptToBase64($data, $secretContext->secret).$separator; |
|
192 | - } else { |
|
193 | - $compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
194 | - $slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
195 | - |
|
196 | - $slideList = $this->getSlideWindows($data, $slideSize); |
|
197 | - $builder = ''; |
|
198 | - foreach ($slideList as $slide) { |
|
199 | - $builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
200 | - } |
|
201 | - return $builder; |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - /* |
|
132 | + function isEncryptData($data,$type) |
|
133 | + { |
|
134 | + if(!is_string($data) || strlen($data) < 4) |
|
135 | + { |
|
136 | + return false; |
|
137 | + } |
|
138 | + |
|
139 | + $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
140 | + $strlen = strlen($data); |
|
141 | + if($data[0] != $separator || $data[$strlen -1] != $separator) |
|
142 | + { |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
147 | + $arrayLength = count($dataArray); |
|
148 | + |
|
149 | + if($separator == PHONE_SEPARATOR_CHAR) |
|
150 | + { |
|
151 | + if($arrayLength != 3) |
|
152 | + { |
|
153 | + return false; |
|
154 | + } |
|
155 | + if($data[$strlen - 2] == $separator) |
|
156 | + { |
|
157 | + return $this->checkEncryptData($dataArray); |
|
158 | + } |
|
159 | + else |
|
160 | + { |
|
161 | + $version = $dataArray[$arrayLength -1]; |
|
162 | + if(is_numeric($version)) |
|
163 | + { |
|
164 | + $base64Val = $dataArray[$arrayLength -2]; |
|
165 | + return $this->isBase64Str($base64Val); |
|
166 | + } |
|
167 | + } |
|
168 | + }else{ |
|
169 | + if($data[strlen($data) - 2] == $separator && $arrayLength == 3) |
|
170 | + { |
|
171 | + return $this->checkEncryptData($dataArray); |
|
172 | + } |
|
173 | + else if($arrayLength == 2) |
|
174 | + { |
|
175 | + return $this->checkEncryptData($dataArray); |
|
176 | + } |
|
177 | + else |
|
178 | + { |
|
179 | + return false; |
|
180 | + } |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + function search($data, $type,$secretContext) |
|
185 | + { |
|
186 | + $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
187 | + if('phone' == $type) { |
|
188 | + if (strlen($data) != 4 ) { |
|
189 | + throw new Exception("phoneNumber error"); |
|
190 | + } |
|
191 | + return $separator.$this->hmacMD5EncryptToBase64($data, $secretContext->secret).$separator; |
|
192 | + } else { |
|
193 | + $compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
194 | + $slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
195 | + |
|
196 | + $slideList = $this->getSlideWindows($data, $slideSize); |
|
197 | + $builder = ''; |
|
198 | + foreach ($slideList as $slide) { |
|
199 | + $builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
200 | + } |
|
201 | + return $builder; |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + /* |
|
206 | 206 | * 加密逻辑 |
207 | 207 | */ |
208 | - function encrypt($data,$type,$version,$secretContext) |
|
209 | - { |
|
210 | - if(!is_string($data)) |
|
211 | - { |
|
212 | - return false; |
|
213 | - } |
|
214 | - |
|
215 | - $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
216 | - $isIndexEncrypt = $this->isIndexEncrypt($type,$version,$secretContext); |
|
217 | - if($isIndexEncrypt || $type == "search"){ |
|
218 | - if('phone' == $type) { |
|
219 | - return $this->encryptPhoneIndex($data,$separator,$secretContext); |
|
220 | - } else { |
|
221 | - $compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
222 | - $slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
223 | - return $this->encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext); |
|
224 | - } |
|
225 | - }else{ |
|
226 | - if('phone' == $type) { |
|
227 | - return $this->encryptPhone($data,$separator,$secretContext); |
|
228 | - } else { |
|
229 | - return $this->encryptNormal($data,$separator,$secretContext); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - } |
|
234 | - |
|
235 | - /* |
|
208 | + function encrypt($data,$type,$version,$secretContext) |
|
209 | + { |
|
210 | + if(!is_string($data)) |
|
211 | + { |
|
212 | + return false; |
|
213 | + } |
|
214 | + |
|
215 | + $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
216 | + $isIndexEncrypt = $this->isIndexEncrypt($type,$version,$secretContext); |
|
217 | + if($isIndexEncrypt || $type == "search"){ |
|
218 | + if('phone' == $type) { |
|
219 | + return $this->encryptPhoneIndex($data,$separator,$secretContext); |
|
220 | + } else { |
|
221 | + $compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
222 | + $slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
223 | + return $this->encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext); |
|
224 | + } |
|
225 | + }else{ |
|
226 | + if('phone' == $type) { |
|
227 | + return $this->encryptPhone($data,$separator,$secretContext); |
|
228 | + } else { |
|
229 | + return $this->encryptNormal($data,$separator,$secretContext); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + } |
|
234 | + |
|
235 | + /* |
|
236 | 236 | * 加密逻辑,手机号码格式 |
237 | 237 | */ |
238 | - function encryptPhone($data,$separator,$secretContext) |
|
239 | - { |
|
240 | - $len = strlen($data); |
|
241 | - if($len < 11) |
|
242 | - { |
|
243 | - return $data; |
|
244 | - } |
|
245 | - $prefixNumber = substr($data,0,$len -8); |
|
246 | - $last8Number = substr($data,$len -8,$len); |
|
247 | - |
|
248 | - return $separator.$prefixNumber.$separator.Security::encrypt($last8Number,$secretContext->secret) |
|
249 | - .$separator.$secretContext->secretVersion.$separator ; |
|
250 | - } |
|
251 | - |
|
252 | - /* |
|
238 | + function encryptPhone($data,$separator,$secretContext) |
|
239 | + { |
|
240 | + $len = strlen($data); |
|
241 | + if($len < 11) |
|
242 | + { |
|
243 | + return $data; |
|
244 | + } |
|
245 | + $prefixNumber = substr($data,0,$len -8); |
|
246 | + $last8Number = substr($data,$len -8,$len); |
|
247 | + |
|
248 | + return $separator.$prefixNumber.$separator.Security::encrypt($last8Number,$secretContext->secret) |
|
249 | + .$separator.$secretContext->secretVersion.$separator ; |
|
250 | + } |
|
251 | + |
|
252 | + /* |
|
253 | 253 | * 加密逻辑,非手机号码格式 |
254 | 254 | */ |
255 | - function encryptNormal($data,$separator,$secretContext) |
|
256 | - { |
|
257 | - return $separator.Security::encrypt($data,$secretContext->secret) |
|
258 | - .$separator.$secretContext->secretVersion.$separator; |
|
259 | - } |
|
255 | + function encryptNormal($data,$separator,$secretContext) |
|
256 | + { |
|
257 | + return $separator.Security::encrypt($data,$secretContext->secret) |
|
258 | + .$separator.$secretContext->secretVersion.$separator; |
|
259 | + } |
|
260 | 260 | |
261 | - /* |
|
261 | + /* |
|
262 | 262 | * 解密逻辑 |
263 | 263 | */ |
264 | - function decrypt($data,$type,$secretContext) |
|
265 | - { |
|
266 | - if(!$this->isEncryptData($data,$type)) |
|
267 | - { |
|
268 | - throw new Exception("数据[".$data."]不是类型为[".$type."]的加密数据"); |
|
269 | - } |
|
270 | - $dataLen = strlen($data); |
|
271 | - $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
272 | - |
|
273 | - $secretData = null; |
|
274 | - if($data[$dataLen - 2] == $separator){ |
|
275 | - $secretData = $this->getIndexSecretData($data,$separator); |
|
276 | - }else{ |
|
277 | - $secretData = $this->getSecretData($data,$separator); |
|
278 | - } |
|
264 | + function decrypt($data,$type,$secretContext) |
|
265 | + { |
|
266 | + if(!$this->isEncryptData($data,$type)) |
|
267 | + { |
|
268 | + throw new Exception("数据[".$data."]不是类型为[".$type."]的加密数据"); |
|
269 | + } |
|
270 | + $dataLen = strlen($data); |
|
271 | + $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
272 | + |
|
273 | + $secretData = null; |
|
274 | + if($data[$dataLen - 2] == $separator){ |
|
275 | + $secretData = $this->getIndexSecretData($data,$separator); |
|
276 | + }else{ |
|
277 | + $secretData = $this->getSecretData($data,$separator); |
|
278 | + } |
|
279 | 279 | |
280 | - if($secretData == null){ |
|
281 | - return $data; |
|
282 | - } |
|
280 | + if($secretData == null){ |
|
281 | + return $data; |
|
282 | + } |
|
283 | 283 | |
284 | - $result = Security::decrypt($secretData->originalBase64Value,$secretContext->secret); |
|
284 | + $result = Security::decrypt($secretData->originalBase64Value,$secretContext->secret); |
|
285 | 285 | |
286 | - if($separator == PHONE_SEPARATOR_CHAR && !$secretData->search) |
|
287 | - { |
|
288 | - return $secretData->originalValue.$result; |
|
289 | - } |
|
290 | - return $result; |
|
291 | - } |
|
286 | + if($separator == PHONE_SEPARATOR_CHAR && !$secretData->search) |
|
287 | + { |
|
288 | + return $secretData->originalValue.$result; |
|
289 | + } |
|
290 | + return $result; |
|
291 | + } |
|
292 | 292 | |
293 | - /* |
|
293 | + /* |
|
294 | 294 | * 判断是否是公钥数据 |
295 | 295 | */ |
296 | - function isPublicData($data,$type) |
|
297 | - { |
|
298 | - $secretData = $this->getSecretDataByType($data,$type); |
|
299 | - if(empty($secretData)){ |
|
300 | - return false; |
|
301 | - } |
|
302 | - if(intval($secretData->secretVersion) < 0){ |
|
303 | - return true; |
|
304 | - } |
|
305 | - return false; |
|
306 | - } |
|
307 | - |
|
308 | - function getSecretDataByType($data,$type) |
|
309 | - { |
|
310 | - $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
311 | - $dataLen = strlen($data); |
|
312 | - |
|
313 | - if($data[$dataLen - 2] == $separator){ |
|
314 | - return $secretData = $this->getIndexSecretData($data,$separator); |
|
315 | - }else{ |
|
316 | - return $secretData = $this->getSecretData($data,$separator); |
|
317 | - } |
|
318 | - } |
|
319 | - |
|
320 | - /* |
|
296 | + function isPublicData($data,$type) |
|
297 | + { |
|
298 | + $secretData = $this->getSecretDataByType($data,$type); |
|
299 | + if(empty($secretData)){ |
|
300 | + return false; |
|
301 | + } |
|
302 | + if(intval($secretData->secretVersion) < 0){ |
|
303 | + return true; |
|
304 | + } |
|
305 | + return false; |
|
306 | + } |
|
307 | + |
|
308 | + function getSecretDataByType($data,$type) |
|
309 | + { |
|
310 | + $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
311 | + $dataLen = strlen($data); |
|
312 | + |
|
313 | + if($data[$dataLen - 2] == $separator){ |
|
314 | + return $secretData = $this->getIndexSecretData($data,$separator); |
|
315 | + }else{ |
|
316 | + return $secretData = $this->getSecretData($data,$separator); |
|
317 | + } |
|
318 | + } |
|
319 | + |
|
320 | + /* |
|
321 | 321 | * 分解密文 |
322 | 322 | */ |
323 | - function getSecretData($data,$separator) |
|
324 | - { |
|
325 | - $secretData = new SecretData; |
|
326 | - $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
327 | - $arrayLength = count($dataArray); |
|
328 | - |
|
329 | - if($separator == PHONE_SEPARATOR_CHAR) |
|
330 | - { |
|
331 | - if($arrayLength != 3){ |
|
332 | - return null; |
|
333 | - }else{ |
|
334 | - $version = $dataArray[2]; |
|
335 | - if(is_numeric($version)) |
|
336 | - { |
|
337 | - $secretData->originalValue = $dataArray[0]; |
|
338 | - $secretData->originalBase64Value = $dataArray[1]; |
|
339 | - $secretData->secretVersion = $version; |
|
340 | - } |
|
341 | - } |
|
342 | - } |
|
343 | - else |
|
344 | - { |
|
345 | - if($arrayLength != 2){ |
|
346 | - return null; |
|
347 | - }else{ |
|
348 | - $version = $dataArray[1]; |
|
349 | - if(is_numeric($version)) |
|
350 | - { |
|
351 | - $secretData->originalBase64Value = $dataArray[0]; |
|
352 | - $secretData->secretVersion = $version; |
|
353 | - } |
|
354 | - } |
|
355 | - } |
|
356 | - return $secretData; |
|
357 | - } |
|
358 | - |
|
359 | - function getIndexSecretData($data,$separator) { |
|
360 | - $secretData = new SecretData; |
|
361 | - $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
362 | - $arrayLength = count($dataArray); |
|
363 | - |
|
364 | - if($separator == PHONE_SEPARATOR_CHAR) { |
|
365 | - if ($arrayLength != 3) { |
|
366 | - return null; |
|
367 | - }else{ |
|
368 | - $version = $dataArray[2]; |
|
369 | - if(is_numeric($version)) |
|
370 | - { |
|
371 | - $secretData->originalValue = $dataArray[0]; |
|
372 | - $secretData->originalBase64Value = $dataArray[1]; |
|
373 | - $secretData->secretVersion = $version; |
|
374 | - } |
|
375 | - } |
|
323 | + function getSecretData($data,$separator) |
|
324 | + { |
|
325 | + $secretData = new SecretData; |
|
326 | + $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
327 | + $arrayLength = count($dataArray); |
|
328 | + |
|
329 | + if($separator == PHONE_SEPARATOR_CHAR) |
|
330 | + { |
|
331 | + if($arrayLength != 3){ |
|
332 | + return null; |
|
333 | + }else{ |
|
334 | + $version = $dataArray[2]; |
|
335 | + if(is_numeric($version)) |
|
336 | + { |
|
337 | + $secretData->originalValue = $dataArray[0]; |
|
338 | + $secretData->originalBase64Value = $dataArray[1]; |
|
339 | + $secretData->secretVersion = $version; |
|
340 | + } |
|
341 | + } |
|
342 | + } |
|
343 | + else |
|
344 | + { |
|
345 | + if($arrayLength != 2){ |
|
346 | + return null; |
|
347 | + }else{ |
|
348 | + $version = $dataArray[1]; |
|
349 | + if(is_numeric($version)) |
|
350 | + { |
|
351 | + $secretData->originalBase64Value = $dataArray[0]; |
|
352 | + $secretData->secretVersion = $version; |
|
353 | + } |
|
354 | + } |
|
355 | + } |
|
356 | + return $secretData; |
|
357 | + } |
|
358 | + |
|
359 | + function getIndexSecretData($data,$separator) { |
|
360 | + $secretData = new SecretData; |
|
361 | + $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
362 | + $arrayLength = count($dataArray); |
|
363 | + |
|
364 | + if($separator == PHONE_SEPARATOR_CHAR) { |
|
365 | + if ($arrayLength != 3) { |
|
366 | + return null; |
|
367 | + }else{ |
|
368 | + $version = $dataArray[2]; |
|
369 | + if(is_numeric($version)) |
|
370 | + { |
|
371 | + $secretData->originalValue = $dataArray[0]; |
|
372 | + $secretData->originalBase64Value = $dataArray[1]; |
|
373 | + $secretData->secretVersion = $version; |
|
374 | + } |
|
375 | + } |
|
376 | 376 | |
377 | - } else { |
|
378 | - if($arrayLength != 3){ |
|
379 | - return null; |
|
380 | - } else { |
|
381 | - $version = $dataArray[2]; |
|
382 | - if(is_numeric($version)) |
|
383 | - { |
|
384 | - $secretData->originalBase64Value = $dataArray[0]; |
|
385 | - $secretData->originalValue = $dataArray[1]; |
|
386 | - $secretData->secretVersion = $version; |
|
387 | - } |
|
388 | - } |
|
389 | - } |
|
390 | - |
|
391 | - $secretData->search = true; |
|
392 | - return $secretData; |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * 判断密文是否支持检索 |
|
397 | - * |
|
398 | - * @param key |
|
399 | - * @param version |
|
400 | - * @return |
|
401 | - */ |
|
402 | - function isIndexEncrypt($key,$version,$secretContext) |
|
403 | - { |
|
404 | - if ($version != null && $version < 0) { |
|
405 | - $key = "previous_".$key; |
|
406 | - } else { |
|
407 | - $key = "current_".$key; |
|
408 | - } |
|
409 | - |
|
410 | - return $secretContext->appConfig != null && |
|
411 | - array_key_exists($key,$secretContext->appConfig) && |
|
412 | - $secretContext->appConfig[$key] == "2"; |
|
413 | - } |
|
414 | - |
|
415 | - function isLetterOrDigit($ch) |
|
416 | - { |
|
417 | - $code = ord($ch); |
|
418 | - if (0 <= $code && $code <= 127) { |
|
419 | - return true; |
|
420 | - } |
|
421 | - return false; |
|
422 | - } |
|
423 | - |
|
424 | - function utf8_strlen($string = null) { |
|
425 | - // 将字符串分解为单元 |
|
426 | - preg_match_all("/./us", $string, $match); |
|
427 | - // 返回单元个数 |
|
428 | - return count($match[0]); |
|
429 | - } |
|
430 | - |
|
431 | - function utf8_substr($string,$start,$end) { |
|
432 | - // 将字符串分解为单元 |
|
433 | - preg_match_all("/./us", $string, $match); |
|
434 | - // 返回单元个数 |
|
435 | - $result = ""; |
|
436 | - for($i = $start; $i < $end; $i++){ |
|
437 | - $result .= $match[0][$i]; |
|
438 | - } |
|
439 | - return $result; |
|
440 | - } |
|
441 | - |
|
442 | - function utf8_str_at($string,$index) { |
|
443 | - // 将字符串分解为单元 |
|
444 | - preg_match_all("/./us", $string, $match); |
|
445 | - // 返回单元个数 |
|
446 | - return $match[0][$index]; |
|
447 | - } |
|
448 | - |
|
449 | - function compress($input,$toLength) { |
|
450 | - if($toLength < 0) { |
|
451 | - return null; |
|
452 | - } |
|
453 | - $output = array(); |
|
454 | - for($i = 0; $i < $toLength; $i++) { |
|
455 | - $output[$i] = chr(0); |
|
456 | - } |
|
457 | - $input = $this->getBytes($input); |
|
458 | - $inputLength = count($input); |
|
459 | - for ($i = 0; $i < $inputLength; $i++) { |
|
460 | - $index_output = $i % $toLength; |
|
461 | - $output[$index_output] = $output[$index_output] ^ $input[$i]; |
|
462 | - } |
|
463 | - return $output; |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * @see #hmacMD5Encrypt |
|
468 | - * |
|
469 | - * @param encryptText |
|
470 | - * 被签名的字符串 |
|
471 | - * @param encryptKey |
|
472 | - * 密钥 |
|
473 | - * @param compressLen压缩长度 |
|
474 | - * @return |
|
475 | - * @throws Exception |
|
476 | - */ |
|
477 | - function hmacMD5EncryptToBase64($encryptText,$encryptKey,$compressLen = 0) { |
|
478 | - $encryptResult = Security::hmac_md5($encryptText,$encryptKey); |
|
479 | - if($compressLen != 0){ |
|
480 | - $encryptResult = $this->compress($encryptResult,$compressLen); |
|
481 | - } |
|
482 | - return base64_encode($this->toStr($encryptResult)); |
|
483 | - } |
|
484 | - |
|
485 | - |
|
486 | - /** |
|
487 | - * 生成滑动窗口 |
|
488 | - * |
|
489 | - * @param input |
|
490 | - * @param slideSize |
|
491 | - * @return |
|
492 | - */ |
|
493 | - function getSlideWindows($input,$slideSize = 4) |
|
494 | - { |
|
495 | - $endIndex = 0; |
|
496 | - $startIndex = 0; |
|
497 | - $currentWindowSize = 0; |
|
498 | - $currentWindow = null; |
|
499 | - $dataLength = $this->utf8_strlen($input); |
|
500 | - $windows = array(); |
|
501 | - while($endIndex < $dataLength || $currentWindowSize > $slideSize) |
|
502 | - { |
|
503 | - $startsWithLetterOrDigit = false; |
|
504 | - if(!empty($currentWindow)){ |
|
505 | - $startsWithLetterOrDigit = $this->isLetterOrDigit($this->utf8_str_at($currentWindow,0)); |
|
506 | - } |
|
507 | - if($endIndex == $dataLength && $startsWithLetterOrDigit == false){ |
|
508 | - break; |
|
509 | - } |
|
510 | - if($currentWindowSize == $slideSize && |
|
511 | - $startsWithLetterOrDigit == false && |
|
512 | - $this->isLetterOrDigit($this->utf8_str_at($input,$endIndex))) { |
|
513 | - $endIndex ++; |
|
514 | - $currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
515 | - $currentWindowSize = 5; |
|
516 | - } else { |
|
517 | - if($endIndex != 0){ |
|
518 | - if($startsWithLetterOrDigit){ |
|
519 | - $currentWindowSize -= 1; |
|
520 | - }else{ |
|
521 | - $currentWindowSize -= 2; |
|
522 | - } |
|
523 | - $startIndex ++; |
|
524 | - } |
|
525 | - |
|
526 | - while ($currentWindowSize < $slideSize && $endIndex < $dataLength) { |
|
527 | - $currentChar = $this->utf8_str_at($input,$endIndex); |
|
528 | - if ($this->isLetterOrDigit($currentChar)) { |
|
529 | - $currentWindowSize += 1; |
|
530 | - } else { |
|
531 | - $currentWindowSize += 2; |
|
532 | - } |
|
533 | - $endIndex++; |
|
534 | - } |
|
535 | - $currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
536 | - } |
|
537 | - array_push($windows,$currentWindow); |
|
538 | - } |
|
539 | - return $windows; |
|
540 | - } |
|
541 | - |
|
542 | - function encryptPhoneIndex($data,$separator,$secretContext) { |
|
543 | - $dataLength = strlen($data); |
|
544 | - if($dataLength < 11) { |
|
545 | - return $data; |
|
546 | - } |
|
547 | - $last4Number = substr($data,$dataLength -4 ,$dataLength); |
|
548 | - return $separator.$this->hmacMD5EncryptToBase64($last4Number,$secretContext->secret).$separator |
|
549 | - .Security::encrypt($data,$secretContext->secret).$separator.$secretContext->secretVersion |
|
550 | - .$separator.$separator; |
|
551 | - } |
|
552 | - |
|
553 | - function encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext) { |
|
554 | - $slideList = $this->getSlideWindows($data, $slideSize); |
|
555 | - $builder = ""; |
|
556 | - foreach ($slideList as $slide) { |
|
557 | - $builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
558 | - } |
|
559 | - return $separator.Security::encrypt($data,$secretContext->secret).$separator.$builder.$separator |
|
560 | - .$secretContext->secretVersion.$separator.$separator; |
|
561 | - } |
|
562 | - |
|
563 | - function getArrayValue($array,$key,$default) { |
|
564 | - if(array_key_exists($key, $array)){ |
|
565 | - return $array[$key]; |
|
566 | - } |
|
567 | - return $default; |
|
568 | - } |
|
569 | - |
|
570 | - function getBytes($string) { |
|
571 | - $bytes = array(); |
|
572 | - for($i = 0; $i < strlen($string); $i++){ |
|
573 | - $bytes[] = ord($string[$i]); |
|
574 | - } |
|
575 | - return $bytes; |
|
576 | - } |
|
577 | - |
|
578 | - function toStr($bytes) { |
|
579 | - if(!is_array($bytes)){ |
|
580 | - return $bytes; |
|
581 | - } |
|
582 | - $str = ''; |
|
583 | - foreach($bytes as $ch) { |
|
584 | - $str .= chr($ch); |
|
585 | - } |
|
586 | - return $str; |
|
587 | - } |
|
588 | - } |
|
377 | + } else { |
|
378 | + if($arrayLength != 3){ |
|
379 | + return null; |
|
380 | + } else { |
|
381 | + $version = $dataArray[2]; |
|
382 | + if(is_numeric($version)) |
|
383 | + { |
|
384 | + $secretData->originalBase64Value = $dataArray[0]; |
|
385 | + $secretData->originalValue = $dataArray[1]; |
|
386 | + $secretData->secretVersion = $version; |
|
387 | + } |
|
388 | + } |
|
389 | + } |
|
390 | + |
|
391 | + $secretData->search = true; |
|
392 | + return $secretData; |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * 判断密文是否支持检索 |
|
397 | + * |
|
398 | + * @param key |
|
399 | + * @param version |
|
400 | + * @return |
|
401 | + */ |
|
402 | + function isIndexEncrypt($key,$version,$secretContext) |
|
403 | + { |
|
404 | + if ($version != null && $version < 0) { |
|
405 | + $key = "previous_".$key; |
|
406 | + } else { |
|
407 | + $key = "current_".$key; |
|
408 | + } |
|
409 | + |
|
410 | + return $secretContext->appConfig != null && |
|
411 | + array_key_exists($key,$secretContext->appConfig) && |
|
412 | + $secretContext->appConfig[$key] == "2"; |
|
413 | + } |
|
414 | + |
|
415 | + function isLetterOrDigit($ch) |
|
416 | + { |
|
417 | + $code = ord($ch); |
|
418 | + if (0 <= $code && $code <= 127) { |
|
419 | + return true; |
|
420 | + } |
|
421 | + return false; |
|
422 | + } |
|
423 | + |
|
424 | + function utf8_strlen($string = null) { |
|
425 | + // 将字符串分解为单元 |
|
426 | + preg_match_all("/./us", $string, $match); |
|
427 | + // 返回单元个数 |
|
428 | + return count($match[0]); |
|
429 | + } |
|
430 | + |
|
431 | + function utf8_substr($string,$start,$end) { |
|
432 | + // 将字符串分解为单元 |
|
433 | + preg_match_all("/./us", $string, $match); |
|
434 | + // 返回单元个数 |
|
435 | + $result = ""; |
|
436 | + for($i = $start; $i < $end; $i++){ |
|
437 | + $result .= $match[0][$i]; |
|
438 | + } |
|
439 | + return $result; |
|
440 | + } |
|
441 | + |
|
442 | + function utf8_str_at($string,$index) { |
|
443 | + // 将字符串分解为单元 |
|
444 | + preg_match_all("/./us", $string, $match); |
|
445 | + // 返回单元个数 |
|
446 | + return $match[0][$index]; |
|
447 | + } |
|
448 | + |
|
449 | + function compress($input,$toLength) { |
|
450 | + if($toLength < 0) { |
|
451 | + return null; |
|
452 | + } |
|
453 | + $output = array(); |
|
454 | + for($i = 0; $i < $toLength; $i++) { |
|
455 | + $output[$i] = chr(0); |
|
456 | + } |
|
457 | + $input = $this->getBytes($input); |
|
458 | + $inputLength = count($input); |
|
459 | + for ($i = 0; $i < $inputLength; $i++) { |
|
460 | + $index_output = $i % $toLength; |
|
461 | + $output[$index_output] = $output[$index_output] ^ $input[$i]; |
|
462 | + } |
|
463 | + return $output; |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * @see #hmacMD5Encrypt |
|
468 | + * |
|
469 | + * @param encryptText |
|
470 | + * 被签名的字符串 |
|
471 | + * @param encryptKey |
|
472 | + * 密钥 |
|
473 | + * @param compressLen压缩长度 |
|
474 | + * @return |
|
475 | + * @throws Exception |
|
476 | + */ |
|
477 | + function hmacMD5EncryptToBase64($encryptText,$encryptKey,$compressLen = 0) { |
|
478 | + $encryptResult = Security::hmac_md5($encryptText,$encryptKey); |
|
479 | + if($compressLen != 0){ |
|
480 | + $encryptResult = $this->compress($encryptResult,$compressLen); |
|
481 | + } |
|
482 | + return base64_encode($this->toStr($encryptResult)); |
|
483 | + } |
|
484 | + |
|
485 | + |
|
486 | + /** |
|
487 | + * 生成滑动窗口 |
|
488 | + * |
|
489 | + * @param input |
|
490 | + * @param slideSize |
|
491 | + * @return |
|
492 | + */ |
|
493 | + function getSlideWindows($input,$slideSize = 4) |
|
494 | + { |
|
495 | + $endIndex = 0; |
|
496 | + $startIndex = 0; |
|
497 | + $currentWindowSize = 0; |
|
498 | + $currentWindow = null; |
|
499 | + $dataLength = $this->utf8_strlen($input); |
|
500 | + $windows = array(); |
|
501 | + while($endIndex < $dataLength || $currentWindowSize > $slideSize) |
|
502 | + { |
|
503 | + $startsWithLetterOrDigit = false; |
|
504 | + if(!empty($currentWindow)){ |
|
505 | + $startsWithLetterOrDigit = $this->isLetterOrDigit($this->utf8_str_at($currentWindow,0)); |
|
506 | + } |
|
507 | + if($endIndex == $dataLength && $startsWithLetterOrDigit == false){ |
|
508 | + break; |
|
509 | + } |
|
510 | + if($currentWindowSize == $slideSize && |
|
511 | + $startsWithLetterOrDigit == false && |
|
512 | + $this->isLetterOrDigit($this->utf8_str_at($input,$endIndex))) { |
|
513 | + $endIndex ++; |
|
514 | + $currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
515 | + $currentWindowSize = 5; |
|
516 | + } else { |
|
517 | + if($endIndex != 0){ |
|
518 | + if($startsWithLetterOrDigit){ |
|
519 | + $currentWindowSize -= 1; |
|
520 | + }else{ |
|
521 | + $currentWindowSize -= 2; |
|
522 | + } |
|
523 | + $startIndex ++; |
|
524 | + } |
|
525 | + |
|
526 | + while ($currentWindowSize < $slideSize && $endIndex < $dataLength) { |
|
527 | + $currentChar = $this->utf8_str_at($input,$endIndex); |
|
528 | + if ($this->isLetterOrDigit($currentChar)) { |
|
529 | + $currentWindowSize += 1; |
|
530 | + } else { |
|
531 | + $currentWindowSize += 2; |
|
532 | + } |
|
533 | + $endIndex++; |
|
534 | + } |
|
535 | + $currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
536 | + } |
|
537 | + array_push($windows,$currentWindow); |
|
538 | + } |
|
539 | + return $windows; |
|
540 | + } |
|
541 | + |
|
542 | + function encryptPhoneIndex($data,$separator,$secretContext) { |
|
543 | + $dataLength = strlen($data); |
|
544 | + if($dataLength < 11) { |
|
545 | + return $data; |
|
546 | + } |
|
547 | + $last4Number = substr($data,$dataLength -4 ,$dataLength); |
|
548 | + return $separator.$this->hmacMD5EncryptToBase64($last4Number,$secretContext->secret).$separator |
|
549 | + .Security::encrypt($data,$secretContext->secret).$separator.$secretContext->secretVersion |
|
550 | + .$separator.$separator; |
|
551 | + } |
|
552 | + |
|
553 | + function encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext) { |
|
554 | + $slideList = $this->getSlideWindows($data, $slideSize); |
|
555 | + $builder = ""; |
|
556 | + foreach ($slideList as $slide) { |
|
557 | + $builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
558 | + } |
|
559 | + return $separator.Security::encrypt($data,$secretContext->secret).$separator.$builder.$separator |
|
560 | + .$secretContext->secretVersion.$separator.$separator; |
|
561 | + } |
|
562 | + |
|
563 | + function getArrayValue($array,$key,$default) { |
|
564 | + if(array_key_exists($key, $array)){ |
|
565 | + return $array[$key]; |
|
566 | + } |
|
567 | + return $default; |
|
568 | + } |
|
569 | + |
|
570 | + function getBytes($string) { |
|
571 | + $bytes = array(); |
|
572 | + for($i = 0; $i < strlen($string); $i++){ |
|
573 | + $bytes[] = ord($string[$i]); |
|
574 | + } |
|
575 | + return $bytes; |
|
576 | + } |
|
577 | + |
|
578 | + function toStr($bytes) { |
|
579 | + if(!is_array($bytes)){ |
|
580 | + return $bytes; |
|
581 | + } |
|
582 | + $str = ''; |
|
583 | + foreach($bytes as $ch) { |
|
584 | + $str .= chr($ch); |
|
585 | + } |
|
586 | + return $str; |
|
587 | + } |
|
588 | + } |
|
589 | 589 | ?> |
590 | 590 | \ No newline at end of file |
@@ -11,17 +11,17 @@ discard block |
||
11 | 11 | |
12 | 12 | function __construct() |
13 | 13 | { |
14 | - if(!defined("PHONE_SEPARATOR_CHAR")) |
|
14 | + if (!defined("PHONE_SEPARATOR_CHAR")) |
|
15 | 15 | { |
16 | - define('PHONE_SEPARATOR_CHAR','$'); |
|
16 | + define('PHONE_SEPARATOR_CHAR', '$'); |
|
17 | 17 | } |
18 | - if(!defined("NICK_SEPARATOR_CHAR")) |
|
18 | + if (!defined("NICK_SEPARATOR_CHAR")) |
|
19 | 19 | { |
20 | - define('NICK_SEPARATOR_CHAR','~'); |
|
20 | + define('NICK_SEPARATOR_CHAR', '~'); |
|
21 | 21 | } |
22 | - if(!defined("NORMAL_SEPARATOR_CHAR")) |
|
22 | + if (!defined("NORMAL_SEPARATOR_CHAR")) |
|
23 | 23 | { |
24 | - define('NORMAL_SEPARATOR_CHAR',chr(1)); |
|
24 | + define('NORMAL_SEPARATOR_CHAR', chr(1)); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | $this->SEPARATOR_CHAR_MAP['nick'] = NICK_SEPARATOR_CHAR; |
@@ -39,9 +39,9 @@ discard block |
||
39 | 39 | function isBase64Str($str) |
40 | 40 | { |
41 | 41 | $strLen = strlen($str); |
42 | - for($i = 0; $i < $strLen ; $i++) |
|
42 | + for ($i = 0; $i < $strLen; $i++) |
|
43 | 43 | { |
44 | - if(!$this->isBase64Char($str[$i])) |
|
44 | + if (!$this->isBase64Char($str[$i])) |
|
45 | 45 | { |
46 | 46 | return false; |
47 | 47 | } |
@@ -54,46 +54,46 @@ discard block |
||
54 | 54 | */ |
55 | 55 | function isBase64Char($char) |
56 | 56 | { |
57 | - return strpos($this->BASE64_ARRAY,$char) !== false; |
|
57 | + return strpos($this->BASE64_ARRAY, $char) !== false; |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | /* |
61 | 61 | * 使用sep字符进行trim |
62 | 62 | */ |
63 | - function trimBySep($str,$sep) |
|
63 | + function trimBySep($str, $sep) |
|
64 | 64 | { |
65 | 65 | $start = 0; |
66 | 66 | $end = strlen($str); |
67 | - for($i = 0; $i < $end; $i++) |
|
67 | + for ($i = 0; $i < $end; $i++) |
|
68 | 68 | { |
69 | - if($str[$i] == $sep) |
|
69 | + if ($str[$i] == $sep) |
|
70 | 70 | { |
71 | - $start = $i + 1; |
|
71 | + $start = $i+1; |
|
72 | 72 | } |
73 | 73 | else |
74 | 74 | { |
75 | 75 | break; |
76 | 76 | } |
77 | 77 | } |
78 | - for($i = $end -1 ; $i >= 0; $i--) |
|
78 | + for ($i = $end-1; $i >= 0; $i--) |
|
79 | 79 | { |
80 | - if($str[$i] == $sep) |
|
80 | + if ($str[$i] == $sep) |
|
81 | 81 | { |
82 | - $end = $i - 1; |
|
82 | + $end = $i-1; |
|
83 | 83 | } |
84 | 84 | else |
85 | 85 | { |
86 | 86 | break; |
87 | 87 | } |
88 | 88 | } |
89 | - return substr($str,$start,$end); |
|
89 | + return substr($str, $start, $end); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | function checkEncryptData($dataArray) |
93 | 93 | { |
94 | - if(count($dataArray) == 2){ |
|
94 | + if (count($dataArray) == 2) { |
|
95 | 95 | return $this->isBase64Str($dataArray[0]); |
96 | - }else{ |
|
96 | + }else { |
|
97 | 97 | return $this->isBase64Str($dataArray[0]) && $this->isBase64Str($dataArray[1]); |
98 | 98 | } |
99 | 99 | } |
@@ -101,10 +101,10 @@ discard block |
||
101 | 101 | /* |
102 | 102 | * 判断是否是加密数据 |
103 | 103 | */ |
104 | - function isEncryptDataArray($array,$type) |
|
104 | + function isEncryptDataArray($array, $type) |
|
105 | 105 | { |
106 | 106 | foreach ($array as $value) { |
107 | - if(!$this->isEncryptData($value,$type)){ |
|
107 | + if (!$this->isEncryptData($value, $type)) { |
|
108 | 108 | return false; |
109 | 109 | } |
110 | 110 | } |
@@ -114,11 +114,11 @@ discard block |
||
114 | 114 | /** |
115 | 115 | * 判断是否是已加密的数据,数据必须是同一个类型 |
116 | 116 | */ |
117 | - function isPartEncryptData($array,$type) |
|
117 | + function isPartEncryptData($array, $type) |
|
118 | 118 | { |
119 | 119 | $result = false; |
120 | 120 | foreach ($array as $value) { |
121 | - if($this->isEncryptData($value,$type)){ |
|
121 | + if ($this->isEncryptData($value, $type)) { |
|
122 | 122 | $result = true; |
123 | 123 | break; |
124 | 124 | } |
@@ -129,48 +129,48 @@ discard block |
||
129 | 129 | /* |
130 | 130 | * 判断是否是加密数据 |
131 | 131 | */ |
132 | - function isEncryptData($data,$type) |
|
132 | + function isEncryptData($data, $type) |
|
133 | 133 | { |
134 | - if(!is_string($data) || strlen($data) < 4) |
|
134 | + if (!is_string($data) || strlen($data) < 4) |
|
135 | 135 | { |
136 | 136 | return false; |
137 | 137 | } |
138 | 138 | |
139 | 139 | $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
140 | 140 | $strlen = strlen($data); |
141 | - if($data[0] != $separator || $data[$strlen -1] != $separator) |
|
141 | + if ($data[0] != $separator || $data[$strlen-1] != $separator) |
|
142 | 142 | { |
143 | 143 | return false; |
144 | 144 | } |
145 | 145 | |
146 | - $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
146 | + $dataArray = explode($separator, $this->trimBySep($data, $separator)); |
|
147 | 147 | $arrayLength = count($dataArray); |
148 | 148 | |
149 | - if($separator == PHONE_SEPARATOR_CHAR) |
|
149 | + if ($separator == PHONE_SEPARATOR_CHAR) |
|
150 | 150 | { |
151 | - if($arrayLength != 3) |
|
151 | + if ($arrayLength != 3) |
|
152 | 152 | { |
153 | 153 | return false; |
154 | 154 | } |
155 | - if($data[$strlen - 2] == $separator) |
|
155 | + if ($data[$strlen-2] == $separator) |
|
156 | 156 | { |
157 | 157 | return $this->checkEncryptData($dataArray); |
158 | 158 | } |
159 | 159 | else |
160 | 160 | { |
161 | - $version = $dataArray[$arrayLength -1]; |
|
162 | - if(is_numeric($version)) |
|
161 | + $version = $dataArray[$arrayLength-1]; |
|
162 | + if (is_numeric($version)) |
|
163 | 163 | { |
164 | - $base64Val = $dataArray[$arrayLength -2]; |
|
164 | + $base64Val = $dataArray[$arrayLength-2]; |
|
165 | 165 | return $this->isBase64Str($base64Val); |
166 | 166 | } |
167 | 167 | } |
168 | - }else{ |
|
169 | - if($data[strlen($data) - 2] == $separator && $arrayLength == 3) |
|
168 | + }else { |
|
169 | + if ($data[strlen($data)-2] == $separator && $arrayLength == 3) |
|
170 | 170 | { |
171 | 171 | return $this->checkEncryptData($dataArray); |
172 | 172 | } |
173 | - else if($arrayLength == 2) |
|
173 | + else if ($arrayLength == 2) |
|
174 | 174 | { |
175 | 175 | return $this->checkEncryptData($dataArray); |
176 | 176 | } |
@@ -181,22 +181,22 @@ discard block |
||
181 | 181 | } |
182 | 182 | } |
183 | 183 | |
184 | - function search($data, $type,$secretContext) |
|
184 | + function search($data, $type, $secretContext) |
|
185 | 185 | { |
186 | 186 | $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
187 | - if('phone' == $type) { |
|
188 | - if (strlen($data) != 4 ) { |
|
187 | + if ('phone' == $type) { |
|
188 | + if (strlen($data) != 4) { |
|
189 | 189 | throw new Exception("phoneNumber error"); |
190 | 190 | } |
191 | 191 | return $separator.$this->hmacMD5EncryptToBase64($data, $secretContext->secret).$separator; |
192 | - } else { |
|
193 | - $compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
194 | - $slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
192 | + }else { |
|
193 | + $compressLen = $this->getArrayValue($secretContext->appConfig, 'encrypt_index_compress_len', 3); |
|
194 | + $slideSize = $this->getArrayValue($secretContext->appConfig, 'encrypt_slide_size', 4); |
|
195 | 195 | |
196 | 196 | $slideList = $this->getSlideWindows($data, $slideSize); |
197 | 197 | $builder = ''; |
198 | 198 | foreach ($slideList as $slide) { |
199 | - $builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
199 | + $builder .= $this->hmacMD5EncryptToBase64($slide, $secretContext->secret, $compressLen); |
|
200 | 200 | } |
201 | 201 | return $builder; |
202 | 202 | } |
@@ -205,28 +205,28 @@ discard block |
||
205 | 205 | /* |
206 | 206 | * 加密逻辑 |
207 | 207 | */ |
208 | - function encrypt($data,$type,$version,$secretContext) |
|
208 | + function encrypt($data, $type, $version, $secretContext) |
|
209 | 209 | { |
210 | - if(!is_string($data)) |
|
210 | + if (!is_string($data)) |
|
211 | 211 | { |
212 | 212 | return false; |
213 | 213 | } |
214 | 214 | |
215 | 215 | $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
216 | - $isIndexEncrypt = $this->isIndexEncrypt($type,$version,$secretContext); |
|
217 | - if($isIndexEncrypt || $type == "search"){ |
|
218 | - if('phone' == $type) { |
|
219 | - return $this->encryptPhoneIndex($data,$separator,$secretContext); |
|
220 | - } else { |
|
221 | - $compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
222 | - $slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
223 | - return $this->encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext); |
|
216 | + $isIndexEncrypt = $this->isIndexEncrypt($type, $version, $secretContext); |
|
217 | + if ($isIndexEncrypt || $type == "search") { |
|
218 | + if ('phone' == $type) { |
|
219 | + return $this->encryptPhoneIndex($data, $separator, $secretContext); |
|
220 | + }else { |
|
221 | + $compressLen = $this->getArrayValue($secretContext->appConfig, 'encrypt_index_compress_len', 3); |
|
222 | + $slideSize = $this->getArrayValue($secretContext->appConfig, 'encrypt_slide_size', 4); |
|
223 | + return $this->encryptNormalIndex($data, $compressLen, $slideSize, $separator, $secretContext); |
|
224 | 224 | } |
225 | - }else{ |
|
226 | - if('phone' == $type) { |
|
227 | - return $this->encryptPhone($data,$separator,$secretContext); |
|
228 | - } else { |
|
229 | - return $this->encryptNormal($data,$separator,$secretContext); |
|
225 | + }else { |
|
226 | + if ('phone' == $type) { |
|
227 | + return $this->encryptPhone($data, $separator, $secretContext); |
|
228 | + }else { |
|
229 | + return $this->encryptNormal($data, $separator, $secretContext); |
|
230 | 230 | } |
231 | 231 | } |
232 | 232 | |
@@ -235,35 +235,35 @@ discard block |
||
235 | 235 | /* |
236 | 236 | * 加密逻辑,手机号码格式 |
237 | 237 | */ |
238 | - function encryptPhone($data,$separator,$secretContext) |
|
238 | + function encryptPhone($data, $separator, $secretContext) |
|
239 | 239 | { |
240 | 240 | $len = strlen($data); |
241 | - if($len < 11) |
|
241 | + if ($len < 11) |
|
242 | 242 | { |
243 | 243 | return $data; |
244 | 244 | } |
245 | - $prefixNumber = substr($data,0,$len -8); |
|
246 | - $last8Number = substr($data,$len -8,$len); |
|
245 | + $prefixNumber = substr($data, 0, $len-8); |
|
246 | + $last8Number = substr($data, $len-8, $len); |
|
247 | 247 | |
248 | - return $separator.$prefixNumber.$separator.Security::encrypt($last8Number,$secretContext->secret) |
|
249 | - .$separator.$secretContext->secretVersion.$separator ; |
|
248 | + return $separator.$prefixNumber.$separator.Security::encrypt($last8Number, $secretContext->secret) |
|
249 | + .$separator.$secretContext->secretVersion.$separator; |
|
250 | 250 | } |
251 | 251 | |
252 | 252 | /* |
253 | 253 | * 加密逻辑,非手机号码格式 |
254 | 254 | */ |
255 | - function encryptNormal($data,$separator,$secretContext) |
|
255 | + function encryptNormal($data, $separator, $secretContext) |
|
256 | 256 | { |
257 | - return $separator.Security::encrypt($data,$secretContext->secret) |
|
257 | + return $separator.Security::encrypt($data, $secretContext->secret) |
|
258 | 258 | .$separator.$secretContext->secretVersion.$separator; |
259 | 259 | } |
260 | 260 | |
261 | 261 | /* |
262 | 262 | * 解密逻辑 |
263 | 263 | */ |
264 | - function decrypt($data,$type,$secretContext) |
|
264 | + function decrypt($data, $type, $secretContext) |
|
265 | 265 | { |
266 | - if(!$this->isEncryptData($data,$type)) |
|
266 | + if (!$this->isEncryptData($data, $type)) |
|
267 | 267 | { |
268 | 268 | throw new Exception("数据[".$data."]不是类型为[".$type."]的加密数据"); |
269 | 269 | } |
@@ -271,19 +271,19 @@ discard block |
||
271 | 271 | $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
272 | 272 | |
273 | 273 | $secretData = null; |
274 | - if($data[$dataLen - 2] == $separator){ |
|
275 | - $secretData = $this->getIndexSecretData($data,$separator); |
|
276 | - }else{ |
|
277 | - $secretData = $this->getSecretData($data,$separator); |
|
274 | + if ($data[$dataLen-2] == $separator) { |
|
275 | + $secretData = $this->getIndexSecretData($data, $separator); |
|
276 | + }else { |
|
277 | + $secretData = $this->getSecretData($data, $separator); |
|
278 | 278 | } |
279 | 279 | |
280 | - if($secretData == null){ |
|
280 | + if ($secretData == null) { |
|
281 | 281 | return $data; |
282 | 282 | } |
283 | 283 | |
284 | - $result = Security::decrypt($secretData->originalBase64Value,$secretContext->secret); |
|
284 | + $result = Security::decrypt($secretData->originalBase64Value, $secretContext->secret); |
|
285 | 285 | |
286 | - if($separator == PHONE_SEPARATOR_CHAR && !$secretData->search) |
|
286 | + if ($separator == PHONE_SEPARATOR_CHAR && !$secretData->search) |
|
287 | 287 | { |
288 | 288 | return $secretData->originalValue.$result; |
289 | 289 | } |
@@ -293,46 +293,46 @@ discard block |
||
293 | 293 | /* |
294 | 294 | * 判断是否是公钥数据 |
295 | 295 | */ |
296 | - function isPublicData($data,$type) |
|
296 | + function isPublicData($data, $type) |
|
297 | 297 | { |
298 | - $secretData = $this->getSecretDataByType($data,$type); |
|
299 | - if(empty($secretData)){ |
|
298 | + $secretData = $this->getSecretDataByType($data, $type); |
|
299 | + if (empty($secretData)) { |
|
300 | 300 | return false; |
301 | 301 | } |
302 | - if(intval($secretData->secretVersion) < 0){ |
|
302 | + if (intval($secretData->secretVersion) < 0) { |
|
303 | 303 | return true; |
304 | 304 | } |
305 | 305 | return false; |
306 | 306 | } |
307 | 307 | |
308 | - function getSecretDataByType($data,$type) |
|
308 | + function getSecretDataByType($data, $type) |
|
309 | 309 | { |
310 | 310 | $separator = $this->SEPARATOR_CHAR_MAP[$type]; |
311 | 311 | $dataLen = strlen($data); |
312 | 312 | |
313 | - if($data[$dataLen - 2] == $separator){ |
|
314 | - return $secretData = $this->getIndexSecretData($data,$separator); |
|
315 | - }else{ |
|
316 | - return $secretData = $this->getSecretData($data,$separator); |
|
313 | + if ($data[$dataLen-2] == $separator) { |
|
314 | + return $secretData = $this->getIndexSecretData($data, $separator); |
|
315 | + }else { |
|
316 | + return $secretData = $this->getSecretData($data, $separator); |
|
317 | 317 | } |
318 | 318 | } |
319 | 319 | |
320 | 320 | /* |
321 | 321 | * 分解密文 |
322 | 322 | */ |
323 | - function getSecretData($data,$separator) |
|
323 | + function getSecretData($data, $separator) |
|
324 | 324 | { |
325 | 325 | $secretData = new SecretData; |
326 | - $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
326 | + $dataArray = explode($separator, $this->trimBySep($data, $separator)); |
|
327 | 327 | $arrayLength = count($dataArray); |
328 | 328 | |
329 | - if($separator == PHONE_SEPARATOR_CHAR) |
|
329 | + if ($separator == PHONE_SEPARATOR_CHAR) |
|
330 | 330 | { |
331 | - if($arrayLength != 3){ |
|
331 | + if ($arrayLength != 3) { |
|
332 | 332 | return null; |
333 | - }else{ |
|
333 | + }else { |
|
334 | 334 | $version = $dataArray[2]; |
335 | - if(is_numeric($version)) |
|
335 | + if (is_numeric($version)) |
|
336 | 336 | { |
337 | 337 | $secretData->originalValue = $dataArray[0]; |
338 | 338 | $secretData->originalBase64Value = $dataArray[1]; |
@@ -342,11 +342,11 @@ discard block |
||
342 | 342 | } |
343 | 343 | else |
344 | 344 | { |
345 | - if($arrayLength != 2){ |
|
345 | + if ($arrayLength != 2) { |
|
346 | 346 | return null; |
347 | - }else{ |
|
347 | + }else { |
|
348 | 348 | $version = $dataArray[1]; |
349 | - if(is_numeric($version)) |
|
349 | + if (is_numeric($version)) |
|
350 | 350 | { |
351 | 351 | $secretData->originalBase64Value = $dataArray[0]; |
352 | 352 | $secretData->secretVersion = $version; |
@@ -356,17 +356,17 @@ discard block |
||
356 | 356 | return $secretData; |
357 | 357 | } |
358 | 358 | |
359 | - function getIndexSecretData($data,$separator) { |
|
359 | + function getIndexSecretData($data, $separator) { |
|
360 | 360 | $secretData = new SecretData; |
361 | - $dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
361 | + $dataArray = explode($separator, $this->trimBySep($data, $separator)); |
|
362 | 362 | $arrayLength = count($dataArray); |
363 | 363 | |
364 | - if($separator == PHONE_SEPARATOR_CHAR) { |
|
364 | + if ($separator == PHONE_SEPARATOR_CHAR) { |
|
365 | 365 | if ($arrayLength != 3) { |
366 | 366 | return null; |
367 | - }else{ |
|
367 | + }else { |
|
368 | 368 | $version = $dataArray[2]; |
369 | - if(is_numeric($version)) |
|
369 | + if (is_numeric($version)) |
|
370 | 370 | { |
371 | 371 | $secretData->originalValue = $dataArray[0]; |
372 | 372 | $secretData->originalBase64Value = $dataArray[1]; |
@@ -374,12 +374,12 @@ discard block |
||
374 | 374 | } |
375 | 375 | } |
376 | 376 | |
377 | - } else { |
|
378 | - if($arrayLength != 3){ |
|
377 | + }else { |
|
378 | + if ($arrayLength != 3) { |
|
379 | 379 | return null; |
380 | - } else { |
|
380 | + }else { |
|
381 | 381 | $version = $dataArray[2]; |
382 | - if(is_numeric($version)) |
|
382 | + if (is_numeric($version)) |
|
383 | 383 | { |
384 | 384 | $secretData->originalBase64Value = $dataArray[0]; |
385 | 385 | $secretData->originalValue = $dataArray[1]; |
@@ -399,16 +399,16 @@ discard block |
||
399 | 399 | * @param version |
400 | 400 | * @return |
401 | 401 | */ |
402 | - function isIndexEncrypt($key,$version,$secretContext) |
|
402 | + function isIndexEncrypt($key, $version, $secretContext) |
|
403 | 403 | { |
404 | 404 | if ($version != null && $version < 0) { |
405 | 405 | $key = "previous_".$key; |
406 | - } else { |
|
406 | + }else { |
|
407 | 407 | $key = "current_".$key; |
408 | 408 | } |
409 | 409 | |
410 | 410 | return $secretContext->appConfig != null && |
411 | - array_key_exists($key,$secretContext->appConfig) && |
|
411 | + array_key_exists($key, $secretContext->appConfig) && |
|
412 | 412 | $secretContext->appConfig[$key] == "2"; |
413 | 413 | } |
414 | 414 | |
@@ -428,36 +428,36 @@ discard block |
||
428 | 428 | return count($match[0]); |
429 | 429 | } |
430 | 430 | |
431 | - function utf8_substr($string,$start,$end) { |
|
431 | + function utf8_substr($string, $start, $end) { |
|
432 | 432 | // 将字符串分解为单元 |
433 | 433 | preg_match_all("/./us", $string, $match); |
434 | 434 | // 返回单元个数 |
435 | 435 | $result = ""; |
436 | - for($i = $start; $i < $end; $i++){ |
|
436 | + for ($i = $start; $i < $end; $i++) { |
|
437 | 437 | $result .= $match[0][$i]; |
438 | 438 | } |
439 | 439 | return $result; |
440 | 440 | } |
441 | 441 | |
442 | - function utf8_str_at($string,$index) { |
|
442 | + function utf8_str_at($string, $index) { |
|
443 | 443 | // 将字符串分解为单元 |
444 | 444 | preg_match_all("/./us", $string, $match); |
445 | 445 | // 返回单元个数 |
446 | 446 | return $match[0][$index]; |
447 | 447 | } |
448 | 448 | |
449 | - function compress($input,$toLength) { |
|
450 | - if($toLength < 0) { |
|
449 | + function compress($input, $toLength) { |
|
450 | + if ($toLength < 0) { |
|
451 | 451 | return null; |
452 | 452 | } |
453 | 453 | $output = array(); |
454 | - for($i = 0; $i < $toLength; $i++) { |
|
454 | + for ($i = 0; $i < $toLength; $i++) { |
|
455 | 455 | $output[$i] = chr(0); |
456 | 456 | } |
457 | 457 | $input = $this->getBytes($input); |
458 | 458 | $inputLength = count($input); |
459 | 459 | for ($i = 0; $i < $inputLength; $i++) { |
460 | - $index_output = $i % $toLength; |
|
460 | + $index_output = $i%$toLength; |
|
461 | 461 | $output[$index_output] = $output[$index_output] ^ $input[$i]; |
462 | 462 | } |
463 | 463 | return $output; |
@@ -474,10 +474,10 @@ discard block |
||
474 | 474 | * @return |
475 | 475 | * @throws Exception |
476 | 476 | */ |
477 | - function hmacMD5EncryptToBase64($encryptText,$encryptKey,$compressLen = 0) { |
|
478 | - $encryptResult = Security::hmac_md5($encryptText,$encryptKey); |
|
479 | - if($compressLen != 0){ |
|
480 | - $encryptResult = $this->compress($encryptResult,$compressLen); |
|
477 | + function hmacMD5EncryptToBase64($encryptText, $encryptKey, $compressLen = 0) { |
|
478 | + $encryptResult = Security::hmac_md5($encryptText, $encryptKey); |
|
479 | + if ($compressLen != 0) { |
|
480 | + $encryptResult = $this->compress($encryptResult, $compressLen); |
|
481 | 481 | } |
482 | 482 | return base64_encode($this->toStr($encryptResult)); |
483 | 483 | } |
@@ -490,7 +490,7 @@ discard block |
||
490 | 490 | * @param slideSize |
491 | 491 | * @return |
492 | 492 | */ |
493 | - function getSlideWindows($input,$slideSize = 4) |
|
493 | + function getSlideWindows($input, $slideSize = 4) |
|
494 | 494 | { |
495 | 495 | $endIndex = 0; |
496 | 496 | $startIndex = 0; |
@@ -498,70 +498,70 @@ discard block |
||
498 | 498 | $currentWindow = null; |
499 | 499 | $dataLength = $this->utf8_strlen($input); |
500 | 500 | $windows = array(); |
501 | - while($endIndex < $dataLength || $currentWindowSize > $slideSize) |
|
501 | + while ($endIndex < $dataLength || $currentWindowSize > $slideSize) |
|
502 | 502 | { |
503 | 503 | $startsWithLetterOrDigit = false; |
504 | - if(!empty($currentWindow)){ |
|
505 | - $startsWithLetterOrDigit = $this->isLetterOrDigit($this->utf8_str_at($currentWindow,0)); |
|
504 | + if (!empty($currentWindow)) { |
|
505 | + $startsWithLetterOrDigit = $this->isLetterOrDigit($this->utf8_str_at($currentWindow, 0)); |
|
506 | 506 | } |
507 | - if($endIndex == $dataLength && $startsWithLetterOrDigit == false){ |
|
507 | + if ($endIndex == $dataLength && $startsWithLetterOrDigit == false) { |
|
508 | 508 | break; |
509 | 509 | } |
510 | - if($currentWindowSize == $slideSize && |
|
510 | + if ($currentWindowSize == $slideSize && |
|
511 | 511 | $startsWithLetterOrDigit == false && |
512 | - $this->isLetterOrDigit($this->utf8_str_at($input,$endIndex))) { |
|
513 | - $endIndex ++; |
|
514 | - $currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
512 | + $this->isLetterOrDigit($this->utf8_str_at($input, $endIndex))) { |
|
513 | + $endIndex++; |
|
514 | + $currentWindow = $this->utf8_substr($input, $startIndex, $endIndex); |
|
515 | 515 | $currentWindowSize = 5; |
516 | - } else { |
|
517 | - if($endIndex != 0){ |
|
518 | - if($startsWithLetterOrDigit){ |
|
516 | + }else { |
|
517 | + if ($endIndex != 0) { |
|
518 | + if ($startsWithLetterOrDigit) { |
|
519 | 519 | $currentWindowSize -= 1; |
520 | - }else{ |
|
520 | + }else { |
|
521 | 521 | $currentWindowSize -= 2; |
522 | 522 | } |
523 | - $startIndex ++; |
|
523 | + $startIndex++; |
|
524 | 524 | } |
525 | 525 | |
526 | 526 | while ($currentWindowSize < $slideSize && $endIndex < $dataLength) { |
527 | - $currentChar = $this->utf8_str_at($input,$endIndex); |
|
527 | + $currentChar = $this->utf8_str_at($input, $endIndex); |
|
528 | 528 | if ($this->isLetterOrDigit($currentChar)) { |
529 | 529 | $currentWindowSize += 1; |
530 | - } else { |
|
530 | + }else { |
|
531 | 531 | $currentWindowSize += 2; |
532 | 532 | } |
533 | 533 | $endIndex++; |
534 | 534 | } |
535 | - $currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
535 | + $currentWindow = $this->utf8_substr($input, $startIndex, $endIndex); |
|
536 | 536 | } |
537 | - array_push($windows,$currentWindow); |
|
537 | + array_push($windows, $currentWindow); |
|
538 | 538 | } |
539 | 539 | return $windows; |
540 | 540 | } |
541 | 541 | |
542 | - function encryptPhoneIndex($data,$separator,$secretContext) { |
|
542 | + function encryptPhoneIndex($data, $separator, $secretContext) { |
|
543 | 543 | $dataLength = strlen($data); |
544 | - if($dataLength < 11) { |
|
544 | + if ($dataLength < 11) { |
|
545 | 545 | return $data; |
546 | 546 | } |
547 | - $last4Number = substr($data,$dataLength -4 ,$dataLength); |
|
548 | - return $separator.$this->hmacMD5EncryptToBase64($last4Number,$secretContext->secret).$separator |
|
549 | - .Security::encrypt($data,$secretContext->secret).$separator.$secretContext->secretVersion |
|
547 | + $last4Number = substr($data, $dataLength-4, $dataLength); |
|
548 | + return $separator.$this->hmacMD5EncryptToBase64($last4Number, $secretContext->secret).$separator |
|
549 | + .Security::encrypt($data, $secretContext->secret).$separator.$secretContext->secretVersion |
|
550 | 550 | .$separator.$separator; |
551 | 551 | } |
552 | 552 | |
553 | - function encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext) { |
|
553 | + function encryptNormalIndex($data, $compressLen, $slideSize, $separator, $secretContext) { |
|
554 | 554 | $slideList = $this->getSlideWindows($data, $slideSize); |
555 | 555 | $builder = ""; |
556 | 556 | foreach ($slideList as $slide) { |
557 | - $builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
557 | + $builder .= $this->hmacMD5EncryptToBase64($slide, $secretContext->secret, $compressLen); |
|
558 | 558 | } |
559 | - return $separator.Security::encrypt($data,$secretContext->secret).$separator.$builder.$separator |
|
559 | + return $separator.Security::encrypt($data, $secretContext->secret).$separator.$builder.$separator |
|
560 | 560 | .$secretContext->secretVersion.$separator.$separator; |
561 | 561 | } |
562 | 562 | |
563 | - function getArrayValue($array,$key,$default) { |
|
564 | - if(array_key_exists($key, $array)){ |
|
563 | + function getArrayValue($array, $key, $default) { |
|
564 | + if (array_key_exists($key, $array)) { |
|
565 | 565 | return $array[$key]; |
566 | 566 | } |
567 | 567 | return $default; |
@@ -569,18 +569,18 @@ discard block |
||
569 | 569 | |
570 | 570 | function getBytes($string) { |
571 | 571 | $bytes = array(); |
572 | - for($i = 0; $i < strlen($string); $i++){ |
|
572 | + for ($i = 0; $i < strlen($string); $i++) { |
|
573 | 573 | $bytes[] = ord($string[$i]); |
574 | 574 | } |
575 | 575 | return $bytes; |
576 | 576 | } |
577 | 577 | |
578 | 578 | function toStr($bytes) { |
579 | - if(!is_array($bytes)){ |
|
579 | + if (!is_array($bytes)) { |
|
580 | 580 | return $bytes; |
581 | 581 | } |
582 | 582 | $str = ''; |
583 | - foreach($bytes as $ch) { |
|
583 | + foreach ($bytes as $ch) { |
|
584 | 584 | $str .= chr($ch); |
585 | 585 | } |
586 | 586 | return $str; |
@@ -69,8 +69,7 @@ discard block |
||
69 | 69 | if($str[$i] == $sep) |
70 | 70 | { |
71 | 71 | $start = $i + 1; |
72 | - } |
|
73 | - else |
|
72 | + } else |
|
74 | 73 | { |
75 | 74 | break; |
76 | 75 | } |
@@ -80,8 +79,7 @@ discard block |
||
80 | 79 | if($str[$i] == $sep) |
81 | 80 | { |
82 | 81 | $end = $i - 1; |
83 | - } |
|
84 | - else |
|
82 | + } else |
|
85 | 83 | { |
86 | 84 | break; |
87 | 85 | } |
@@ -93,7 +91,7 @@ discard block |
||
93 | 91 | { |
94 | 92 | if(count($dataArray) == 2){ |
95 | 93 | return $this->isBase64Str($dataArray[0]); |
96 | - }else{ |
|
94 | + } else{ |
|
97 | 95 | return $this->isBase64Str($dataArray[0]) && $this->isBase64Str($dataArray[1]); |
98 | 96 | } |
99 | 97 | } |
@@ -155,8 +153,7 @@ discard block |
||
155 | 153 | if($data[$strlen - 2] == $separator) |
156 | 154 | { |
157 | 155 | return $this->checkEncryptData($dataArray); |
158 | - } |
|
159 | - else |
|
156 | + } else |
|
160 | 157 | { |
161 | 158 | $version = $dataArray[$arrayLength -1]; |
162 | 159 | if(is_numeric($version)) |
@@ -165,16 +162,14 @@ discard block |
||
165 | 162 | return $this->isBase64Str($base64Val); |
166 | 163 | } |
167 | 164 | } |
168 | - }else{ |
|
165 | + } else{ |
|
169 | 166 | if($data[strlen($data) - 2] == $separator && $arrayLength == 3) |
170 | 167 | { |
171 | 168 | return $this->checkEncryptData($dataArray); |
172 | - } |
|
173 | - else if($arrayLength == 2) |
|
169 | + } else if($arrayLength == 2) |
|
174 | 170 | { |
175 | 171 | return $this->checkEncryptData($dataArray); |
176 | - } |
|
177 | - else |
|
172 | + } else |
|
178 | 173 | { |
179 | 174 | return false; |
180 | 175 | } |
@@ -222,7 +217,7 @@ discard block |
||
222 | 217 | $slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
223 | 218 | return $this->encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext); |
224 | 219 | } |
225 | - }else{ |
|
220 | + } else{ |
|
226 | 221 | if('phone' == $type) { |
227 | 222 | return $this->encryptPhone($data,$separator,$secretContext); |
228 | 223 | } else { |
@@ -273,7 +268,7 @@ discard block |
||
273 | 268 | $secretData = null; |
274 | 269 | if($data[$dataLen - 2] == $separator){ |
275 | 270 | $secretData = $this->getIndexSecretData($data,$separator); |
276 | - }else{ |
|
271 | + } else{ |
|
277 | 272 | $secretData = $this->getSecretData($data,$separator); |
278 | 273 | } |
279 | 274 | |
@@ -312,7 +307,7 @@ discard block |
||
312 | 307 | |
313 | 308 | if($data[$dataLen - 2] == $separator){ |
314 | 309 | return $secretData = $this->getIndexSecretData($data,$separator); |
315 | - }else{ |
|
310 | + } else{ |
|
316 | 311 | return $secretData = $this->getSecretData($data,$separator); |
317 | 312 | } |
318 | 313 | } |
@@ -330,7 +325,7 @@ discard block |
||
330 | 325 | { |
331 | 326 | if($arrayLength != 3){ |
332 | 327 | return null; |
333 | - }else{ |
|
328 | + } else{ |
|
334 | 329 | $version = $dataArray[2]; |
335 | 330 | if(is_numeric($version)) |
336 | 331 | { |
@@ -339,12 +334,11 @@ discard block |
||
339 | 334 | $secretData->secretVersion = $version; |
340 | 335 | } |
341 | 336 | } |
342 | - } |
|
343 | - else |
|
337 | + } else |
|
344 | 338 | { |
345 | 339 | if($arrayLength != 2){ |
346 | 340 | return null; |
347 | - }else{ |
|
341 | + } else{ |
|
348 | 342 | $version = $dataArray[1]; |
349 | 343 | if(is_numeric($version)) |
350 | 344 | { |
@@ -364,7 +358,7 @@ discard block |
||
364 | 358 | if($separator == PHONE_SEPARATOR_CHAR) { |
365 | 359 | if ($arrayLength != 3) { |
366 | 360 | return null; |
367 | - }else{ |
|
361 | + } else{ |
|
368 | 362 | $version = $dataArray[2]; |
369 | 363 | if(is_numeric($version)) |
370 | 364 | { |
@@ -517,7 +511,7 @@ discard block |
||
517 | 511 | if($endIndex != 0){ |
518 | 512 | if($startsWithLetterOrDigit){ |
519 | 513 | $currentWindowSize -= 1; |
520 | - }else{ |
|
514 | + } else{ |
|
521 | 515 | $currentWindowSize -= 2; |
522 | 516 | } |
523 | 517 | $startIndex ++; |
@@ -1,543 +1,543 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - include './SecurityUtil.php'; |
|
4 | - include './SecretGetRequest.php'; |
|
5 | - include './TopSdkFeedbackUploadRequest.php'; |
|
6 | - include './iCache.php'; |
|
7 | - include '../../TopSdk.php'; |
|
8 | - |
|
9 | - class SecurityClient |
|
10 | - { |
|
11 | - private $topClient ; |
|
12 | - private $randomNum ; |
|
13 | - private $securityUtil; |
|
14 | - private $cacheClient = null; |
|
15 | - |
|
16 | - function __construct($client, $random) |
|
17 | - { |
|
18 | - |
|
19 | - define('APP_SECRET_TYPE','2'); |
|
20 | - define('APP_USER_SECRET_TYPE','3'); |
|
21 | - |
|
22 | - $this->topClient = $client; |
|
23 | - $this->randomNum = $random; |
|
24 | - $this->securityUtil = new SecurityUtil(); |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * 设置缓存处理器 |
|
29 | - */ |
|
30 | - function setCacheClient($cache) |
|
31 | - { |
|
32 | - $this->cacheClient = $cache; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * 密文检索,在秘钥升级场景下兼容查询 |
|
37 | - * |
|
38 | - * @see #search(String, String, String, Long) |
|
39 | - * @return |
|
40 | - */ |
|
41 | - function searchPrevious($data,$type,$session = null) |
|
42 | - { |
|
43 | - return $this->searchInner($data,$type,$session,-1); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * 密文检索(每个用户单独分配秘钥) |
|
48 | - * |
|
49 | - * @see #search(String, String, String, Long) |
|
50 | - * @return |
|
51 | - */ |
|
52 | - function search($data,$type,$session = null) |
|
53 | - { |
|
54 | - return $this->searchInner($data,$type,$session,null); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * 密文检索。 手机号码格式:$base64(H-MAC(phone后4位))$ simple格式:base64(H-MAC(滑窗)) |
|
59 | - * |
|
60 | - * @param data |
|
61 | - * 明文数据 |
|
62 | - * @param type |
|
63 | - * 加密字段类型(例如:simple\phone) |
|
64 | - * @param session |
|
65 | - * 用户身份,用户级加密必填 |
|
66 | - * @param version |
|
67 | - * 秘钥历史版本 |
|
68 | - * @return |
|
69 | - */ |
|
70 | - function searchInner($data, $type, $session,$version) |
|
71 | - { |
|
72 | - if(empty($data) || empty($type)){ |
|
73 | - return $data; |
|
74 | - } |
|
75 | - |
|
76 | - $secretContext = null; |
|
3 | + include './SecurityUtil.php'; |
|
4 | + include './SecretGetRequest.php'; |
|
5 | + include './TopSdkFeedbackUploadRequest.php'; |
|
6 | + include './iCache.php'; |
|
7 | + include '../../TopSdk.php'; |
|
8 | + |
|
9 | + class SecurityClient |
|
10 | + { |
|
11 | + private $topClient ; |
|
12 | + private $randomNum ; |
|
13 | + private $securityUtil; |
|
14 | + private $cacheClient = null; |
|
15 | + |
|
16 | + function __construct($client, $random) |
|
17 | + { |
|
18 | + |
|
19 | + define('APP_SECRET_TYPE','2'); |
|
20 | + define('APP_USER_SECRET_TYPE','3'); |
|
21 | + |
|
22 | + $this->topClient = $client; |
|
23 | + $this->randomNum = $random; |
|
24 | + $this->securityUtil = new SecurityUtil(); |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * 设置缓存处理器 |
|
29 | + */ |
|
30 | + function setCacheClient($cache) |
|
31 | + { |
|
32 | + $this->cacheClient = $cache; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * 密文检索,在秘钥升级场景下兼容查询 |
|
37 | + * |
|
38 | + * @see #search(String, String, String, Long) |
|
39 | + * @return |
|
40 | + */ |
|
41 | + function searchPrevious($data,$type,$session = null) |
|
42 | + { |
|
43 | + return $this->searchInner($data,$type,$session,-1); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * 密文检索(每个用户单独分配秘钥) |
|
48 | + * |
|
49 | + * @see #search(String, String, String, Long) |
|
50 | + * @return |
|
51 | + */ |
|
52 | + function search($data,$type,$session = null) |
|
53 | + { |
|
54 | + return $this->searchInner($data,$type,$session,null); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * 密文检索。 手机号码格式:$base64(H-MAC(phone后4位))$ simple格式:base64(H-MAC(滑窗)) |
|
59 | + * |
|
60 | + * @param data |
|
61 | + * 明文数据 |
|
62 | + * @param type |
|
63 | + * 加密字段类型(例如:simple\phone) |
|
64 | + * @param session |
|
65 | + * 用户身份,用户级加密必填 |
|
66 | + * @param version |
|
67 | + * 秘钥历史版本 |
|
68 | + * @return |
|
69 | + */ |
|
70 | + function searchInner($data, $type, $session,$version) |
|
71 | + { |
|
72 | + if(empty($data) || empty($type)){ |
|
73 | + return $data; |
|
74 | + } |
|
75 | + |
|
76 | + $secretContext = null; |
|
77 | 77 | |
78 | - $secretContext = $this->callSecretApiWithCache($session,$version); |
|
79 | - $this->incrCounter(3,$type,$secretContext,true); |
|
80 | - |
|
81 | - if(empty($secretContext) || empty($secretContext->secret)) { |
|
82 | - return $data; |
|
83 | - } |
|
84 | - |
|
85 | - return $this->securityUtil->search($data, $type,$secretContext); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * 单条数据解密,使用appkey级别公钥 |
|
91 | - * 非加密数据直接返回原文 |
|
92 | - */ |
|
93 | - function decryptPublic($data,$type) |
|
94 | - { |
|
95 | - return $this->decrypt($data,$type,null); |
|
96 | - } |
|
97 | - /** |
|
98 | - * 单条数据解密 |
|
99 | - * 非加密数据直接返回原文 |
|
100 | - */ |
|
101 | - function decrypt($data,$type,$session) |
|
102 | - { |
|
103 | - if(empty($data) || empty($type)){ |
|
104 | - return $data; |
|
105 | - } |
|
106 | - $secretData = $this->securityUtil->getSecretDataByType($data,$type); |
|
107 | - if(empty($secretData)){ |
|
108 | - return $data; |
|
109 | - } |
|
110 | - |
|
111 | - if($this->securityUtil->isPublicData($data,$type)){ |
|
112 | - $secretContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
113 | - } else { |
|
114 | - $secretContext = $this->callSecretApiWithCache($session,$secretData->secretVersion); |
|
115 | - } |
|
116 | - $this->incrCounter(2,$type,$secretContext,true); |
|
78 | + $secretContext = $this->callSecretApiWithCache($session,$version); |
|
79 | + $this->incrCounter(3,$type,$secretContext,true); |
|
80 | + |
|
81 | + if(empty($secretContext) || empty($secretContext->secret)) { |
|
82 | + return $data; |
|
83 | + } |
|
84 | + |
|
85 | + return $this->securityUtil->search($data, $type,$secretContext); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * 单条数据解密,使用appkey级别公钥 |
|
91 | + * 非加密数据直接返回原文 |
|
92 | + */ |
|
93 | + function decryptPublic($data,$type) |
|
94 | + { |
|
95 | + return $this->decrypt($data,$type,null); |
|
96 | + } |
|
97 | + /** |
|
98 | + * 单条数据解密 |
|
99 | + * 非加密数据直接返回原文 |
|
100 | + */ |
|
101 | + function decrypt($data,$type,$session) |
|
102 | + { |
|
103 | + if(empty($data) || empty($type)){ |
|
104 | + return $data; |
|
105 | + } |
|
106 | + $secretData = $this->securityUtil->getSecretDataByType($data,$type); |
|
107 | + if(empty($secretData)){ |
|
108 | + return $data; |
|
109 | + } |
|
110 | + |
|
111 | + if($this->securityUtil->isPublicData($data,$type)){ |
|
112 | + $secretContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
113 | + } else { |
|
114 | + $secretContext = $this->callSecretApiWithCache($session,$secretData->secretVersion); |
|
115 | + } |
|
116 | + $this->incrCounter(2,$type,$secretContext,true); |
|
117 | 117 | |
118 | - return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * 多条数据解密,使用appkey级别公钥 |
|
123 | - * 非加密数据直接返回原文 |
|
124 | - */ |
|
125 | - function decryptBatchPublic($array,$type) |
|
126 | - { |
|
127 | - if(empty($array) || empty($type)){ |
|
128 | - return null; |
|
129 | - } |
|
130 | - |
|
131 | - $result = array(); |
|
132 | - foreach ($array as $value) { |
|
133 | - $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
|
134 | - $secretContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
135 | - |
|
136 | - if(empty($secretData)){ |
|
137 | - $result[$value] = $value; |
|
138 | - }else{ |
|
139 | - $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
|
140 | - $this->incrCounter(2,$type,$secretContext,true); |
|
141 | - } |
|
142 | - |
|
143 | - $this->flushCounter($secretContext); |
|
144 | - } |
|
145 | - |
|
146 | - return $result; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * 多条数据解密,必须是同一个type和用户,返回结果是 KV结果 |
|
151 | - * 非加密数据直接返回原文 |
|
152 | - */ |
|
153 | - function decryptBatch($array,$type,$session) |
|
154 | - { |
|
155 | - if(empty($array) || empty($type)){ |
|
156 | - return null; |
|
157 | - } |
|
158 | - |
|
159 | - $result = array(); |
|
160 | - foreach ($array as $value) { |
|
161 | - $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
|
162 | - if(empty($secretData)){ |
|
163 | - $result[$value] = $value; |
|
164 | - } else if($this->securityUtil->isPublicData($value,$type)){ |
|
165 | - $appContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
166 | - $result[$value] = $this->securityUtil->decrypt($value,$type,$appContext); |
|
167 | - $this->incrCounter(2,$type,$appContext,false); |
|
168 | - $this->flushCounter($appContext); |
|
169 | - } else { |
|
170 | - $secretContext = $this->callSecretApiWithCache($session,$secretData->secretVersion); |
|
171 | - $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
|
172 | - $this->incrCounter(2,$type,$secretContext,false); |
|
173 | - $this->flushCounter($secretContext); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - return $result; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * 使用上一版本秘钥解密,app级别公钥 |
|
182 | - */ |
|
183 | - function decryptPreviousPublic($data,$type) |
|
184 | - { |
|
185 | - $secretContext = $this->callSecretApiWithCache(null,-1); |
|
186 | - return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
187 | - } |
|
188 | - /** |
|
189 | - * 使用上一版本秘钥解密,一般只用于更新秘钥 |
|
190 | - */ |
|
191 | - function decryptPrevious($data,$type,$session) |
|
192 | - { |
|
193 | - if($this->securityUtil->isPublicData($data,$type)){ |
|
194 | - $secretContext = $this->callSecretApiWithCache(null,-1); |
|
195 | - } else { |
|
196 | - $secretContext = $this->callSecretApiWithCache($session,-1); |
|
197 | - } |
|
198 | - return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * 加密单条数据,使用app级别公钥 |
|
203 | - */ |
|
204 | - function encryptPublic($data,$type,$version = null) |
|
205 | - { |
|
206 | - return $this->encrypt($data,$type,null,$version); |
|
207 | - } |
|
208 | - /** |
|
209 | - * 加密单条数据 |
|
210 | - */ |
|
211 | - function encrypt($data,$type,$session = null,$version = null) |
|
212 | - { |
|
213 | - if(empty($data) || empty($type)){ |
|
214 | - return null; |
|
215 | - } |
|
216 | - $secretContext = $this->callSecretApiWithCache($session,null); |
|
217 | - $this->incrCounter(1,$type,$secretContext,true); |
|
218 | - |
|
219 | - return $this->securityUtil->encrypt($data,$type,$version,$secretContext); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * 加密多条数据,使用app级别公钥 |
|
224 | - */ |
|
225 | - function encryptBatchPublic($array,$type,$version = null) |
|
226 | - { |
|
227 | - if(empty($array) || empty($type)){ |
|
228 | - return null; |
|
229 | - } |
|
230 | - $secretContext = $this->callSecretApiWithCache(null,null); |
|
231 | - $result = array(); |
|
232 | - foreach ($array as $value) { |
|
233 | - $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
|
234 | - $this->incrCounter(1,$type,$secretContext,false); |
|
235 | - } |
|
236 | - $this->flushCounter($secretContext); |
|
237 | - |
|
238 | - return $result; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * 加密多条数据,必须是同一个type和用户,返回结果是 KV结果 |
|
243 | - */ |
|
244 | - function encryptBatch($array,$type,$session,$version = null) |
|
245 | - { |
|
246 | - if(empty($array) || empty($type)){ |
|
247 | - return null; |
|
248 | - } |
|
249 | - $secretContext = $this->callSecretApiWithCache($session,null); |
|
250 | - $result = array(); |
|
251 | - foreach ($array as $value) { |
|
252 | - $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
|
253 | - $this->incrCounter(1,$type,$secretContext,false); |
|
254 | - } |
|
255 | - $this->flushCounter($secretContext); |
|
256 | - return $result; |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * 使用上一版本秘钥加密,使用app级别公钥 |
|
261 | - */ |
|
262 | - function encryptPreviousPublic($data,$type) |
|
263 | - { |
|
264 | - $secretContext = $this->callSecretApiWithCache(null,-1); |
|
265 | - $this->incrCounter(1,$type,$secretContext,true); |
|
266 | - |
|
267 | - return $this->securityUtil->encrypt($data,$type,$secretContext->version,$secretContext); |
|
268 | - } |
|
269 | - /** |
|
270 | - * 使用上一版本秘钥加密,一般只用于更新秘钥 |
|
271 | - */ |
|
272 | - function encryptPrevious($data,$type,$session) |
|
273 | - { |
|
274 | - $secretContext = $this->callSecretApiWithCache($session,-1); |
|
275 | - $this->incrCounter(1,$type,$secretContext,true); |
|
276 | - |
|
277 | - return $this->securityUtil->encrypt($data,$type,$secretContext); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * 根据session生成秘钥 |
|
282 | - */ |
|
283 | - function initSecret($session) |
|
284 | - { |
|
285 | - return $this->callSecretApiWithCache($session,null); |
|
286 | - } |
|
287 | - |
|
288 | - function buildCacheKey($session,$secretVersion) |
|
289 | - { |
|
290 | - if(empty($session)){ |
|
291 | - return $this->topClient->getAppkey(); |
|
292 | - } |
|
293 | - if(empty($secretVersion)){ |
|
294 | - return $session ; |
|
295 | - } |
|
296 | - return $session.'_'.$secretVersion ; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - function generateCustomerSession($userId) |
|
301 | - { |
|
302 | - return '_'.$userId ; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * 判断是否是已加密的数据 |
|
307 | - */ |
|
308 | - function isEncryptData($data,$type) |
|
309 | - { |
|
310 | - if(empty($data) || empty($type)){ |
|
311 | - return false; |
|
312 | - } |
|
313 | - return $this->securityUtil->isEncryptData($data,$type); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * 判断是否是已加密的数据,数据必须是同一个类型 |
|
318 | - */ |
|
319 | - function isEncryptDataArray($array,$type) |
|
320 | - { |
|
321 | - if(empty($array) || empty($type)){ |
|
322 | - return false; |
|
323 | - } |
|
324 | - return $this->securityUtil->isEncryptDataArray($array,$type); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * 判断数组中的数据是否存在密文,存在任何一个返回true,否则false |
|
329 | - */ |
|
330 | - function isPartEncryptData($array,$type) |
|
331 | - { |
|
332 | - if(empty($array) || empty($type)){ |
|
333 | - return false; |
|
334 | - } |
|
335 | - return $this->securityUtil->isPartEncryptData($array,$type); |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * 获取秘钥,使用缓存 |
|
340 | - */ |
|
341 | - function callSecretApiWithCache($session,$secretVersion) |
|
342 | - { |
|
343 | - if($this->cacheClient) |
|
344 | - { |
|
345 | - $time = time(); |
|
346 | - $cacheKey = $this->buildCacheKey($session,$secretVersion); |
|
347 | - $secretContext = $this->cacheClient->getCache($cacheKey); |
|
348 | - |
|
349 | - if($secretContext) |
|
350 | - { |
|
351 | - if($this->canUpload($secretContext)){ |
|
352 | - if($this->report($secretContext)){ |
|
353 | - $this->clearReport($secretContext); |
|
354 | - } |
|
355 | - } |
|
356 | - } |
|
357 | - |
|
358 | - if($secretContext && $secretContext->invalidTime > $time) |
|
359 | - { |
|
360 | - return $secretContext; |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - $secretContext = $this->callSecretApi($session,$secretVersion); |
|
365 | - |
|
366 | - if($this->cacheClient) |
|
367 | - { |
|
368 | - $secretContext->cacheKey = $cacheKey; |
|
369 | - $this->cacheClient->setCache($cacheKey,$secretContext); |
|
370 | - } |
|
371 | - |
|
372 | - return $secretContext; |
|
373 | - } |
|
374 | - |
|
375 | - function incrCounter($op,$type,$secretContext,$flush) |
|
376 | - { |
|
377 | - if($op == 1){ |
|
378 | - switch ($type) { |
|
379 | - case 'nick': |
|
380 | - $secretContext->encryptNickNum ++ ; |
|
381 | - break; |
|
382 | - case 'simple': |
|
383 | - $secretContext->encryptSimpleNum ++ ; |
|
384 | - break; |
|
385 | - case 'receiver_name': |
|
386 | - $secretContext->encryptReceiverNameNum ++ ; |
|
387 | - break; |
|
388 | - case 'phone': |
|
389 | - $secretContext->encryptPhoneNum ++ ; |
|
390 | - break; |
|
391 | - default: |
|
392 | - break; |
|
393 | - } |
|
394 | - }else if($op == 2){ |
|
395 | - switch ($type) { |
|
396 | - case 'nick': |
|
397 | - $secretContext->decryptNickNum ++ ; |
|
398 | - break; |
|
399 | - case 'simple': |
|
400 | - $secretContext->decryptSimpleNum ++ ; |
|
401 | - break; |
|
402 | - case 'receiver_name': |
|
403 | - $secretContext->decryptReceiverNameNum ++ ; |
|
404 | - break; |
|
405 | - case 'phone': |
|
406 | - $secretContext->decryptPhoneNum ++ ; |
|
407 | - break; |
|
408 | - default: |
|
409 | - break; |
|
410 | - } |
|
411 | - }else{ |
|
412 | - switch ($type) { |
|
413 | - case 'nick': |
|
414 | - $secretContext->searchNickNum ++ ; |
|
415 | - break; |
|
416 | - case 'simple': |
|
417 | - $secretContext->searchSimpleNum ++ ; |
|
418 | - break; |
|
419 | - case 'receiver_name': |
|
420 | - $secretContext->searchReceiverNameNum ++ ; |
|
421 | - break; |
|
422 | - case 'phone': |
|
423 | - $secretContext->searchPhoneNum ++ ; |
|
424 | - break; |
|
425 | - default: |
|
426 | - break; |
|
427 | - } |
|
428 | - } |
|
429 | - |
|
430 | - if($flush && $this->cacheClient){ |
|
431 | - $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - function flushCounter($secretContext) |
|
436 | - { |
|
437 | - if($this->cacheClient){ |
|
438 | - $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
|
439 | - } |
|
440 | - } |
|
441 | - |
|
442 | - function clearReport($secretContext) |
|
443 | - { |
|
444 | - $secretContext->encryptPhoneNum = 0; |
|
445 | - $secretContext->encryptNickNum = 0; |
|
446 | - $secretContext->encryptReceiverNameNum = 0; |
|
447 | - $secretContext->encryptSimpleNum = 0; |
|
448 | - $secretContext->encryptSearchNum = 0; |
|
449 | - $secretContext->decryptPhoneNum = 0; |
|
450 | - $secretContext->decryptNickNum = 0; |
|
451 | - $secretContext->decryptReceiverNameNum = 0; |
|
452 | - $secretContext->decryptSimpleNum = 0; |
|
453 | - $secretContext->decryptSearchNum = 0; |
|
454 | - $secretContext->searchPhoneNum = 0; |
|
455 | - $secretContext->searchNickNum = 0; |
|
456 | - $secretContext->searchReceiverNameNum = 0; |
|
457 | - $secretContext->searchSimpleNum = 0; |
|
458 | - $secretContext->searchSearchNum = 0; |
|
459 | - $secretContext->lastUploadTime = time(); |
|
460 | - } |
|
461 | - |
|
462 | - function canUpload($secretContext) |
|
463 | - { |
|
464 | - $current = time(); |
|
465 | - if($current - $secretContext->lastUploadTime > 300){ |
|
466 | - return true; |
|
467 | - } |
|
468 | - return false; |
|
469 | - } |
|
470 | - |
|
471 | - /* |
|
118 | + return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * 多条数据解密,使用appkey级别公钥 |
|
123 | + * 非加密数据直接返回原文 |
|
124 | + */ |
|
125 | + function decryptBatchPublic($array,$type) |
|
126 | + { |
|
127 | + if(empty($array) || empty($type)){ |
|
128 | + return null; |
|
129 | + } |
|
130 | + |
|
131 | + $result = array(); |
|
132 | + foreach ($array as $value) { |
|
133 | + $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
|
134 | + $secretContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
135 | + |
|
136 | + if(empty($secretData)){ |
|
137 | + $result[$value] = $value; |
|
138 | + }else{ |
|
139 | + $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
|
140 | + $this->incrCounter(2,$type,$secretContext,true); |
|
141 | + } |
|
142 | + |
|
143 | + $this->flushCounter($secretContext); |
|
144 | + } |
|
145 | + |
|
146 | + return $result; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * 多条数据解密,必须是同一个type和用户,返回结果是 KV结果 |
|
151 | + * 非加密数据直接返回原文 |
|
152 | + */ |
|
153 | + function decryptBatch($array,$type,$session) |
|
154 | + { |
|
155 | + if(empty($array) || empty($type)){ |
|
156 | + return null; |
|
157 | + } |
|
158 | + |
|
159 | + $result = array(); |
|
160 | + foreach ($array as $value) { |
|
161 | + $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
|
162 | + if(empty($secretData)){ |
|
163 | + $result[$value] = $value; |
|
164 | + } else if($this->securityUtil->isPublicData($value,$type)){ |
|
165 | + $appContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
166 | + $result[$value] = $this->securityUtil->decrypt($value,$type,$appContext); |
|
167 | + $this->incrCounter(2,$type,$appContext,false); |
|
168 | + $this->flushCounter($appContext); |
|
169 | + } else { |
|
170 | + $secretContext = $this->callSecretApiWithCache($session,$secretData->secretVersion); |
|
171 | + $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
|
172 | + $this->incrCounter(2,$type,$secretContext,false); |
|
173 | + $this->flushCounter($secretContext); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + return $result; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * 使用上一版本秘钥解密,app级别公钥 |
|
182 | + */ |
|
183 | + function decryptPreviousPublic($data,$type) |
|
184 | + { |
|
185 | + $secretContext = $this->callSecretApiWithCache(null,-1); |
|
186 | + return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
187 | + } |
|
188 | + /** |
|
189 | + * 使用上一版本秘钥解密,一般只用于更新秘钥 |
|
190 | + */ |
|
191 | + function decryptPrevious($data,$type,$session) |
|
192 | + { |
|
193 | + if($this->securityUtil->isPublicData($data,$type)){ |
|
194 | + $secretContext = $this->callSecretApiWithCache(null,-1); |
|
195 | + } else { |
|
196 | + $secretContext = $this->callSecretApiWithCache($session,-1); |
|
197 | + } |
|
198 | + return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * 加密单条数据,使用app级别公钥 |
|
203 | + */ |
|
204 | + function encryptPublic($data,$type,$version = null) |
|
205 | + { |
|
206 | + return $this->encrypt($data,$type,null,$version); |
|
207 | + } |
|
208 | + /** |
|
209 | + * 加密单条数据 |
|
210 | + */ |
|
211 | + function encrypt($data,$type,$session = null,$version = null) |
|
212 | + { |
|
213 | + if(empty($data) || empty($type)){ |
|
214 | + return null; |
|
215 | + } |
|
216 | + $secretContext = $this->callSecretApiWithCache($session,null); |
|
217 | + $this->incrCounter(1,$type,$secretContext,true); |
|
218 | + |
|
219 | + return $this->securityUtil->encrypt($data,$type,$version,$secretContext); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * 加密多条数据,使用app级别公钥 |
|
224 | + */ |
|
225 | + function encryptBatchPublic($array,$type,$version = null) |
|
226 | + { |
|
227 | + if(empty($array) || empty($type)){ |
|
228 | + return null; |
|
229 | + } |
|
230 | + $secretContext = $this->callSecretApiWithCache(null,null); |
|
231 | + $result = array(); |
|
232 | + foreach ($array as $value) { |
|
233 | + $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
|
234 | + $this->incrCounter(1,$type,$secretContext,false); |
|
235 | + } |
|
236 | + $this->flushCounter($secretContext); |
|
237 | + |
|
238 | + return $result; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * 加密多条数据,必须是同一个type和用户,返回结果是 KV结果 |
|
243 | + */ |
|
244 | + function encryptBatch($array,$type,$session,$version = null) |
|
245 | + { |
|
246 | + if(empty($array) || empty($type)){ |
|
247 | + return null; |
|
248 | + } |
|
249 | + $secretContext = $this->callSecretApiWithCache($session,null); |
|
250 | + $result = array(); |
|
251 | + foreach ($array as $value) { |
|
252 | + $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
|
253 | + $this->incrCounter(1,$type,$secretContext,false); |
|
254 | + } |
|
255 | + $this->flushCounter($secretContext); |
|
256 | + return $result; |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * 使用上一版本秘钥加密,使用app级别公钥 |
|
261 | + */ |
|
262 | + function encryptPreviousPublic($data,$type) |
|
263 | + { |
|
264 | + $secretContext = $this->callSecretApiWithCache(null,-1); |
|
265 | + $this->incrCounter(1,$type,$secretContext,true); |
|
266 | + |
|
267 | + return $this->securityUtil->encrypt($data,$type,$secretContext->version,$secretContext); |
|
268 | + } |
|
269 | + /** |
|
270 | + * 使用上一版本秘钥加密,一般只用于更新秘钥 |
|
271 | + */ |
|
272 | + function encryptPrevious($data,$type,$session) |
|
273 | + { |
|
274 | + $secretContext = $this->callSecretApiWithCache($session,-1); |
|
275 | + $this->incrCounter(1,$type,$secretContext,true); |
|
276 | + |
|
277 | + return $this->securityUtil->encrypt($data,$type,$secretContext); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * 根据session生成秘钥 |
|
282 | + */ |
|
283 | + function initSecret($session) |
|
284 | + { |
|
285 | + return $this->callSecretApiWithCache($session,null); |
|
286 | + } |
|
287 | + |
|
288 | + function buildCacheKey($session,$secretVersion) |
|
289 | + { |
|
290 | + if(empty($session)){ |
|
291 | + return $this->topClient->getAppkey(); |
|
292 | + } |
|
293 | + if(empty($secretVersion)){ |
|
294 | + return $session ; |
|
295 | + } |
|
296 | + return $session.'_'.$secretVersion ; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + function generateCustomerSession($userId) |
|
301 | + { |
|
302 | + return '_'.$userId ; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * 判断是否是已加密的数据 |
|
307 | + */ |
|
308 | + function isEncryptData($data,$type) |
|
309 | + { |
|
310 | + if(empty($data) || empty($type)){ |
|
311 | + return false; |
|
312 | + } |
|
313 | + return $this->securityUtil->isEncryptData($data,$type); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * 判断是否是已加密的数据,数据必须是同一个类型 |
|
318 | + */ |
|
319 | + function isEncryptDataArray($array,$type) |
|
320 | + { |
|
321 | + if(empty($array) || empty($type)){ |
|
322 | + return false; |
|
323 | + } |
|
324 | + return $this->securityUtil->isEncryptDataArray($array,$type); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * 判断数组中的数据是否存在密文,存在任何一个返回true,否则false |
|
329 | + */ |
|
330 | + function isPartEncryptData($array,$type) |
|
331 | + { |
|
332 | + if(empty($array) || empty($type)){ |
|
333 | + return false; |
|
334 | + } |
|
335 | + return $this->securityUtil->isPartEncryptData($array,$type); |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * 获取秘钥,使用缓存 |
|
340 | + */ |
|
341 | + function callSecretApiWithCache($session,$secretVersion) |
|
342 | + { |
|
343 | + if($this->cacheClient) |
|
344 | + { |
|
345 | + $time = time(); |
|
346 | + $cacheKey = $this->buildCacheKey($session,$secretVersion); |
|
347 | + $secretContext = $this->cacheClient->getCache($cacheKey); |
|
348 | + |
|
349 | + if($secretContext) |
|
350 | + { |
|
351 | + if($this->canUpload($secretContext)){ |
|
352 | + if($this->report($secretContext)){ |
|
353 | + $this->clearReport($secretContext); |
|
354 | + } |
|
355 | + } |
|
356 | + } |
|
357 | + |
|
358 | + if($secretContext && $secretContext->invalidTime > $time) |
|
359 | + { |
|
360 | + return $secretContext; |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + $secretContext = $this->callSecretApi($session,$secretVersion); |
|
365 | + |
|
366 | + if($this->cacheClient) |
|
367 | + { |
|
368 | + $secretContext->cacheKey = $cacheKey; |
|
369 | + $this->cacheClient->setCache($cacheKey,$secretContext); |
|
370 | + } |
|
371 | + |
|
372 | + return $secretContext; |
|
373 | + } |
|
374 | + |
|
375 | + function incrCounter($op,$type,$secretContext,$flush) |
|
376 | + { |
|
377 | + if($op == 1){ |
|
378 | + switch ($type) { |
|
379 | + case 'nick': |
|
380 | + $secretContext->encryptNickNum ++ ; |
|
381 | + break; |
|
382 | + case 'simple': |
|
383 | + $secretContext->encryptSimpleNum ++ ; |
|
384 | + break; |
|
385 | + case 'receiver_name': |
|
386 | + $secretContext->encryptReceiverNameNum ++ ; |
|
387 | + break; |
|
388 | + case 'phone': |
|
389 | + $secretContext->encryptPhoneNum ++ ; |
|
390 | + break; |
|
391 | + default: |
|
392 | + break; |
|
393 | + } |
|
394 | + }else if($op == 2){ |
|
395 | + switch ($type) { |
|
396 | + case 'nick': |
|
397 | + $secretContext->decryptNickNum ++ ; |
|
398 | + break; |
|
399 | + case 'simple': |
|
400 | + $secretContext->decryptSimpleNum ++ ; |
|
401 | + break; |
|
402 | + case 'receiver_name': |
|
403 | + $secretContext->decryptReceiverNameNum ++ ; |
|
404 | + break; |
|
405 | + case 'phone': |
|
406 | + $secretContext->decryptPhoneNum ++ ; |
|
407 | + break; |
|
408 | + default: |
|
409 | + break; |
|
410 | + } |
|
411 | + }else{ |
|
412 | + switch ($type) { |
|
413 | + case 'nick': |
|
414 | + $secretContext->searchNickNum ++ ; |
|
415 | + break; |
|
416 | + case 'simple': |
|
417 | + $secretContext->searchSimpleNum ++ ; |
|
418 | + break; |
|
419 | + case 'receiver_name': |
|
420 | + $secretContext->searchReceiverNameNum ++ ; |
|
421 | + break; |
|
422 | + case 'phone': |
|
423 | + $secretContext->searchPhoneNum ++ ; |
|
424 | + break; |
|
425 | + default: |
|
426 | + break; |
|
427 | + } |
|
428 | + } |
|
429 | + |
|
430 | + if($flush && $this->cacheClient){ |
|
431 | + $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + function flushCounter($secretContext) |
|
436 | + { |
|
437 | + if($this->cacheClient){ |
|
438 | + $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
|
439 | + } |
|
440 | + } |
|
441 | + |
|
442 | + function clearReport($secretContext) |
|
443 | + { |
|
444 | + $secretContext->encryptPhoneNum = 0; |
|
445 | + $secretContext->encryptNickNum = 0; |
|
446 | + $secretContext->encryptReceiverNameNum = 0; |
|
447 | + $secretContext->encryptSimpleNum = 0; |
|
448 | + $secretContext->encryptSearchNum = 0; |
|
449 | + $secretContext->decryptPhoneNum = 0; |
|
450 | + $secretContext->decryptNickNum = 0; |
|
451 | + $secretContext->decryptReceiverNameNum = 0; |
|
452 | + $secretContext->decryptSimpleNum = 0; |
|
453 | + $secretContext->decryptSearchNum = 0; |
|
454 | + $secretContext->searchPhoneNum = 0; |
|
455 | + $secretContext->searchNickNum = 0; |
|
456 | + $secretContext->searchReceiverNameNum = 0; |
|
457 | + $secretContext->searchSimpleNum = 0; |
|
458 | + $secretContext->searchSearchNum = 0; |
|
459 | + $secretContext->lastUploadTime = time(); |
|
460 | + } |
|
461 | + |
|
462 | + function canUpload($secretContext) |
|
463 | + { |
|
464 | + $current = time(); |
|
465 | + if($current - $secretContext->lastUploadTime > 300){ |
|
466 | + return true; |
|
467 | + } |
|
468 | + return false; |
|
469 | + } |
|
470 | + |
|
471 | + /* |
|
472 | 472 | * 上报信息 |
473 | 473 | */ |
474 | - function report($secretContext) |
|
475 | - { |
|
476 | - $request = new TopSdkFeedbackUploadRequest; |
|
477 | - $request->setContent($secretContext->toLogString()); |
|
478 | - |
|
479 | - if(empty($secretContext->session)){ |
|
480 | - $request->setType(APP_SECRET_TYPE); |
|
481 | - }else{ |
|
482 | - $request->setType(APP_USER_SECRET_TYPE); |
|
483 | - } |
|
484 | - |
|
485 | - $response = $this->topClient->execute($request,$secretContext->session); |
|
486 | - if($response->code == 0){ |
|
487 | - return true; |
|
488 | - } |
|
489 | - return false; |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * 获取秘钥,不使用缓存 |
|
494 | - */ |
|
495 | - function callSecretApi($session,$secretVersion) |
|
496 | - { |
|
497 | - $request = new TopSecretGetRequest; |
|
498 | - $request->setRandomNum($this->randomNum); |
|
499 | - if($secretVersion) |
|
500 | - { |
|
501 | - if(intval($secretVersion) < 0 || $session == null){ |
|
502 | - $session = null; |
|
503 | - $secretVersion = -1 * intval($secretVersion < 0); |
|
504 | - } |
|
505 | - $request->setSecretVersion($secretVersion); |
|
506 | - } |
|
474 | + function report($secretContext) |
|
475 | + { |
|
476 | + $request = new TopSdkFeedbackUploadRequest; |
|
477 | + $request->setContent($secretContext->toLogString()); |
|
478 | + |
|
479 | + if(empty($secretContext->session)){ |
|
480 | + $request->setType(APP_SECRET_TYPE); |
|
481 | + }else{ |
|
482 | + $request->setType(APP_USER_SECRET_TYPE); |
|
483 | + } |
|
484 | + |
|
485 | + $response = $this->topClient->execute($request,$secretContext->session); |
|
486 | + if($response->code == 0){ |
|
487 | + return true; |
|
488 | + } |
|
489 | + return false; |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * 获取秘钥,不使用缓存 |
|
494 | + */ |
|
495 | + function callSecretApi($session,$secretVersion) |
|
496 | + { |
|
497 | + $request = new TopSecretGetRequest; |
|
498 | + $request->setRandomNum($this->randomNum); |
|
499 | + if($secretVersion) |
|
500 | + { |
|
501 | + if(intval($secretVersion) < 0 || $session == null){ |
|
502 | + $session = null; |
|
503 | + $secretVersion = -1 * intval($secretVersion < 0); |
|
504 | + } |
|
505 | + $request->setSecretVersion($secretVersion); |
|
506 | + } |
|
507 | 507 | |
508 | - $topSession = $session; |
|
509 | - if($session != null && $session[0] == '_') |
|
510 | - { |
|
511 | - $request->setCustomerUserId(substr($session,1)); |
|
512 | - $topSession = null; |
|
513 | - } |
|
514 | - |
|
515 | - $response = $this->topClient->execute($request,$topSession); |
|
516 | - if($response->code != 0){ |
|
517 | - throw new Exception($response->msg); |
|
518 | - } |
|
519 | - |
|
520 | - $time = time(); |
|
521 | - $secretContext = new SecretContext(); |
|
522 | - $secretContext->maxInvalidTime = $time + intval($response->max_interval); |
|
523 | - $secretContext->invalidTime = $time + intval($response->interval); |
|
524 | - $secretContext->secret = strval($response->secret); |
|
525 | - $secretContext->session = $session; |
|
526 | - if(!empty($response->app_config)){ |
|
527 | - $tmpJson = json_decode($response->app_config); |
|
528 | - $appConfig = array(); |
|
529 | - foreach ($tmpJson as $key => $value){ |
|
530 | - $appConfig[$key] = $value; |
|
531 | - } |
|
532 | - $secretContext->appConfig = $appConfig; |
|
533 | - } |
|
534 | - |
|
535 | - if(empty($session)){ |
|
536 | - $secretContext->secretVersion = -1 * intval($response->secret_version); |
|
537 | - }else{ |
|
538 | - $secretContext->secretVersion = intval($response->secret_version); |
|
539 | - } |
|
540 | - return $secretContext; |
|
541 | - } |
|
542 | - } |
|
508 | + $topSession = $session; |
|
509 | + if($session != null && $session[0] == '_') |
|
510 | + { |
|
511 | + $request->setCustomerUserId(substr($session,1)); |
|
512 | + $topSession = null; |
|
513 | + } |
|
514 | + |
|
515 | + $response = $this->topClient->execute($request,$topSession); |
|
516 | + if($response->code != 0){ |
|
517 | + throw new Exception($response->msg); |
|
518 | + } |
|
519 | + |
|
520 | + $time = time(); |
|
521 | + $secretContext = new SecretContext(); |
|
522 | + $secretContext->maxInvalidTime = $time + intval($response->max_interval); |
|
523 | + $secretContext->invalidTime = $time + intval($response->interval); |
|
524 | + $secretContext->secret = strval($response->secret); |
|
525 | + $secretContext->session = $session; |
|
526 | + if(!empty($response->app_config)){ |
|
527 | + $tmpJson = json_decode($response->app_config); |
|
528 | + $appConfig = array(); |
|
529 | + foreach ($tmpJson as $key => $value){ |
|
530 | + $appConfig[$key] = $value; |
|
531 | + } |
|
532 | + $secretContext->appConfig = $appConfig; |
|
533 | + } |
|
534 | + |
|
535 | + if(empty($session)){ |
|
536 | + $secretContext->secretVersion = -1 * intval($response->secret_version); |
|
537 | + }else{ |
|
538 | + $secretContext->secretVersion = intval($response->secret_version); |
|
539 | + } |
|
540 | + return $secretContext; |
|
541 | + } |
|
542 | + } |
|
543 | 543 | ?> |
544 | 544 | \ No newline at end of file |
@@ -8,16 +8,16 @@ discard block |
||
8 | 8 | |
9 | 9 | class SecurityClient |
10 | 10 | { |
11 | - private $topClient ; |
|
12 | - private $randomNum ; |
|
11 | + private $topClient; |
|
12 | + private $randomNum; |
|
13 | 13 | private $securityUtil; |
14 | 14 | private $cacheClient = null; |
15 | 15 | |
16 | 16 | function __construct($client, $random) |
17 | 17 | { |
18 | 18 | |
19 | - define('APP_SECRET_TYPE','2'); |
|
20 | - define('APP_USER_SECRET_TYPE','3'); |
|
19 | + define('APP_SECRET_TYPE', '2'); |
|
20 | + define('APP_USER_SECRET_TYPE', '3'); |
|
21 | 21 | |
22 | 22 | $this->topClient = $client; |
23 | 23 | $this->randomNum = $random; |
@@ -38,9 +38,9 @@ discard block |
||
38 | 38 | * @see #search(String, String, String, Long) |
39 | 39 | * @return |
40 | 40 | */ |
41 | - function searchPrevious($data,$type,$session = null) |
|
41 | + function searchPrevious($data, $type, $session = null) |
|
42 | 42 | { |
43 | - return $this->searchInner($data,$type,$session,-1); |
|
43 | + return $this->searchInner($data, $type, $session, -1); |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
@@ -49,9 +49,9 @@ discard block |
||
49 | 49 | * @see #search(String, String, String, Long) |
50 | 50 | * @return |
51 | 51 | */ |
52 | - function search($data,$type,$session = null) |
|
52 | + function search($data, $type, $session = null) |
|
53 | 53 | { |
54 | - return $this->searchInner($data,$type,$session,null); |
|
54 | + return $this->searchInner($data, $type, $session, null); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -67,22 +67,22 @@ discard block |
||
67 | 67 | * 秘钥历史版本 |
68 | 68 | * @return |
69 | 69 | */ |
70 | - function searchInner($data, $type, $session,$version) |
|
70 | + function searchInner($data, $type, $session, $version) |
|
71 | 71 | { |
72 | - if(empty($data) || empty($type)){ |
|
72 | + if (empty($data) || empty($type)) { |
|
73 | 73 | return $data; |
74 | 74 | } |
75 | 75 | |
76 | 76 | $secretContext = null; |
77 | 77 | |
78 | - $secretContext = $this->callSecretApiWithCache($session,$version); |
|
79 | - $this->incrCounter(3,$type,$secretContext,true); |
|
78 | + $secretContext = $this->callSecretApiWithCache($session, $version); |
|
79 | + $this->incrCounter(3, $type, $secretContext, true); |
|
80 | 80 | |
81 | - if(empty($secretContext) || empty($secretContext->secret)) { |
|
81 | + if (empty($secretContext) || empty($secretContext->secret)) { |
|
82 | 82 | return $data; |
83 | 83 | } |
84 | 84 | |
85 | - return $this->securityUtil->search($data, $type,$secretContext); |
|
85 | + return $this->securityUtil->search($data, $type, $secretContext); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | |
@@ -90,54 +90,54 @@ discard block |
||
90 | 90 | * 单条数据解密,使用appkey级别公钥 |
91 | 91 | * 非加密数据直接返回原文 |
92 | 92 | */ |
93 | - function decryptPublic($data,$type) |
|
93 | + function decryptPublic($data, $type) |
|
94 | 94 | { |
95 | - return $this->decrypt($data,$type,null); |
|
95 | + return $this->decrypt($data, $type, null); |
|
96 | 96 | } |
97 | 97 | /** |
98 | 98 | * 单条数据解密 |
99 | 99 | * 非加密数据直接返回原文 |
100 | 100 | */ |
101 | - function decrypt($data,$type,$session) |
|
101 | + function decrypt($data, $type, $session) |
|
102 | 102 | { |
103 | - if(empty($data) || empty($type)){ |
|
103 | + if (empty($data) || empty($type)) { |
|
104 | 104 | return $data; |
105 | 105 | } |
106 | - $secretData = $this->securityUtil->getSecretDataByType($data,$type); |
|
107 | - if(empty($secretData)){ |
|
106 | + $secretData = $this->securityUtil->getSecretDataByType($data, $type); |
|
107 | + if (empty($secretData)) { |
|
108 | 108 | return $data; |
109 | 109 | } |
110 | 110 | |
111 | - if($this->securityUtil->isPublicData($data,$type)){ |
|
112 | - $secretContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
113 | - } else { |
|
114 | - $secretContext = $this->callSecretApiWithCache($session,$secretData->secretVersion); |
|
111 | + if ($this->securityUtil->isPublicData($data, $type)) { |
|
112 | + $secretContext = $this->callSecretApiWithCache(null, $secretData->secretVersion); |
|
113 | + }else { |
|
114 | + $secretContext = $this->callSecretApiWithCache($session, $secretData->secretVersion); |
|
115 | 115 | } |
116 | - $this->incrCounter(2,$type,$secretContext,true); |
|
116 | + $this->incrCounter(2, $type, $secretContext, true); |
|
117 | 117 | |
118 | - return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
118 | + return $this->securityUtil->decrypt($data, $type, $secretContext); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
122 | 122 | * 多条数据解密,使用appkey级别公钥 |
123 | 123 | * 非加密数据直接返回原文 |
124 | 124 | */ |
125 | - function decryptBatchPublic($array,$type) |
|
125 | + function decryptBatchPublic($array, $type) |
|
126 | 126 | { |
127 | - if(empty($array) || empty($type)){ |
|
127 | + if (empty($array) || empty($type)) { |
|
128 | 128 | return null; |
129 | 129 | } |
130 | 130 | |
131 | 131 | $result = array(); |
132 | 132 | foreach ($array as $value) { |
133 | - $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
|
134 | - $secretContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
133 | + $secretData = $this->securityUtil->getSecretDataByType($value, $type); |
|
134 | + $secretContext = $this->callSecretApiWithCache(null, $secretData->secretVersion); |
|
135 | 135 | |
136 | - if(empty($secretData)){ |
|
136 | + if (empty($secretData)) { |
|
137 | 137 | $result[$value] = $value; |
138 | - }else{ |
|
139 | - $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
|
140 | - $this->incrCounter(2,$type,$secretContext,true); |
|
138 | + }else { |
|
139 | + $result[$value] = $this->securityUtil->decrypt($value, $type, $secretContext); |
|
140 | + $this->incrCounter(2, $type, $secretContext, true); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | $this->flushCounter($secretContext); |
@@ -150,26 +150,26 @@ discard block |
||
150 | 150 | * 多条数据解密,必须是同一个type和用户,返回结果是 KV结果 |
151 | 151 | * 非加密数据直接返回原文 |
152 | 152 | */ |
153 | - function decryptBatch($array,$type,$session) |
|
153 | + function decryptBatch($array, $type, $session) |
|
154 | 154 | { |
155 | - if(empty($array) || empty($type)){ |
|
155 | + if (empty($array) || empty($type)) { |
|
156 | 156 | return null; |
157 | 157 | } |
158 | 158 | |
159 | 159 | $result = array(); |
160 | 160 | foreach ($array as $value) { |
161 | - $secretData = $this->securityUtil->getSecretDataByType($value,$type); |
|
162 | - if(empty($secretData)){ |
|
161 | + $secretData = $this->securityUtil->getSecretDataByType($value, $type); |
|
162 | + if (empty($secretData)) { |
|
163 | 163 | $result[$value] = $value; |
164 | - } else if($this->securityUtil->isPublicData($value,$type)){ |
|
165 | - $appContext = $this->callSecretApiWithCache(null,$secretData->secretVersion); |
|
166 | - $result[$value] = $this->securityUtil->decrypt($value,$type,$appContext); |
|
167 | - $this->incrCounter(2,$type,$appContext,false); |
|
164 | + }else if ($this->securityUtil->isPublicData($value, $type)) { |
|
165 | + $appContext = $this->callSecretApiWithCache(null, $secretData->secretVersion); |
|
166 | + $result[$value] = $this->securityUtil->decrypt($value, $type, $appContext); |
|
167 | + $this->incrCounter(2, $type, $appContext, false); |
|
168 | 168 | $this->flushCounter($appContext); |
169 | - } else { |
|
170 | - $secretContext = $this->callSecretApiWithCache($session,$secretData->secretVersion); |
|
171 | - $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
|
172 | - $this->incrCounter(2,$type,$secretContext,false); |
|
169 | + }else { |
|
170 | + $secretContext = $this->callSecretApiWithCache($session, $secretData->secretVersion); |
|
171 | + $result[$value] = $this->securityUtil->decrypt($value, $type, $secretContext); |
|
172 | + $this->incrCounter(2, $type, $secretContext, false); |
|
173 | 173 | $this->flushCounter($secretContext); |
174 | 174 | } |
175 | 175 | } |
@@ -180,58 +180,58 @@ discard block |
||
180 | 180 | /** |
181 | 181 | * 使用上一版本秘钥解密,app级别公钥 |
182 | 182 | */ |
183 | - function decryptPreviousPublic($data,$type) |
|
183 | + function decryptPreviousPublic($data, $type) |
|
184 | 184 | { |
185 | - $secretContext = $this->callSecretApiWithCache(null,-1); |
|
186 | - return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
185 | + $secretContext = $this->callSecretApiWithCache(null, -1); |
|
186 | + return $this->securityUtil->decrypt($data, $type, $secretContext); |
|
187 | 187 | } |
188 | 188 | /** |
189 | 189 | * 使用上一版本秘钥解密,一般只用于更新秘钥 |
190 | 190 | */ |
191 | - function decryptPrevious($data,$type,$session) |
|
191 | + function decryptPrevious($data, $type, $session) |
|
192 | 192 | { |
193 | - if($this->securityUtil->isPublicData($data,$type)){ |
|
194 | - $secretContext = $this->callSecretApiWithCache(null,-1); |
|
195 | - } else { |
|
196 | - $secretContext = $this->callSecretApiWithCache($session,-1); |
|
193 | + if ($this->securityUtil->isPublicData($data, $type)) { |
|
194 | + $secretContext = $this->callSecretApiWithCache(null, -1); |
|
195 | + }else { |
|
196 | + $secretContext = $this->callSecretApiWithCache($session, -1); |
|
197 | 197 | } |
198 | - return $this->securityUtil->decrypt($data,$type,$secretContext); |
|
198 | + return $this->securityUtil->decrypt($data, $type, $secretContext); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
202 | 202 | * 加密单条数据,使用app级别公钥 |
203 | 203 | */ |
204 | - function encryptPublic($data,$type,$version = null) |
|
204 | + function encryptPublic($data, $type, $version = null) |
|
205 | 205 | { |
206 | - return $this->encrypt($data,$type,null,$version); |
|
206 | + return $this->encrypt($data, $type, null, $version); |
|
207 | 207 | } |
208 | 208 | /** |
209 | 209 | * 加密单条数据 |
210 | 210 | */ |
211 | - function encrypt($data,$type,$session = null,$version = null) |
|
211 | + function encrypt($data, $type, $session = null, $version = null) |
|
212 | 212 | { |
213 | - if(empty($data) || empty($type)){ |
|
213 | + if (empty($data) || empty($type)) { |
|
214 | 214 | return null; |
215 | 215 | } |
216 | - $secretContext = $this->callSecretApiWithCache($session,null); |
|
217 | - $this->incrCounter(1,$type,$secretContext,true); |
|
216 | + $secretContext = $this->callSecretApiWithCache($session, null); |
|
217 | + $this->incrCounter(1, $type, $secretContext, true); |
|
218 | 218 | |
219 | - return $this->securityUtil->encrypt($data,$type,$version,$secretContext); |
|
219 | + return $this->securityUtil->encrypt($data, $type, $version, $secretContext); |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | /** |
223 | 223 | * 加密多条数据,使用app级别公钥 |
224 | 224 | */ |
225 | - function encryptBatchPublic($array,$type,$version = null) |
|
225 | + function encryptBatchPublic($array, $type, $version = null) |
|
226 | 226 | { |
227 | - if(empty($array) || empty($type)){ |
|
227 | + if (empty($array) || empty($type)) { |
|
228 | 228 | return null; |
229 | 229 | } |
230 | - $secretContext = $this->callSecretApiWithCache(null,null); |
|
230 | + $secretContext = $this->callSecretApiWithCache(null, null); |
|
231 | 231 | $result = array(); |
232 | 232 | foreach ($array as $value) { |
233 | - $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
|
234 | - $this->incrCounter(1,$type,$secretContext,false); |
|
233 | + $result[$value] = $this->securityUtil->encrypt($value, $type, $version, $secretContext); |
|
234 | + $this->incrCounter(1, $type, $secretContext, false); |
|
235 | 235 | } |
236 | 236 | $this->flushCounter($secretContext); |
237 | 237 | |
@@ -241,16 +241,16 @@ discard block |
||
241 | 241 | /** |
242 | 242 | * 加密多条数据,必须是同一个type和用户,返回结果是 KV结果 |
243 | 243 | */ |
244 | - function encryptBatch($array,$type,$session,$version = null) |
|
244 | + function encryptBatch($array, $type, $session, $version = null) |
|
245 | 245 | { |
246 | - if(empty($array) || empty($type)){ |
|
246 | + if (empty($array) || empty($type)) { |
|
247 | 247 | return null; |
248 | 248 | } |
249 | - $secretContext = $this->callSecretApiWithCache($session,null); |
|
249 | + $secretContext = $this->callSecretApiWithCache($session, null); |
|
250 | 250 | $result = array(); |
251 | 251 | foreach ($array as $value) { |
252 | - $result[$value] = $this->securityUtil->encrypt($value,$type,$version,$secretContext); |
|
253 | - $this->incrCounter(1,$type,$secretContext,false); |
|
252 | + $result[$value] = $this->securityUtil->encrypt($value, $type, $version, $secretContext); |
|
253 | + $this->incrCounter(1, $type, $secretContext, false); |
|
254 | 254 | } |
255 | 255 | $this->flushCounter($secretContext); |
256 | 256 | return $result; |
@@ -259,22 +259,22 @@ discard block |
||
259 | 259 | /** |
260 | 260 | * 使用上一版本秘钥加密,使用app级别公钥 |
261 | 261 | */ |
262 | - function encryptPreviousPublic($data,$type) |
|
262 | + function encryptPreviousPublic($data, $type) |
|
263 | 263 | { |
264 | - $secretContext = $this->callSecretApiWithCache(null,-1); |
|
265 | - $this->incrCounter(1,$type,$secretContext,true); |
|
264 | + $secretContext = $this->callSecretApiWithCache(null, -1); |
|
265 | + $this->incrCounter(1, $type, $secretContext, true); |
|
266 | 266 | |
267 | - return $this->securityUtil->encrypt($data,$type,$secretContext->version,$secretContext); |
|
267 | + return $this->securityUtil->encrypt($data, $type, $secretContext->version, $secretContext); |
|
268 | 268 | } |
269 | 269 | /** |
270 | 270 | * 使用上一版本秘钥加密,一般只用于更新秘钥 |
271 | 271 | */ |
272 | - function encryptPrevious($data,$type,$session) |
|
272 | + function encryptPrevious($data, $type, $session) |
|
273 | 273 | { |
274 | - $secretContext = $this->callSecretApiWithCache($session,-1); |
|
275 | - $this->incrCounter(1,$type,$secretContext,true); |
|
274 | + $secretContext = $this->callSecretApiWithCache($session, -1); |
|
275 | + $this->incrCounter(1, $type, $secretContext, true); |
|
276 | 276 | |
277 | - return $this->securityUtil->encrypt($data,$type,$secretContext); |
|
277 | + return $this->securityUtil->encrypt($data, $type, $secretContext); |
|
278 | 278 | } |
279 | 279 | |
280 | 280 | /** |
@@ -282,160 +282,160 @@ discard block |
||
282 | 282 | */ |
283 | 283 | function initSecret($session) |
284 | 284 | { |
285 | - return $this->callSecretApiWithCache($session,null); |
|
285 | + return $this->callSecretApiWithCache($session, null); |
|
286 | 286 | } |
287 | 287 | |
288 | - function buildCacheKey($session,$secretVersion) |
|
288 | + function buildCacheKey($session, $secretVersion) |
|
289 | 289 | { |
290 | - if(empty($session)){ |
|
290 | + if (empty($session)) { |
|
291 | 291 | return $this->topClient->getAppkey(); |
292 | 292 | } |
293 | - if(empty($secretVersion)){ |
|
294 | - return $session ; |
|
293 | + if (empty($secretVersion)) { |
|
294 | + return $session; |
|
295 | 295 | } |
296 | - return $session.'_'.$secretVersion ; |
|
296 | + return $session.'_'.$secretVersion; |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | |
300 | 300 | function generateCustomerSession($userId) |
301 | 301 | { |
302 | - return '_'.$userId ; |
|
302 | + return '_'.$userId; |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | /** |
306 | 306 | * 判断是否是已加密的数据 |
307 | 307 | */ |
308 | - function isEncryptData($data,$type) |
|
308 | + function isEncryptData($data, $type) |
|
309 | 309 | { |
310 | - if(empty($data) || empty($type)){ |
|
310 | + if (empty($data) || empty($type)) { |
|
311 | 311 | return false; |
312 | 312 | } |
313 | - return $this->securityUtil->isEncryptData($data,$type); |
|
313 | + return $this->securityUtil->isEncryptData($data, $type); |
|
314 | 314 | } |
315 | 315 | |
316 | 316 | /** |
317 | 317 | * 判断是否是已加密的数据,数据必须是同一个类型 |
318 | 318 | */ |
319 | - function isEncryptDataArray($array,$type) |
|
319 | + function isEncryptDataArray($array, $type) |
|
320 | 320 | { |
321 | - if(empty($array) || empty($type)){ |
|
321 | + if (empty($array) || empty($type)) { |
|
322 | 322 | return false; |
323 | 323 | } |
324 | - return $this->securityUtil->isEncryptDataArray($array,$type); |
|
324 | + return $this->securityUtil->isEncryptDataArray($array, $type); |
|
325 | 325 | } |
326 | 326 | |
327 | 327 | /** |
328 | 328 | * 判断数组中的数据是否存在密文,存在任何一个返回true,否则false |
329 | 329 | */ |
330 | - function isPartEncryptData($array,$type) |
|
330 | + function isPartEncryptData($array, $type) |
|
331 | 331 | { |
332 | - if(empty($array) || empty($type)){ |
|
332 | + if (empty($array) || empty($type)) { |
|
333 | 333 | return false; |
334 | 334 | } |
335 | - return $this->securityUtil->isPartEncryptData($array,$type); |
|
335 | + return $this->securityUtil->isPartEncryptData($array, $type); |
|
336 | 336 | } |
337 | 337 | |
338 | 338 | /** |
339 | 339 | * 获取秘钥,使用缓存 |
340 | 340 | */ |
341 | - function callSecretApiWithCache($session,$secretVersion) |
|
341 | + function callSecretApiWithCache($session, $secretVersion) |
|
342 | 342 | { |
343 | - if($this->cacheClient) |
|
343 | + if ($this->cacheClient) |
|
344 | 344 | { |
345 | 345 | $time = time(); |
346 | - $cacheKey = $this->buildCacheKey($session,$secretVersion); |
|
346 | + $cacheKey = $this->buildCacheKey($session, $secretVersion); |
|
347 | 347 | $secretContext = $this->cacheClient->getCache($cacheKey); |
348 | 348 | |
349 | - if($secretContext) |
|
349 | + if ($secretContext) |
|
350 | 350 | { |
351 | - if($this->canUpload($secretContext)){ |
|
352 | - if($this->report($secretContext)){ |
|
351 | + if ($this->canUpload($secretContext)) { |
|
352 | + if ($this->report($secretContext)) { |
|
353 | 353 | $this->clearReport($secretContext); |
354 | 354 | } |
355 | 355 | } |
356 | 356 | } |
357 | 357 | |
358 | - if($secretContext && $secretContext->invalidTime > $time) |
|
358 | + if ($secretContext && $secretContext->invalidTime > $time) |
|
359 | 359 | { |
360 | 360 | return $secretContext; |
361 | 361 | } |
362 | 362 | } |
363 | 363 | |
364 | - $secretContext = $this->callSecretApi($session,$secretVersion); |
|
364 | + $secretContext = $this->callSecretApi($session, $secretVersion); |
|
365 | 365 | |
366 | - if($this->cacheClient) |
|
366 | + if ($this->cacheClient) |
|
367 | 367 | { |
368 | 368 | $secretContext->cacheKey = $cacheKey; |
369 | - $this->cacheClient->setCache($cacheKey,$secretContext); |
|
369 | + $this->cacheClient->setCache($cacheKey, $secretContext); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | return $secretContext; |
373 | 373 | } |
374 | 374 | |
375 | - function incrCounter($op,$type,$secretContext,$flush) |
|
375 | + function incrCounter($op, $type, $secretContext, $flush) |
|
376 | 376 | { |
377 | - if($op == 1){ |
|
377 | + if ($op == 1) { |
|
378 | 378 | switch ($type) { |
379 | 379 | case 'nick': |
380 | - $secretContext->encryptNickNum ++ ; |
|
380 | + $secretContext->encryptNickNum++; |
|
381 | 381 | break; |
382 | 382 | case 'simple': |
383 | - $secretContext->encryptSimpleNum ++ ; |
|
383 | + $secretContext->encryptSimpleNum++; |
|
384 | 384 | break; |
385 | 385 | case 'receiver_name': |
386 | - $secretContext->encryptReceiverNameNum ++ ; |
|
386 | + $secretContext->encryptReceiverNameNum++; |
|
387 | 387 | break; |
388 | 388 | case 'phone': |
389 | - $secretContext->encryptPhoneNum ++ ; |
|
389 | + $secretContext->encryptPhoneNum++; |
|
390 | 390 | break; |
391 | 391 | default: |
392 | 392 | break; |
393 | 393 | } |
394 | - }else if($op == 2){ |
|
394 | + }else if ($op == 2) { |
|
395 | 395 | switch ($type) { |
396 | 396 | case 'nick': |
397 | - $secretContext->decryptNickNum ++ ; |
|
397 | + $secretContext->decryptNickNum++; |
|
398 | 398 | break; |
399 | 399 | case 'simple': |
400 | - $secretContext->decryptSimpleNum ++ ; |
|
400 | + $secretContext->decryptSimpleNum++; |
|
401 | 401 | break; |
402 | 402 | case 'receiver_name': |
403 | - $secretContext->decryptReceiverNameNum ++ ; |
|
403 | + $secretContext->decryptReceiverNameNum++; |
|
404 | 404 | break; |
405 | 405 | case 'phone': |
406 | - $secretContext->decryptPhoneNum ++ ; |
|
406 | + $secretContext->decryptPhoneNum++; |
|
407 | 407 | break; |
408 | 408 | default: |
409 | 409 | break; |
410 | 410 | } |
411 | - }else{ |
|
411 | + }else { |
|
412 | 412 | switch ($type) { |
413 | 413 | case 'nick': |
414 | - $secretContext->searchNickNum ++ ; |
|
414 | + $secretContext->searchNickNum++; |
|
415 | 415 | break; |
416 | 416 | case 'simple': |
417 | - $secretContext->searchSimpleNum ++ ; |
|
417 | + $secretContext->searchSimpleNum++; |
|
418 | 418 | break; |
419 | 419 | case 'receiver_name': |
420 | - $secretContext->searchReceiverNameNum ++ ; |
|
420 | + $secretContext->searchReceiverNameNum++; |
|
421 | 421 | break; |
422 | 422 | case 'phone': |
423 | - $secretContext->searchPhoneNum ++ ; |
|
423 | + $secretContext->searchPhoneNum++; |
|
424 | 424 | break; |
425 | 425 | default: |
426 | 426 | break; |
427 | 427 | } |
428 | 428 | } |
429 | 429 | |
430 | - if($flush && $this->cacheClient){ |
|
431 | - $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
|
430 | + if ($flush && $this->cacheClient) { |
|
431 | + $this->cacheClient->setCache($secretContext->cacheKey, $secretContext); |
|
432 | 432 | } |
433 | 433 | } |
434 | 434 | |
435 | 435 | function flushCounter($secretContext) |
436 | 436 | { |
437 | - if($this->cacheClient){ |
|
438 | - $this->cacheClient->setCache($secretContext->cacheKey,$secretContext); |
|
437 | + if ($this->cacheClient) { |
|
438 | + $this->cacheClient->setCache($secretContext->cacheKey, $secretContext); |
|
439 | 439 | } |
440 | 440 | } |
441 | 441 | |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | function canUpload($secretContext) |
463 | 463 | { |
464 | 464 | $current = time(); |
465 | - if($current - $secretContext->lastUploadTime > 300){ |
|
465 | + if ($current-$secretContext->lastUploadTime > 300) { |
|
466 | 466 | return true; |
467 | 467 | } |
468 | 468 | return false; |
@@ -476,14 +476,14 @@ discard block |
||
476 | 476 | $request = new TopSdkFeedbackUploadRequest; |
477 | 477 | $request->setContent($secretContext->toLogString()); |
478 | 478 | |
479 | - if(empty($secretContext->session)){ |
|
479 | + if (empty($secretContext->session)) { |
|
480 | 480 | $request->setType(APP_SECRET_TYPE); |
481 | - }else{ |
|
481 | + }else { |
|
482 | 482 | $request->setType(APP_USER_SECRET_TYPE); |
483 | 483 | } |
484 | 484 | |
485 | - $response = $this->topClient->execute($request,$secretContext->session); |
|
486 | - if($response->code == 0){ |
|
485 | + $response = $this->topClient->execute($request, $secretContext->session); |
|
486 | + if ($response->code == 0) { |
|
487 | 487 | return true; |
488 | 488 | } |
489 | 489 | return false; |
@@ -492,49 +492,49 @@ discard block |
||
492 | 492 | /** |
493 | 493 | * 获取秘钥,不使用缓存 |
494 | 494 | */ |
495 | - function callSecretApi($session,$secretVersion) |
|
495 | + function callSecretApi($session, $secretVersion) |
|
496 | 496 | { |
497 | 497 | $request = new TopSecretGetRequest; |
498 | 498 | $request->setRandomNum($this->randomNum); |
499 | - if($secretVersion) |
|
499 | + if ($secretVersion) |
|
500 | 500 | { |
501 | - if(intval($secretVersion) < 0 || $session == null){ |
|
501 | + if (intval($secretVersion) < 0 || $session == null) { |
|
502 | 502 | $session = null; |
503 | - $secretVersion = -1 * intval($secretVersion < 0); |
|
503 | + $secretVersion = -1*intval($secretVersion < 0); |
|
504 | 504 | } |
505 | 505 | $request->setSecretVersion($secretVersion); |
506 | 506 | } |
507 | 507 | |
508 | 508 | $topSession = $session; |
509 | - if($session != null && $session[0] == '_') |
|
509 | + if ($session != null && $session[0] == '_') |
|
510 | 510 | { |
511 | - $request->setCustomerUserId(substr($session,1)); |
|
511 | + $request->setCustomerUserId(substr($session, 1)); |
|
512 | 512 | $topSession = null; |
513 | 513 | } |
514 | 514 | |
515 | - $response = $this->topClient->execute($request,$topSession); |
|
516 | - if($response->code != 0){ |
|
515 | + $response = $this->topClient->execute($request, $topSession); |
|
516 | + if ($response->code != 0) { |
|
517 | 517 | throw new Exception($response->msg); |
518 | 518 | } |
519 | 519 | |
520 | 520 | $time = time(); |
521 | 521 | $secretContext = new SecretContext(); |
522 | - $secretContext->maxInvalidTime = $time + intval($response->max_interval); |
|
523 | - $secretContext->invalidTime = $time + intval($response->interval); |
|
522 | + $secretContext->maxInvalidTime = $time+intval($response->max_interval); |
|
523 | + $secretContext->invalidTime = $time+intval($response->interval); |
|
524 | 524 | $secretContext->secret = strval($response->secret); |
525 | 525 | $secretContext->session = $session; |
526 | - if(!empty($response->app_config)){ |
|
526 | + if (!empty($response->app_config)) { |
|
527 | 527 | $tmpJson = json_decode($response->app_config); |
528 | 528 | $appConfig = array(); |
529 | - foreach ($tmpJson as $key => $value){ |
|
529 | + foreach ($tmpJson as $key => $value) { |
|
530 | 530 | $appConfig[$key] = $value; |
531 | 531 | } |
532 | 532 | $secretContext->appConfig = $appConfig; |
533 | 533 | } |
534 | 534 | |
535 | - if(empty($session)){ |
|
536 | - $secretContext->secretVersion = -1 * intval($response->secret_version); |
|
537 | - }else{ |
|
535 | + if (empty($session)) { |
|
536 | + $secretContext->secretVersion = -1*intval($response->secret_version); |
|
537 | + }else { |
|
538 | 538 | $secretContext->secretVersion = intval($response->secret_version); |
539 | 539 | } |
540 | 540 | return $secretContext; |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | |
136 | 136 | if(empty($secretData)){ |
137 | 137 | $result[$value] = $value; |
138 | - }else{ |
|
138 | + } else{ |
|
139 | 139 | $result[$value] = $this->securityUtil->decrypt($value,$type,$secretContext); |
140 | 140 | $this->incrCounter(2,$type,$secretContext,true); |
141 | 141 | } |
@@ -391,7 +391,7 @@ discard block |
||
391 | 391 | default: |
392 | 392 | break; |
393 | 393 | } |
394 | - }else if($op == 2){ |
|
394 | + } else if($op == 2){ |
|
395 | 395 | switch ($type) { |
396 | 396 | case 'nick': |
397 | 397 | $secretContext->decryptNickNum ++ ; |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | default: |
409 | 409 | break; |
410 | 410 | } |
411 | - }else{ |
|
411 | + } else{ |
|
412 | 412 | switch ($type) { |
413 | 413 | case 'nick': |
414 | 414 | $secretContext->searchNickNum ++ ; |
@@ -478,7 +478,7 @@ discard block |
||
478 | 478 | |
479 | 479 | if(empty($secretContext->session)){ |
480 | 480 | $request->setType(APP_SECRET_TYPE); |
481 | - }else{ |
|
481 | + } else{ |
|
482 | 482 | $request->setType(APP_USER_SECRET_TYPE); |
483 | 483 | } |
484 | 484 | |
@@ -534,7 +534,7 @@ discard block |
||
534 | 534 | |
535 | 535 | if(empty($session)){ |
536 | 536 | $secretContext->secretVersion = -1 * intval($response->secret_version); |
537 | - }else{ |
|
537 | + } else{ |
|
538 | 538 | $secretContext->secretVersion = intval($response->secret_version); |
539 | 539 | } |
540 | 540 | return $secretContext; |
@@ -1,61 +1,61 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - class SecretContext |
|
4 | - { |
|
5 | - var $secret; |
|
6 | - var $secretVersion; |
|
7 | - var $invalidTime; |
|
8 | - var $maxInvalidTime; |
|
9 | - var $appConfig; |
|
10 | - |
|
11 | - var $cacheKey = ''; |
|
12 | - var $session = ''; |
|
13 | - var $encryptPhoneNum = 0; |
|
14 | - var $encryptNickNum = 0; |
|
15 | - var $encryptReceiverNameNum = 0; |
|
16 | - var $encryptSimpleNum = 0; |
|
17 | - var $encryptSearchNum = 0; |
|
18 | - |
|
19 | - var $decryptPhoneNum = 0; |
|
20 | - var $decryptNickNum = 0; |
|
21 | - var $decryptReceiverNameNum = 0; |
|
22 | - var $decryptSimpleNum = 0; |
|
23 | - var $decryptSearchNum = 0; |
|
24 | - |
|
25 | - var $searchPhoneNum = 0; |
|
26 | - var $searchNickNum = 0; |
|
27 | - var $searchReceiverNameNum = 0; |
|
28 | - var $searchSimpleNum = 0; |
|
29 | - var $searchSearchNum = 0; |
|
30 | - |
|
31 | - var $lastUploadTime; |
|
32 | - |
|
33 | - function toLogString() |
|
34 | - { |
|
35 | - return $this->session.','.$this->encryptPhoneNum.','.$this->encryptNickNum.',' |
|
36 | - .$this->encryptReceiverNameNum.','.$this->encryptSimpleNum.','.$this->encryptSearchNum.',' |
|
37 | - .$this->decryptPhoneNum.','.$this->decryptNickNum.','.$this->decryptReceiverNameNum.',' |
|
38 | - .$this->decryptSimpleNum.','.$this->decryptSearchNum.','.$this->searchPhoneNum.',' |
|
39 | - .$this->searchNickNum.','.$this->searchReceiverNameNum.','.$this->searchSimpleNum.',' |
|
40 | - .$this->searchSearchNum ; |
|
41 | - } |
|
42 | - |
|
43 | - function __construct() |
|
44 | - { |
|
45 | - $this->lastUploadTime = time(); |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - class SecretData |
|
50 | - { |
|
51 | - var $originalValue; |
|
52 | - var $originalBase64Value; |
|
53 | - var $secretVersion; |
|
54 | - var $search; |
|
55 | - |
|
56 | - function __construct() |
|
57 | - { |
|
58 | - |
|
59 | - } |
|
60 | - } |
|
3 | + class SecretContext |
|
4 | + { |
|
5 | + var $secret; |
|
6 | + var $secretVersion; |
|
7 | + var $invalidTime; |
|
8 | + var $maxInvalidTime; |
|
9 | + var $appConfig; |
|
10 | + |
|
11 | + var $cacheKey = ''; |
|
12 | + var $session = ''; |
|
13 | + var $encryptPhoneNum = 0; |
|
14 | + var $encryptNickNum = 0; |
|
15 | + var $encryptReceiverNameNum = 0; |
|
16 | + var $encryptSimpleNum = 0; |
|
17 | + var $encryptSearchNum = 0; |
|
18 | + |
|
19 | + var $decryptPhoneNum = 0; |
|
20 | + var $decryptNickNum = 0; |
|
21 | + var $decryptReceiverNameNum = 0; |
|
22 | + var $decryptSimpleNum = 0; |
|
23 | + var $decryptSearchNum = 0; |
|
24 | + |
|
25 | + var $searchPhoneNum = 0; |
|
26 | + var $searchNickNum = 0; |
|
27 | + var $searchReceiverNameNum = 0; |
|
28 | + var $searchSimpleNum = 0; |
|
29 | + var $searchSearchNum = 0; |
|
30 | + |
|
31 | + var $lastUploadTime; |
|
32 | + |
|
33 | + function toLogString() |
|
34 | + { |
|
35 | + return $this->session.','.$this->encryptPhoneNum.','.$this->encryptNickNum.',' |
|
36 | + .$this->encryptReceiverNameNum.','.$this->encryptSimpleNum.','.$this->encryptSearchNum.',' |
|
37 | + .$this->decryptPhoneNum.','.$this->decryptNickNum.','.$this->decryptReceiverNameNum.',' |
|
38 | + .$this->decryptSimpleNum.','.$this->decryptSearchNum.','.$this->searchPhoneNum.',' |
|
39 | + .$this->searchNickNum.','.$this->searchReceiverNameNum.','.$this->searchSimpleNum.',' |
|
40 | + .$this->searchSearchNum ; |
|
41 | + } |
|
42 | + |
|
43 | + function __construct() |
|
44 | + { |
|
45 | + $this->lastUploadTime = time(); |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + class SecretData |
|
50 | + { |
|
51 | + var $originalValue; |
|
52 | + var $originalBase64Value; |
|
53 | + var $secretVersion; |
|
54 | + var $search; |
|
55 | + |
|
56 | + function __construct() |
|
57 | + { |
|
58 | + |
|
59 | + } |
|
60 | + } |
|
61 | 61 | ?> |
62 | 62 | \ No newline at end of file |
@@ -37,7 +37,7 @@ |
||
37 | 37 | .$this->decryptPhoneNum.','.$this->decryptNickNum.','.$this->decryptReceiverNameNum.',' |
38 | 38 | .$this->decryptSimpleNum.','.$this->decryptSearchNum.','.$this->searchPhoneNum.',' |
39 | 39 | .$this->searchNickNum.','.$this->searchReceiverNameNum.','.$this->searchSimpleNum.',' |
40 | - .$this->searchSearchNum ; |
|
40 | + .$this->searchSearchNum; |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | function __construct() |
@@ -2,34 +2,34 @@ |
||
2 | 2 | |
3 | 3 | class TopSecretGetRequest |
4 | 4 | { |
5 | - private $apiParas = array(); |
|
5 | + private $apiParas = array(); |
|
6 | 6 | |
7 | - public function getApiMethodName() |
|
8 | - { |
|
9 | - return "taobao.top.secret.get"; |
|
10 | - } |
|
7 | + public function getApiMethodName() |
|
8 | + { |
|
9 | + return "taobao.top.secret.get"; |
|
10 | + } |
|
11 | 11 | |
12 | - public function getApiParas() |
|
13 | - { |
|
14 | - return $this->apiParas; |
|
15 | - } |
|
12 | + public function getApiParas() |
|
13 | + { |
|
14 | + return $this->apiParas; |
|
15 | + } |
|
16 | 16 | |
17 | - public function setRandomNum($random){ |
|
18 | - $this->apiParas['random_num'] = $random; |
|
19 | - } |
|
17 | + public function setRandomNum($random){ |
|
18 | + $this->apiParas['random_num'] = $random; |
|
19 | + } |
|
20 | 20 | |
21 | - public function setCustomerUserId($customId){ |
|
22 | - $this->apiParas['customer_user_id'] = $customId; |
|
23 | - } |
|
21 | + public function setCustomerUserId($customId){ |
|
22 | + $this->apiParas['customer_user_id'] = $customId; |
|
23 | + } |
|
24 | 24 | |
25 | - public function setSecretVersion($version){ |
|
26 | - $this->apiParas['secret_version'] = $version; |
|
27 | - } |
|
25 | + public function setSecretVersion($version){ |
|
26 | + $this->apiParas['secret_version'] = $version; |
|
27 | + } |
|
28 | 28 | |
29 | - public function check(){} |
|
29 | + public function check(){} |
|
30 | 30 | |
31 | - public function putOtherTextParam($key, $value) { |
|
32 | - $this->apiParas[$key] = $value; |
|
33 | - $this->$key = $value; |
|
34 | - } |
|
31 | + public function putOtherTextParam($key, $value) { |
|
32 | + $this->apiParas[$key] = $value; |
|
33 | + $this->$key = $value; |
|
34 | + } |
|
35 | 35 | } |
@@ -14,19 +14,19 @@ |
||
14 | 14 | return $this->apiParas; |
15 | 15 | } |
16 | 16 | |
17 | - public function setRandomNum($random){ |
|
17 | + public function setRandomNum($random) { |
|
18 | 18 | $this->apiParas['random_num'] = $random; |
19 | 19 | } |
20 | 20 | |
21 | - public function setCustomerUserId($customId){ |
|
21 | + public function setCustomerUserId($customId) { |
|
22 | 22 | $this->apiParas['customer_user_id'] = $customId; |
23 | 23 | } |
24 | 24 | |
25 | - public function setSecretVersion($version){ |
|
25 | + public function setSecretVersion($version) { |
|
26 | 26 | $this->apiParas['secret_version'] = $version; |
27 | 27 | } |
28 | 28 | |
29 | - public function check(){} |
|
29 | + public function check() {} |
|
30 | 30 | |
31 | 31 | public function putOtherTextParam($key, $value) { |
32 | 32 | $this->apiParas[$key] = $value; |
@@ -1,10 +1,10 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | - include './SecurityClient.php'; |
|
4 | - include './YacCache.php'; |
|
3 | + include './SecurityClient.php'; |
|
4 | + include './YacCache.php'; |
|
5 | 5 | |
6 | 6 | |
7 | - $c = new TopClient; |
|
7 | + $c = new TopClient; |
|
8 | 8 | $c->appkey = '576216'; |
9 | 9 | $c->secretKey = 'd1e44cec2f6c8a2c73342595b711decc'; |
10 | 10 | $c->gatewayUrl = 'https://10.218.128.111/router/rest'; |
@@ -25,8 +25,8 @@ discard block |
||
25 | 25 | |
26 | 26 | if($client->isEncryptData($encryptValue,$type)) |
27 | 27 | { |
28 | - $originalValue = $client->decrypt($encryptValue,$type,$session); |
|
29 | - echo "解密后:".$originalValue.PHP_EOL; |
|
28 | + $originalValue = $client->decrypt($encryptValue,$type,$session); |
|
29 | + echo "解密后:".$originalValue.PHP_EOL; |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | $originalValue = $client->decrypt('~YjW+T6rCmKcc0tGqzWIDaQ==~-113~','nick',$session); |
@@ -37,23 +37,23 @@ discard block |
||
37 | 37 | $client->decryptBatch($secArray,'nick',$session); |
38 | 38 | |
39 | 39 | |
40 | - $typeArray = array('normal','nick','receiver_name'); |
|
40 | + $typeArray = array('normal','nick','receiver_name'); |
|
41 | 41 | |
42 | - $val2 = '啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊看哦【啊啊啊的'; |
|
42 | + $val2 = '啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊看哦【啊啊啊的'; |
|
43 | 43 | |
44 | - foreach ($typeArray as $type2) { |
|
45 | - echo "==============================TOP================================".PHP_EOL; |
|
46 | - $encty2 = $client->encrypt($val2,$type2,$session); |
|
47 | - echo $type2."|明文:".$val2." ---->密文:".$encty2.PHP_EOL; |
|
48 | - if($client->isEncryptData($encty2,$type2)) |
|
49 | - { |
|
50 | - $originalValue = $client->decrypt($encty2,$type2,$session); |
|
51 | - echo "解密后:".$originalValue.PHP_EOL; |
|
44 | + foreach ($typeArray as $type2) { |
|
45 | + echo "==============================TOP================================".PHP_EOL; |
|
46 | + $encty2 = $client->encrypt($val2,$type2,$session); |
|
47 | + echo $type2."|明文:".$val2." ---->密文:".$encty2.PHP_EOL; |
|
48 | + if($client->isEncryptData($encty2,$type2)) |
|
49 | + { |
|
50 | + $originalValue = $client->decrypt($encty2,$type2,$session); |
|
51 | + echo "解密后:".$originalValue.PHP_EOL; |
|
52 | 52 | echo "search明文:".$originalValue." -->".$client->search($originalValue,$type2,$session).PHP_EOL; |
53 | - }else{ |
|
54 | - echo "不是加密数据".PHP_EOL; |
|
55 | - } |
|
56 | - } |
|
53 | + }else{ |
|
54 | + echo "不是加密数据".PHP_EOL; |
|
55 | + } |
|
56 | + } |
|
57 | 57 | |
58 | 58 | $encryptNick = $client->encrypt("xxxuxxxuxxxu","nick"); |
59 | 59 | echo "加密后:".$encryptNick.PHP_EOL; |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | |
12 | 12 | $session = '6101701a21788e0e44743d5f1032ccd5276f00ea6a2d9092050695162'; |
13 | 13 | |
14 | - $client = new SecurityClient($c,'S7/xdg4AD7WooWY7+g11qoBpaVsEkonULDJPEiMcXPE='); |
|
14 | + $client = new SecurityClient($c, 'S7/xdg4AD7WooWY7+g11qoBpaVsEkonULDJPEiMcXPE='); |
|
15 | 15 | $yac = new YacCache; |
16 | 16 | $client->setCacheClient($yac); |
17 | 17 | |
@@ -19,50 +19,50 @@ discard block |
||
19 | 19 | $val = '13834566786'; |
20 | 20 | |
21 | 21 | echo "原文:13834566786".PHP_EOL; |
22 | - $encryptValue = $client->encrypt($val,$type,$session); |
|
22 | + $encryptValue = $client->encrypt($val, $type, $session); |
|
23 | 23 | echo "加密后:".$encryptValue.PHP_EOL; |
24 | - echo "search明文:".$val." -->".$client->search("6786",$type,$session).PHP_EOL; |
|
24 | + echo "search明文:".$val." -->".$client->search("6786", $type, $session).PHP_EOL; |
|
25 | 25 | |
26 | - if($client->isEncryptData($encryptValue,$type)) |
|
26 | + if ($client->isEncryptData($encryptValue, $type)) |
|
27 | 27 | { |
28 | - $originalValue = $client->decrypt($encryptValue,$type,$session); |
|
28 | + $originalValue = $client->decrypt($encryptValue, $type, $session); |
|
29 | 29 | echo "解密后:".$originalValue.PHP_EOL; |
30 | 30 | } |
31 | 31 | |
32 | - $originalValue = $client->decrypt('~YjW+T6rCmKcc0tGqzWIDaQ==~-113~','nick',$session); |
|
32 | + $originalValue = $client->decrypt('~YjW+T6rCmKcc0tGqzWIDaQ==~-113~', 'nick', $session); |
|
33 | 33 | echo "公钥解密后:".$originalValue.PHP_EOL; |
34 | 34 | |
35 | 35 | |
36 | 36 | $secArray = array('~YjW+T6rCmKcc0tGqzWIDaQ==~-113~'); |
37 | - $client->decryptBatch($secArray,'nick',$session); |
|
37 | + $client->decryptBatch($secArray, 'nick', $session); |
|
38 | 38 | |
39 | 39 | |
40 | - $typeArray = array('normal','nick','receiver_name'); |
|
40 | + $typeArray = array('normal', 'nick', 'receiver_name'); |
|
41 | 41 | |
42 | 42 | $val2 = '啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊看哦【啊啊啊的'; |
43 | 43 | |
44 | 44 | foreach ($typeArray as $type2) { |
45 | 45 | echo "==============================TOP================================".PHP_EOL; |
46 | - $encty2 = $client->encrypt($val2,$type2,$session); |
|
46 | + $encty2 = $client->encrypt($val2, $type2, $session); |
|
47 | 47 | echo $type2."|明文:".$val2." ---->密文:".$encty2.PHP_EOL; |
48 | - if($client->isEncryptData($encty2,$type2)) |
|
48 | + if ($client->isEncryptData($encty2, $type2)) |
|
49 | 49 | { |
50 | - $originalValue = $client->decrypt($encty2,$type2,$session); |
|
50 | + $originalValue = $client->decrypt($encty2, $type2, $session); |
|
51 | 51 | echo "解密后:".$originalValue.PHP_EOL; |
52 | - echo "search明文:".$originalValue." -->".$client->search($originalValue,$type2,$session).PHP_EOL; |
|
53 | - }else{ |
|
52 | + echo "search明文:".$originalValue." -->".$client->search($originalValue, $type2, $session).PHP_EOL; |
|
53 | + }else { |
|
54 | 54 | echo "不是加密数据".PHP_EOL; |
55 | 55 | } |
56 | 56 | } |
57 | 57 | |
58 | - $encryptNick = $client->encrypt("xxxuxxxuxxxu","nick"); |
|
58 | + $encryptNick = $client->encrypt("xxxuxxxuxxxu", "nick"); |
|
59 | 59 | echo "加密后:".$encryptNick.PHP_EOL; |
60 | - echo "search明文:xxxuxxxuxxxu -->".$client->search("xxxu","nick").PHP_EOL; |
|
61 | - if($client->isEncryptData($encryptNick,"nick")) |
|
60 | + echo "search明文:xxxuxxxuxxxu -->".$client->search("xxxu", "nick").PHP_EOL; |
|
61 | + if ($client->isEncryptData($encryptNick, "nick")) |
|
62 | 62 | { |
63 | - $originalNick = $client->decryptPublic($encryptNick,"nick"); |
|
63 | + $originalNick = $client->decryptPublic($encryptNick, "nick"); |
|
64 | 64 | echo "解密后:".$originalNick.PHP_EOL; |
65 | - }else{ |
|
65 | + }else { |
|
66 | 66 | echo "不是加密数据 ".$encryptNick.PHP_EOL; |
67 | 67 | } |
68 | 68 | ?> |
69 | 69 | \ No newline at end of file |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | $originalValue = $client->decrypt($encty2,$type2,$session); |
51 | 51 | echo "解密后:".$originalValue.PHP_EOL; |
52 | 52 | echo "search明文:".$originalValue." -->".$client->search($originalValue,$type2,$session).PHP_EOL; |
53 | - }else{ |
|
53 | + } else{ |
|
54 | 54 | echo "不是加密数据".PHP_EOL; |
55 | 55 | } |
56 | 56 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | { |
63 | 63 | $originalNick = $client->decryptPublic($encryptNick,"nick"); |
64 | 64 | echo "解密后:".$originalNick.PHP_EOL; |
65 | - }else{ |
|
65 | + } else{ |
|
66 | 66 | echo "不是加密数据 ".$encryptNick.PHP_EOL; |
67 | 67 | } |
68 | 68 | ?> |
69 | 69 | \ No newline at end of file |
@@ -1,97 +1,97 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - include './SecretContext.php'; |
|
4 | - include './iCache.php'; |
|
5 | - include '../../TopSdk.php'; |
|
3 | + include './SecretContext.php'; |
|
4 | + include './iCache.php'; |
|
5 | + include '../../TopSdk.php'; |
|
6 | 6 | |
7 | - class SecretCounterUtil |
|
8 | - { |
|
9 | - private $topClient ; |
|
10 | - private $cacheClient = null; |
|
7 | + class SecretCounterUtil |
|
8 | + { |
|
9 | + private $topClient ; |
|
10 | + private $cacheClient = null; |
|
11 | 11 | |
12 | - private $counterMap; |
|
12 | + private $counterMap; |
|
13 | 13 | |
14 | - function __construct($client) |
|
15 | - { |
|
16 | - $this->topClient = $client; |
|
14 | + function __construct($client) |
|
15 | + { |
|
16 | + $this->topClient = $client; |
|
17 | 17 | |
18 | - $counterMap = array(); |
|
19 | - } |
|
18 | + $counterMap = array(); |
|
19 | + } |
|
20 | 20 | |
21 | - /* |
|
21 | + /* |
|
22 | 22 | * 如果不走缓存模式,析构即调用API回传统计信息 |
23 | 23 | */ |
24 | - function __destruct() |
|
25 | - { |
|
26 | - if($this->cacheClient == null){ |
|
24 | + function __destruct() |
|
25 | + { |
|
26 | + if($this->cacheClient == null){ |
|
27 | 27 | |
28 | - } |
|
29 | - } |
|
28 | + } |
|
29 | + } |
|
30 | 30 | |
31 | - function report($session) |
|
32 | - { |
|
33 | - $request = new TopSdkFeedbackUploadRequest; |
|
34 | - } |
|
31 | + function report($session) |
|
32 | + { |
|
33 | + $request = new TopSdkFeedbackUploadRequest; |
|
34 | + } |
|
35 | 35 | |
36 | - function setCacheClient($cache) |
|
37 | - { |
|
38 | - $this->cacheClient = $cache; |
|
39 | - } |
|
36 | + function setCacheClient($cache) |
|
37 | + { |
|
38 | + $this->cacheClient = $cache; |
|
39 | + } |
|
40 | 40 | |
41 | - function incrDecrypt($delt,$session,$type) |
|
42 | - { |
|
43 | - $item = getItem($session); |
|
44 | - if($item == null){ |
|
45 | - $item = new SecretCounter(); |
|
46 | - putItem($session,$item); |
|
47 | - } |
|
41 | + function incrDecrypt($delt,$session,$type) |
|
42 | + { |
|
43 | + $item = getItem($session); |
|
44 | + if($item == null){ |
|
45 | + $item = new SecretCounter(); |
|
46 | + putItem($session,$item); |
|
47 | + } |
|
48 | 48 | |
49 | - if($type == "nick"){ |
|
50 | - $item->$decryptNickNum += $delt; |
|
51 | - }else if($type == "receiver_name"){ |
|
52 | - $item->$decryptReceiverNameNum += $delt ; |
|
53 | - }else if($type == "phone"){ |
|
54 | - $item->$decryptPhoneNum += $delt ; |
|
55 | - }else if($type == "simple"){ |
|
56 | - $item->$decryptSimpleNum += $delt ; |
|
57 | - } |
|
58 | - } |
|
49 | + if($type == "nick"){ |
|
50 | + $item->$decryptNickNum += $delt; |
|
51 | + }else if($type == "receiver_name"){ |
|
52 | + $item->$decryptReceiverNameNum += $delt ; |
|
53 | + }else if($type == "phone"){ |
|
54 | + $item->$decryptPhoneNum += $delt ; |
|
55 | + }else if($type == "simple"){ |
|
56 | + $item->$decryptSimpleNum += $delt ; |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - function incrEncrypt($delt,$session,$type) |
|
61 | - { |
|
62 | - $item = getItem($session); |
|
63 | - if($item == null){ |
|
64 | - $item = new SecretCounter(); |
|
65 | - putItem($session,$item); |
|
66 | - } |
|
60 | + function incrEncrypt($delt,$session,$type) |
|
61 | + { |
|
62 | + $item = getItem($session); |
|
63 | + if($item == null){ |
|
64 | + $item = new SecretCounter(); |
|
65 | + putItem($session,$item); |
|
66 | + } |
|
67 | 67 | |
68 | - if($type == "nick"){ |
|
69 | - $item->$encryptNickNum += $delt ; |
|
70 | - }else if($type == "receiver_name"){ |
|
71 | - $item->$encryptReceiverNameNum += $delt ; |
|
72 | - }else if($type == "phone"){ |
|
73 | - $item->$encryptPhoneNum += $delt ; |
|
74 | - }else if($type == "simple"){ |
|
75 | - $item->$encryptSimpleNum += $delt ; |
|
76 | - } |
|
77 | - } |
|
68 | + if($type == "nick"){ |
|
69 | + $item->$encryptNickNum += $delt ; |
|
70 | + }else if($type == "receiver_name"){ |
|
71 | + $item->$encryptReceiverNameNum += $delt ; |
|
72 | + }else if($type == "phone"){ |
|
73 | + $item->$encryptPhoneNum += $delt ; |
|
74 | + }else if($type == "simple"){ |
|
75 | + $item->$encryptSimpleNum += $delt ; |
|
76 | + } |
|
77 | + } |
|
78 | 78 | |
79 | - function getItem($session) |
|
80 | - { |
|
81 | - if($this->cacheClient == null){ |
|
82 | - return $counterMap[$session]; |
|
83 | - }else{ |
|
84 | - return $this->cacheClient->getCache('s_'.$session); |
|
85 | - } |
|
86 | - } |
|
79 | + function getItem($session) |
|
80 | + { |
|
81 | + if($this->cacheClient == null){ |
|
82 | + return $counterMap[$session]; |
|
83 | + }else{ |
|
84 | + return $this->cacheClient->getCache('s_'.$session); |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - function putItem($session,$item) |
|
89 | - { |
|
90 | - if($this->cacheClient == null){ |
|
91 | - $counterMap[$session] = $item; |
|
92 | - }else{ |
|
93 | - $this->cacheClient->setCache('s_'.$session,$item); |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
88 | + function putItem($session,$item) |
|
89 | + { |
|
90 | + if($this->cacheClient == null){ |
|
91 | + $counterMap[$session] = $item; |
|
92 | + }else{ |
|
93 | + $this->cacheClient->setCache('s_'.$session,$item); |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | 97 | ?> |
98 | 98 | \ No newline at end of file |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | |
7 | 7 | class SecretCounterUtil |
8 | 8 | { |
9 | - private $topClient ; |
|
9 | + private $topClient; |
|
10 | 10 | private $cacheClient = null; |
11 | 11 | |
12 | 12 | private $counterMap; |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | */ |
24 | 24 | function __destruct() |
25 | 25 | { |
26 | - if($this->cacheClient == null){ |
|
26 | + if ($this->cacheClient == null) { |
|
27 | 27 | |
28 | 28 | } |
29 | 29 | } |
@@ -38,59 +38,59 @@ discard block |
||
38 | 38 | $this->cacheClient = $cache; |
39 | 39 | } |
40 | 40 | |
41 | - function incrDecrypt($delt,$session,$type) |
|
41 | + function incrDecrypt($delt, $session, $type) |
|
42 | 42 | { |
43 | 43 | $item = getItem($session); |
44 | - if($item == null){ |
|
44 | + if ($item == null) { |
|
45 | 45 | $item = new SecretCounter(); |
46 | - putItem($session,$item); |
|
46 | + putItem($session, $item); |
|
47 | 47 | } |
48 | 48 | |
49 | - if($type == "nick"){ |
|
49 | + if ($type == "nick") { |
|
50 | 50 | $item->$decryptNickNum += $delt; |
51 | - }else if($type == "receiver_name"){ |
|
52 | - $item->$decryptReceiverNameNum += $delt ; |
|
53 | - }else if($type == "phone"){ |
|
54 | - $item->$decryptPhoneNum += $delt ; |
|
55 | - }else if($type == "simple"){ |
|
56 | - $item->$decryptSimpleNum += $delt ; |
|
51 | + }else if ($type == "receiver_name") { |
|
52 | + $item->$decryptReceiverNameNum += $delt; |
|
53 | + }else if ($type == "phone") { |
|
54 | + $item->$decryptPhoneNum += $delt; |
|
55 | + }else if ($type == "simple") { |
|
56 | + $item->$decryptSimpleNum += $delt; |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | |
60 | - function incrEncrypt($delt,$session,$type) |
|
60 | + function incrEncrypt($delt, $session, $type) |
|
61 | 61 | { |
62 | 62 | $item = getItem($session); |
63 | - if($item == null){ |
|
63 | + if ($item == null) { |
|
64 | 64 | $item = new SecretCounter(); |
65 | - putItem($session,$item); |
|
65 | + putItem($session, $item); |
|
66 | 66 | } |
67 | 67 | |
68 | - if($type == "nick"){ |
|
69 | - $item->$encryptNickNum += $delt ; |
|
70 | - }else if($type == "receiver_name"){ |
|
71 | - $item->$encryptReceiverNameNum += $delt ; |
|
72 | - }else if($type == "phone"){ |
|
73 | - $item->$encryptPhoneNum += $delt ; |
|
74 | - }else if($type == "simple"){ |
|
75 | - $item->$encryptSimpleNum += $delt ; |
|
68 | + if ($type == "nick") { |
|
69 | + $item->$encryptNickNum += $delt; |
|
70 | + }else if ($type == "receiver_name") { |
|
71 | + $item->$encryptReceiverNameNum += $delt; |
|
72 | + }else if ($type == "phone") { |
|
73 | + $item->$encryptPhoneNum += $delt; |
|
74 | + }else if ($type == "simple") { |
|
75 | + $item->$encryptSimpleNum += $delt; |
|
76 | 76 | } |
77 | 77 | } |
78 | 78 | |
79 | 79 | function getItem($session) |
80 | 80 | { |
81 | - if($this->cacheClient == null){ |
|
81 | + if ($this->cacheClient == null) { |
|
82 | 82 | return $counterMap[$session]; |
83 | - }else{ |
|
83 | + }else { |
|
84 | 84 | return $this->cacheClient->getCache('s_'.$session); |
85 | 85 | } |
86 | 86 | } |
87 | 87 | |
88 | - function putItem($session,$item) |
|
88 | + function putItem($session, $item) |
|
89 | 89 | { |
90 | - if($this->cacheClient == null){ |
|
90 | + if ($this->cacheClient == null) { |
|
91 | 91 | $counterMap[$session] = $item; |
92 | - }else{ |
|
93 | - $this->cacheClient->setCache('s_'.$session,$item); |
|
92 | + }else { |
|
93 | + $this->cacheClient->setCache('s_'.$session, $item); |
|
94 | 94 | } |
95 | 95 | } |
96 | 96 | } |
@@ -48,11 +48,11 @@ discard block |
||
48 | 48 | |
49 | 49 | if($type == "nick"){ |
50 | 50 | $item->$decryptNickNum += $delt; |
51 | - }else if($type == "receiver_name"){ |
|
51 | + } else if($type == "receiver_name"){ |
|
52 | 52 | $item->$decryptReceiverNameNum += $delt ; |
53 | - }else if($type == "phone"){ |
|
53 | + } else if($type == "phone"){ |
|
54 | 54 | $item->$decryptPhoneNum += $delt ; |
55 | - }else if($type == "simple"){ |
|
55 | + } else if($type == "simple"){ |
|
56 | 56 | $item->$decryptSimpleNum += $delt ; |
57 | 57 | } |
58 | 58 | } |
@@ -67,11 +67,11 @@ discard block |
||
67 | 67 | |
68 | 68 | if($type == "nick"){ |
69 | 69 | $item->$encryptNickNum += $delt ; |
70 | - }else if($type == "receiver_name"){ |
|
70 | + } else if($type == "receiver_name"){ |
|
71 | 71 | $item->$encryptReceiverNameNum += $delt ; |
72 | - }else if($type == "phone"){ |
|
72 | + } else if($type == "phone"){ |
|
73 | 73 | $item->$encryptPhoneNum += $delt ; |
74 | - }else if($type == "simple"){ |
|
74 | + } else if($type == "simple"){ |
|
75 | 75 | $item->$encryptSimpleNum += $delt ; |
76 | 76 | } |
77 | 77 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | { |
81 | 81 | if($this->cacheClient == null){ |
82 | 82 | return $counterMap[$session]; |
83 | - }else{ |
|
83 | + } else{ |
|
84 | 84 | return $this->cacheClient->getCache('s_'.$session); |
85 | 85 | } |
86 | 86 | } |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | { |
90 | 90 | if($this->cacheClient == null){ |
91 | 91 | $counterMap[$session] = $item; |
92 | - }else{ |
|
92 | + } else{ |
|
93 | 93 | $this->cacheClient->setCache('s_'.$session,$item); |
94 | 94 | } |
95 | 95 | } |