Passed
Push — master ( 0d1074...eef78d )
by Malte
02:00
created

Protocol::setCertValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/*
3
* File: ImapProtocol.php
4
* Category: Protocol
5
* Author: M.Goldenbaum
6
* Created: 16.09.20 18:27
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\PHPIMAP\Connection\Protocols;
14
15
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
16
17
/**
18
 * Class Protocol
19
 *
20
 * @package Webklex\PHPIMAP\Connection\Protocols
21
 */
22
abstract class Protocol {
23
24
    /**
25
     * Default connection timeout in seconds
26
     */
27
    protected $connection_timeout = 30;
28
29
    /**
30
     * @var boolean
31
     */
32
    protected $debug = false;
33
34
    /**
35
     * @var false|resource
36
     */
37
    public $stream = false;
38
39
    /**
40
     * Connection encryption method
41
     * @var mixed $encryption
42
     */
43
    protected $encryption = false;
44
45
    /**
46
     * Set to false to ignore SSL certificate validation
47
     * @var bool
48
     */
49
    protected $cert_validation = true;
50
51
    /**
52
     * Proxy settings
53
     * @var array
54
     */
55
    protected $proxy = [
56
        'socket' => null,
57
        'request_fulluri' => false,
58
        'username' => null,
59
        'password' => null,
60
    ];
61
62
    /**
63
     * Get an available cryptographic method
64
     *
65
     * @return int
66
     */
67
    public function getCryptoMethod() {
68
        // Allow the best TLS version(s) we can
69
        $cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT;
70
71
        // PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT
72
        // so add them back in manually if we can
73
        if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
74
            $cryptoMethod = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
75
        }elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) {
76
            $cryptoMethod = STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
77
        }
78
79
        return $cryptoMethod;
80
    }
81
82
    /**
83
     * Enable SSL certificate validation
84
     *
85
     * @return $this
86
     */
87
    public function enableCertValidation() {
88
        $this->cert_validation = true;
89
        return $this;
90
    }
91
92
    /**
93
     * Disable SSL certificate validation
94
     * @return $this
95
     */
96
    public function disableCertValidation() {
97
        $this->cert_validation = false;
98
        return $this;
99
    }
100
101
    /**
102
     * Set SSL certificate validation
103
     * @var int $cert_validation
104
     *
105
     * @return $this
106
     */
107
    public function setCertValidation($cert_validation) {
108
        $this->cert_validation = $cert_validation;
0 ignored issues
show
Documentation Bug introduced by
The property $cert_validation was declared of type boolean, but $cert_validation is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
109
        return $this;
110
    }
111
112
    /**
113
     * Should we validate SSL certificate?
114
     *
115
     * @return bool
116
     */
117
    public function getCertValidation() {
118
        return $this->cert_validation;
119
    }
120
121
    /**
122
     * Set connection proxy settings
123
     * @var array $options
124
     *
125
     * @return $this
126
     */
127
    public function setProxy($options) {
128
        foreach ($this->proxy as $key => $val) {
129
            if (isset($options[$key])) {
130
                $this->proxy[$key] = $options[$key];
131
            }
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * Get the current proxy settings
139
     *
140
     * @return array
141
     */
142
    public function getProxy() {
143
        return $this->proxy;
144
    }
145
146
    /**
147
     * Prepare socket options
148
     *
149
     * @return array
150
     */
151
    private function defaultSocketOptions() {
152
        $options = [];
153
        if ($this->encryption != false) {
154
            $options["ssl"] = [
155
                'verify_peer_name' => $this->getCertValidation(),
156
                'verify_peer'      => $this->getCertValidation(),
157
            ];
158
        }
159
160
        if ($this->proxy["socket"] != null) {
161
            $options["proxy"] = $this->proxy["socket"];
162
            $options["request_fulluri"] = $this->proxy["request_fulluri"];
163
164
            if ($this->proxy["username"] != null) {
165
                $auth = base64_encode($this->proxy["username"].':'.$this->proxy["password"]);
166
167
                $options["header"] = [
168
                    "Proxy-Authorization: Basic $auth"
169
                ];
170
            }
171
        }
172
173
        return $options;
174
    }
175
176
    /**
177
     * Create a new resource stream
178
     * @param $transport
179
     * @param string $host hostname or IP address of IMAP server
180
     * @param int $port of IMAP server, default is 143 (993 for ssl)
181
     * @param int $timeout timeout in seconds for initiating session
182
     *
183
     * @return resource|boolean The socket created.
184
     * @throws ConnectionFailedException
185
     * @throws \ErrorException
186
     */
187
    protected function createStream($transport, $host, $port, $timeout) {
188
        $socket = "$transport://$host:$port";
189
        $stream = stream_socket_client($socket, $errno, $errstr, $timeout,
190
            STREAM_CLIENT_CONNECT,
191
            stream_context_create($this->defaultSocketOptions())
192
        );
193
194
        if (!$stream) {
0 ignored issues
show
introduced by
$stream is of type false|resource, thus it always evaluated to false.
Loading history...
195
            throw new ConnectionFailedException("Failed to connect to host", 0, $error);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $error seems to be never defined.
Loading history...
196
        }
197
198
        if (false === stream_set_timeout($stream, $timeout)) {
199
            throw new ConnectionFailedException('Failed to set stream timeout');
200
        }
201
202
        return $stream;
203
    }
204
205
    /**
206
     * @return int
207
     */
208
    public function getConnectionTimeout() {
209
        return $this->connection_timeout;
210
    }
211
212
    /**
213
     * @param int $connection_timeout
214
     * @return Protocol
215
     */
216
    public function setConnectionTimeout($connection_timeout) {
217
        if ($connection_timeout !== null) {
0 ignored issues
show
introduced by
The condition $connection_timeout !== null is always true.
Loading history...
218
            $this->connection_timeout = $connection_timeout;
219
        }
220
        return $this;
221
    }
222
223
}