Passed
Push — v6 ( fe399c...515a76 )
by 光春
06:18
created
src/service/taobao/bin/top/security/TopSdkFeedbackUploadRequest.php 1 patch
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -7,56 +7,56 @@
 block discarded – undo
7 7
  */
8 8
 class TopSdkFeedbackUploadRequest
9 9
 {
10
-	/** 
11
-	 * 具体内容,json形式
12
-	 **/
13
-	private $content;
10
+    /** 
11
+     * 具体内容,json形式
12
+     **/
13
+    private $content;
14 14
 	
15
-	/** 
16
-	 * 1、回传加密信息
17
-	 **/
18
-	private $type;
15
+    /** 
16
+     * 1、回传加密信息
17
+     **/
18
+    private $type;
19 19
 	
20
-	private $apiParas = array();
20
+    private $apiParas = array();
21 21
 	
22
-	public function setContent($content)
23
-	{
24
-		$this->content = $content;
25
-		$this->apiParas["content"] = $content;
26
-	}
22
+    public function setContent($content)
23
+    {
24
+        $this->content = $content;
25
+        $this->apiParas["content"] = $content;
26
+    }
27 27
 
28
-	public function getContent()
29
-	{
30
-		return $this->content;
31
-	}
28
+    public function getContent()
29
+    {
30
+        return $this->content;
31
+    }
32 32
 
33
-	public function setType($type)
34
-	{
35
-		$this->type = $type;
36
-		$this->apiParas["type"] = $type;
37
-	}
33
+    public function setType($type)
34
+    {
35
+        $this->type = $type;
36
+        $this->apiParas["type"] = $type;
37
+    }
38 38
 
39
-	public function getType()
40
-	{
41
-		return $this->type;
42
-	}
39
+    public function getType()
40
+    {
41
+        return $this->type;
42
+    }
43 43
 
44
-	public function getApiMethodName()
45
-	{
46
-		return "taobao.top.sdk.feedback.upload";
47
-	}
44
+    public function getApiMethodName()
45
+    {
46
+        return "taobao.top.sdk.feedback.upload";
47
+    }
48 48
 	
49
-	public function getApiParas()
50
-	{
51
-		return $this->apiParas;
52
-	}
49
+    public function getApiParas()
50
+    {
51
+        return $this->apiParas;
52
+    }
53 53
 	
54
-	public function check()
55
-	{
56
-	}
54
+    public function check()
55
+    {
56
+    }
57 57
 	
58
-	public function putOtherTextParam($key, $value) {
59
-		$this->apiParas[$key] = $value;
60
-		$this->$key = $value;
61
-	}
58
+    public function putOtherTextParam($key, $value) {
59
+        $this->apiParas[$key] = $value;
60
+        $this->$key = $value;
61
+    }
62 62
 }
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/iCache.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -4,11 +4,11 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/YacCache.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -4,35 +4,35 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/SecurityUtil.php 1 patch
Indentation   +557 added lines, -557 removed lines patch added patch discarded remove patch
@@ -1,589 +1,589 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/SecurityClient.php 1 patch
Indentation   +535 added lines, -535 removed lines patch added patch discarded remove patch
@@ -1,543 +1,543 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/SecretContext.php 1 patch
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -1,61 +1,61 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/SecretGetRequest.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -2,34 +2,34 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/SecurityTest.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -1,10 +1,10 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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;
Please login to merge, or discard this patch.
src/service/taobao/bin/top/security/SecretCounterUtil.php 1 patch
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -1,97 +1,97 @@
 block discarded – undo
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
Please login to merge, or discard this patch.