function.php
11.1 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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
use Model\emailSql;
/**
* 进程pid
* @return int
* @author:dc
* @time 2023/2/18 14:00
*/
function posix_pid(){
$pid = getmypid();
return $pid ? $pid : 0;
}
/**
* redis 驱动
* @return \Lib\RedisPool|\Lib\Redis
* @author:dc
* @time 2023/2/13 9:44
*/
function redis():\Lib\RedisPool|\Lib\Redis {
if(co::getCid() != -1){
return new \Lib\RedisPool();
}
return \Lib\Redis::instance();
}
/**
* 操作db
* @return \Lib\Db|\Lib\DbPool
* @author:dc
* @time 2023/2/13 14:15
*/
function db():\Lib\Db|\Lib\DbPool{
// 池子
if(co::getCid() != -1){
return new \Lib\DbPool();
}
// 非池子
return \Lib\Db::instance();
}
/**
* 记录日志
* @param $message
* @author:dc
* @time 2023/2/10 14:58
*/
function logs($message,$filename=null){
if(is_array($message)){
$message = print_r($message,true);
}
// $a = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT,4);
// $message .= print_r($a,1);
return \Lib\Log::append($message, $filename);
}
/**
* 消息输出
* @param $message
* @author:dc
* @time 2023/2/10 15:42
*/
function _echo($message){
echo date('Y-m-d H:i:s').' '.$message."\n";
}
/**
* 文本消息,多语言
* @param $key
* @return mixed
* @author:dc
* @time 2023/2/13 10:51
*/
function __($key):mixed{
return $key ? \Lib\Lang::msg($key) : '';
}
/**
* @return \Lib\App
* @author:dc
* @time 2023/2/13 11:48
*/
function app():\Lib\App{
return \Lib\App::instance();
}
/**
* 过滤函数
* @param $value
* @param null $filter
* @return array|false|float|int|mixed
* @author:dc
* @time 2023/2/13 11:54
*/
function my_filter($value,$filter=null){
if(is_array($value)){
foreach ($value as $key=>$val){
$value[$key] = my_filter($val,$filter);
}
} else {
// 过滤函数
if(!is_array($filter)){
$filter = [$filter];
}
// 合并默认过滤
$filter = array_merge(['trim'], $filter);
// 循环过滤
foreach ($filter as $fil){
$fil && $value = call_user_func($fil,$value);
}
}
// 强制转类型
if(is_numeric($value)&&strlen($value)<10&&intval(substr($value,0,1))!==0){
if(str_contains($value, '.')){
$value = floatval($value);
}else{
$value = intval($value);
}
}
return $value;
}
/**
* db 组合条件
* @param array $where
* @param string $ar
* @return string
* @author:dc
* @time 2023/2/17 10:41
*/
function dbWhere(array $where, string $ar = 'and'):string{
$sql = [];
foreach ($where as $f=>$v){
list($f,$t) = explode('.',$f.'.');
if($f!=='_'){
$f = '`'.str_replace('|','`.`',$f).'`';
}
if(is_array($v)){
if($f ==='_'){
$sql = array_merge($sql,$v);
continue;
}
$v = array_map(function ($n){
if (is_string($n)){
return "'".addslashes($n)."'";
}
return $n;
},$v);
if(count($v)===1){
if($t=='notin'){
$sql[] = "{$f} != ".$v[0];
}elseif($t){
$sql[] = "{$f} {$t} ".$v[0];
}else{
// 只有一个值时就是 =
$sql[] = "{$f} = ".$v[0];
}
}elseif (count($v) > 1){
$sql[] = "{$f} ".($t=='notin'?'not in':($t?:'in'))." (".implode(',',$v).")";
}
}else{
if($f ==='_'){
$sql[] = $v;
}else{
$sql[] = "{$f} = '". (is_string($v) ? addslashes($v): $v) ."'";
}
}
}
return implode(' '.$ar.' ',$sql);
}
/**
* db 更新sql
* @param array $data
* @return string
* @author:dc
* @time 2023/2/17 10:43
*/
function dbUpdate(array $data):string {
$sql = [];
foreach ($data as $f=>$v){
$sql[] = "`{$f}` = :{$f}";
}
return implode(' , ',$sql);
}
/**
* 获取前端提交过来的邮箱
* @return array
* @throws \Lib\Err
* @author:dc
* @time 2023/3/10 14:57
*/
function web_request_emails():array {
$emails = app()->request('emails');
if(is_array($emails)){
}elseif($emails && strpos($emails,',') > 0){
$emails = explode(',',$emails);
}else{
$emails = [$emails];
}
// $emails = is_array($emails) ? $emails : [$emails];
foreach ($emails as $k=>$email){
if(!$email || !\Lib\Verify::sEmail($email)){
unset($emails[$k]);
}
}
if(empty($emails)){
app()->e('email_request_required');
}
return array_values($emails);
}
/**
* 前端获取邮箱一个
* @return string
* @throws \Lib\Err
* @author:dc
* @time 2023/3/10 16:05
*/
function web_request_email():string {
$email = app()->request('email');
if(!$email || !\Lib\Verify::sEmail($email)){
app()->e('email_request_required');
}
return $email;
}
/**
* 分页数据
* @param $item
* @param $total
* @param $page
* @param $limit
* @return array
* @author:dc
* @time 2023/2/17 15:05
*/
function listsPage($item,$total,$page,$limit){
return [
'item' => $item,
'page' => $page,
'total' => $total,
'limit' => $limit,
];
}
/**
* 把返回的数据集转换成Tree
* @param $list array 数据列表
* @param string|int $pk 主键|root
* @param string $pid 父id
* @param string $child 子键
* @param int $root 获取哪个id下面
* @param bool $empty_child 当子数据不存在,是否要返回空子数据
* @return array
*/
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$empty_child=true) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
if($empty_child){
$list[$key][$child] = [];
}
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$refer[$parentId][$child][] = & $list[$key];
}
}
}
}
return $tree;
}
/**
* todo:: 立即开始同步邮件,非必要请不要手动调用,,系统有定时调用,
* @author:dc
* @time 2023/2/18 11:04
*/
function start_now_mail(){
$id = 0;
while (true){
$ids = db()->all('select `id` from `'.\Model\emailSql::$table.'` where `id` > '.$id.' order by `id` asc limit 1000 offset 0');
if(!$ids){
break;
}
foreach ($ids as $v){
$id = $v['id'];
redis()->rPush('sync_email_lists', $v['id']);
}
}
}
/**
* @return string[]
* @author:dc
* @time 2023/4/13 11:02
*/
function folderAliasMap():array {
return [
'INBOX' => '收件箱',
// qq的
'Sent Messages' => '发件箱',
'Sent' => '发件箱',
'Drafts' => '草稿箱',
'Junk' => '垃圾箱',
'INBOX.Junk' => '垃圾箱',
'Deleted Messages' => '回收站',
'Trash' => '回收站',
'垃圾邮件' => '垃圾箱',
'已删除' => '回收站',
'已删除邮件' => '回收站',
'已发送' => '发件箱',
'已发邮件' => '发件箱',
'INBOX.Sent' => '发件箱',
// 'INBOX.Archive' => '归档',
'垃圾桶' => '回收站',
'垃圾郵件' => '垃圾箱',
'寄件備份' => '发件箱',
'草稿' => '草稿箱',
//Gmail
'Sent Mail' => '发件箱',
'Starred' => '星标邮件',
'已加星标' => '星标邮件',
'已加星號' => '星标邮件',
];
}
/**
* 固定文件夹的名称,统一
* @param $folder
* @return string
* @author:dc
* @time 2023/3/21 16:00
*/
function folderAlias(string $folder):string {
$folder_map = folderAliasMap();
foreach ($folder_map as $key=>$name){
if(strtolower($folder) == strtolower($key)){
return $name;
}
}
return $folder_map[$folder]??$folder;
}
/**
* true false
* @param $val
* @return bool
* @author:dc
* @time 2023/4/1 17:42
*/
function bool_Val($val):bool {
if($val === 'true'){
return true;
}
if($val === 'false'){
return false;
}
if(is_numeric($val)){
return intval($val) ? true : false;
}
return $val ? true : false;
}
/**
* @param string $name
* @return bool
* @author:dc
* @time 2023/4/15 16:50
*/
function paramHas(string $name):bool {
return app()->requestHas($name);
}
/**
* Sample text:
$text = '<b>sample</b> text with <div>tags</div>';
Result for strip_tags($text):
sample text with tags
Result for strip_tags_content($text):
text with
Result for strip_tags_content($text, '<b>'):
<b>sample</b> text with
Result for strip_tags_content($text, '<b>', TRUE);
text with <div>tags</div>
I hope that someone is useful :)
* 删除html标记
* @param $text
* @param string $tags
* @param false $invert
* @return string|string[]|null
* @author:dc
* @time 2023/6/20 18:05
*/
function strip_tags_content($text, $tags = '', $invert = FALSE) {
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
return $text;
}
/**
* 返回邮件的uuid来区分是否是同一封邮件
* @return string
* @author:dc
* @time 2023/9/18 9:33
*/
function get_email_uuid($subject,$udate,$form,$to,$size){
return md5(json_encode([$subject,$udate,$form,$to,$size]));
}
/**
* 转码
* @param string $str
* @param string $to
* @param string|null $from
* @return string
* @author:dc
* @time 2024/8/29 10:51
*/
function mb_iconv($str, $to, $from = null) {
try {
return mb_convert_encoding($str, $to, $from);
}catch (Throwable $e){
return $str;
}
}
/**
* 数据元,就是那个表 分表 list_xxxx body_xxx
* @return array|false|float|int|mixed|string|null
* @author:dc
* @time 2024/9/9 16:47
*/
function get_request_origin(){
$request_origin = app()->request('request_origin');
if(preg_match("/^[a-z]{1,10}_\d{1,10}$/",$request_origin)){
return $request_origin;
}
return '';
}
/**
* 抛出异常
* @param string $message
* @throws Exception
* @author:dc
* @time 2024/9/26 9:46
*/
function abort(string $message = ''){
throw new Exception($message);
}