1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Security\Encryption\Handlers; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Contracts\Security\EncrypterInterface; |
15
|
|
|
use BlitzPHP\Utilities\String\Text; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Classe de base pour les gestionnaires de chiffrement |
19
|
|
|
*/ |
20
|
|
|
abstract class BaseHandler implements EncrypterInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Clé de démarrage |
24
|
|
|
*/ |
25
|
|
|
protected ?string $key = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructeur |
29
|
|
|
*/ |
30
|
|
|
public function __construct(?object $config = null) |
31
|
|
|
{ |
32
|
|
|
$config ??= (object) config('encryption'); |
33
|
|
|
|
34
|
|
|
// rendre les paramètres facilement accessibles |
35
|
|
|
foreach (get_object_vars($config) as $key => $value) { |
36
|
|
|
if (property_exists($this, $key)) { |
37
|
|
|
$this->{$key} = $value; |
38
|
|
|
} elseif (property_exists($this, $key = Text::camel($key))) { |
39
|
|
|
$this->{$key} = $value; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
|
|
public function getKey(): string |
48
|
|
|
{ |
49
|
|
|
return $this->key; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Byte-safe substr() |
54
|
|
|
*/ |
55
|
|
|
protected static function substr(string $str, int $start, ?int $length = null): string |
56
|
|
|
{ |
57
|
|
|
return mb_substr($str, $start, $length, '8bit'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Fourni un accès en lecture seule à certaines de nos propriétés |
62
|
|
|
* |
63
|
|
|
* @param string $key Nom de la propriete |
64
|
|
|
* |
65
|
|
|
* @return array|bool|int|string|null |
66
|
|
|
*/ |
67
|
|
|
public function __get($key) |
68
|
|
|
{ |
69
|
|
|
if ($this->__isset($key)) { |
70
|
|
|
return $this->{$key}; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Assure la vérification de certaines de nos propriétés |
78
|
|
|
* |
79
|
|
|
* @param string $key Nom de la propriete |
80
|
|
|
*/ |
81
|
|
|
public function __isset($key): bool |
82
|
|
|
{ |
83
|
|
|
return property_exists($this, $key); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|