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