2018-07-18 09:50:46 +00:00
|
|
|
<?php
|
|
|
|
|
2018-07-19 10:48:17 +00:00
|
|
|
namespace CookieWarning;
|
|
|
|
|
|
|
|
use Config;
|
|
|
|
use ConfigException;
|
|
|
|
use IContextSource;
|
|
|
|
use MWException;
|
|
|
|
use WANObjectCache;
|
|
|
|
|
|
|
|
class Decisions {
|
2018-07-18 09:50:46 +00:00
|
|
|
private $config;
|
|
|
|
private $geoLocation;
|
2018-07-18 10:37:17 +00:00
|
|
|
private $cache;
|
|
|
|
|
2019-11-14 14:10:56 +00:00
|
|
|
private const CACHE_KEY = 'cookieWarningIpLookupCache:';
|
2018-07-18 09:50:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
* @param GeoLocation $geoLocation
|
2018-07-18 10:37:17 +00:00
|
|
|
* @param WANObjectCache $cache
|
2018-07-18 09:50:46 +00:00
|
|
|
*/
|
2018-07-18 10:37:17 +00:00
|
|
|
public function __construct( Config $config, GeoLocation $geoLocation, WANObjectCache $cache ) {
|
2018-07-18 09:50:46 +00:00
|
|
|
$this->config = $config;
|
|
|
|
$this->geoLocation = $geoLocation;
|
2018-07-18 10:37:17 +00:00
|
|
|
$this->cache = $cache;
|
2018-07-18 09:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks, if the CookieWarning information bar should be visible to this user on
|
|
|
|
* this page.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return bool Returns true, if the cookie warning should be visible, false otherwise.
|
|
|
|
* @throws ConfigException
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function shouldShowCookieWarning( IContextSource $context ) {
|
|
|
|
$user = $context->getUser();
|
|
|
|
|
|
|
|
return $this->config->get( 'CookieWarningEnabled' ) &&
|
|
|
|
!$user->getBoolOption( 'cookiewarning_dismissed', false ) &&
|
|
|
|
!$context->getRequest()->getCookie( 'cookiewarning_dismissed' ) &&
|
|
|
|
( $this->config->get( 'CookieWarningGeoIPLookup' ) === 'js' ||
|
|
|
|
$this->isInConfiguredRegion( $context ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks, if the user is in one of the configured regions.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return bool
|
|
|
|
* @throws ConfigException
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
private function isInConfiguredRegion( IContextSource $context ) {
|
|
|
|
if ( !$this->config->get( 'CookieWarningForCountryCodes' ) ||
|
|
|
|
$this->config->get( 'CookieWarningGeoIPLookup' ) === 'none' ) {
|
|
|
|
wfDebugLog( 'CookieWarning', 'IP geolocation not configured, skipping.' );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:37:17 +00:00
|
|
|
$countryCode = $this->getCountryCodeFromIP( $context->getRequest()->getIP() );
|
2018-07-18 09:50:46 +00:00
|
|
|
|
2018-07-20 12:22:47 +00:00
|
|
|
return $countryCode === '' || array_key_exists( $countryCode,
|
2018-07-18 09:50:46 +00:00
|
|
|
$this->config->get( 'CookieWarningForCountryCodes' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @throws ConfigException
|
|
|
|
*/
|
|
|
|
public function shouldAddResourceLoaderComponents() {
|
|
|
|
return $this->config->get( 'CookieWarningGeoIPLookup' ) === 'js' &&
|
|
|
|
is_array( $this->config->get( 'CookieWarningForCountryCodes' ) );
|
|
|
|
}
|
2018-07-18 10:37:17 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-11 23:39:52 +00:00
|
|
|
* @param string $currentIP
|
2018-07-20 12:22:47 +00:00
|
|
|
* @return string The country code associated with the IP or empty string if not able to locate.
|
2018-07-18 10:37:17 +00:00
|
|
|
* @throws ConfigException
|
|
|
|
*/
|
|
|
|
private function getCountryCodeFromIP( $currentIP ) {
|
|
|
|
$cacheKey = $this->cache->makeGlobalKey( __CLASS__, self::CACHE_KEY . $currentIP );
|
|
|
|
$lookedUpCountryCode = $this->cache->get( $cacheKey );
|
|
|
|
|
|
|
|
if ( is_string( $lookedUpCountryCode ) ) {
|
|
|
|
return $lookedUpCountryCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
wfDebugLog( 'CookieWarning', 'Try to locate the user\'s IP address.' );
|
2018-11-04 15:15:42 +00:00
|
|
|
$location = $this->geoLocation->locate( $currentIP );
|
|
|
|
if ( $location === null ) {
|
2018-07-18 10:37:17 +00:00
|
|
|
wfDebugLog( 'CookieWarning',
|
2018-07-20 12:22:47 +00:00
|
|
|
'Locating the user\'s IP address failed or is misconfigured.' );
|
2018-07-18 10:37:17 +00:00
|
|
|
|
2018-07-20 12:22:47 +00:00
|
|
|
return '';
|
2018-07-18 10:37:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 15:15:42 +00:00
|
|
|
$this->cache->set( $cacheKey, $location );
|
2018-07-18 10:37:17 +00:00
|
|
|
|
2018-11-04 15:15:42 +00:00
|
|
|
wfDebugLog( 'CookieWarning',
|
|
|
|
'Locating the user was successful, located region: ' . $location );
|
2018-07-18 10:37:17 +00:00
|
|
|
|
2018-11-04 15:15:42 +00:00
|
|
|
return $location;
|
2018-07-18 10:37:17 +00:00
|
|
|
}
|
2018-07-18 09:50:46 +00:00
|
|
|
}
|