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
|
|
|
namespace EasyWeChat\Kernel\Support; |
13
|
|
|
|
14
|
|
|
/* |
15
|
|
|
* helpers. |
16
|
|
|
* |
17
|
|
|
* @author overtrue <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generate a signature. |
22
|
|
|
* |
23
|
|
|
* @param array $attributes |
24
|
|
|
* @param string $key |
25
|
|
|
* @param string $encryptMethod |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
function generate_sign(array $attributes, $key, $encryptMethod = 'md5') |
30
|
|
|
{ |
31
|
|
|
ksort($attributes); |
32
|
|
|
|
33
|
|
|
$attributes['key'] = $key; |
34
|
|
|
|
35
|
|
|
return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $signType |
40
|
|
|
* @param string $secretKey |
41
|
|
|
* |
42
|
|
|
* @return \Closure|string |
43
|
|
|
*/ |
44
|
|
|
function get_encrypt_method(string $signType, string $secretKey = '') { |
45
|
|
|
if ('HMAC-SHA256' === $signType) { |
46
|
|
|
return function ($str) use ($secretKey) { |
47
|
|
|
return hash_hmac('sha256', $str, $secretKey); |
48
|
|
|
}; |
49
|
|
|
} |
50
|
|
|
return 'md5'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get client ip. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
function get_client_ip() |
59
|
|
|
{ |
60
|
|
|
if (!empty($_SERVER['REMOTE_ADDR'])) { |
61
|
|
|
$ip = $_SERVER['REMOTE_ADDR']; |
62
|
|
|
} else { |
63
|
|
|
// for php-cli(phpunit etc.) |
64
|
|
|
$ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get current server ip. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
function get_server_ip() |
76
|
|
|
{ |
77
|
|
|
if (!empty($_SERVER['SERVER_ADDR'])) { |
78
|
|
|
$ip = $_SERVER['SERVER_ADDR']; |
79
|
|
|
} elseif (!empty($_SERVER['SERVER_NAME'])) { |
80
|
|
|
$ip = gethostbyname($_SERVER['SERVER_NAME']); |
81
|
|
|
} else { |
82
|
|
|
// for php-cli(phpunit etc.) |
83
|
|
|
$ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return current url. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
function current_url() |
95
|
|
|
{ |
96
|
|
|
$protocol = 'http://'; |
97
|
|
|
|
98
|
|
|
if ((!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) || ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? 'http') === 'https') { |
99
|
|
|
$protocol = 'https://'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return random string. |
107
|
|
|
* |
108
|
|
|
* @param string $length |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
function str_random($length) |
113
|
|
|
{ |
114
|
|
|
return Str::random($length); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $content |
119
|
|
|
* @param string $publicKey |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
function rsa_public_encrypt($content, $publicKey) |
124
|
|
|
{ |
125
|
|
|
$encrypted = ''; |
126
|
|
|
openssl_public_encrypt($content, $encrypted, openssl_pkey_get_public($publicKey), OPENSSL_PKCS1_OAEP_PADDING); |
127
|
|
|
|
128
|
|
|
return base64_encode($encrypted); |
129
|
|
|
} |
130
|
|
|
|