审查视图

function.php 10.5 KB
1  
邓超 authored
1 2
<?php
1  
邓超 authored
3 4
use Model\emailSql;
1  
邓超 authored
5 6

/**
1  
邓超 authored
7 8 9 10 11 12 13 14 15 16
 * 进程pid
 * @return int
 * @author:dc
 * @time 2023/2/18 14:00
 */
function posix_pid(){
    $pid = getmypid();
    return $pid ? $pid : 0;
}
邓超 authored
17
1  
邓超 authored
18
/**
1  
邓超 authored
19
 * redis 驱动
邓超 authored
20
 * @return \Lib\RedisPool|\Lib\Redis
1  
邓超 authored
21 22 23
 * @author:dc
 * @time 2023/2/13 9:44
 */
邓超 authored
24 25
function redis():\Lib\RedisPool|\Lib\Redis {
x  
邓超 authored
26
    if(co::getCid() != -1){
邓超 authored
27 28
        return new \Lib\RedisPool();
    }
1  
邓超 authored
29
邓超 authored
30
    return \Lib\Redis::instance();
1  
邓超 authored
31 32 33 34 35 36

}


/**
 * 操作db
邓超 authored
37
 * @return \Lib\Db|\Lib\DbPool
1  
邓超 authored
38
 * @author:dc
1  
邓超 authored
39
 * @time 2023/2/13 14:15
1  
邓超 authored
40
 */
邓超 authored
41 42
function db():\Lib\Db|\Lib\DbPool{
    // 池子
x  
邓超 authored
43
    if(co::getCid() != -1){
邓超 authored
44 45 46 47
        return new \Lib\DbPool();
    }
    // 非池子
    return \Lib\Db::instance();
1  
邓超 authored
48 49 50 51 52 53 54 55 56 57 58 59
}




/**
 * 记录日志
 * @param $message
 * @author:dc
 * @time 2023/2/10 14:58
 */
function logs($message,$filename=null){
1  
邓超 authored
60 61 62
    if(is_array($message)){
        $message = print_r($message,true);
    }
x  
邓超 authored
63 64
//    $a = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT,4);
//    $message .= print_r($a,1);
邓超 authored
65
    return \Lib\Log::append($message, $filename);
1  
邓超 authored
66
1  
邓超 authored
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}


/**
 * 消息输出
 * @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{
1  
邓超 authored
90
    return $key ? \Lib\Lang::msg($key) : '';
1  
邓超 authored
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
}


/**
 * @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){
x  
邓超 authored
132
        if(str_contains($value, '.')){
1  
邓超 authored
133 134 135 136 137 138 139 140
            $value = floatval($value);
        }else{
            $value = intval($value);
        }
    }

    return $value;
}
1  
邓超 authored
141 142 143 144 145 146 147 148 149 150 151 152

/**
 *  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){
x  
邓超 authored
153 154

        list($f,$t) = explode('.',$f.'.');
x  
邓超 authored
155 156 157
        if($f!=='_'){
            $f = '`'.str_replace('|','`.`',$f).'`';
        }
x  
邓超 authored
158
1  
邓超 authored
159
        if(is_array($v)){
x  
邓超 authored
160 161 162 163
            if($f ==='_'){
                $sql = array_merge($sql,$v);
                continue;
            }
1  
邓超 authored
164
            $v = array_map(function ($n){
1  
邓超 authored
165 166 167 168
                if (is_string($n)){
                    return "'".addslashes($n)."'";
                }
                return $n;
1  
邓超 authored
169
            },$v);
1  
邓超 authored
170
            if(count($v)===1){
x  
邓超 authored
171
                if($t=='notin'){
邓超 authored
172
                    $sql[] = "{$f} != ".$v[0];
x  
邓超 authored
173 174
                }else{
                    // 只有一个值时就是 =
邓超 authored
175
                    $sql[] = "{$f} = ".$v[0];
x  
邓超 authored
176 177
                }
1  
邓超 authored
178
            }elseif (count($v) > 1){
邓超 authored
179
                $sql[] = "{$f} ".($t=='notin'?'not in':'in')." (".implode(',',$v).")";
1  
邓超 authored
180 181
            }
1  
邓超 authored
182
        }else{
x  
邓超 authored
183 184 185
            if($f ==='_'){
                $sql[] = $v;
            }else{
邓超 authored
186
                $sql[] = "{$f} = '". (is_string($v) ? addslashes($v): $v) ."'";
x  
邓超 authored
187 188
            }
1  
邓超 authored
189 190
        }
1  
邓超 authored
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    }
    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);
}

1  
邓超 authored
211
/**
1  
邓超 authored
212 213 214
 * 获取前端提交过来的邮箱
 * @return array
 * @throws \Lib\Err
1  
邓超 authored
215
 * @author:dc
1  
邓超 authored
216
 * @time 2023/3/10 14:57
1  
邓超 authored
217
 */
1  
邓超 authored
218 219
function web_request_emails():array {
    $emails = app()->request('emails');
x  
邓超 authored
220 221 222

    if(is_array($emails)){
x  
邓超 authored
223
    }elseif($emails && strpos($emails,',') > 0){
x  
邓超 authored
224 225 226 227 228 229
        $emails = explode(',',$emails);
    }else{
        $emails = [$emails];
    }

//    $emails = is_array($emails) ? $emails : [$emails];
1  
邓超 authored
230
    foreach ($emails as $k=>$email){
1  
邓超 authored
231
        if(!$email || !\Lib\Verify::sEmail($email)){
1  
邓超 authored
232 233 234
            unset($emails[$k]);
        }
    }
1  
邓超 authored
235
1  
邓超 authored
236 237
    if(empty($emails)){
        app()->e('email_request_required');
1  
邓超 authored
238
    }
1  
邓超 authored
239 240

    return array_values($emails);
1  
邓超 authored
241
}
1  
邓超 authored
242
1  
邓超 authored
243 244 245 246 247 248 249 250 251 252
/**
 * 前端获取邮箱一个
 * @return string
 * @throws \Lib\Err
 * @author:dc
 * @time 2023/3/10 16:05
 */
function web_request_email():string {
    $email = app()->request('email');
1  
邓超 authored
253
    if(!$email || !\Lib\Verify::sEmail($email)){
1  
邓超 authored
254 255 256 257 258 259 260 261
        app()->e('email_request_required');
    }

    return $email;
}


1  
邓超 authored
262
1  
邓超 authored
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
/**
 * 分页数据
 * @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,
    ];
}
1  
邓超 authored
281 282 283 284



1  
邓超 authored
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
/**
 * 把返回的数据集转换成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;
}
1  
邓超 authored
321 322

1  
邓超 authored
323 324 325 326 327
/**
 * todo:: 立即开始同步邮件,非必要请不要手动调用,,系统有定时调用,
 * @author:dc
 * @time 2023/2/18 11:04
 */
1  
邓超 authored
328
function start_now_mail(){
1  
邓超 authored
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    $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']);
        }
    }

}
1  
邓超 authored
344
/**
邓超 authored
345
 * @return string[]
1  
邓超 authored
346
 * @author:dc
邓超 authored
347
 * @time 2023/4/13 11:02
1  
邓超 authored
348
 */
邓超 authored
349 350
function folderAliasMap():array {
    return [
1  
邓超 authored
351 352 353
        'INBOX' =>  '收件箱',
        // qq的
        'Sent Messages' =>  '发件箱',
x  
邓超 authored
354
        'Sent' =>  '发件箱',
1  
邓超 authored
355 356
        'Drafts' =>  '草稿箱',
        'Junk' =>  '垃圾箱',
邓超 authored
357
        'INBOX.Junk' =>  '垃圾箱',
1  
邓超 authored
358
        'Deleted Messages' =>  '回收站',
x  
邓超 authored
359
        'Trash' =>  '回收站',
1  
邓超 authored
360 361 362

        '垃圾邮件' =>  '垃圾箱',
        '已删除' =>  '回收站',
x  
邓超 authored
363
        '已删除邮件' =>  '回收站',
1  
邓超 authored
364
        '已发送' =>  '发件箱',
邓超 authored
365
        '已发邮件' =>  '发件箱',
邓超 authored
366 367 368
        'INBOX.Sent' =>  '发件箱',

//        'INBOX.Archive' =>  '归档',
1  
邓超 authored
369
1  
邓超 authored
370
        '垃圾桶' =>  '回收站',
1  
邓超 authored
371 372 373 374
        '垃圾郵件' =>  '垃圾箱',
        '寄件備份' =>  '发件箱',
        '草稿' =>  '草稿箱',
朱世亮 authored
375
        //Gmail
376 377 378
        'Sent Mail' => '发件箱',
        'Starred' => '星标邮件',
        '已加星标' => '星标邮件',
379
        '已加星號' => '星标邮件',
朱世亮 authored
380
1  
邓超 authored
381
    ];
邓超 authored
382 383 384 385 386 387 388 389 390
}
/**
 * 固定文件夹的名称,统一
 * @param $folder
 * @return string
 * @author:dc
 * @time 2023/3/21 16:00
 */
function folderAlias(string $folder):string {
1  
邓超 authored
391
邓超 authored
392
    $folder_map = folderAliasMap();
1  
邓超 authored
393 394 395 396 397 398 399 400

    foreach ($folder_map as $key=>$name){
        if(strtolower($folder) == strtolower($key)){
            return $name;
        }
    }

1  
邓超 authored
401
    return $folder_map[$folder]??$folder;
1  
邓超 authored
402
1  
邓超 authored
403
}
1  
邓超 authored
404 405

1  
邓超 authored
406 407 408 409 410 411 412 413
/**
 * true false
 * @param $val
 * @return bool
 * @author:dc
 * @time 2023/4/1 17:42
 */
function bool_Val($val):bool {
x  
邓超 authored
414
    if($val === 'true'){
1  
邓超 authored
415 416
        return true;
    }
x  
邓超 authored
417
    if($val === 'false'){
1  
邓超 authored
418 419 420 421 422 423
        return false;
    }

    if(is_numeric($val)){
        return intval($val) ? true : false;
    }
1  
邓超 authored
424
1  
邓超 authored
425
    return $val ? true : false;
1  
邓超 authored
426
1  
邓超 authored
427
}
1  
邓超 authored
428
x  
邓超 authored
429 430 431 432 433 434 435 436 437
/**
 * @param string $name
 * @return bool
 * @author:dc
 * @time 2023/4/15 16:50
 */
function paramHas(string $name):bool {
    return app()->requestHas($name);
}
1  
邓超 authored
438 439

x  
邓超 authored
440 441 442 443 444 445 446 447 448
/**
 * 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
1  
邓超 authored
449
x  
邓超 authored
450 451
    Result for strip_tags_content($text, '<b>'):
    <b>sample</b> text with
1  
邓超 authored
452
x  
邓超 authored
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
    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;
}
1  
邓超 authored
481 482

邓超 authored
483 484 485 486 487 488
/**
 * 返回邮件的uuid来区分是否是同一封邮件
 * @return string
 * @author:dc
 * @time 2023/9/18 9:33
 */
邓超 authored
489
function get_email_uuid($subject,$udate,$form,$to,$size){
邓超 authored
490
邓超 authored
491
    return md5(json_encode([$subject,$udate,$form,$to,$size]));
邓超 authored
492 493 494 495

}

x  
邓超 authored
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
/**
 * 转码
 * @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;
    }
}