Files
ltsecurityinc/app/code/Plumrocket/CookieConsent/view/frontend/templates/cookie_service.phtml
T

125 lines
3.9 KiB
PHTML

<?php
/**
* @package Plumrocket_CookieConsent
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
* @license https://plumrocket.com/license End-user License Agreement
*/
?>
<?php
/** @var \Plumrocket\CookieConsent\Block\RestrictionConfig $block */
/** @var \Plumrocket\Base\ViewModel\CspNonce $cspNonceViewModel */
$cspNonceViewModel = $block->getData('cspNonceViewModel');
?>
<script <?= /* @noEscape */ $cspNonceViewModel->getNonce() ?>>
var prCookieService = {
defaultAttributes: {path: '/'},
converter: {
write: function (value) {
return encodeURIComponent(value);
},
read: function (value) {
return decodeURIComponent(value)
}
},
decode: function(string)
{
return string.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
},
set: function (key, value, attributes)
{
if (typeof document === 'undefined') {
return;
}
attributes = Object.assign(this.defaultAttributes, attributes);
if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
}
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
try {
var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {}
value = this.converter.write(value);
key = this.converter.write(String(key))
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
.replace(/[\(\)]/g, escape);
var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue;
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue;
}
// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}
return (document.cookie = key + '=' + value + stringifiedAttributes);
},
remove: function (key, attributes) {
this.set(key, '', {...attributes, ...{
expires: -1
}});
},
get: function (key, json = false)
{
if (typeof document === 'undefined') {
return;
}
var jar = {};
var cookies = document.cookie ? document.cookie.split('; ') : [];
var i = 0;
for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var cookie = parts.slice(1).join('=');
if (!json && cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
}
try {
var name = this.converter.write(parts[0]);
cookie = this.converter.read(cookie);
if (json) {
try {
cookie = JSON.parse(cookie);
} catch (e) {}
}
jar[name] = cookie;
if (key === name) {
break;
}
} catch (e) {}
}
return key ? jar[key] : jar;
}
}
</script>