Completed
Push — develop ( 3b8721...9b6358 )
by Chris
02:45
created

ComodoDecodeCSR::setCSR()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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\GetSetUnset;
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 2
    public function getCN()
43
    {
44 2
        $CSRInfo = $this->decodeCSR();
45 2
        return $CSRInfo['subject']['CN'];
46
    }
47
48 10
    public function setCSR($CSR)
49
    {
50
        //TODO Check that this is a valid CSR
51 10
        $this->CSR = $CSR;
52 10
        $this->Form['csr'] = $CSR;
53 10
    }
54
55 10
    public function fetchHashes()
56
    {
57 10
        $client = new Client();
58
59 10
        $this->request = $client->request('POST', $this->getEndpoint(), [
1 ignored issue
show
Documentation Bug introduced by
The method getEndpoint does not exist on object<Xigen\ComodoDecodeCSR>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
60 10
            'form_params' => $this->Form
61 10
        ]);
62
63 10
        return $this->processResponse();
64
    }
65
66 2
    public function checkInstalled()
67
    {
68 2
        $domain = $this->getCN();
69 2
        $URL = 'http://' . $domain . "/" . $this->getMD5() . '.txt';
1 ignored issue
show
Documentation Bug introduced by
The method getMD5 does not exist on object<Xigen\ComodoDecodeCSR>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
70
71 2
        $client = new Client();
72
73
        try {
74 2
            $request = $client->request('GET', $URL);
75 2
        } catch (ClientException $e) {
76 1
            return false;
77
        }
78
79 1
        $response = "" . $request->getBody();
80 1
        return $this->checkDVC($response);
81
    }
82
83 7
    public function generateDVC()
84
    {
85 7
        $DVC = $this->getSHA1() . "\n";
1 ignored issue
show
Documentation Bug introduced by
The method getSHA1 does not exist on object<Xigen\ComodoDecodeCSR>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86 7
        $DVC .= "comodoca.com\n";
87
88 7
        return $DVC;
89
    }
90
91 7
    public function checkDVC($response)
92
    {
93 7
        $DVC = $this->generateDVC();
94
95
        //If the response matches the DVC value return true
96 7
        if ($response === $DVC) {
97 2
            return true;
98
        }
99
100
        //Check if last 2 characters are new lines
101 5
        if (substr($response, -2) === "\n\n") {
102 1
            $response = substr($response, 0, -2) . "\n";
103 1
        }
104
105
        //Check if last character is not a new line
106 5
        if (substr($response, -1) !== "\n") {
107
            //Add said new line
108 2
            $response = $response . "\n";
109 2
        }
110
111
        //Check it again
112 5
        if ($response === $DVC) {
113 2
            return true;
114
        }
115
116 3
        return false;
117
    }
118
119 10
    private function decodeCSR()
120
    {
121 2
        $data = openssl_csr_get_public_key($this->getCSR());
1 ignored issue
show
Documentation Bug introduced by
The method getCSR does not exist on object<Xigen\ComodoDecodeCSR>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
122 2
        $details = openssl_pkey_get_details($data);
123 2
        $key = $details['key'];
124 2
        $subject = openssl_csr_get_subject($this->getCSR());
1 ignored issue
show
Documentation Bug introduced by
The method getCSR does not exist on object<Xigen\ComodoDecodeCSR>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
125
126
        return array(
127 2
            "subject" => $subject,
128 10
            "key" => $key
129 2
        );
130
    }
131
132 10
    private function processResponse()
133
    {
134 10
        $response = $this->request->getBody();
135 10
        $lines = explode("\n", $response);
136 10
        $data = array();
137
        //Remove the first array as we don't need the SAN and can cause problems
138
        //with a multi domain SAN
139 10
        unset($lines[0]);
140
141 10
        foreach ($lines as $v) {
142 10
            if (!empty($v)) {
143 10
                $value = explode("=", $v);
144 10
                $data[$value[0]] = $value[1];
145 10
            }
146 10
        }
147
148 10
        $this->setMD5($data["md5"]);
1 ignored issue
show
Documentation Bug introduced by
The method setMD5 does not exist on object<Xigen\ComodoDecodeCSR>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149 10
        $this->setSHA1($data["sha1"]);
1 ignored issue
show
Documentation Bug introduced by
The method setSHA1 does not exist on object<Xigen\ComodoDecodeCSR>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
150
151 10
        return $data ? $data : false;
152
    }
153
}
154