Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
@@ -11,7 +11,7 @@
|
||||
namespace IPS;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
@@ -467,8 +467,8 @@ class _GeoLocation
|
||||
/**
|
||||
* Get by IP address
|
||||
*
|
||||
* @param string $ip IP Address
|
||||
* @return \IPS\GeoLocation
|
||||
* @param string|array $ip IP Address or array of IP addresses
|
||||
* @return \IPS\GeoLocation|array
|
||||
* @throws \BadFunctionCallException Service is not available
|
||||
* @throws \IPS\Http\Request\Exception Error communicating with external service
|
||||
* @throws \RuntimeException Error within the external service
|
||||
@@ -488,24 +488,66 @@ class _GeoLocation
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
}
|
||||
|
||||
/* If the parameter is an array, the response will be an array */
|
||||
if( \is_array( $ip ) )
|
||||
{
|
||||
$result = array();
|
||||
$needToLookup = array();
|
||||
}
|
||||
|
||||
/* Check the cache */
|
||||
try
|
||||
{
|
||||
$data = \IPS\Db::i()->select( 'data', 'core_geoip_cache', array( 'ip_address=?', $ip ) )->first();
|
||||
if ( !$data )
|
||||
if( \is_array( $ip ) )
|
||||
{
|
||||
/* @note This was changed from an \UnderflowException to be consistent with the \OutOfRangeException thrown later on, otherwise the system will keep trying even when it should not. cleanup task will remove out of date entries and it can try again then. */
|
||||
throw new \OutOfRangeException;
|
||||
$atLeastOneFailed = FALSE;
|
||||
|
||||
foreach( $ip as $ipAddress )
|
||||
{
|
||||
try
|
||||
{
|
||||
$data = \IPS\Db::i()->select( 'data', 'core_geoip_cache', array( 'ip_address=?', $ipAddress ) )->first();
|
||||
|
||||
$result[ $ipAddress ] = $data;
|
||||
}
|
||||
catch( \UnderflowException $e )
|
||||
{
|
||||
$atLeastOneFailed = TRUE;
|
||||
$needToLookup[] = $ipAddress;
|
||||
}
|
||||
}
|
||||
|
||||
/* If any of the requested IP addresses failed, we need to look them up */
|
||||
if( $atLeastOneFailed )
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = \IPS\Db::i()->select( 'data', 'core_geoip_cache', array( 'ip_address=?', $ip ) )->first();
|
||||
if ( !$data )
|
||||
{
|
||||
/* @note This was changed from an \UnderflowException to be consistent with the \OutOfRangeException thrown later on, otherwise the system will keep trying even when it should not. cleanup task will remove out of date entries and it can try again then. */
|
||||
throw new \OutOfRangeException;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Not in the cache - get from tge external service */
|
||||
/* Not in the cache - get from the external service */
|
||||
catch ( \UnderflowException $e )
|
||||
{
|
||||
/* Fetch */
|
||||
$response = \IPS\Http\Url::ips( 'geoip/' . urlencode( $ip ) )->request()->login( \IPS\Settings::i()->ipb_reg_number, '' )->get();
|
||||
|
||||
/* Fetch the details */
|
||||
if( \is_array( $ip ) AND \count( $needToLookup ) )
|
||||
{
|
||||
$response = \IPS\Http\Url::ips( 'geoip/' . urlencode( json_encode( $needToLookup ) ) )->request()->login( \IPS\Settings::i()->ipb_reg_number, '' )->get();
|
||||
}
|
||||
else
|
||||
{
|
||||
$response = \IPS\Http\Url::ips( 'geoip/' . urlencode( $ip ) )->request()->login( \IPS\Settings::i()->ipb_reg_number, '' )->get();
|
||||
}
|
||||
|
||||
/* If it's a 404, the IP doesn't exist, we still store NULL to prevent multiple calls */
|
||||
if ( $response->httpResponseCode == 404 )
|
||||
{
|
||||
@@ -523,24 +565,62 @@ class _GeoLocation
|
||||
else
|
||||
{
|
||||
$data = (string) $response;
|
||||
|
||||
if( \is_array( $ip ) )
|
||||
{
|
||||
$data = json_decode( $data, true );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Cache */
|
||||
\IPS\Db::i()->replace( 'core_geoip_cache', array(
|
||||
'ip_address' => $ip,
|
||||
'data' => $data,
|
||||
'date' => time()
|
||||
) );
|
||||
}
|
||||
|
||||
/* If there's nothing, throw an exception */
|
||||
if ( !$data )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
if( \is_array( $ip ) )
|
||||
{
|
||||
if( \is_array( $data ) )
|
||||
{
|
||||
foreach( $data as $k => $v )
|
||||
{
|
||||
/* Database and buildFromJson need this as json */
|
||||
$v = $v ? json_encode( $v ) : NULL;
|
||||
|
||||
$result[ $k ] = $v;
|
||||
\IPS\Db::i()->replace( 'core_geoip_cache', array(
|
||||
'ip_address' => $k,
|
||||
'data' => $v,
|
||||
'date' => time()
|
||||
) );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Db::i()->replace( 'core_geoip_cache', array(
|
||||
'ip_address' => $ip,
|
||||
'data' => $data,
|
||||
'date' => time()
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return static::buildFromJson( $data );
|
||||
if( \is_array( $ip ) )
|
||||
{
|
||||
foreach( $result as $ipAddress => $ipData )
|
||||
{
|
||||
$result[ $ipAddress ] = $ipData ? static::buildFromJson( $ipData ) : NULL;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If there's nothing, throw an exception */
|
||||
if ( !$data )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
}
|
||||
|
||||
return static::buildFromJson( $data );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -645,11 +725,11 @@ class _GeoLocation
|
||||
$output[] = $name;
|
||||
}
|
||||
|
||||
foreach ( array( 'addressLines', 'city', 'region', 'postalCode' ) as $k )
|
||||
foreach ( array( 'business', 'addressLines', 'city', 'region', 'postalCode' ) as $k )
|
||||
{
|
||||
if ( $this->$k )
|
||||
if ( isset( $this->$k ) and $this->$k )
|
||||
{
|
||||
if ( is_array( $this->$k ) )
|
||||
if ( \is_array( $this->$k ) )
|
||||
{
|
||||
foreach ( $this->$k as $v )
|
||||
{
|
||||
@@ -727,7 +807,7 @@ class _GeoLocation
|
||||
$address = json_decode( $data, TRUE );
|
||||
$mapper = new static;
|
||||
|
||||
if ( is_array( $address ) )
|
||||
if ( \is_array( $address ) )
|
||||
{
|
||||
foreach( $address as $k => $v )
|
||||
{
|
||||
@@ -753,7 +833,10 @@ class _GeoLocation
|
||||
*/
|
||||
public function apiOutput( \IPS\Member $authorizedMember = NULL )
|
||||
{
|
||||
return $this;
|
||||
$returnObject = clone $this;
|
||||
$returnObject->addressLines = array_values( $returnObject->addressLines );
|
||||
|
||||
return $returnObject;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user