Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+176 -112
View File
@@ -11,21 +11,70 @@
namespace IPS;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use BadFunctionCallException;
use BadMethodCallException;
use IPS\GeoLocation\GeoCoder;
use IPS\GeoLocation\Maps\Google;
use IPS\GeoLocation\Maps\Mapbox;
use IPS\Http\Request\Exception;
use IPS\Http\Url;
use IPS\Platform\Bridge;
use OutOfRangeException;
use RuntimeException;
use UnderflowException;
use function count;
use function defined;
use function is_array;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* GeoLocation
*/
class _GeoLocation
class GeoLocation
{
/**
* @brief EU Member States
*/
const EU = array(
'AT',
'BE',
'BG',
'HR',
'CY',
'CZ',
'DK',
'EE',
'FI',
'FR',
'DE',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'PL',
'PT',
'RO',
'SK',
'SI',
'ES',
'SE'
);
/**
* @brief Country Code List
*/
public static $countries = array(
public static array $countries = array(
'AF', // Afghanistan
'AX', // Åland Islands
'AL', // Albania
@@ -280,7 +329,7 @@ class _GeoLocation
/**
* @brief State List
*/
public static $states = array(
public static array $states = array(
'AU' => array(
'Australian Capital Territory',
'New South Wales',
@@ -426,89 +475,95 @@ class _GeoLocation
/**
* @brief Latitude
*/
public $lat;
public ?float $lat = NULL;
/**
* @brief Longitude
*/
public $long;
public ?float $long = NULL;
/**
* @brief Address Lines
*/
public $addressLines = array( NULL );
public array $addressLines = array( NULL );
/**
* @brief City
*/
public $city;
public ?string $city = NULL;
/**
* @brief Region
*/
public $region;
public ?string $region = NULL;
/**
* @brief Country (2 character code)
*/
public $country;
public ?string $country = NULL;
/**
* @brief Postal Code
*/
public $postalCode;
public ?string $postalCode = NULL;
/**
* @brief Place Name
*/
public $placeName;
public ?string $placeName = NULL;
/**
* @brief County
*/
public ?string $county = NULL;
/**
* @brief Map
*/
protected $map;
protected mixed $map = NULL;
/**
* @brief VAT (used in Commerce)
*/
public ?string $vat = NULL;
/**
* Get Requester Location
*
* @return \IPS\GeoLocation
* @throws \BadFunctionCallException Service is not available
* @throws \IPS\Http\Request\Exception Error communicating with external service
* @throws \RuntimeException Error within the external service
*/
public static function getRequesterLocation(): \IPS\GeoLocation
* @return GeoLocation
*/
public static function getRequesterLocation(): GeoLocation
{
return static::getByIp( \IPS\Request::i()->ipAddress() );
return Bridge::i()->getRequesterLocation();
}
/**
* Get by IP address
*
* @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
* @throws \OutOfRangeException IP address has no data
* @param array|string $ip IP Address or array of IP addresses
* @return GeoLocation|array
* @throws BadFunctionCallException Service is not available
* @throws Exception Error communicating with external service
* @throws RuntimeException Error within the external service
* @throws OutOfRangeException IP address has no data
*/
public static function getByIp( $ip )
public static function getByIp( array|string $ip ): GeoLocation|array
{
/* If the service is not turned on - throw an exception */
if ( !\IPS\Settings::i()->ipsgeoip )
if ( !Settings::i()->ipsgeoip )
{
throw new \BadFunctionCallException;
throw new BadFunctionCallException;
}
/* If the license key is invalid or expired the service won't work, so throw an exception */
$licenseData = \IPS\IPS::licenseKey();
$licenseData = IPS::licenseKey();
if( !$licenseData or !$licenseData['active'] )
{
throw new \BadFunctionCallException;
throw new BadFunctionCallException;
}
/* If the parameter is an array, the response will be an array */
if( \is_array( $ip ) )
if( is_array( $ip ) )
{
$result = array();
$needToLookup = array();
@@ -517,7 +572,7 @@ class _GeoLocation
/* Check the cache */
try
{
if( \is_array( $ip ) )
if( is_array( $ip ) )
{
$atLeastOneFailed = FALSE;
@@ -525,11 +580,11 @@ class _GeoLocation
{
try
{
$data = \IPS\Db::i()->select( 'data', 'core_geoip_cache', array( 'ip_address=?', $ipAddress ) )->first();
$data = Db::i()->select( 'data', 'core_geoip_cache', array( 'ip_address=?', $ipAddress ) )->first();
$result[ $ipAddress ] = $data;
}
catch( \UnderflowException $e )
catch( UnderflowException $e )
{
$atLeastOneFailed = TRUE;
$needToLookup[] = $ipAddress;
@@ -539,31 +594,31 @@ class _GeoLocation
/* If any of the requested IP addresses failed, we need to look them up */
if( $atLeastOneFailed )
{
throw new \UnderflowException;
throw new UnderflowException;
}
}
else
{
$data = \IPS\Db::i()->select( 'data', 'core_geoip_cache', array( 'ip_address=?', $ip ) )->first();
$data = 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;
throw new OutOfRangeException;
}
}
}
/* Not in the cache - get from the external service */
catch ( \UnderflowException $e )
catch ( UnderflowException $e )
{
/* Fetch the details */
if( \is_array( $ip ) AND \count( $needToLookup ) )
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();
$response = Url::ips( 'geoip/' . urlencode( json_encode( $needToLookup ) ) )->request()->login( Settings::i()->ipb_reg_number, '' )->get();
}
else
{
$response = \IPS\Http\Url::ips( 'geoip/' . urlencode( $ip ) )->request()->login( \IPS\Settings::i()->ipb_reg_number, '' )->get();
$response = Url::ips( 'geoip/' . urlencode( $ip ) )->request()->login( Settings::i()->ipb_reg_number, '' )->get();
}
/* If it's a 404, the IP doesn't exist, we still store NULL to prevent multiple calls */
@@ -575,8 +630,8 @@ class _GeoLocation
/* If it's anything other than a 200, log it and throw exception */
elseif ( $response->httpResponseCode != 200 )
{
\IPS\Log::log( "GeoIP Error\n\nRequested IP: {$ip}\n\nResponse:\n" . print_r( $response, TRUE ), 'geoip' );
throw new \RuntimeException;
Log::log( "GeoIP Error\n\nRequested IP: {$ip}\n\nResponse:\n" . print_r( $response, TRUE ), 'geoip' );
throw new RuntimeException;
}
/* Otherwise it's fine */
@@ -584,16 +639,16 @@ class _GeoLocation
{
$data = (string) $response;
if( \is_array( $ip ) )
if( is_array( $ip ) )
{
$data = json_decode( $data, true );
}
}
/* Cache */
if( \is_array( $ip ) )
if( is_array( $ip ) )
{
if( \is_array( $data ) )
if( is_array( $data ) )
{
foreach( $data as $k => $v )
{
@@ -601,7 +656,7 @@ class _GeoLocation
$v = $v ? json_encode( $v ) : NULL;
$result[ $k ] = $v;
\IPS\Db::i()->replace( 'core_geoip_cache', array(
Db::i()->replace( 'core_geoip_cache', array(
'ip_address' => $k,
'data' => $v,
'date' => time()
@@ -611,7 +666,7 @@ class _GeoLocation
}
else
{
\IPS\Db::i()->replace( 'core_geoip_cache', array(
Db::i()->replace( 'core_geoip_cache', array(
'ip_address' => $ip,
'data' => $data,
'date' => time()
@@ -620,7 +675,7 @@ class _GeoLocation
}
/* Return */
if( \is_array( $ip ) )
if( is_array( $ip ) and isset( $result ))
{
foreach( $result as $ipAddress => $ipData )
{
@@ -632,9 +687,9 @@ class _GeoLocation
else
{
/* If there's nothing, throw an exception */
if ( !$data )
if ( !isset( $data ) or !$data )
{
throw new \OutOfRangeException;
throw new OutOfRangeException;
}
return static::buildFromJson( $data );
@@ -644,36 +699,36 @@ class _GeoLocation
/**
* Geocode Location
*
* @param string $input Location search term
* @param string|null $input Location search term
* @return static
*/
public static function geocodeLocation( $input = NULL )
public static function geocodeLocation( string $input = NULL ): static
{
return \IPS\GeoLocation\GeoCoder::i()->decodeLocation( $input );
return GeoCoder::i()->decodeLocation( $input );
}
/**
* @brief Cached lookups to prevent duplicate lookups
*/
static protected $lookupCache = array( 'latlong' => array(), 'address' => array() );
static protected array $lookupCache = array( 'latlong' => array(), 'address' => array() );
/**
* Get by latitude and longitude
*
* @param float $lat Latitude
* @param float $long Longitude
* @return \IPS\GeoLocation
* @throws \BadFunctionCallException
* @throws \IPS\Http\Request\Exception
* @param float $lat Latitude
* @param float $long Longitude
* @return GeoLocation
* @throws BadFunctionCallException
* @throws Exception
*/
public static function getByLatLong( $lat, $long )
public static function getByLatLong( float $lat, float $long ): GeoLocation
{
if( isset( static::$lookupCache['latlong'][ $lat . 'x' . $long ] ) )
{
return static::$lookupCache['latlong'][ $lat . 'x' . $long ];
}
static::$lookupCache['latlong'][ $lat . 'x' . $long ] = \IPS\GeoLocation\GeoCoder::i()->decodeLatLong( $lat, $long );
static::$lookupCache['latlong'][ $lat . 'x' . $long ] = GeoCoder::i()->decodeLatLong( $lat, $long );
return static::$lookupCache['latlong'][ $lat . 'x' . $long ];
}
@@ -681,11 +736,11 @@ class _GeoLocation
/**
* Get the latitude and longitude for the current object. Address must be set.
*
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
* @return void
* @throws \BadMethodCallException
* @throws BadMethodCallException
*/
public function getLatLong( $setAddress = FALSE )
public function getLatLong(bool $setAddress=FALSE ) : void
{
$lookupKey = md5( $this->toString() );
@@ -701,7 +756,7 @@ class _GeoLocation
return;
}
\IPS\GeoLocation\GeoCoder::i()->setLatLong( $this, $setAddress );
GeoCoder::i()->setLatLong( $this, $setAddress );
static::$lookupCache['address'][ $lookupKey ] = json_encode( $this );
}
@@ -709,21 +764,30 @@ class _GeoLocation
/**
* Build from JSON
*
* @param string $json JSON data
* @return |IPS\GeoLocation
* @param string|null $json JSON data
* @return GeoLocation
*/
public static function buildFromJson( $json )
public static function buildFromJson( ?string $json ): static
{
$json = json_decode( $json, TRUE );
$obj = new static;
if ( !empty( $json ) )
if( empty( $json ) )
{
foreach ( $json as $k => $v )
{
$obj->$k = $v;
}
return new static;
}
return $obj;
if( $json = json_decode( $json, TRUE ) )
{
$obj = new static;
if ( !empty( $json ) )
{
foreach ( $json as $k => $v )
{
$obj->$k = $v;
}
}
return $obj;
}
return new static;
}
/**
@@ -739,13 +803,13 @@ class _GeoLocation
/**
* Convert to string
*
* @param string $separator Separator
* @param string|null $name Optional name to add to the address
* @param string $separator Separator
* @param string|null $name Optional name to add to the address
* @return string
* @note While some places like France capitalize the surname, this cannot be done automatically because the surname could be supplied as the first or last
name value, and the name could contain more than one string that constitutes the surname.
* name value, and the name could contain more than one string that constitutes the surname.
*/
public function toString( $separator=', ', $name=NULL )
public function toString( string $separator=', ', string $name=NULL ): string
{
$output = array();
@@ -758,7 +822,7 @@ class _GeoLocation
{
if ( isset( $this->$k ) and $this->$k )
{
if ( \is_array( $this->$k ) )
if ( is_array( $this->$k ) )
{
foreach ( $this->$k as $v )
{
@@ -774,13 +838,13 @@ class _GeoLocation
}
}
}
if ( $this->country and $this->country !== static::buildFromJson( \IPS\Settings::i()->site_address )->country )
if ( $this->country and $this->country !== static::buildFromJson( Settings::i()->site_address )->country )
{
try
{
$output[] = \IPS\Member::loggedIn()->language()->get( htmlspecialchars( 'country-' . $this->country, ENT_DISALLOWED, 'UTF-8', FALSE ), FALSE, array( 'strtoupper' => TRUE ) );
$output[] = strtoupper( Member::loggedIn()->language()->get( htmlspecialchars( 'country-' . $this->country, ENT_DISALLOWED, 'UTF-8', FALSE ) ) );
}
catch ( \UnderflowException $e )
catch ( UnderflowException $e )
{
$output[] = htmlspecialchars( $this->country, ENT_DISALLOWED, 'UTF-8', FALSE );
}
@@ -801,24 +865,24 @@ class _GeoLocation
/**
* Build Map
*
* @return \IPS\GeoLocation\Map
* @throws \BadMethodCallException
* @return mixed
* @throws BadMethodCallException
*/
public function map()
public function map(): mixed
{
if ( $this->map === NULL )
{
if ( \IPS\Settings::i()->googlemaps and \IPS\Settings::i()->google_maps_api_key )
if ( Settings::i()->googlemaps and Settings::i()->google_maps_api_key )
{
$this->map = new \IPS\GeoLocation\Maps\Google( $this );
$this->map = new Google( $this );
}
else if ( \IPS\Settings::i()->mapbox and \IPS\Settings::i()->mapbox_api_key)
else if ( Settings::i()->mapbox and Settings::i()->mapbox_api_key)
{
$this->map = new \IPS\GeoLocation\Maps\Mapbox( $this );
$this->map = new Mapbox( $this );
}
else
{
throw new \BadMethodCallException;
throw new BadMethodCallException;
}
}
@@ -828,15 +892,15 @@ class _GeoLocation
/**
* Return value to use in template
*
* @param string $data Data to parse
* @param string $data Data to parse
* @return string
*/
public static function parseForOutput( $data )
public static function parseForOutput( string $data ): string
{
$address = json_decode( $data, TRUE );
$mapper = new static;
if ( \is_array( $address ) )
if ( is_array( $address ) )
{
foreach( $address as $k => $v )
{
@@ -846,21 +910,21 @@ class _GeoLocation
return (string) $mapper;
}
/**
* Get output for API
*
* @param \IPS\Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
* @return array
* @apiresponse float lat Latitude
* @apiresponse float long Longitude
* @apiresponse [string] addressLines Lines of the street address
* @apiresponse string city City
* @apiresponse string region State/Region
* @apiresponse string country 2-letter country code
* @apiresponse string postalCode ZIP/Postal Code
* @param Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
* @return GeoLocation|array
* @apiresponse float lat Latitude
* @apiresponse float long Longitude
* @apiresponse [string] addressLines Lines of the street address
* @apiresponse string city City
* @apiresponse string region State/Region
* @apiresponse string country 2-letter country code
* @apiresponse string postalCode ZIP/Postal Code
*/
public function apiOutput( \IPS\Member $authorizedMember = NULL )
public function apiOutput( ?Member $authorizedMember = NULL ): GeoLocation|array
{
$returnObject = clone $this;
$returnObject->addressLines = array_values( $returnObject->addressLines );
@@ -873,8 +937,8 @@ class _GeoLocation
*
* @return bool
*/
public static function enabled()
public static function enabled(): bool
{
return ( ( \IPS\Settings::i()->googlemaps and \IPS\Settings::i()->google_maps_api_key ) || ( \IPS\Settings::i()->mapbox and \IPS\Settings::i()->mapbox_api_key ) );
return ( ( Settings::i()->googlemaps and Settings::i()->google_maps_api_key ) || ( Settings::i()->mapbox and Settings::i()->mapbox_api_key ) );
}
}