3namespace Bitrix\Main\Web;
6use Bitrix\Main\Security\Cipher;
7use Bitrix\Main\Security\SecurityException;
8use Bitrix\Main\Session\KernelSession;
9use Bitrix\Main\SystemException;
20 private const SIGN_PREFIX =
'-crpt-';
21 private const CIPHER_KEY_SUFFIX =
'cookiecrypter';
37 if (isset($this->cipher))
44 $this->cipher =
new Cipher();
45 $this->cipherKey = $configuration->get(
'crypto')[
'crypto_key'] ??
null;
46 if (!$this->cipherKey)
48 throw new SystemException(
'There is no crypto[crypto_key] in .settings.php. Generate it.');
62 return $key . self::CIPHER_KEY_SUFFIX;
76 foreach ($this->
packCookie($cookie, $encryptedValue) as $partCookie)
91 public function decrypt(
string $name,
string $value, iterable $cookies): string
118 $length = \strlen($encryptedValue);
119 $maxContentLength = self::COOKIE_MAX_SIZE - self::COOKIE_RESERVED_SUFFIX_BYTES - \strlen($cookie->
getName());
122 $parts = ($length / $maxContentLength);
126 $startPosition =
$i * $maxContentLength;
127 $partCookie =
new Cookie(
"{$cookie->getName()}_{$i}", substr($encryptedValue, $startPosition, $maxContentLength));
129 $pack[
"{$cookie->getOriginalName()}_{$i}"] = $partCookie;
135 $mainCookie =
new Cookie($cookie->
getName(), $this->prependSign(implode(
',', array_keys($pack))));
138 array_unshift($pack, $mainCookie);
149 protected function unpackCookie(
string $mainCookie, iterable $cookies): string
152 $packedNames = array_flip(array_filter(explode(
',', $mainCookie)));
155 foreach ($cookies as
$name => $value)
157 if (!isset($packedNames[
$name]))
162 $parts[$packedNames[
$name]] = $value;
169 $encryptedValue = implode(
'', $parts);
185 if (\function_exists(
'gzencode'))
187 $value = gzencode($value);
190 return $this->encodeUrlSafeB64($this->cipher->encrypt($value, $this->getCipherKey()));
204 $value = $this->cipher->decrypt($this->decodeUrlSafeB64($value), $this->
getCipherKey());
205 if (\function_exists(
'gzdecode'))
207 $value = gzdecode($value);
218 private function decodeUrlSafeB64(
string $input): string
220 $padLength = 4 - \strlen($input) % 4;
221 $input .= str_repeat(
'=', $padLength);
223 return base64_decode(strtr($input,
'-_',
'+/')) ?:
'';
231 private function encodeUrlSafeB64(
string $input): string
233 return str_replace(
'=',
'', strtr(base64_encode($input),
'+/',
'-_'));
252 public function shouldDecrypt(
string $cookieName,
string $cookieValue): bool
259 return str_starts_with($cookieValue, self::SIGN_PREFIX);
269 return self::SIGN_PREFIX . $value;
279 return substr($value, \strlen(self::SIGN_PREFIX));
static getInstance($moduleId=null)
prependSign(string $value)
packCookie(CryptoCookie $cookie, string $encryptedValue)
prependSuffixToKey(string $key)
const COOKIE_RESERVED_SUFFIX_BYTES
shouldDecrypt(string $cookieName, string $cookieValue)
encrypt(CryptoCookie $cookie)
decryptValue(string $value)
removeSign(string $value)
decrypt(string $name, string $value, iterable $cookies)
shouldEncrypt(Cookie $cookie)
encryptValue(string $value)
unpackCookie(string $mainCookie, iterable $cookies)
copyAttributesTo(Cookie $cookie)
if(empty($signedUserToken)) $key
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"