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
|
|
|
{ |
46
|
|
|
if ('HMAC-SHA256' === $signType) { |
47
|
|
|
return function ($str) use ($secretKey) { |
48
|
|
|
return hash_hmac('sha256', $str, $secretKey); |
49
|
|
|
}; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return 'md5'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get client ip. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
function get_client_ip() |
61
|
|
|
{ |
62
|
|
|
if (!empty($_SERVER['REMOTE_ADDR'])) { |
63
|
|
|
$ip = $_SERVER['REMOTE_ADDR']; |
64
|
|
|
} else { |
65
|
|
|
// for php-cli(phpunit etc.) |
66
|
|
|
$ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get current server ip. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
function get_server_ip() |
78
|
|
|
{ |
79
|
|
|
if (!empty($_SERVER['SERVER_ADDR'])) { |
80
|
|
|
$ip = $_SERVER['SERVER_ADDR']; |
81
|
|
|
} elseif (!empty($_SERVER['SERVER_NAME'])) { |
82
|
|
|
$ip = gethostbyname($_SERVER['SERVER_NAME']); |
83
|
|
|
} else { |
84
|
|
|
// for php-cli(phpunit etc.) |
85
|
|
|
$ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return current url. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
function current_url() |
97
|
|
|
{ |
98
|
|
|
$protocol = 'http://'; |
99
|
|
|
|
100
|
|
|
if ((!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) || ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? 'http') === 'https') { |
101
|
|
|
$protocol = 'https://'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return random string. |
109
|
|
|
* |
110
|
|
|
* @param string $length |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
function str_random($length) |
115
|
|
|
{ |
116
|
|
|
return Str::random($length); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $content |
121
|
|
|
* @param string $publicKey |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
function rsa_public_encrypt($content, $publicKey) |
126
|
|
|
{ |
127
|
|
|
$encrypted = ''; |
128
|
|
|
openssl_public_encrypt($content, $encrypted, openssl_pkey_get_public($publicKey), OPENSSL_PKCS1_OAEP_PADDING); |
129
|
|
|
|
130
|
|
|
return base64_encode($encrypted); |
131
|
|
|
} |
132
|
|
|
|