1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate; |
7
|
|
|
use Spatie\SslCertificate\SslCertificate; |
8
|
|
|
use Spatie\UptimeMonitor\Events\InvalidSslCertificateFound; |
9
|
|
|
use Spatie\UptimeMonitor\Events\SoonExpiringSslCertificateFound; |
10
|
|
|
use Spatie\UptimeMonitor\Events\ValidSslCertificateFound; |
11
|
|
|
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus; |
12
|
|
|
|
13
|
|
|
trait SupportsSslCertificateCheck |
14
|
|
|
{ |
15
|
|
|
public function checkSslCertificate() |
16
|
|
|
{ |
17
|
|
|
try { |
18
|
|
|
$certificate = SslCertificate::createForHostName($this->url->getHost()); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$this->updateWithCertificate($certificate); |
21
|
|
|
|
22
|
|
|
} catch (CouldNotDownloadCertificate $exception) { |
23
|
|
|
$this->updateWithCertificateException($exception); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function updateWithCertificate(SslCertificate $certificate) |
28
|
|
|
{ |
29
|
|
|
$this->ssl_certificate_status = $certificate->isValid($this->url) |
|
|
|
|
30
|
|
|
? SslCertificateStatus::VALID |
31
|
|
|
: SslCertificateStatus::INVALID; |
32
|
|
|
|
33
|
|
|
$this->ssl_certificate_expiration_date = $certificate->expirationDate(); |
|
|
|
|
34
|
|
|
$this->ssl_certificate_issuer = $certificate->getIssuer(); |
|
|
|
|
35
|
|
|
$this->save(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->fireEventsForUpdatedSiteWithCertificate($this, $certificate); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function updateWithCertificateException(Exception $exception) |
41
|
|
|
{ |
42
|
|
|
$this->ssl_certificate_status = SslCertificateStatus::INVALID; |
43
|
|
|
$this->ssl_certificate_expiration_date = null; |
44
|
|
|
$this->ssl_certificate_issuer = ''; |
45
|
|
|
$this->ssl_certificate_failure_reason = $exception->getMessage(); |
|
|
|
|
46
|
|
|
$this->save(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
event(new InvalidSslCertificateFound($this, $exception->getMessage())); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function fireEventsForUpdatedSiteWithCertificate(Site $site, SslCertificate $certificate) |
52
|
|
|
{ |
53
|
|
|
if ($this->ssl_certificate_status === SslCertificateStatus::VALID) { |
54
|
|
|
event(new ValidSslCertificateFound($this, $certificate)); |
55
|
|
|
|
56
|
|
|
if ($certificate->expirationDate()->diffInDays() <= config('laravel-uptime-monitor.notifications.send_notification_when_ssl_certificate_will_expire_in_days')) { |
57
|
|
|
event(new SoonExpiringSslCertificateFound($site, $certificate)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->ssl_certificate_status === SslCertificateStatus::INVALID) { |
64
|
|
|
|
65
|
|
|
$reason = 'Unknown reason'; |
66
|
|
|
|
67
|
|
|
if ($certificate->appliesToUrl($this->url)) { |
68
|
|
|
$reason = "Certificate does not apply to {$this->url} but only to these domains: " . implode(',', $certificate->getAdditionalDomains()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($certificate->isExpired()) { |
72
|
|
|
$reason = "The certificate is expired"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
event(new InvalidSslCertificateFound($this, $reason, $certificate)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: