-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunctions.php
149 lines (129 loc) · 3.84 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
use axios\tools\HMac;
use axios\tools\Path;
use axios\tools\SM3;
use axios\tools\UUID;
use axios\tools\XMLParser;
if (!function_exists('render_str')) {
function render_str(string $template, array $params, string $left_tag = '${', string $right_tag = '}'): string
{
foreach ($params as $name => $value) {
$template = str_replace($left_tag . $name . $right_tag, $value, $template);
}
return $template;
}
}
if (!function_exists('exec_command')) {
function exec_command($cmd, ?string $cwd = null)
{
if (null !== $cwd) {
$cmd = 'cd ' . $cwd . ' && ' . $cmd;
}
while (@ob_end_flush()) {
continue;
} // end all output buffers if any
$proc = popen($cmd, 'r');
while (!feof($proc)) {
echo fread($proc, 4096);
@flush();
}
}
}
if (!function_exists('hmac')) {
function hmac(string $algorithm, string $data = '', string $secret = '', bool $raw_output = false): string
{
$hamc = new HMac();
$res = $hamc->count($algorithm, $data, $secret, $raw_output);
unset($hamc);
return $res;
}
}
if (!function_exists('halt') && function_exists('dump')) {
function halt(...$args)
{
dump(...$args);
exit();
}
}
if (!function_exists('sm3')) {
function sm3(string $str, bool $raw_output = false): string
{
$sm3 = new SM3();
$sm3->encode($str);
return $raw_output ? $sm3->getBinary() : $sm3->getHex();
}
}
if (!function_exists('sm3_file')) {
function sm3_file(string $filepath, bool $raw_output = false): string
{
$sm3 = new SM3();
$sm3->encodeFile($filepath);
return $raw_output ? $sm3->getBinary() : $sm3->getHex();
}
}
if (!function_exists('xml_encode')) {
function xml_encode(array $data, $root_node = 'data', $root_attr = [], $item_node = 'item', $item_key = 'id', $encoding = 'utf-8'): string
{
return XMLParser::encode($data, $root_node, $root_attr, $item_node, $item_key, $encoding);
}
}
if (!function_exists('xml_decode')) {
function xml_decode(string $xml_string): array
{
return XMLParser::decode($xml_string);
}
}
if (!function_exists('uuid')) {
function uuid(string $salt = ''): string
{
$uuid = new UUID($salt);
$str = $uuid->v2();
unset($uuid);
return $str;
}
}
if (!function_exists('path_join')) {
function path_join(string ...$paths): string
{
return Path::join(...$paths);
}
}
if (!function_exists('client_ip')) {
/*
* get the IP address of the client
*
* @param int $type 0 => return IP string; 1=> return IP number
* @param bool $adv advance mode
*
* @return mixed
*/
function client_ip(int $type = 0, bool $advance = false)
{
$type = $type ? 1 : 0;
static $ip = null;
if (null !== $ip) {
return $ip[$type];
}
if ($advance) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos) {
unset($arr[$pos]);
}
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP address legitimacy verification
$long = sprintf('%u', ip2long((string) $ip));
$ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
return $ip[$type];
}
}