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 ( cc194b...accbf4 )
by Daniel
12:20 queued 01:33
created

Downloader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 43.64%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 24
cts 55
cp 0.4364
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
B downloadCertificateFromUrl() 0 41 3
B prepareCertificateResponse() 0 28 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\Handler;
8
use LiquidWeb\SslCertificate\Exceptions\InvalidUrl;
9
10
class Downloader
11
{
12 6
    public static function downloadCertificateFromUrl(string $url, int $timeout = 30): array
13
    {
14
        // Trusted variable to keep track of SSL trust
15 6
        $trusted = true;
16 6
        $sslConfig = StreamConfig::configSecure();
17 6
        $parsedUrl = new Url($url);
18 1
        $client = null;
19
20
        try {
21 1
            $client = stream_socket_client(
22 1
                "ssl://{$parsedUrl->getTestURL()}",
23 1
                $errorNumber,
24 1
                $errorDescription,
25 1
                $timeout,
26 1
                STREAM_CLIENT_CONNECT,
27 1
                $sslConfig->getContext()
28
            );
29
            unset($sslConfig);
30 1
        } catch (Throwable $thrown) {
31
            // Try agian in insecure mode
32 1
            $sslConfig = StreamConfig::configInsecure();
33 1
            $trusted = false;
34
35
            try {
36
                // As the URL failed verification we set to false
37 1
                $client = stream_socket_client(
38 1
                    "ssl://{$parsedUrl->getTestURL()}",
39 1
                    $errorNumber,
40 1
                    $errorDescription,
41 1
                    $timeout,
42 1
                    STREAM_CLIENT_CONNECT,
43 1
                    $sslConfig->getContext()
44
                );
45
                unset($sslConfig);
46 1
            } catch (Throwable $thrown) {
47 1
                (new Handler($thrown))->downloadHandler($parsedUrl);
48
            }
49
        }
50
51
        return self::prepareCertificateResponse($client, $trusted, $parsedUrl);
52
    }
53
54
    private static function prepareCertificateResponse($resultClient, bool $trusted, Url $parsedUrl): array
55
    {
56
        $response = stream_context_get_options($resultClient);
57
        $connectionInfo = stream_get_meta_data($resultClient)['crypto'];
58
        unset($resultClient);
59
        $mainCert = openssl_x509_parse($response['ssl']['peer_certificate'], true);
60
61
        $full_chain = [];
62
        if (count($response['ssl']['peer_certificate_chain']) > 1) {
63
            foreach ($response['ssl']['peer_certificate_chain'] as $cert) {
64
                $parsedCert = openssl_x509_parse($cert, true);
65
                $isChain = ! ($parsedCert['hash'] === $mainCert['hash']);
66
                if ($isChain === true) {
67
                    array_push($full_chain, $parsedCert);
68
                }
69
            }
70
        }
71
72
        return [
73
            'inputDomain' => $parsedUrl->getInputUrl(),
74
            'tested' => $parsedUrl->getTestURL(),
75
            'trusted' => $trusted,
76
            'dns-resolves-to' => $parsedUrl->getIp(),
77
            'cert' => $mainCert,
78
            'full_chain' => $full_chain,
79
            'connection' => $connectionInfo,
80
        ];
81
    }
82
83
    public static function downloadRevocationListFromUrl(string $url): array
84
    {
85
        $parsedUrl = new Url($url);
86
        $csrConfig = StreamConfig::configCrl();
87
        $file = file_get_contents($parsedUrl->getValidatedURL(), false, $csrConfig->getContext());
88
        unset($csrConfig, $parsedUrl);
89
        $x509 = new X509();
90
        $crl = $x509->loadCRL($file);
91
        unset($x509, $file);
92
93
        return $crl;
94
    }
95
}
96