Completed
Push — master ( 8dbad0...facf10 )
by Chris
02:35
created

ComodoDecodeCSR::checkInstalled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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