1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
cryptofield.php
См. документацию.
1
<?php
8
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\Config\Configuration\getValue
static getValue($name)
Определения
configuration.php:24
Bitrix\Main\ORM\Fields\CryptoField
Определения
cryptofield.php:14
Bitrix\Main\ORM\Fields\CryptoField\encrypt
encrypt($data)
Определения
cryptofield.php:110
Bitrix\Main\ORM\Fields\CryptoField\cryptoAvailable
static cryptoAvailable($key='')
Определения
cryptofield.php:73
Bitrix\Main\ORM\Fields\CryptoField\getDefaultKey
static getDefaultKey()
Определения
cryptofield.php:99
Bitrix\Main\ORM\Fields\CryptoField\decrypt
decrypt($data)
Определения
cryptofield.php:130
Bitrix\Main\ORM\Fields\CryptoField\__construct
__construct($name, $parameters=array())
Определения
cryptofield.php:31
Bitrix\Main\ORM\Fields\CryptoField\$cipher
static $cipher
Определения
cryptofield.php:19
Bitrix\Main\ORM\Fields\CryptoField\$cryptoKey
$cryptoKey
Определения
cryptofield.php:15
Bitrix\Main\ORM\Fields\Field\$name
$name
Определения
field.php:27
Bitrix\Main\ORM\Fields\Field\addFetchDataModifier
addFetchDataModifier($modifier)
Определения
field.php:351
Bitrix\Main\ORM\Fields\Field\addSaveDataModifier
addSaveDataModifier($modifier)
Определения
field.php:427
Bitrix\Main\ORM\Fields\TextField
Определения
textfield.php:20
Bitrix\Main\Security\Cipher
Определения
cipher.php:11
Bitrix\Main\Security\SecurityException
Определения
securityexception.php:6
$options
$options
Определения
commerceml2.php:49
$data
$data['IS_AVAILABLE']
Определения
.description.php:13
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
Bitrix\Main\Security
Определения
asymmetriccipher.php:8
$key
if(empty($signedUserToken)) $key
Определения
quickway.php:257
bitrix
modules
main
lib
orm
fields
cryptofield.php
Создано системой
1.14.0