| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\UptimeMonitor\Models\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Spatie\SslCertificate\SslCertificate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Spatie\UptimeMonitor\Events\CertificateCheckFailed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Spatie\UptimeMonitor\Events\CertificateCheckSucceeded; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Spatie\UptimeMonitor\Events\CertificateExpiresSoon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Spatie\UptimeMonitor\Models\Enums\CertificateStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Spatie\UptimeMonitor\Models\Monitor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | trait SupportsCertificateCheck | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public function checkCertificate(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             $certificate = SslCertificate::createForHostName($this->url->getHost()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             $this->setCertificate($certificate); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         } catch (Exception $exception) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             $this->setCertificateException($exception); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function setCertificate(SslCertificate $certificate): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->certificate_status = $certificate->isValid($this->url) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ? CertificateStatus::VALID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             : CertificateStatus::INVALID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->certificate_expiration_date = $certificate->expirationDate(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->certificate_issuer = $certificate->getIssuer(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->save(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->fireEventsForUpdatedMonitorWithCertificate($this, $certificate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function setCertificateException(Exception $exception): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->certificate_status = CertificateStatus::INVALID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->certificate_expiration_date = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->certificate_issuer = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->certificate_check_failure_reason = $exception->getMessage(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->save(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         event(new CertificateCheckFailed($this, $exception->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected function fireEventsForUpdatedMonitorWithCertificate(Monitor $monitor, SslCertificate $certificate): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ($this->certificate_status === CertificateStatus::VALID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             event(new CertificateCheckSucceeded($this, $certificate)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             if ($certificate->expirationDate()->diffInDays() <= config('uptime-monitor.certificate_check.fire_expiring_soon_event_if_certificate_expires_within_days')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 event(new CertificateExpiresSoon($monitor, $certificate)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if ($this->certificate_status === CertificateStatus::INVALID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $reason = 'Unknown'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if (! $certificate->appliesToUrl($this->url)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $reason = "Certificate does not apply to {$this->url} but only to these domains: ".implode(',', $certificate->getAdditionalDomains()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             if ($certificate->isExpired()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $reason = 'The certificate has expired'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->certificate_check_failure_reason = $reason; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->save(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             event(new CertificateCheckFailed($this, $reason, $certificate)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                        
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: