ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,438 @@
|
||||
<?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 prCookieConsentApi = {
|
||||
CUSTOMER_CONSENT: 'pr-cookie-consent',
|
||||
SYSTEM_COOKIES: [
|
||||
'PHPSESSID',
|
||||
this.CUSTOMER_CONSENT,
|
||||
'user_allowed_save_cookie',
|
||||
],
|
||||
config: {},
|
||||
whitelist: {},
|
||||
isConfigured: false,
|
||||
/**
|
||||
* Contains callbacks that run after all configuration come.
|
||||
*
|
||||
* @type {function[]}
|
||||
*/
|
||||
configuredCallbacks: [],
|
||||
initCookieBlocking: function () {
|
||||
var cookieDesc = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie')
|
||||
|| Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie');
|
||||
if (cookieDesc && cookieDesc.configurable) {
|
||||
var api = this;
|
||||
Object.defineProperty(document, 'cookie', {
|
||||
get: function () {
|
||||
return cookieDesc.get.call(document);
|
||||
},
|
||||
set: function (cookie) {
|
||||
let cookieName = cookie.substring(0, cookie.indexOf('=')).trim();
|
||||
document.dispatchEvent(new CustomEvent('pr-cookie-consent-api:cookie-blocking:validation', {
|
||||
detail: {cookieName: cookieName, isAllowed: api.isAllowed(cookieName), cookie: cookie}
|
||||
}));
|
||||
|
||||
if (! api.isAllowed(cookieName)) {
|
||||
api.logger.warn('CookieConsent: blocked cookie "' + cookieName + '"');
|
||||
return;
|
||||
}
|
||||
cookieDesc.set.call(document, cookie);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
getCookieValue: function (name) {
|
||||
var values = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)');
|
||||
return values ? decodeURIComponent(values.pop()) : '';
|
||||
},
|
||||
configure: function (configJson) {
|
||||
this.logger.init();
|
||||
this.userScript.init();
|
||||
|
||||
try {
|
||||
this.config = JSON.parse(configJson);
|
||||
} catch (e) {
|
||||
console.error('CookieConsent: error has happened during parse JSON');
|
||||
return false;
|
||||
}
|
||||
|
||||
var finalizeConfiguration = () => {
|
||||
this.isConfigured = true;
|
||||
this.configuredCallbacks.forEach(function (callback) {
|
||||
callback();
|
||||
}.bind(this));
|
||||
this.clearRejectedCookie();
|
||||
document.dispatchEvent(new Event('pr-cookie-consent-api:configured'));
|
||||
};
|
||||
null === this.canManageCookie()
|
||||
? this.fetchRestrictionState(finalizeConfiguration)
|
||||
: finalizeConfiguration();
|
||||
},
|
||||
canManageCookie: function () {
|
||||
if (null !== this.config.canManageCookie) {
|
||||
return this.config.canManageCookie;
|
||||
}
|
||||
|
||||
if (undefined !== prCookieService.get('pr-can-manage-cookie')) {
|
||||
this.config.canManageCookie = prCookieService.get('pr-can-manage-cookie') === "true";
|
||||
return this.config.canManageCookie;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
fetchRestrictionState: function (callback) {
|
||||
var url = this.config.canManageCookieApiUrl + '?_=' + Date.now();
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', url, true);
|
||||
xhr.setRequestHeader('Accept', 'application/json');
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState !== 4) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
var body = xhr.responseText || '';
|
||||
var parsed;
|
||||
try { parsed = JSON.parse(body); } catch (_) { parsed = body; }
|
||||
var status = String(parsed).toLowerCase() === "true";
|
||||
|
||||
prCookieService.set('pr-can-manage-cookie', status, {
|
||||
path: prCookieConsentApi.config.cookie.path,
|
||||
expires: prCookieConsentApi.config.mage.lifetime
|
||||
? Math.ceil(prCookieConsentApi.config.mage.lifetime / 86400)
|
||||
: 0
|
||||
});
|
||||
|
||||
prCookieConsentApi.config.canManageCookie = status;
|
||||
} else {
|
||||
console.error('CookieConsent: restriction config is invalid');
|
||||
}
|
||||
|
||||
callback();
|
||||
};
|
||||
xhr.send(null);
|
||||
},
|
||||
/**
|
||||
* @param {function} callback
|
||||
* @returns {prCookieConsentApi}
|
||||
*/
|
||||
addConfiguredCallback: function (callback) {
|
||||
this.configuredCallbacks.push(callback);
|
||||
return this;
|
||||
},
|
||||
<?php // This logic has php alternative in \Plumrocket\CookieConsent\Model\Category\IsAllowed ?>
|
||||
isAllowedCategory: function (categoryKey) {
|
||||
if (! this.canManageCookie()) {
|
||||
return true;
|
||||
}
|
||||
if (this.isEssentialCategory(categoryKey)) {
|
||||
return true;
|
||||
}
|
||||
if (this.isOptIn()) {
|
||||
if (this.isAllCategoriesAllowed()) {
|
||||
return true;
|
||||
}
|
||||
return this.consent.get().includes(categoryKey);
|
||||
}
|
||||
return this.config.canUseCookieBeforeOptIn;
|
||||
},
|
||||
getDomain: function (cookieName) {
|
||||
return this.config.cookies[cookieName] ? this.config.cookies[cookieName].domain : null;
|
||||
},
|
||||
<?php // This logic has php alternative in \Plumrocket\CookieConsent\Model\Cookie\IsAllowed ?>
|
||||
isAllowed: function (cookieName) {
|
||||
if (this.isSystemCookie(cookieName)) {
|
||||
return true;
|
||||
}
|
||||
if (! this.canManageCookie()) {
|
||||
return true;
|
||||
}
|
||||
if (this.whitelist[cookieName]) {
|
||||
return true;
|
||||
}
|
||||
cookieName = this.getTrueCookieName(cookieName);
|
||||
if (! this.isOptIn()) {
|
||||
if (this.config.canUseCookieBeforeOptIn) {
|
||||
return true;
|
||||
}
|
||||
if (this.isKnownCookie(cookieName)) {
|
||||
return this.isInEssentialCategory(cookieName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (! this.isKnownCookie(cookieName)) {
|
||||
return ! this.config.canBlockUnknownCookie;
|
||||
}
|
||||
return this.isInAllowedCategory(cookieName);
|
||||
},
|
||||
isAllCategoriesAllowed: function () {
|
||||
if (null === this.cache.get('allowAllCategories')) {
|
||||
var customerConsent = this.consent.get();
|
||||
if (customerConsent && customerConsent.includes('all') ) {
|
||||
this.cache.set('allowAllCategories', true);
|
||||
} else {
|
||||
var allowedWebsites = this.websiteRestriction.getAllowed();
|
||||
this.cache.set('allowAllCategories', allowedWebsites[this.config.mage.website] === 1);
|
||||
}
|
||||
}
|
||||
return this.cache.get('allowAllCategories');
|
||||
},
|
||||
isEssentialCategory: function (categoryKey) {
|
||||
return this.config.essentialCategoryKeys.includes(categoryKey);
|
||||
},
|
||||
isInEssentialCategory: function (cookieName) {
|
||||
return this.isEssentialCategory(this.getCookieCategory(cookieName));
|
||||
},
|
||||
isInAllowedCategory: function (cookieName) {
|
||||
return this.isAllowedCategory(this.getCookieCategory(cookieName));
|
||||
},
|
||||
getCookieCategory: function (cookieName) {
|
||||
return this.config.cookieToCategoryMapping[cookieName];
|
||||
},
|
||||
isOptIn: function () {
|
||||
var allowedWithDefaultCookie = this.websiteRestriction.getAllowed()[this.config.mage.website] === 1;
|
||||
return Boolean(this.consent.get()) || allowedWithDefaultCookie;
|
||||
},
|
||||
isSystemCookie: function (cookieName) {
|
||||
return this.SYSTEM_COOKIES.includes(cookieName);
|
||||
},
|
||||
isKnownCookie: function (cookieName) {
|
||||
return this.config.cookieToCategoryMapping.hasOwnProperty(cookieName);
|
||||
},
|
||||
getTrueCookieName: function (cookieName) {
|
||||
var keys = Object.keys(this.config.dynamicNamesPatterns), key;
|
||||
for (var i = 0, length = keys.length; i < length; i++) {
|
||||
key = keys[i];
|
||||
if (new RegExp(this.config.dynamicNamesPatterns[key]).test(cookieName)) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
return cookieName;
|
||||
},
|
||||
clearRejectedCookie: function () {
|
||||
let cookies = prCookieService.get();
|
||||
Object.keys(cookies).forEach(function(cookieName) {
|
||||
if (this.isAllowed(cookieName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var encodedCookieName = cookieName = encodeURIComponent(String(cookieName))
|
||||
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
|
||||
.replace(/[\(\)]/g, escape);
|
||||
|
||||
this.whitelist[encodedCookieName] = true;
|
||||
if (this.getDomain(cookieName)) {
|
||||
prCookieService.remove(cookieName, {domain: this.getDomain(cookieName)});
|
||||
} else {
|
||||
prCookieService.remove(cookieName);
|
||||
if (this.config.cookie.parentDomain) {
|
||||
prCookieService.remove(cookieName, {domain: this.config.cookie.parentDomain});
|
||||
}
|
||||
}
|
||||
this.logger.warn('CookieConsent: remove cookie "' + cookieName + '"');
|
||||
if (typeof prCookieService.get(cookieName) !== 'undefined') {
|
||||
this.logger.error('CookieConsent: fail to remove cookie "' + cookieName + '"');
|
||||
}
|
||||
this.whitelist[encodedCookieName] = false;
|
||||
}.bind(this));
|
||||
}
|
||||
};
|
||||
|
||||
prCookieConsentApi.userScript = {
|
||||
/**
|
||||
* List of callbacks and categories they depend on.
|
||||
*/
|
||||
pendingScripts: [],
|
||||
/**
|
||||
* Run script after model is configured or after user provided consent.
|
||||
*/
|
||||
init: function () {
|
||||
prCookieConsentApi.addConfiguredCallback(this.executePendingScripts.bind(this));
|
||||
prCookieConsentApi.consent.addCallback(this.executePendingScripts.bind(this));
|
||||
},
|
||||
/**
|
||||
* Run or delay script.
|
||||
*
|
||||
* @param {function} callback
|
||||
* @param {string} categoryKey
|
||||
*/
|
||||
execute: function (callback, categoryKey) {
|
||||
if (prCookieConsentApi.isConfigured && prCookieConsentApi.isAllowedCategory(categoryKey)) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
this.pendingScripts.push({callback: callback, categoryKey: categoryKey, executed: false});
|
||||
},
|
||||
/**
|
||||
* Run pending scripts.
|
||||
*/
|
||||
executePendingScripts: function () {
|
||||
this.pendingScripts.forEach(function (script) {
|
||||
if (false === script.executed && prCookieConsentApi.isAllowedCategory(script.categoryKey)) {
|
||||
script.callback();
|
||||
script.executed = true;
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
};
|
||||
|
||||
prCookieConsentApi.websiteRestriction = {
|
||||
getAllowed: function () {
|
||||
var allowedWebsites = prCookieConsentApi.getCookieValue(prCookieConsentApi.config.mage.cookieName);
|
||||
return allowedWebsites ? JSON.parse(allowedWebsites) : {};
|
||||
},
|
||||
allowCurrent: function () {
|
||||
return this.set(prCookieConsentApi.config.mage.website, true);
|
||||
},
|
||||
disallowCurrent: function () {
|
||||
return this.set(prCookieConsentApi.config.mage.website, false);
|
||||
},
|
||||
/**
|
||||
* @param {number} website
|
||||
* @param {boolean} flag
|
||||
* @return {CookieRestriction.websiteRestriction}
|
||||
*/
|
||||
set: function (website, flag) {
|
||||
var allowedWebsites = this.getAllowed();
|
||||
|
||||
if (flag) {
|
||||
allowedWebsites[website] = 1;
|
||||
} else {
|
||||
delete allowedWebsites[website];
|
||||
}
|
||||
|
||||
prCookieService.set(prCookieConsentApi.config.mage.cookieName, allowedWebsites, {
|
||||
path: prCookieConsentApi.config.cookie.path,
|
||||
expires: prCookieConsentApi.config.mage.lifetime ? Math.ceil(prCookieConsentApi.config.mage.lifetime / 86400) : 0
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
prCookieConsentApi.cache = {
|
||||
cacheData: {allowAllCategories: null},
|
||||
get: function (key) {
|
||||
return this.cacheData[key];
|
||||
},
|
||||
set: function (key, value) {
|
||||
this.cacheData[key] = value;
|
||||
},
|
||||
reset: function () {
|
||||
this.cacheData = {allowAllCategories: null};
|
||||
}
|
||||
};
|
||||
|
||||
prCookieConsentApi.logger = {
|
||||
level: 0, /* disabled */
|
||||
init: function () {
|
||||
if (window.location.hash === '#pr-enable-cookie-log') {
|
||||
this.level = 1; /* only warnings and errors */
|
||||
}
|
||||
if (window.location.hash === '#pr-enable-cookie-log-all') {
|
||||
this.level = 2; /* all logs */
|
||||
}
|
||||
},
|
||||
log: function () {
|
||||
this.level > 1 && console.log.apply(console, arguments);
|
||||
},
|
||||
warn: function () {
|
||||
this.level > 0 && console.warn.apply(console, arguments);
|
||||
},
|
||||
error: function () {
|
||||
this.level > 0 && console.error.apply(console, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
prCookieConsentApi.consent = {
|
||||
/**
|
||||
* Contains callbacks that run after user consent.
|
||||
*
|
||||
* @type {function[]}
|
||||
*/
|
||||
callbacks: [],
|
||||
allowAllCategories: function () {
|
||||
prCookieConsentApi.cache.reset();
|
||||
prCookieConsentApi.consent.set(['all']);
|
||||
return this;
|
||||
},
|
||||
declineAll: function () {
|
||||
prCookieConsentApi.cache.reset();
|
||||
prCookieConsentApi.consent.set([]);
|
||||
return this;
|
||||
},
|
||||
set: function (allowedCategories) {
|
||||
prCookieService.set(prCookieConsentApi.CUSTOMER_CONSENT, JSON.stringify(allowedCategories), {
|
||||
expires: prCookieConsentApi.config.consent.expiry,
|
||||
path: prCookieConsentApi.config.cookie.path,
|
||||
domain: prCookieConsentApi.config.cookie.domain
|
||||
});
|
||||
|
||||
if (allowedCategories.includes('all')) {
|
||||
prCookieConsentApi.websiteRestriction.allowCurrent();
|
||||
} else {
|
||||
prCookieConsentApi.websiteRestriction.disallowCurrent();
|
||||
}
|
||||
prCookieConsentApi.cache.reset();
|
||||
|
||||
var isAccepting = allowedCategories.length;
|
||||
|
||||
this.callbacks.forEach(function (callback) {
|
||||
callback(allowedCategories)
|
||||
});
|
||||
|
||||
fetch(prCookieConsentApi.config.consent.logUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: JSON.stringify({acceptedKeys: allowedCategories})
|
||||
})
|
||||
.then(this.pageReloadAfterAction.bind(this, isAccepting));
|
||||
},
|
||||
/**
|
||||
* Reload page by configuration and action
|
||||
*
|
||||
* @param isAccepting
|
||||
*/
|
||||
pageReloadAfterAction: function (isAccepting) {
|
||||
if (isAccepting && prCookieConsentApi.config.consent.reloadAfterAccept) {
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
if (! isAccepting && prCookieConsentApi.config.consent.reloadAfterDecline) {
|
||||
window.location.reload();
|
||||
}
|
||||
},
|
||||
get: function () {
|
||||
var consent = prCookieConsentApi.getCookieValue(prCookieConsentApi.CUSTOMER_CONSENT);
|
||||
return consent ? JSON.parse(consent) : false;
|
||||
},
|
||||
addCallback: function (callback) {
|
||||
this.callbacks.push(callback);
|
||||
return this;
|
||||
},
|
||||
};
|
||||
|
||||
prCookieConsentApi.configure('<?= $block->escapeJs($block->getSerializedRestrictionConfig()) ?>');
|
||||
prCookieConsentApi.initCookieBlocking();
|
||||
|
||||
if (typeof define === 'function') {
|
||||
define('prCookieService', function () {return prCookieService});
|
||||
define('prCookieConsentApi', function () {return prCookieConsentApi;});
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user