ImapClient.php
2.3 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
<?php
class ImapClient {
    /**
     * 资源
     * @var resource|false
     */
    private $socket;
    protected bool $debug = false;
    protected string $host;
    /**
     * 是否是非阻塞模式
     * @var bool
     */
    public $isBlocking = false;
    public function __construct(string $host){
        $this->host = $host;
        $this->socket = false;
    }
    /**
     * 打开链接
     * @param string $out_ip 这个是出口ip
     * @param int $timeout // 连接超时时间
     * @return bool|string
     * @author:dc
     * @time 2025/3/31 9:27
     */
    public function open(string $out_ip, int $timeout=3){
        $content = stream_context_create([
            'ssl' => [
                'verify_peer' => false, // 有的证书和域名不匹配,这里关闭认证
                'verify_peer_name' => false,// 有的证书和域名不匹配,这里关闭认证
            ],
            'socket' => [
                'bindto' => $out_ip.":0" // 绑定到指定的本地 IP 地址
            ]
        ]);
        $flags = STREAM_CLIENT_CONNECT;
        $this->socket = stream_socket_client(
            $this->host,
            $errno,
            $error,
            $timeout,
            $flags,
            $content
        );
        if($error){
            return $error;
        }
        if (!$this->socket) {
            return $this->host." connection fail.";
        }
        // 设置为非阻塞模式
        $this->isBlocking = stream_set_blocking($this->socket, false);
        return true;
    }
    protected $isRead = true;
    /**
     * 写
     * @param string $cmd
     * @return false|int
     * @author:dc
     * @time 2024/9/13 15:47
     */
    public function write(string $cmd){
        error_clear_last();
        $num = @fwrite($this->socket, $cmd);
        if(!$num){
            $last = error_get_last();
            if($last){
                return $last['message']??'';
            }
        }
        return $num;
    }
    /**
     * 读
     * @return false|string
     * @author:dc
     * @time 2024/9/13 15:49
     */
    public function readLine(){
        return @fgets($this->socket,2048);
    }
    public function __destruct()
    {
        if($this->socket) @fclose($this->socket);
        unset($this->socket);
        // TODO: Implement __destruct() method.
    }
}