-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathcommon.php
493 lines (434 loc) · 13 KB
/
common.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
// 应用公共文件
use app\model\User;
use think\facade\Cache;
use think\helper\Str;
require dirname(__DIR__) . "/plugin/common.php";
function template_path_get(): string
{
return app()->getRootPath() . config("view.view_dir_name") . '/index/' . config_get('global.template') . DIRECTORY_SEPARATOR;
}
function msg($status = "ok", $message = "success", $data = [])
{
return json([
"status" => $status,
"message" => $message,
"data" => $data,
]);
}
function success($data = [], $message = "success")
{
return msg('ok', $message, $data);
}
function error($message = "error", $data = [])
{
return msg('error', $message, $data);
}
function is_login()
{
return User::isLogin();
}
function get_user()
{
return User::getUser();
}
function get_username()
{
return get_user()->username;
}
function is_admin($user = null)
{
return User::isAdmin();
}
function get_master_path($path = '')
{
$path = trim($path, '\\//');
$adminPath = trim(config_get('global.admin_path'), '\\//');
if (!empty($path)) {
$adminPath .= "/$path";
}
return "/$adminPath";
}
function reset_opcache()
{
if (function_exists('opcache_reset')) opcache_reset();
}
function clear_cache($flag = false)
{
Cache::clear();
if ($flag) {
reset_opcache();
}
}
function aoaostar_get($url, $headers = [])
{
$headers = array_merge([
'User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
], $headers);
// 创建一个新 cURL 资源
$curl = curl_init();
// 设置URL和相应的选项
// 需要获取的 URL 地址
curl_setopt($curl, CURLOPT_URL, $url);
#启用时会将头文件的信息作为数据流输出。
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_PROXY, \think\facade\Env::get('app.proxy'));
#在尝试连接时等待的秒数。设置为 0,则无限等待。
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
#允许 cURL 函数执行的最长秒数。
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
#关闭ssl
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
#TRUE 将 curl_exec获取的信息以字符串返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
#设置header
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// 跟踪重定向
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// 抓取 URL 并把它传递给浏览器
$res = curl_exec($curl);
// 关闭 cURL 资源,并且释放系统资源
if ($res === false) {
return "CURL Error:" . curl_error($curl);
}
curl_close($curl);
return $res;
}
function aoaostar_post(
$url,
$post,
$headers = []
)
{
// 初始化
$headers = array_merge([
'User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
], $headers);
// 创建一个新 cURL 资源
$curl = curl_init();
// 设置URL和相应的选项
// 需要获取的 URL 地址
curl_setopt($curl, CURLOPT_URL, $url);
#启用时会将头文件的信息作为数据流输出。
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_PROXY, \think\facade\Env::get('app.proxy'));
#设置头部信息
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
#在尝试连接时等待的秒数。设置为 0,则无限等待。
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
#允许 cURL 函数执行的最长秒数。
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
#设置请求信息
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
#关闭ssl
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
#TRUE 将 curl_exec获取的信息以字符串返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 抓取 URL 并把它传递给浏览器
$return = curl_exec($curl);
if ($return === false) {
return 'CURL Error:' . curl_error($curl);
}
curl_close($curl);
return $return;
}
function aoaostar_curl($url, $put, $headers = [],
$type = 'PUT'
)
{
$headers = array_merge([
'User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
], $headers);
// 创建一个新 cURL 资源
$curl = curl_init();
// 设置URL和相应的选项
// 需要获取的 URL 地址
curl_setopt($curl, CURLOPT_URL, $url);
#启用时会将头文件的信息作为数据流输出。
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_PROXY, \think\facade\Env::get('app.proxy'));
#设置头部信息
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
#在尝试连接时等待的秒数。设置为 0,则无限等待。
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
#允许 cURL 函数执行的最长秒数。
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
#设置请求信息
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($curl, CURLOPT_POSTFIELDS, $put); //定义提交的数据
#关闭ssl
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
#TRUE 将 curl_exec获取的信息以字符串返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 抓取 URL 并把它传递给浏览器
$return = curl_exec($curl);
if ($return === false) {
return "CURL Error:" . curl_error($curl);
}
curl_close($curl);
return $return;
}
function unzip($filepath, $filename)
{
if (!file_exists($filepath)) {
return false;
}
$zip = new ZipArchive;
if ($zip->open($filepath) === true) {
$zip->extractTo($filename);
$zip->close();
return true;
}
return false;
}
//多维转一维数组
function multi2one($data, $dir = '', $step = '')
{
$list = [];
foreach ($data as $k => $v) {
if (is_array($v)) {
$list = array_merge($list, multi2one($v, $dir . $step . $k, $step));
} else {
$list[] = ltrim($dir . $step . $v, '\\/');
}
}
return $list;
}
function tree_relative($dir)
{
if (!is_dir($dir)) {
return [basename($dir)];
}
$arr = [];
$scandir = scandir($dir);
foreach ($scandir as $v) {
if ($v != '.' && $v != '..') {
if (is_dir("$dir/$v")) {
$arr[$v] = tree_relative("$dir/$v");
} else {
$arr[] = $v;
}
}
}
return $arr;
}
function copy_dir($src, $target)
{
if (!is_dir($target)) {
mkdir($target, 0755, true);
}
foreach (glob($src . '/*') as $filename) {
$targetFilename = $target . '/' . basename($filename);
if (is_dir($filename)) {
// 如果是目录,递归合并子目录下的文件。
copy_dir($filename, $targetFilename);
} elseif (is_file($filename)) {
copy($filename, $targetFilename);
}
}
}
function del_tree($dir)
{
if (!file_exists($dir)) {
return true;
}
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? del_tree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
function parse_github_url($url)
{
$parse_url = parse_url($url);
$explode = explode('/', $parse_url['path']);
$explode = array_values(array_filter($explode));
$github = new stdClass();
$github->owner = $explode[0];
$github->repo = $explode[1];
if (count($explode) <= 2) {
$github->action = 'tree';
$github->branch = 'master';
$github->path = '';
} else {
$github->action = $explode[2];
$github->branch = $explode[3];
$github->path = implode('/', array_slice($explode, 4));
}
return $github;
}
function tree_github($github, $type = 'tree')
{
$arr = [];
$get = aoaostar_get("https://api.github.com/repos/$github->owner/$github->repo/contents/$github->path?ref=$github->branch");
$res = json_decode($get);
if (empty($res)) {
throw new Exception('请求失败');
}
foreach ($res as $v) {
if (empty($v->download_url)) {
$tmp = clone $github;
$tmp->path = "$tmp->path/$v->name";
if ($type == 'tree') {
$arr[$v->name] = tree_github($tmp);
} else {
$arr = array_merge($arr, tree_github($tmp, $type));
}
} else {
if ($type == 'tree') {
$arr[] = $v->name;
} else {
// $arr["$github->path/$v->name"] = $v->download_url;
$arr[$v->path] = $v->download_url;
}
}
}
return $arr;
}
function config_get($key, $default = '')
{
$model = \app\model\Config::getByKey($key);
if (isset($model->value)) {
return $model->value ?: $default;
}
$arr = [];
foreach ($model as $v) {
$arr[substr($v->key, strlen($key))] = $v->value;
}
return $arr ?: ($default ?: []);
}
function config_set($key, $value)
{
$model = \app\model\Config::getByKey($key);
$model->key = $key;
$model->value = $value;
return $model->save();
}
function get_version()
{
return VERSION;
}
function format_date($timestamp = null)
{
if ($timestamp === null) {
$timestamp = time();
}
return date('Y-m-d H:i:s', $timestamp);
}
//当前命名空间的包名
function base_space_name($space)
{
$str_replace = str_replace('\\', '/', $space);
return basename($str_replace);
}
function cdnjs_cdn($file = '')
{
$config_get = config_get('cdn.cdnjs', 'https://cdn.staticfile.org');
return $config_get . $file;
}
function npm_cdn($file = '')
{
$config_get = config_get('cdn.npm', 'https://cdn.jsdelivr.net/npm');
return $config_get . $file;
}
function captcha_api()
{
return (string)url('/api/captcha');
}
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
if (!function_exists('str_starts_with')) {
function str_starts_with($str, $start)
{
return (@substr_compare($str, $start, 0, strlen($start)) == 0);
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool
{
$needle_len = strlen($needle);
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, -$needle_len));
}
}
if (!function_exists('is_valid_url')) {
function is_valid_url($url = null)
{
if (empty($url)) return false;
if (!is_string($url)) return false;
$filter_var = boolval(filter_var($url, FILTER_VALIDATE_URL));
if ($filter_var) return $filter_var;
$parse_url = parse_url($url);
$path = array_pop($parse_url);
$url = str_ireplace($path, '/' . urlencode($path), $url);
return boolval(filter_var($url, FILTER_VALIDATE_URL));
}
}
if (!function_exists('client_ip')) {
function client_ip()
{
$ip = request()->server('HTTP_CF_CONNECTING_IP');
return $ip ?: request()->ip();
}
}
if (!function_exists('rand_ip')) {
function rand_ip()
{
return mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255);
}
}
if (!function_exists('get_content_type')) {
function get_content_type($filename)
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
switch ($extension) {
case 'css':
$mime_type = 'text/css';
break;
case 'js':
$mime_type = 'application/javascript';
break;
case 'svg':
$mime_type = 'image/svg+xml';
break;
default:
$f_open = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($f_open, $filename);
finfo_close($f_open);
}
return $mime_type;
}
}
if (!function_exists('avatar_cdn')) {
function avatar_cdn($str)
{
if (Str::contains($str, ['gitee.com'])) {
$url = preg_replace('#^(http(s?))?(://)#', '', $str);
return "https://i0.wp.com/$url";
}
return $str;
}
}
if (!function_exists('get_enabled_oauth_mode')) {
function get_enabled_oauth_mode()
{
//获取oauth启用信息
$oauth = config_get('oauth.');
$arr = [];
foreach ($oauth as $k => $v) {
if (Str::endsWith($k, '.enable') && substr_count($k, '.') === 1) {
if ($v == 1) {
$arr[] = substr($k, 0, strpos($k, '.'));
}
}
}
return $arr;
}
}