-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.php
More file actions
executable file
·378 lines (352 loc) · 9.82 KB
/
helper.php
File metadata and controls
executable file
·378 lines (352 loc) · 9.82 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/*
* Copyright (c) 2023. Ankio. All Rights Reserved.
*/
/**
* File helper.php
* Created By ankio.
* Date : 2022/11/9
* Time : 12:47
* Description : 助手函数
*/
use cleanphp\App;
use cleanphp\base\Argument;
use cleanphp\base\Dump;
use cleanphp\base\Route;
use cleanphp\closure\Exceptions\PhpVersionNotSupportedException;
use cleanphp\closure\SerializableClosure;
use cleanphp\file\Log;
use cleanphp\process\Async;
use cleanphp\process\AsyncObject;
/**
* 获取静态文件的content-type
* @param string $filename
* @return string
*/
function file_type(string $filename): string
{
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
"woff2" => 'font/woff2',
"ttf" => 'font/ttf',
);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$ext = strtolower($extension);
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
return 'application/octet-stream';
}
/**
* 输出所有变量
* @param ...$args
* @return void
*/
function dumps(...$args)
{
$line = debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line'] . "\n";
foreach ($args as $v) {
dump($v, false, $line);
}
App::exit("调用输出命令退出");
}
/**
* 输出变量内容
* @param mixed $var 预输出的变量名
* @param false $exit 输出变量后是否退出进程
* @param string|null $line
*/
function dump(mixed $var, bool $exit = false, string $line = null): void
{
if (!App::$debug) return;//不是调试模式就直接返回
if ($line === null)
$line = debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line'] . "\n";
if (App::$cli) {
echo $line;
var_dump($var);
if ($exit) {
App::exit("调用输出命令退出");
}
return;
}
$tpl = "";
if ($line !== "") {
$tpl .= <<<EOF
<style>pre {display: block;padding: 10px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;word-break: break-all;word-wrap: break-word;background-color:#f5f5f5;border: 1px solid #ccc;border-radius: 4px;}</style><div style="text-align: left">
<pre class="xdebug-var-dump" dir="ltr"><small>{$line}</small>\r\n
EOF;
} else {
$tpl .= <<<EOF
<style>pre {display: block;padding: 10px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;word-break: break-all;word-wrap: break-word;background-color:#f5f5f5;border: 1px solid #ccc;border-radius: 4px;}</style><div style="text-align: left"><pre class="xdebug-var-dump" dir="ltr">
EOF;
}
$dump = new Dump();
$tpl .= $dump->dumpType($var);
$tpl .= '</pre></div>';
echo $tpl;
if ($exit) {
App::exit("调用输出命令退出");
}
}
/**
* 传入样例类型,对目标进行类型转换
* @param $sample mixed 样例
* @param $data mixed 需要转换的类型
* @return bool|float|int|mixed|string
*/
function parse_type($sample, $data)
{
if (is_array($data)) return $data;
elseif (is_int($sample)) return intval($data);
elseif (is_string($sample)) return strval($data);
elseif (is_bool($sample)) return boolval($data);
elseif (is_float($sample)) return floatval($data);
elseif (is_double($sample)) return doubleval($data);
return $data;
}
/**
* 从get参数中获取
* @param ?string $key
* @param mixed $default
* @return bool|float|int|mixed|string|null
*/
function get(string $key = null, $default = null)
{
return Argument::get($key, $default);
}
/**
* 从post参数中获取
* @param ?string $key
* @param mixed $default
* @return bool|float|int|mixed|string|null
*/
function post(string $key = null, $default = null)
{
return Argument::post($key, $default);
}
/**
* 从所有参数中获取
* @param ?string $key
* @param mixed $default
* @return bool|float|int|mixed|string|null
*/
function arg(string $key = null, $default = null)
{
return Argument::arg($key, $default);
}
/**
* 生成符合路由规则的URL
* @param string $m 模块名
* @param string $c 控制器名
* @param string $a 方法
* @param array $param 参数数组
*
* @return string
*/
function url(string $m = 'index', string $c = 'main', string $a = 'index', array $param = []): string
{
return Route::url(...func_get_args());
}
//闭包序列化
function traversalClosure($array, $callback)
{
if ($array instanceof Closure) {
$callback($array);
return $array;
}
if (is_string($array) && str_starts_with($array, "__SerializableClosure__")) {
$item = substr($array, 23);
$callback($item);
return $array;
}
if (is_array($array) || (is_object($array))) {
foreach ($array as &$item) {
if (is_array($item) || (is_object($item) && !$item instanceof Closure)) {
$item = traversalClosure($item, $callback);
} elseif ($item instanceof Closure) {
$callback($item);
} elseif (is_string($item) && str_starts_with($item, "__SerializableClosure__")) {
$item = substr($item, 23);
$callback($item);
}
}
}
return $array;
}
/**
* Serialize
*
* @param mixed $data
* @return string
*/
function __serialize(mixed $data): string
{
return serialize(traversalClosure($data, function (&$item) {
try {
$item = "__SerializableClosure__" . serialize(new SerializableClosure($item));
} catch (PhpVersionNotSupportedException $e) {
Log::record("序列化失败", $e->getMessage());
$item = "";
}
}));
}
/**
* Unserialize
*
* @param string|null $data
* @return mixed
*/
function __unserialize(?string $data): mixed
{
if (empty($data)) return null;
App::$debug && Log::record("__unserialize", $data);
$result = unserialize($data);
traversalClosure($result, function (&$item) {
$item = unserialize($item)->getClosure();
});
return $result;
}
/**
* 启动一个异步任务
* @param Closure $function 任务函数
* @param int $timeout 异步任务的最长运行时间,单位为秒
* @return AsyncObject|null
*/
function go(Closure $function, int $timeout = 300): ?AsyncObject
{
if (App::$cli) return null;
return Async::start($function, $timeout);
}
/**
* 等待任务执行完成
* @param ...$objects
* @return void
*/
function wait(...$objects)
{
Async::wait(...$objects);
}
/**
* 不区分大小写的in_array
* @param $value
* @param $array
* @return bool
*/
function in_array_case($value, $array): bool
{
if (!is_array($array)) return false;
return in_array(strtolower($value), array_map('strtolower', $array));
}
/**
* 获取随机字符串
* @param int $length 字符串长度
* @param bool $upper 是否包含大写字母
* @param bool $lower 是否包含小写字母
* @param bool $number 是否包含数字
* @return string
*/
function rand_str(int $length = 8, bool $upper = true, bool $lower = true, bool $number = true): string
{
$charsList = [
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'0123456789',
];
$chars = "";
if ($upper) {
$chars .= $charsList[1];
}
if ($lower) {
$chars .= $charsList[0];
}
if ($number) {
$chars .= $charsList[2];
}
if ($chars === "") {
$chars = $charsList[2];
}
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $chars[mt_rand(0, strlen($chars) - 1)];
}
return $password;
}
/**
* 过滤可能存在危险的字符
* @param $input
* @return array|string|null
*/
function filter_characters($input): array|string|null
{
return preg_replace('/[^\x{4e00}-\x{9fa5}a-zA-Z0-9_.\-]/u', '', $input);
}
/**
* 检查一个文件名是否为文件,这里处理了len最长的问题
* @param $file
* @return bool
*/
function is_file_exists($file): bool
{
if (str_contains($file, "\n") || strlen($file) > 1000 || !is_file($file)) {
return false;
}
return true;
}
/**
* 字符串转换utf-8
* @param $text
* @param string $encode_code
* @return string
*/
function convert($text, string $encode_code = "UTF-8"): string
{
$encode = mb_detect_encoding($text, mb_detect_order());
if ($encode !== $encode_code)
$text = mb_convert_encoding($text, $encode_code, $encode);
return $text;
}