_isCookieSet()) { if ($this->_isSignedIn()) { $this->_setcookie(1); }else{ $setcookie = true; } }else{ error_log(var_export($this->_getCookieValue(),true)); if ($this->_isSignedIn()) { $this->_setcookie(1); }else{ if ($this->_getCookieValue() === false) { $setcookie = true; } } } if ($setcookie) { $ip = $this->_getIp(); $countryCode = $this->_getCountryCode($ip); if ($this->_userMustConsent($countryCode)) { $this->_setcookie(0); }else{ $this->_setcookie(2); } } $this->_renderHeaders(); $this->_renderJs(); } /** * determines if the cookie is already set * * @uses _name * @return boolean */ protected function _isCookieSet(){ if (array_key_exists($this->_name, $_COOKIE) && $_COOKIE[$this->_name] !== '') { return true; } return false; } /** * determines if a value is a valid cookie value * * @param integer $val * @return boolean */ protected function _validCookieValue($val){ if (!is_int($val)) { return false; } if ($val === 0 || $val === 1 || $val === 2) { return true; } return false; } /** * Gets the cookie value if it exists and is valid * * @uses _isCookieSet * @uses _name * @uses _validCookieValue * @return mixed False if the cookie doesn't exist or is invalid, the int value otherwise; */ protected function _getCookieValue(){ if ($this->_isCookieSet()) { $val = (int) $_COOKIE[$this->_name]; if ($this->_validCookieValue($val)) { return $val; } } return false; } /** * gets the domain the cookie should be set on. * * @return string */ protected function _getCookieDomain(){ $domain = $_SERVER['HTTP_HOST']; $domainParts = explode('.', $domain); if (count($domainParts) > 2) { $domainParts = array_slice($domainParts, -2); $domain = '.' . $domainParts[0] . '.' . $domainParts[1]; } return $domain; } /** * sets the cookie value * * @param integer $val * @uses _validCookieValue * @uses _getCookieDomain * @uses _name */ protected function _setCookie($val){ if (!$this->_validCookieValue($val)) { return; } $name = $this->_name; $path = '/'; $domain = $this->_getCookieDomain(); setcookie($name, $val, 0, $path, $domain); } /** * returns the array of parser query string for the referrer * * @uses _refererQueryParams * @return array */ protected function _getRefererQueryParams(){ if ($this->_refererQueryParams !== null) { return $this->_refererQueryParams; } if (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) { parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $this->_refererQueryParams); }else{ $this->_refererQueryParams = []; } return $this->_refererQueryParams; } /** * determines what ip is associated with the current request * * @uses _getRefererQueryParams * @return string */ protected function _getIp(){ $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['WRI_IS_INTERNAL']) && $_SERVER['WRI_IS_INTERNAL'] == 1) { $query = $this->_getRefererQueryParams(); if (array_key_exists('wriauth_ip', $query) && !empty($query['wriauth_ip'])) { if (filter_var($query['wriauth_ip'], FILTER_VALIDATE_IP) !== false) { $ip = $query['wriauth_ip']; } } if (array_key_exists('wriauth_ip', $_GET) && !empty($_GET['wriauth_ip'])) { if (filter_var($_GET['wriauth_ip'], FILTER_VALIDATE_IP) !== false) { $ip = $_GET['wriauth_ip']; } } } if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { if (filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP) !== false) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } } return $ip; } /** * pareses request data and determines if a user is signed in or not * * @uses _getRefererQueryParams * @return boolean */ protected function _isSignedIn(){ $signedIn = false; $query = $this->_getRefererQueryParams(); if (array_key_exists('signedin', $query) && !empty($query['signedin']) && strtolower($query['signedin']) === 'true') { $signedIn = true; } if (array_key_exists('signedin', $_GET) && !empty($_GET['signedin']) && strtolower($_GET['signedin']) === 'true') { $signedIn = true; } return $signedIn; } /** * pareses request data and determines if the message should be added * * @uses _getRefererQueryParams * @return string */ protected function _getElement(){ $regex ='|^[A-Za-z]{1}[A-Za-z0-9-_:\.]*$|i'; $el = ''; $query = $this->_getRefererQueryParams(); if (array_key_exists('el', $query) && !empty($query['el'])) { if(preg_match($regex, $query['el']) === 1){ $el = $query['el']; } } if (array_key_exists('el', $_GET) && !empty($_GET['el'])) { if(preg_match($regex, $_GET['el']) === 1){ $el = $_GET['el']; } } return $el; } /** * Looks up the country code for a passed in ip address * * @param string $ip The ip address to use to find the country code * @return string */ protected function _getCountryCode($ip){ $countrycode = 'us'; $maxmind = new Wri_MaxmindApi(); $result = $maxmind->legacyCity($ip); if ($result !== false && $result['country_code'] !== null) { $countrycode = strtolower($result['country_code']); } return $countrycode; } /** * gets the lang the user wants that we support * * @uses _supportedLangs * @return string */ protected function _getLanguage(){ if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) { // break the field into parts $parts = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); foreach ($parts as $part) { // strip quality values if the exist and lowercase everything // en-GB;q=0.9 -> en-gb $stripped = explode(';', strtolower($part)); // strip the localization of if it exists to get the general lang // en-gb -> en $lang = explode('-', $stripped[0]); if (in_array($lang[0], $this->_supportedLangs)) { // a preferred lang is valid so use it return $lang[0]; } } } //nothing good was found so use the default return $this->_supportedLangs[0]; } /** * gets the display paragraph for the passed in language. * If the language doesn't exist the default is returned. * * @param string $language the $language the user wants * @return string */ protected function _getParagraph($language){ switch ($language) { case 'de': return 'Diese Website verwendet Cookies, um Ihr Online-Erlebnis mit unseren Diensten zu verbessern, wie in unseren Datenschutzbestimmungen beschrieben.'; case 'es': return 'Este sitio web utiliza cookies para optimizar y mejorar su experiencia al navegar o usarnuestros servicios, como se lo describe en nuestra política de privacidad.'; case 'fr': return 'Ce site internet utilise des cookies pour optimiser votre experience de nos services sur le site selon les conditions prévues par notre politique de confidentialité.'; case 'ja': return 'このWebサイトでは,お客様がサイト上のサービスをより便利にお使いになれるように,弊社のプライバシーポリシーに記載の通りクッキーを使用しています.'; case 'ko': return '이 웹 사이트는 쿠키를 사용하여 개인 정보 보호 정책에 언급된 바와 같이 사이트의 서비스 이용에 대한 사용자 경험을최적화합니다.'; case 'pt': return 'Este site usa cookies para otimizar sua experiência com nossos serviços no site, conforme descrito em nossa Política de Privacidade.'; case 'ru': return 'Этот веб-сайт использует файлы cookie для оптимизации вашего взаимодействия с нашими услугами на сайте, как описано в нашей Политике конфиденциальности.'; case 'zh': return '本网站使用 cookie 来优化您对我们网站服务的体验,详情可参照我们的隐私政策。'; default: return 'This website uses cookies to optimize your experience with our services on the site, as described in our Privacy Policy.'; } } /** * gets the text for the close button for the passed in language. * If the language doesn't exist the default is returned. * * @param string $language the $language the user wants * @return string */ protected function _getButton($language){ switch ($language) { case 'de': return 'Akzeptieren und schließen'; case 'es': return 'Aceptar y Cerrar'; case 'fr': return 'Accepter et Fermer'; case 'ja': return '同意して閉じる'; case 'ko': return '동의 & 닫기'; case 'pt': return 'Aceitar e Fechar'; case 'ru': return 'Принять и закрыть'; case 'zh': return '同意并关闭'; default: return 'Accept & Close'; } } /** * determines if a given country must give consent * * @param string $countryCode the country code to check * @uses _consentCountries * @return boolean */ protected function _userMustConsent($countryCode){ return in_array($countryCode, $this->_consentCountries); } /** * sets various response headers * * @uses _headerExpires */ protected function _renderHeaders(){ header('Content-Type: text/javascript'); header('Expires: '.gmdate('D, d M Y H:i:s', time() + $this->_headerExpires).' GMT'); } /** * renders the js needed to handle cookie consent * * @uses _name * @uses _getCookieDomain * @uses _cookieExpires * @uses _getLanguage * @uses _getParagraph * @uses _getButton * @uses _getElement */ protected function _renderJs(){ $cookieName = $this->_name; $cookeDomain = $this->_getCookieDomain(); $cookeExpire = $this->_cookieExpires; $lang = $this->_getLanguage(); $paragraph = $this->_getParagraph($lang); $button = $this->_getButton($lang); $el = $this->_getElement(); echo <<'; document.getElementById('__cookie-consent-button').addEventListener('click', closeClicked, false); } function closeClicked(e){ wrapper = document.getElementById('__cookie-consent-wrapper'); if (wrapper !== null) { wrapper.style.display = 'none'; wrapper.outerHTML = ''; } setCookie(); } function shouldRender(){ let cookies = {}; let rawCookies = document.cookie.split(';'); for (let i = 0; i < rawCookies.length; i++) { let bits = rawCookies[i].split('='); cookies[bits[0].trim()] = bits[1].trim(); } if(cookies.hasOwnProperty('$cookieName')){ let cookieValue = cookies['$cookieName']; if(cookieValue == 0){ return true; }else{ return false; } }else{ return true; } } function setCookie(){ let domain = '$cookeDomain'; let expire = new Date(); expire.setSeconds(expire.getSeconds() + $cookeExpire); document.cookie = '$cookieName=1;' + 'expires=' + expire + ';domain=' + domain + ';path=/'; } } })(window); EOF; } } $theCookieConsent = new cookieConsent();