1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* helpers.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @see https://github.com/overtrue |
19
|
|
|
* @see http://overtrue.me |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace EasyWeChat\Payment; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Generate a signature. |
26
|
|
|
* |
27
|
|
|
* @param array $attributes |
28
|
|
|
* @param string $key |
29
|
|
|
* @param string $encryptMethod |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
function generate_sign(array $attributes, $key, $encryptMethod = 'md5') |
34
|
|
|
{ |
35
|
20 |
|
ksort($attributes); |
36
|
|
|
|
37
|
20 |
|
$attributes['key'] = $key; |
38
|
|
|
|
39
|
20 |
|
return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))])); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get client ip. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
function get_client_ip() |
|
|
|
|
48
|
|
|
{ |
49
|
1 |
|
if (!empty($_SERVER['REMOTE_ADDR'])) { |
50
|
|
|
$ip = $_SERVER['REMOTE_ADDR']; |
51
|
|
|
} else { |
52
|
|
|
// for php-cli(phpunit etc.) |
53
|
1 |
|
$ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get current server ip. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
function get_server_ip() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (!empty($_SERVER['SERVER_ADDR'])) { |
67
|
|
|
$ip = $_SERVER['SERVER_ADDR']; |
68
|
|
|
} elseif (!empty($_SERVER['SERVER_NAME'])) { |
69
|
|
|
$ip = gethostbyname($_SERVER['SERVER_NAME']); |
70
|
|
|
} else { |
71
|
|
|
// for php-cli(phpunit etc.) |
72
|
|
|
$ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; |
76
|
|
|
} |
77
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: