Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
<?php
|
2018-07-19 10:48:17 +00:00
|
|
|
|
|
|
|
namespace CookieWarning;
|
|
|
|
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
/**
|
|
|
|
* GeoLocation implementation
|
|
|
|
*/
|
|
|
|
|
2018-07-19 10:48:17 +00:00
|
|
|
use Config;
|
|
|
|
use ConfigException;
|
|
|
|
use Http;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use IP;
|
|
|
|
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
/**
|
|
|
|
* Implements the GeoLocation class, which allows to locate the user based on the IP address.
|
|
|
|
*/
|
|
|
|
class GeoLocation {
|
|
|
|
private $config;
|
|
|
|
private $countryCode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
*/
|
2018-07-18 09:50:46 +00:00
|
|
|
public function __construct( Config $config ) {
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the country code, if the last call to self::locate() returned true. Otherwise, NULL.
|
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getCountryCode() {
|
|
|
|
return $this->countryCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to locate the IP address set with self::setIP() using the geolocation service
|
|
|
|
* configured with the $wgCookieWarningGeoIPServiceURL configuration variable. If the config
|
|
|
|
* isn't set, this function returns NULL. If the config is set, but the URL is invalid or an
|
|
|
|
* other problem occures which resulted in a failed locating process, this function returns
|
|
|
|
* false, otherwise it returns true.
|
|
|
|
*
|
2018-07-18 09:50:46 +00:00
|
|
|
* @param string $ip The IP address to lookup
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
* @return bool|null NULL if no geolocation service configured, false on error, true otherwise.
|
2018-07-18 09:50:46 +00:00
|
|
|
* @throws ConfigException
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
*/
|
2018-07-18 09:50:46 +00:00
|
|
|
public function locate( $ip ) {
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
$this->countryCode = null;
|
2018-07-18 09:50:46 +00:00
|
|
|
if ( !IP::isValid( $ip ) ) {
|
|
|
|
throw new InvalidArgumentException( "$ip is not a valid IP address." );
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
}
|
|
|
|
if ( !$this->config->get( 'CookieWarningGeoIPServiceURL' ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$requestUrl = $this->config->get( 'CookieWarningGeoIPServiceURL' );
|
|
|
|
if ( substr( $requestUrl, -1 ) !== '/' ) {
|
|
|
|
$requestUrl .= '/';
|
|
|
|
}
|
2018-07-18 09:50:46 +00:00
|
|
|
$json = Http::get( $requestUrl . $ip, [
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
'timeout' => '2'
|
|
|
|
] );
|
|
|
|
if ( !$json ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$returnObject = json_decode( $json );
|
|
|
|
if ( $returnObject === null || !property_exists( $returnObject, 'country_code' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->countryCode = $returnObject->country_code;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|