Completed
Push — master ( 432186...3af16c )
by Chris
04:32 queued 01:29
created

ComodoDecodeCSR::generateDVC()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
/**
3
* @author Chris Hilsdon <[email protected]>
4
*/
5
namespace Xigen;
6
7
use GuzzleHttp\Client;
8
9
class ComodoDecodeCSR
10
{
11
    use Traits\ComodoDecodeCSR\Getters;
12
    use Traits\ComodoDecodeCSR\Setters;
13
14
    protected $MD5;
15
    protected $SHA1;
16
    protected $Endpoint = "https://secure.comodo.net/products/!decodeCSR";
17
    protected $CSR;
18
    protected $Form = [
19
        'responseFormat' => 'N',
20
        'showErrorCodes' => 'N',
21
        'showErrorMessages' => 'N',
22
        'showFieldNames' => 'N',
23
        'showEmptyFields' => 'N',
24
        'showCN' => 'N',
25
        'showAddress' => 'N',
26
        'showPublicKey' => 'N',
27
        'showKeySize' => 'N',
28
        'showSANDNSNames' => 'Y',
29
        'showCSR' => 'N',
30
        'showCSRHashes' => 'Y',
31
        'showSignatureAlgorithm' => 'N',
32
        'product' => '',
33
        'countryNameType' => 'TWOCHAR'
34
    ];
35
    private $request;
36
37 2
    public function fetchHashes()
38
    {
39 2
        $client = new Client();
40
41 2
        $this->request = $client->request('POST', $this->Endpoint, [
42 2
            'form_params' => $this->Form
43 2
        ]);
44
45 2
        return $this->processResponce();
46
    }
47
48 1
    public function checkInstalled()
49
    {
50 1
        $CSRInfo = $this->decodeCSR();
51 1
        $domain = $CSRInfo['subject']['CN'];
52 1
        $URL = 'http://' . $domain . "/" . $this->getmd5() . '.txt';
53
54 1
        $client = new Client();
55
56 1
        $request = $client->request('GET', $URL);
57 1
        $responce = "" . $request->getBody();
58 1
        return ($responce === $this->generateDVC());
59
    }
60
61 1
    public function generateDVC()
62
    {
63 1
        $DVC = $this->getSHA1() . "\n";
64 1
        $DVC .= "comodoca.com\n";
65
66 1
        return $DVC;
67
    }
68
69 1
    private function decodeCSR()
70
    {
71 1
        $data = openssl_csr_get_public_key($this->getCSR());
72 1
        $details = openssl_pkey_get_details($data);
73 1
        $key = $details['key'];
74 1
        $subject = openssl_csr_get_subject($this->getCSR());
75
76
        return array(
77 1
            "subject" => $subject,
78
            "key" => $key
79 1
        );
80
    }
81
82 2
    private function processResponce()
83
    {
84 2
        $responce = $this->request->getBody();
85 2
        $lines = explode("\n", $responce);
86 2
        $data = array();
87
88 2
        foreach ($lines as $v) {
89 2
            if (!empty($v)) {
90 2
                $value = explode("=", $v);
91 2
                $data[$value[0]] = $value[1];
92 2
            }
93 2
        }
94
95 2
        $this->setMD5($data["md5"]);
96 2
        $this->setSHA1($data["sha1"]);
97
98 2
        return $data ? $data : false;
99
    }
100
}
101