审查视图

cmd/mail_del.php 4.2 KB
邓超 authored
1 2 3 4 5 6 7 8 9 10
<?php


/**
 * 循环本地,验证远程是否存在 不存在则删除本地
 */
//error_reporting();

use Swoole\Process;
x  
邓超 authored
11
include_once __DIR__."/../vendor/autoload.php";
邓超 authored
12 13 14

function start(){
x  
邓超 authored
15
    swoole_set_process_name('php-email-sync-list-check');
邓超 authored
16
x  
邓超 authored
17
    $id = 0;
x  
邓超 authored
18
x  
邓超 authored
19
//    $goNum = 0;
x  
邓超 authored
20 21
    // 循环阻塞
    while (true){
邓超 authored
22
x  
邓超 authored
23
        $id = db()->value('select `id` from `'.\Model\emailSql::$table.'` where `id` > '.$id.' order by `id` asc limit 1');
邓超 authored
24
x  
邓超 authored
25 26
        if($id){
            // 启动一个协程
x  
邓超 authored
27 28
//            go(function () use ($id,&$goNum){
//                $goNum++;
x  
邓超 authored
29 30 31 32 33 34 35
                // 开始同步
                try {
                    sync($id);
                }catch (\Throwable $e){
                    echo $e->getMessage();
                }
                \Lib\Log::getInstance()->write();
x  
邓超 authored
36
x  
邓超 authored
37 38 39 40
//                co::defer(function () use (&$goNum){
//                    $goNum--;
//                });
//            });
邓超 authored
41
x  
邓超 authored
42 43
        }else{
            break;
邓超 authored
44
        }
x  
邓超 authored
45
    }
邓超 authored
46
x  
邓超 authored
47 48 49
//    while ($goNum>0){
//        co::sleep(1);
//    }
x  
邓超 authored
50
    _echo('结束了');
邓超 authored
51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

/**
 * 开始同步, 这里是主要的业务代码
 * @param $email_id
 * @param $worker_id
 * @return int
 * @author:dc
 * @time 2023/3/10 10:19
 */
function sync($email_id){

    $email = db()->first(\Model\emailSql::first($email_id));
    if(!$email || $email['pwd_error']){
x  
邓超 authored
65 66

        // 密码错误,或者超过一个月没有更新的邮箱 清空数据
x  
邓超 authored
67
        if($email['pwd_error'] && $email['updated_at'] && strtotime($email['updated_at']) < (time()-86400*7) ){
x  
邓超 authored
68 69 70
            db()->delete(\Model\listsSql::$table,['email_id'=>$email['id']]);
        }
邓超 authored
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        return 0;
    }

    // 读取到邮箱中的文件夹
    $folders = db()->all(\Model\folderSql::all($email['id']));
    if(!$folders){
        return 3;
    }


    $mailServer = new Lib\Mail\Mail($email['email'],base64_decode($email['password']),$email['imap']);

    // 登录服务器
    if($mailServer->login()!==1){
        return 2;
    }

    $call = function ($email_id,$folder_id,$origin_folder) use ($mailServer){
邓超 authored
89
        _echo('run e '.$email_id.' fn '.$origin_folder);
邓超 authored
90 91 92 93 94 95 96 97 98
        // gmail 邮箱 这个是不可选的
        if($origin_folder == '[Gmail]'){
            return;
        }
        // 同步父文件夹
        $mailServer->client->selectFolder($origin_folder);
        $page = 0;
        $db = db();
        while (1){
x  
邓超 authored
99
            $ids = $db->all("select `id`,`uid` from ".\Model\listsSql::$table." where `email_id` = {$email_id} and `folder_id` = {$folder_id} and `udate` < ".strtotime("-1 day")." limit 100 offset ".($page*100));
邓超 authored
100 101
            $page++;
            if($ids){
x  
邓超 authored
102 103 104 105 106 107 108 109
                try {
                    $result = $mailServer->client->fetch(array_column($ids,'uid'),'UID',true);
                    $result = array_column($result,'UID','UID');
                }catch (Throwable $e){
                    _echo($e->getMessage());
                    return;
                }
邓超 authored
110
                foreach ($ids as $id){
x  
邓超 authored
111 112
                    $uid = $id['uid']; $id = $id['id'];
                    if(!$result || !isset($result[$uid])){
邓超 authored
113
                        // 删除 如果远程没有,就删除本地
x  
邓超 authored
114
                        _echo('删除 e '.$email_id.' f '.$folder_id.' u '.$uid.' id '.$id.' d '.$db->delete(\Model\listsSql::$table,['id'=>$id]).' fd '.$db->delete('fob_hot_mail',['lists_id'=>$id]).' body '.$db->delete(\Model\bodySql::$table,['lists_id'=>$id]));
邓超 authored
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
                    }
                }
            }
            // 结束了
            if(count($ids) < 100){
                break;
            }

        }


    };

//    $folders = list_to_tree($folders);
    foreach ($folders as $folder){
        try {
x  
邓超 authored
131 132 133 134 135
            $is = true;
            foreach ($folders as $f){
                // 是否存在下级
                if($f['pid'] == $folder['id']){
                    $is = false;
邓超 authored
136 137 138
                }
            }
x  
邓超 authored
139 140
            if($is) $call($email_id,$folder['id'],$folder['origin_folder']);
邓超 authored
141
        }catch (\Throwable $e){
x  
邓超 authored
142
            echo $e->getMessage();
邓超 authored
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        }
    }


    $email = null;
    $mailServer = null;
}


if(!function_exists("imap_8bit")){
    echo '请安装imap扩展';
    exit(0);
}

x  
邓超 authored
158 159 160
\Co\run(function (){
    start();
});
邓超 authored
161 162 163 164 165 166 167 168 169 170