mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CookieWarning
synced 2024-11-23 13:56:53 +00:00
d1b64431e9
Change-Id: I525345c7be91e83c6050870613eb08a6e8763238
63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace CookieWarning;
|
|
|
|
use InvalidArgumentException;
|
|
use MediaWiki\Http\HttpRequestFactory;
|
|
use Wikimedia\IPUtils;
|
|
|
|
/**
|
|
* Implements the GeoLocation class, which allows to locate the user based on the IP address.
|
|
*/
|
|
class HttpGeoLocation implements GeoLocation {
|
|
/** @var string */
|
|
private $geoIPServiceURL;
|
|
/** @var array */
|
|
private $locatedIPs = [];
|
|
|
|
/** @var HttpRequestFactory */
|
|
private $httpRequestFactory;
|
|
|
|
/**
|
|
* @param string $geoIPServiceURL
|
|
* @param HttpRequestFactory $httpRequestFactory
|
|
*/
|
|
public function __construct( $geoIPServiceURL, HttpRequestFactory $httpRequestFactory ) {
|
|
if ( !is_string( $geoIPServiceURL ) || !$geoIPServiceURL ) {
|
|
throw new InvalidArgumentException( 'The geoIPServiceUL is invalid' );
|
|
}
|
|
$this->geoIPServiceURL = $geoIPServiceURL;
|
|
$this->httpRequestFactory = $httpRequestFactory;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
* @param string $ip The IP address to lookup
|
|
* @return string|null
|
|
*/
|
|
public function locate( $ip ) {
|
|
if ( isset( $this->locatedIPs[$ip] ) ) {
|
|
return $this->locatedIPs[$ip];
|
|
}
|
|
if ( !IPUtils::isValid( $ip ) ) {
|
|
throw new InvalidArgumentException( "$ip is not a valid IP address." );
|
|
}
|
|
if ( substr( $this->geoIPServiceURL, -1 ) !== '/' ) {
|
|
$this->geoIPServiceURL .= '/';
|
|
}
|
|
$json = $this->httpRequestFactory->get( $this->geoIPServiceURL . $ip, [
|
|
'timeout' => '2',
|
|
] );
|
|
if ( !$json ) {
|
|
return null;
|
|
}
|
|
$returnObject = json_decode( $json );
|
|
if ( $returnObject === null || !property_exists( $returnObject, 'country_code' ) ) {
|
|
return null;
|
|
}
|
|
$this->locatedIPs[$ip] = $returnObject->country_code;
|
|
|
|
return $this->locatedIPs[$ip];
|
|
}
|
|
}
|