GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 598255...a2b070 )
by Daniel
03:16
created

Downloader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 94.55%

Importance

Changes 0
Metric Value
dl 0
loc 104
ccs 52
cts 55
cp 0.9455
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
B downloadCertificateFromUrl() 0 60 8
B prepareCertificateResponse() 0 27 4
A downloadRevocationListFromUrl() 0 12 1
1
<?php
2
3
namespace LiquidWeb\SslCertificate;
4
5
use Throwable;
6
use phpseclib\File\X509;
7
use LiquidWeb\SslCertificate\Exceptions\CouldNotDownloadCertificate;
8
9
class Downloader
10
{
11 8
    public static function downloadCertificateFromUrl(string $url, int $timeout = 30): array
12
    {
13
        // Trusted variable to keep track of SSL trust
14 8
        $trusted = true;
15 8
        $sslConfig = StreamConfig::configSecure();
16 8
        $parsedUrl = new Url($url);
17 7
        $hostName = $parsedUrl->getHostName();
18 7
        $client = null;
19
20
        try {
21 7
            $client = stream_socket_client(
22 7
                "ssl://{$parsedUrl->getTestURL()}",
23
                $errorNumber,
24
                $errorDescription,
25
                $timeout,
26 7
                STREAM_CLIENT_CONNECT,
27 7
                $sslConfig->getContext()
28
            );
29 2
            unset($sslConfig);
30 5
        } catch (Throwable $thrown) {
31
            // Try agian in insecure mode
32 5
            $sslConfig = StreamConfig::configInsecure();
33
34
            try {
35
                // As the URL failed verification we set to false
36 5
                $trusted = false;
37 5
                $client = stream_socket_client(
38 5
                    "ssl://{$parsedUrl->getTestURL()}",
39
                    $errorNumber,
40
                    $errorDescription,
41
                    $timeout,
42 5
                    STREAM_CLIENT_CONNECT,
43 5
                    $sslConfig->getContext()
44
                );
45 1
                unset($sslConfig);
46
47 1
                return self::prepareCertificateResponse($client, $trusted, $parsedUrl->getIp(), $parsedUrl->getTestURL());
48 4
            } catch (Throwable $thrown) {
49 4
                if (str_contains($thrown->getMessage(), 'getaddrinfo failed')) {
50
                    throw CouldNotDownloadCertificate::hostDoesNotExist($hostName);
51
                }
52
53 4
                if (str_contains($thrown->getMessage(), 'error:14090086')) {
54
                    throw CouldNotDownloadCertificate::noCertificateInstalled($hostName);
55
                }
56
57 4
                if (str_contains($thrown->getMessage(), 'error:14077410') || str_contains($thrown->getMessage(), 'error:140770FC')) {
58 3
                    throw CouldNotDownloadCertificate::failedHandshake($parsedUrl);
59
                }
60
61 1
                if (str_contains($thrown->getMessage(), '(Connection timed out)')) {
62 1
                    throw CouldNotDownloadCertificate::connectionTimeout($parsedUrl->getTestURL());
63
                }
64
65
                throw CouldNotDownloadCertificate::unknownError($parsedUrl->getTestURL(), $thrown->getMessage());
66
            }
67
        }
68
69 2
        return self::prepareCertificateResponse($client, $trusted, $parsedUrl->getIp(), $parsedUrl->getTestURL());
70
    }
71
72 3
    private static function prepareCertificateResponse($resultClient, bool $trusted, string $domainIp, string $testedUrl): array
73
    {
74
        $results = [
75 3
            'tested' => $testedUrl,
76 3
            'trusted' => $trusted,
77 3
            'dns-resolves-to' => $domainIp,
78
            'cert' => null,
79
            'full_chain' => [],
80
            'connection' => [],
81
        ];
82 3
        $response = stream_context_get_options($resultClient);
83 3
        $results['connection'] = stream_get_meta_data($resultClient)['crypto'];
84 3
        unset($resultClient);
85 3
        $results['cert'] = openssl_x509_parse($response['ssl']['peer_certificate'], true);
86
87 3
        if (count($response['ssl']['peer_certificate_chain']) > 1) {
88 2
            foreach ($response['ssl']['peer_certificate_chain'] as $cert) {
89 2
                $parsedCert = openssl_x509_parse($cert, true);
90 2
                $isChain = ! ($parsedCert['hash'] === $results['cert']['hash']);
91 2
                if ($isChain === true) {
92 2
                    array_push($results['full_chain'], $parsedCert);
93
                }
94
            }
95
        }
96
97 3
        return $results;
98
    }
99
100 2
    public static function downloadRevocationListFromUrl(string $url): array
101
    {
102 2
        $parsedUrl = new Url($url);
103 2
        $csrConfig = StreamConfig::configCrl();
104 2
        $file = file_get_contents($parsedUrl->getValidatedURL(), false, $csrConfig->getContext());
105 2
        unset($csrConfig, $parsedUrl);
106 2
        $x509 = new X509();
107 2
        $crl = $x509->loadCRL($file); // see ev2009a.crl
108 2
        unset($x509, $file);
109
110 2
        return $crl;
111
    }
112
}
113