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\CFDI\Certificate; |
13
|
|
|
|
14
|
|
|
class CER extends Certificate |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $cerFile; |
18
|
|
|
|
19
|
|
|
/** @var integer */ |
20
|
|
|
protected $chunkLength = 64; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
public $decodeExtension = 'cer.pem'; |
24
|
|
|
|
25
|
|
|
public function __construct(string $cerFile) |
26
|
|
|
{ |
27
|
|
|
$this->cerFile = file_get_contents( |
28
|
|
|
$this->getOriginalRouteFile($cerFile) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function decode(): string |
33
|
|
|
{ |
34
|
|
|
$prefix = "-----BEGIN CERTIFICATE-----\n"; |
35
|
|
|
$suffix = "-----END CERTIFICATE-----\n"; |
36
|
|
|
|
37
|
|
|
$pem = base64_encode($this->cerFile); |
38
|
|
|
$pem = chunk_split($pem, $this->chunkLength, "\n") ; |
39
|
|
|
$pem = $prefix.$pem.$suffix; |
40
|
|
|
|
41
|
|
|
return $pem; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getCertificateNumber(): string |
45
|
|
|
{ |
46
|
|
|
$data = $this->parseCertificate(); |
47
|
|
|
$data = str_split($data['serialNumberHex'], 2); |
48
|
|
|
|
49
|
|
|
$serialNumber = null; |
50
|
|
|
|
51
|
|
|
for ($i = 0; $i < sizeof($data); $i++) { |
|
|
|
|
52
|
|
|
$serialNumber .= substr($data[$i], 1); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $serialNumber; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getExpirationDate(): string |
59
|
|
|
{ |
60
|
|
|
$data = $this->parseCertificate(); |
61
|
|
|
|
62
|
|
|
return $this->dateFormat($data['validTo_time_t']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getInitialDate(): string |
66
|
|
|
{ |
67
|
|
|
$data = $this->parseCertificate(); |
68
|
|
|
|
69
|
|
|
return $this->dateFormat($data['validFrom_time_t']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function parseCertificate(): array |
73
|
|
|
{ |
74
|
|
|
return openssl_x509_parse($this->decode()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function dateFormat(string $date): string |
78
|
|
|
{ |
79
|
|
|
return date('Y-m-d H:i:s', $date); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: