|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the cfdi-certificate project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kinedu |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Kinedu\CfdiCertificate\Strategies; |
|
13
|
|
|
|
|
14
|
|
|
class CerStrategy |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* File to decode. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $file; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Chunk length. |
|
25
|
|
|
* |
|
26
|
|
|
* @var integer |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $chunklen = 64; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a new cer strategy instance. |
|
32
|
|
|
* |
|
33
|
|
|
* @param $file |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct($file) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->file = file_get_contents($file); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Convert .cer to .pem |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function convertToPem() : string |
|
46
|
|
|
{ |
|
47
|
|
|
$prefix = "-----BEGIN CERTIFICATE-----\n"; |
|
48
|
|
|
$suffix = "-----END CERTIFICATE-----\n"; |
|
49
|
|
|
|
|
50
|
|
|
$pem = base64_encode($this->file); |
|
51
|
|
|
$pem = chunk_split($pem, $this->chunklen, "\n") ; |
|
52
|
|
|
$pem = $prefix.$pem.$suffix; |
|
53
|
|
|
|
|
54
|
|
|
return $pem; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getNoCertificado() |
|
61
|
|
|
{ |
|
62
|
|
|
$data = $this->parseCertificate(); |
|
63
|
|
|
$data = str_split($data['serialNumberHex'], 2); |
|
64
|
|
|
|
|
65
|
|
|
$serialNumber = null; |
|
66
|
|
|
|
|
67
|
|
|
for ($i = 0; $i < sizeof($data); $i++) { |
|
|
|
|
|
|
68
|
|
|
$serialNumber .= substr($data[$i], 1); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $serialNumber; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getExpirationDate() |
|
78
|
|
|
{ |
|
79
|
|
|
$data = $this->parseCertificate(); |
|
80
|
|
|
|
|
81
|
|
|
return $this->dateFormat($data['validTo_time_t']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getInitialDate() |
|
88
|
|
|
{ |
|
89
|
|
|
$data = $this->parseCertificate(); |
|
90
|
|
|
|
|
91
|
|
|
return $this->dateFormat($data['validFrom_time_t']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function parseCertificate() |
|
98
|
|
|
{ |
|
99
|
|
|
return openssl_x509_parse($this->convertToPem()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $date |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function dateFormat(string $date) |
|
106
|
|
|
{ |
|
107
|
|
|
return date('Y-m-d H:i:s', $date); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: