Completed
Push — master ( fff8c1...8dbad0 )
by Chris
02:37
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.003

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
crap 2.003
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
14
class ComodoDecodeCSR
15
{
16
    use Traits\ComodoDecodeCSR\Getters;
17
    use Traits\ComodoDecodeCSR\Setters;
18
19
    protected $MD5;
20
    protected $SHA1;
21
    protected $Endpoint = "https://secure.comodo.net/products/!decodeCSR";
22
    protected $CSR;
23
    protected $Form = [
24
        'responseFormat' => 'N',
25
        'showErrorCodes' => 'N',
26
        'showErrorMessages' => 'N',
27
        'showFieldNames' => 'N',
28
        'showEmptyFields' => 'N',
29
        'showCN' => 'N',
30
        'showAddress' => 'N',
31
        'showPublicKey' => 'N',
32
        'showKeySize' => 'N',
33
        'showSANDNSNames' => 'Y',
34
        'showCSR' => 'N',
35
        'showCSRHashes' => 'Y',
36
        'showSignatureAlgorithm' => 'N',
37
        'product' => '',
38
        'countryNameType' => 'TWOCHAR'
39
    ];
40
    private $request;
41
42 3
    public function fetchHashes()
43
    {
44 3
        $client = new Client();
45
46 3
        $this->request = $client->request('POST', $this->getEndpoint(), [
47 3
            'form_params' => $this->Form
48 3
        ]);
49
50 3
        return $this->processResponce();
51
    }
52
53 3
    public function checkInstalled()
54
    {
55 1
        $CSRInfo = $this->decodeCSR();
56 1
        $domain = $CSRInfo['subject']['CN'];
57 1
        $URL = 'http://' . $domain . "/" . $this->getmd5() . '.txt';
58
59 1
        $client = new Client();
60
61
        try {
62 1
            $request = $client->request('GET', $URL);
63 1
        } catch (ClientException $e) {
0 ignored issues
show
Bug introduced by
The class Xigen\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
64
            return false;
65 3
        }
66
        
67 1
        $responce = "" . $request->getBody();
68 1
        return ($responce === $this->generateDVC());
69
    }
70
71 1
    public function generateDVC()
72
    {
73 1
        $DVC = $this->getSHA1() . "\n";
74 1
        $DVC .= "comodoca.com\n";
75
76 1
        return $DVC;
77
    }
78
79 1
    private function decodeCSR()
80
    {
81 1
        $data = openssl_csr_get_public_key($this->getCSR());
82 1
        $details = openssl_pkey_get_details($data);
83 1
        $key = $details['key'];
84 1
        $subject = openssl_csr_get_subject($this->getCSR());
85
86
        return array(
87 1
            "subject" => $subject,
88
            "key" => $key
89 1
        );
90
    }
91
92 3
    private function processResponce()
93
    {
94 3
        $responce = $this->request->getBody();
95 3
        $lines = explode("\n", $responce);
96 3
        $data = array();
97
        //Remove the first array as we don't need the SAN and can cause problems
98
        //with a multi domain SAN
99 3
        unset($lines[0]);
100
101 3
        foreach ($lines as $v) {
102 3
            if (!empty($v)) {
103 3
                $value = explode("=", $v);
104 3
                $data[$value[0]] = $value[1];
105 3
            }
106 3
        }
107
108 3
        $this->setMD5($data["md5"]);
109 3
        $this->setSHA1($data["sha1"]);
110
111 3
        return $data ? $data : false;
112
    }
113
}
114