-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCheckSSL.php
231 lines (191 loc) · 5.67 KB
/
CheckSSL.php
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
<?php declare(strict_types=1);
namespace JrBarros;
use DateTime;
use DateTimeZone;
use Exception;
use RuntimeException;
/**
* Class CheckSSL
* @package JrBarros
*/
class CheckSSL
{
protected array $urls;
protected array $result;
protected string $dateFormat;
protected string $formatString;
protected ?string $timeZone;
protected float $timeOut;
/**
* CheckSSL constructor.
* @param array $url
* @param string $dateFormat
* @param string $formatString
* @param string|null $timeZone
* @param float $timeOut
* @throws Exception
*/
public function __construct(array $url = [],string $dateFormat = 'U',string $formatString = 'Y-m-d\TH:i:s\Z',?string $timeZone = null,float $timeOut = 30)
{
! empty($url) ? $this->add($url) : $this->urls = $url;
$this->dateFormat = $dateFormat;
$this->timeZone = $timeZone;
$this->formatString = $formatString;
$this->timeOut = $timeOut;
$this->result = [];
}
/**
* @param array $data
* @return CheckSSL
* @throws \Exception
*/
public function add(...$data): CheckSSL
{
/** @var array|string $url */
foreach ($data as $url) {
if (is_iterable($url)) {
foreach ($url as $i) {
$this->add($i);
}
continue;
}
if (empty($url)) {
throw new \Exception('please target url is empty');
}
if (! $this->isValidUrl($url)) {
throw new \Exception('malformed URLs');
}
$cleanUrl = parse_url($url, PHP_URL_HOST);
if ($cleanUrl === null) {
throw new \Exception('seriously malformed URLs');
}
$this->urls[] = $cleanUrl;
}
return $this;
}
/**
* @return array
* @throws Exception
*/
public function check(): ?array
{
foreach ($this->urls as $item) {
/** @var resource|false $cert */
$cert = $this->getCert($item);
if ($cert === false) {
$this->result[$item] = null;
continue;
}
$this->result[$item] = $this->getSLLInformation($cert);
}
return $this->getResults();
}
public function getTimeout(): float
{
return $this->timeOut;
}
/**
* @param resource|false $siteStream
* @return array
* @throws Exception
*/
private function getSLLInformation($siteStream): array
{
try {
if (! is_resource($siteStream) || get_resource_type($siteStream) !== 'stream' ) {
throw new RuntimeException('param $siteStream not type stream');
}
$certStream = stream_context_get_params($siteStream);
$cert = $this->getCertFromArray($certStream);
$certInfo = openssl_x509_parse($cert);
$isValid = time() <= $certInfo['validTo_time_t'];
$valid_from = $this->normalizeDate((string) $certInfo['validFrom_time_t']);
$valid_to = $this->normalizeDate((string) $certInfo['validTo_time_t']);
} catch (Exception $exception) {
throw new RuntimeException($exception->getMessage());
}
return [
'is_valid' => $isValid,
'created_at' => $valid_from,
'valid_until' => $valid_to
];
}
/**
* @return array|mixed
*/
private function getResults()
{
if (count($this->result) === 1) {
return current($this->result);
}
return $this->result;
}
/**
* @return resource
*/
private function getStreamContext()
{
return stream_context_create(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'capture_peer_cert' => true
]
]
);
}
/**
* @param string $url
* @return false|resource
*/
private function getCert(string $url)
{
try {
$messageError = 'error to get certificate';
$cert = @stream_socket_client(
'ssl://' . $url. ':443',
$errno,
$messageError,
$this->timeOut,
STREAM_CLIENT_CONNECT,
$this->getStreamContext()
);
} catch (\Exception $exception) {
throw new RuntimeException($exception->getMessage());
}
return $cert;
}
/**
* @param string $timeStamp
* @return string|false
*/
private function normalizeDate(string $timeStamp)
{
$timeZone = null;
if ($this->timeZone !== null) {
$timeZone = new DateTimeZone($this->timeZone);
}
return DateTime::createFromFormat($this->dateFormat, $timeStamp, $timeZone)->format($this->formatString);
}
/**
* @param array $certStream
* @return resource
*/
private function getCertFromArray(array $certStream)
{
return $certStream['options']['ssl']['peer_certificate'];
}
/**
* @param string $data
* @return bool
*/
private function isValidUrl(string $data): bool
{
$regex =
"%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*" .
"[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*" .
"(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu";
return (1 === preg_match($regex,$data));
}
}