Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
cryptofield.php
1
<?php
9
namespace
Bitrix\Main\ORM\Fields
;
10
11
use
Bitrix\Main\Security
;
12
13
class
CryptoField
extends
TextField
14
{
15
protected
$cryptoKey
;
16
17
// we might want to use different algorithms, so single var might not be enough
19
protected
static
$cipher
;
20
31
public
function
__construct
(
$name
, $parameters = array())
32
{
33
parent::__construct(
$name
, $parameters);
34
35
$enabled =
true
;
36
if
(isset($parameters[
'crypto_enabled'
]))
37
{
38
$enabled = (bool)$parameters[
'crypto_enabled'
];
39
}
40
41
if
($enabled)
42
{
43
if
(isset($parameters[
'crypto_key'
]) && $parameters[
'crypto_key'
] <>
''
)
44
{
45
$this->cryptoKey = $parameters[
'crypto_key'
];
46
}
47
else
48
{
49
// get the key from the settings
50
if
(($key = static::getDefaultKey()) <>
''
)
51
{
52
$this->cryptoKey = $key;
53
}
54
}
55
56
if
(static::cryptoAvailable($this->cryptoKey))
57
{
58
$this->
addSaveDataModifier
(array($this,
'encrypt'
));
59
$this->
addFetchDataModifier
(array($this,
'decrypt'
));
60
}
61
else
62
{
63
throw
new \Bitrix\Main\NotSupportedException(
"Crypto is not available."
);
64
}
65
}
66
}
67
73
public
static
function
cryptoAvailable
($key =
''
)
74
{
75
if
($key ==
''
)
76
{
77
// get the key from the settings
78
$key = static::getDefaultKey();
79
}
80
81
if
($key <>
''
)
82
{
83
if
(static::$cipher ===
null
)
84
{
85
try
86
{
87
static::$cipher =
new
Security\Cipher
();
88
}
89
catch
(Security\
SecurityException
$e)
90
{
91
static::$cipher =
false
;
92
}
93
}
94
}
95
96
return
($key <>
''
&& static::$cipher);
97
}
98
99
public
static
function
getDefaultKey
()
100
{
101
// get the key from the settings
102
$options = \Bitrix\Main\Config\Configuration::getValue(
"crypto"
);
103
if
(isset($options[
"crypto_key"
]))
104
{
105
return
$options[
"crypto_key"
];
106
}
107
return
''
;
108
}
109
110
public
function
encrypt
($data)
111
{
112
if
($data ==
''
)
113
{
114
//is empty data still a secret?
115
return
$data;
116
}
117
try
118
{
119
//encrypt the data
120
$value = static::$cipher->encrypt($data, $this->cryptoKey);
121
return
base64_encode($value);
122
}
123
catch
(Security\
SecurityException
$e)
124
{
125
trigger_error(
"Error on encrypting the field {$this->getEntity()->getName()}.{$this->getName()}: {$e->getMessage()}"
, E_USER_WARNING);
126
return
null
;
127
}
128
}
129
130
public
function
decrypt
($data)
131
{
132
if
($data ==
''
)
133
{
134
//is empty data still a secret?
135
return
$data;
136
}
137
try
138
{
139
//decrypt the data
140
$value = base64_decode($data);
141
$value = static::$cipher->decrypt($value, $this->cryptoKey);
142
return
$value;
143
}
144
catch
(Security\
SecurityException
$e)
145
{
146
trigger_error(
"Error on decrypting the field {$this->getEntity()->getName()}.{$this->getName()}: {$e->getMessage()}"
, E_USER_WARNING);
147
return
null
;
148
}
149
}
150
}
Bitrix\Main\ORM\Fields\CryptoField
Definition
cryptofield.php:14
Bitrix\Main\ORM\Fields\CryptoField\encrypt
encrypt($data)
Definition
cryptofield.php:110
Bitrix\Main\ORM\Fields\CryptoField\cryptoAvailable
static cryptoAvailable($key='')
Definition
cryptofield.php:73
Bitrix\Main\ORM\Fields\CryptoField\getDefaultKey
static getDefaultKey()
Definition
cryptofield.php:99
Bitrix\Main\ORM\Fields\CryptoField\decrypt
decrypt($data)
Definition
cryptofield.php:130
Bitrix\Main\ORM\Fields\CryptoField\__construct
__construct($name, $parameters=array())
Definition
cryptofield.php:31
Bitrix\Main\ORM\Fields\CryptoField\$cipher
static $cipher
Definition
cryptofield.php:19
Bitrix\Main\ORM\Fields\CryptoField\$cryptoKey
$cryptoKey
Definition
cryptofield.php:15
Bitrix\Main\ORM\Fields\Field\$name
$name
Definition
field.php:27
Bitrix\Main\ORM\Fields\Field\addFetchDataModifier
addFetchDataModifier($modifier)
Definition
field.php:344
Bitrix\Main\ORM\Fields\Field\addSaveDataModifier
addSaveDataModifier($modifier)
Definition
field.php:420
Bitrix\Main\ORM\Fields\TextField
Definition
textfield.php:20
Bitrix\Main\Security\Cipher
Definition
cipher.php:11
Bitrix\Main\Security\SecurityException
Definition
securityexception.php:5
Bitrix\Main\ORM\Fields
Definition
arrayfield.php:9
Bitrix\Main\Security
Definition
asymmetriccipher.php:8
modules
main
lib
orm
fields
cryptofield.php
Создано системой
1.10.0