Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,23 +11,30 @@
|
||||
namespace IPS\Geolocation\Api\GraphQL;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use IPS\Api\GraphQL\TypeRegistry;
|
||||
use IPS\GeoLocation;
|
||||
use IPS\Member;
|
||||
use IPS\Settings;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
use function strtoupper;
|
||||
|
||||
/* 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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base class for GeoLocation
|
||||
*/
|
||||
class _GeoLocationType extends ObjectType
|
||||
class GeoLocationType extends ObjectType
|
||||
{
|
||||
/**
|
||||
* Get root type
|
||||
*
|
||||
* @return array
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -106,20 +113,20 @@ class _GeoLocationType extends ObjectType
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
protected static function buildAddressPiece($geolocation, $type)
|
||||
protected static function buildAddressPiece($geolocation, $type): string
|
||||
{
|
||||
$output = array();
|
||||
if ( $geolocation->$type )
|
||||
{
|
||||
if ( $type == 'country' )
|
||||
{
|
||||
if( $geolocation->country !== \IPS\GeoLocation::buildFromJson( \IPS\Settings::i()->site_address )->country )
|
||||
if( $geolocation->country !== GeoLocation::buildFromJson( Settings::i()->site_address )->country )
|
||||
{
|
||||
try
|
||||
{
|
||||
$output[] = \IPS\Member::loggedIn()->language()->get( htmlspecialchars( 'country-' . $geolocation->country, ENT_DISALLOWED, 'UTF-8', FALSE ), FALSE, array( 'strtoupper' => TRUE ) );
|
||||
$output[] = strtoupper( Member::loggedIn()->language()->get( htmlspecialchars( 'country-' . $geolocation->country, ENT_DISALLOWED, 'UTF-8', FALSE ) ) );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
$output[] = htmlspecialchars( $geolocation->country, ENT_DISALLOWED, 'UTF-8', FALSE );
|
||||
}
|
||||
@@ -127,7 +134,7 @@ class _GeoLocationType extends ObjectType
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( \is_array( $geolocation->$type ) )
|
||||
if ( is_array( $geolocation->$type ) )
|
||||
{
|
||||
foreach ( $geolocation->$type as $v )
|
||||
{
|
||||
|
||||
@@ -10,21 +10,21 @@
|
||||
*/
|
||||
|
||||
namespace IPS\GeoLocation\Api\GraphQL;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use function defined;
|
||||
|
||||
/* 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' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* \IPS\GeoLocation base types
|
||||
*/
|
||||
class _TypeRegistry
|
||||
class TypeRegistry
|
||||
{
|
||||
protected static $geolocation;
|
||||
protected static ?GeoLocationType $geolocation;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -37,8 +37,8 @@ class _TypeRegistry
|
||||
/**
|
||||
* @return GeoLocationType
|
||||
*/
|
||||
public static function geolocation()
|
||||
public static function geolocation() : GeoLocationType
|
||||
{
|
||||
return self::$geolocation ?: (self::$geolocation = new \IPS\GeoLocation\Api\GraphQL\GeoLocationType());
|
||||
return self::$geolocation ?: (self::$geolocation = new GeoLocationType());
|
||||
}
|
||||
}
|
||||
@@ -11,43 +11,50 @@
|
||||
namespace IPS\GeoLocation;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use BadMethodCallException;
|
||||
use IPS\GeoLocation\GeoCoder\Google;
|
||||
use IPS\GeoLocation\GeoCoder\Mapbox;
|
||||
use IPS\Settings;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* GeoCoder abstract class
|
||||
*/
|
||||
abstract class _GeoCoder
|
||||
abstract class GeoCoder
|
||||
{
|
||||
/**
|
||||
* @brief Cached GeoCoder instance
|
||||
*/
|
||||
protected static $instance = NULL;
|
||||
protected static GeoCoder|null $instance = NULL;
|
||||
|
||||
/**
|
||||
* Return instance of GeoCoder
|
||||
*
|
||||
* @return \IPS\GeoCoder
|
||||
* @throws \BadMethodCallException
|
||||
* @return GeoCoder|null
|
||||
* @throws BadMethodCallException
|
||||
*/
|
||||
public static function i()
|
||||
public static function i(): GeoCoder|null
|
||||
{
|
||||
if( static::$instance === 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 )
|
||||
{
|
||||
static::$instance = new \IPS\GeoLocation\GeoCoder\Google();
|
||||
static::$instance = new Google();
|
||||
}
|
||||
elseif ( \IPS\Settings::i()->mapbox and \IPS\Settings::i()->mapbox_api_key )
|
||||
elseif ( Settings::i()->mapbox and Settings::i()->mapbox_api_key )
|
||||
{
|
||||
static::$instance = new \IPS\GeoLocation\GeoCoder\Mapbox();
|
||||
static::$instance = new Mapbox();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadMethodCallException;
|
||||
throw new BadMethodCallException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,40 +11,53 @@
|
||||
namespace IPS\GeoLocation\GeoCoder;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use BadFunctionCallException;
|
||||
use BadMethodCallException;
|
||||
use IPS\GeoLocation;
|
||||
use IPS\GeoLocation\GeoCoder;
|
||||
use IPS\Http\Request\Exception;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Settings;
|
||||
use RuntimeException;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Google GeoCoder class
|
||||
*/
|
||||
class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
class Google extends GeoCoder
|
||||
{
|
||||
/**
|
||||
* Get by location string
|
||||
*
|
||||
* @param string $location
|
||||
* @return \IPS\GeoLocation
|
||||
* @throws \BadFunctionCallException
|
||||
* @return GeoLocation
|
||||
* @throws BadFunctionCallException
|
||||
*/
|
||||
public static function decodeLocation( string $location )
|
||||
public static function decodeLocation( string $location ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->googlemaps AND \IPS\Settings::i()->google_maps_api_key )
|
||||
if ( Settings::i()->googlemaps AND Settings::i()->google_maps_api_key )
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
$data = Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
'address' => $location,
|
||||
'sensor' => 'false',
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key_secret ?: \IPS\Settings::i()->google_maps_api_key
|
||||
'key' => Settings::i()->google_maps_api_key_secret ?: Settings::i()->google_maps_api_key
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Google is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
if( isset( $data['results'][0]['geometry'] ) AND $data['results'][0]['geometry']['location'] )
|
||||
{
|
||||
@@ -83,6 +96,11 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -99,37 +117,37 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 decodeLatLong( $lat, $long )
|
||||
public static function decodeLatLong( float $lat, float $long ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->googlemaps AND \IPS\Settings::i()->google_maps_api_key )
|
||||
if ( Settings::i()->googlemaps AND Settings::i()->google_maps_api_key )
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
$data = Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
'latlng' => "{$lat},{$long}",
|
||||
'sensor' => 'false',
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key_secret ?: \IPS\Settings::i()->google_maps_api_key
|
||||
'key' => Settings::i()->google_maps_api_key_secret ?: Settings::i()->google_maps_api_key
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
$obj->lat = $lat;
|
||||
$obj->long = $long;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Google is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
foreach( $data['results'][0]['address_components'] as $component )
|
||||
{
|
||||
@@ -157,6 +175,11 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -173,36 +196,36 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latitude and longitude for the current object. Address must be set.
|
||||
*
|
||||
* @param \IPS\GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @param GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @return void
|
||||
* @throws \BadMethodCallException
|
||||
* @throws BadMethodCallException
|
||||
*/
|
||||
public function setLatLong( \IPS\GeoLocation &$geoLocation, $setAddress = FALSE )
|
||||
public function setLatLong(GeoLocation &$geoLocation, bool $setAddress=FALSE ) : void
|
||||
{
|
||||
if ( \IPS\Settings::i()->googlemaps AND \IPS\Settings::i()->google_maps_api_key AND $geoLocation->toString() )
|
||||
if ( Settings::i()->googlemaps AND Settings::i()->google_maps_api_key AND $geoLocation->toString() )
|
||||
{
|
||||
try
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
$data = Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
'address' => $geoLocation->toString(),
|
||||
'sensor' => 'false',
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key_secret ?: \IPS\Settings::i()->google_maps_api_key
|
||||
'key' => Settings::i()->google_maps_api_key_secret ?: Settings::i()->google_maps_api_key
|
||||
) )->request()->get()->decodeJson();
|
||||
}
|
||||
catch( \RuntimeException $e )
|
||||
catch( RuntimeException $e )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !\count( $data['results'] ) )
|
||||
if ( !count( $data['results'] ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -257,7 +280,7 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,38 +11,51 @@
|
||||
namespace IPS\GeoLocation\GeoCoder;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use BadFunctionCallException;
|
||||
use BadMethodCallException;
|
||||
use IPS\GeoLocation;
|
||||
use IPS\GeoLocation\GeoCoder;
|
||||
use IPS\Http\Request\Exception;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Settings;
|
||||
use RuntimeException;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapbox GeoCoder class
|
||||
*/
|
||||
class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
class Mapbox extends GeoCoder
|
||||
{
|
||||
/**
|
||||
* Get by location string
|
||||
*
|
||||
* @param string $location
|
||||
* @return \IPS\GeoLocation
|
||||
* @throws \BadFunctionCallException
|
||||
* @return GeoLocation
|
||||
* @throws BadFunctionCallException
|
||||
*/
|
||||
public static function decodeLocation( string $location )
|
||||
public static function decodeLocation( string $location ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->mapbox AND \IPS\Settings::i()->mapbox_api_key )
|
||||
if ( Settings::i()->mapbox AND Settings::i()->mapbox_api_key )
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => \IPS\Settings::i()->mapbox_api_key,
|
||||
$data = Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => Settings::i()->mapbox_api_key,
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Mapbox is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
if( isset( $data['results'][0]['geometry'] ) AND isset( $data['results'][0]['geometry']['coordinates'] ) )
|
||||
{
|
||||
@@ -78,6 +91,11 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -94,36 +112,36 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 decodeLatLong( $lat, $long )
|
||||
public static function decodeLatLong( float $lat, float $long ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->mapbox AND \IPS\Settings::i()->mapbox_api_key )
|
||||
if ( Settings::i()->mapbox AND Settings::i()->mapbox_api_key )
|
||||
{
|
||||
$location = $long . ',' . $lat;
|
||||
$data = \IPS\Http\Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => \IPS\Settings::i()->mapbox_api_key,
|
||||
$data = Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => Settings::i()->mapbox_api_key,
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
$obj->lat = $lat;
|
||||
$obj->long = $long;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Mapbox is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
foreach( $data['results'][0]['address_components'] as $component )
|
||||
{
|
||||
@@ -151,6 +169,11 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -167,34 +190,34 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latitude and longitude for the current object. Address must be set.
|
||||
*
|
||||
* @param \IPS\GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @param GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @return void
|
||||
* @throws \BadMethodCallException
|
||||
* @throws BadMethodCallException
|
||||
*/
|
||||
public function setLatLong( \IPS\GeoLocation &$geoLocation, $setAddress = FALSE )
|
||||
public function setLatLong( GeoLocation &$geoLocation, bool $setAddress=FALSE ) : void
|
||||
{
|
||||
if ( \IPS\Settings::i()->mapbox AND \IPS\Settings::i()->mapbox_api_key AND $geoLocation->toString() )
|
||||
if ( Settings::i()->mapbox AND Settings::i()->mapbox_api_key AND $geoLocation->toString() )
|
||||
{
|
||||
try
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/" . urlencode( html_entity_decode( $geoLocation->toString() ) ) . ".json" )->setQueryString( array(
|
||||
'access_token' => \IPS\Settings::i()->mapbox_api_key,
|
||||
$data = Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/" . urlencode( html_entity_decode( $geoLocation->toString() ) ) . ".json" )->setQueryString( array(
|
||||
'access_token' => Settings::i()->mapbox_api_key,
|
||||
) )->request()->get()->decodeJson();
|
||||
}
|
||||
catch( \RuntimeException $e )
|
||||
catch( RuntimeException $e )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !isset( $data['features'] ) or !\count( $data['features'] ) )
|
||||
if ( !isset( $data['features'] ) or !count( $data['features'] ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -206,7 +229,7 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
|
||||
if( $setAddress === TRUE )
|
||||
{
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
foreach( $data['results'][0]['address_components'] as $component )
|
||||
{
|
||||
@@ -234,6 +257,11 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
$geoLocation->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$geoLocation->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$geoLocation->city = $component['long_name'];
|
||||
@@ -249,7 +277,7 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
}
|
||||
+176
-112
@@ -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 ) );
|
||||
}
|
||||
}
|
||||
@@ -11,29 +11,39 @@
|
||||
namespace IPS\GeoLocation\Maps;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\GeoLocation;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\Output;
|
||||
use IPS\Settings;
|
||||
use IPS\Theme;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Google Maps
|
||||
*/
|
||||
class _Google
|
||||
class Google
|
||||
{
|
||||
/**
|
||||
* @brief GeoLocation
|
||||
*/
|
||||
public $geoLocation;
|
||||
public ?GeoLocation $geolocation = NULL;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \IPS\GeoLocation $geoLocation Location
|
||||
* @param GeoLocation $geoLocation Location
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( \IPS\GeoLocation $geoLocation )
|
||||
public function __construct( GeoLocation $geoLocation )
|
||||
{
|
||||
$this->geolocation = $geoLocation;
|
||||
}
|
||||
@@ -41,50 +51,61 @@ class _Google
|
||||
/**
|
||||
* Render
|
||||
*
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|NULL $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @param int $scale Google maps scale to use (https://developers.google.com/maps/documentation/static-maps/intro#scale_values)
|
||||
* @param string $maptype Type of map to use. Valid values are roadmap (default), satellite, terrain, and hybrid (https://developers.google.com/maps/documentation/static-maps/intro#MapTypes)
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|null $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @param int $scale Google maps scale to use (https://developers.google.com/maps/documentation/static-maps/intro#scale_values)
|
||||
* @param string $maptype Type of map to use. Valid values are roadmap (default), satellite, terrain, and hybrid (https://developers.google.com/maps/documentation/static-maps/intro#MapTypes)
|
||||
* @return string
|
||||
*/
|
||||
public function render( $width, $height, $zoom=NULL, $scale=1, $maptype='roadmap' )
|
||||
public function render( int $width, int $height, float $zoom=NULL, int $scale=1, string $maptype='roadmap' ): string
|
||||
{
|
||||
\IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'global_core.js', 'core', 'global' ) );
|
||||
/* Check permissions */
|
||||
if( Settings::i()->google_maps_groups != '*' and !Member::loggedIn()->inGroup( explode( ",", Settings::i()->google_maps_groups ) ) )
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
return \IPS\Settings::i()->google_maps_static_use_embed ?
|
||||
\IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->googleMap( array(
|
||||
Output::i()->jsFiles = array_merge( Output::i()->jsFiles, Output::i()->js( 'global_core.js', 'core', 'global' ) );
|
||||
|
||||
if( empty( $zoom ) )
|
||||
{
|
||||
$zoom = Settings::i()->google_maps_zoom ?: null;
|
||||
}
|
||||
|
||||
return Settings::i()->google_maps_static_use_embed ?
|
||||
Theme::i()->getTemplate( 'global', 'core', 'global' )->googleMap( array(
|
||||
'lat' => $this->geolocation->lat,
|
||||
'long' => $this->geolocation->long,
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key,
|
||||
'key' => Settings::i()->google_maps_api_key,
|
||||
'zoom' => $zoom,
|
||||
'scale' => $scale,
|
||||
'maptype' => $maptype,
|
||||
'width' => $width,
|
||||
'height' => $height )) :
|
||||
\IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->staticMap( \IPS\Http\Url::external( 'https://maps.google.com/' )->setQueryString( 'q', $this->_getLocation() ), $this->mapUrl( $width, $height, $zoom, $scale, $maptype ), $this->geolocation->lat, $this->geolocation->long, $width, $height );
|
||||
Theme::i()->getTemplate( 'global', 'core', 'global' )->staticMap( Url::external( 'https://maps.google.com/' )->setQueryString( 'q', $this->_getLocation() ), $this->mapUrl( $width, $height, $zoom, $scale, $maptype ), $this->geolocation->lat, $this->geolocation->long, $width, $height );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the map image URL
|
||||
*
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|NULL $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @param int $scale Google maps scale to use (https://developers.google.com/maps/documentation/static-maps/intro#scale_values)
|
||||
* @param string $maptype Type of map to use. Valid values are roadmap (default), satellite, terrain, and hybrid (https://developers.google.com/maps/documentation/static-maps/intro#MapTypes)
|
||||
* @return \IPS\Http\Url|NULL
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|null $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @param int $scale Google maps scale to use (https://developers.google.com/maps/documentation/static-maps/intro#scale_values)
|
||||
* @param string $maptype Type of map to use. Valid values are roadmap (default), satellite, terrain, and hybrid (https://developers.google.com/maps/documentation/static-maps/intro#MapTypes)
|
||||
* @return Url|NULL
|
||||
*/
|
||||
public function mapUrl( $width, $height, $zoom=NULL, $scale=1, $maptype='roadmap' )
|
||||
public function mapUrl( int $width, int $height, float $zoom=NULL, int $scale=1, string $maptype='roadmap' ): ?Url
|
||||
{
|
||||
$location = $this->_getLocation();
|
||||
|
||||
return \IPS\Http\Url::external( 'https://maps.googleapis.com/maps/api/staticmap' )->setQueryString( array(
|
||||
return Url::external( 'https://maps.googleapis.com/maps/api/staticmap' )->setQueryString( array(
|
||||
'center' => $location,
|
||||
'zoom' => $zoom === NULL ? NULL : ceil( $zoom * 8 ),
|
||||
'size' => "{$width}x{$height}",
|
||||
'markers' => $location,
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key,
|
||||
'key' => Settings::i()->google_maps_api_key,
|
||||
'scale' => $scale,
|
||||
'maptype' => $maptype
|
||||
) );
|
||||
@@ -95,7 +116,7 @@ class _Google
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _getLocation()
|
||||
protected function _getLocation(): string
|
||||
{
|
||||
if ( $this->geolocation->lat and $this->geolocation->long )
|
||||
{
|
||||
@@ -108,7 +129,7 @@ class _Google
|
||||
{
|
||||
if ( $this->geolocation->$k )
|
||||
{
|
||||
if ( \is_array( $this->geolocation->$k ) )
|
||||
if ( is_array( $this->geolocation->$k ) )
|
||||
{
|
||||
foreach ( array_reverse( $this->geolocation->$k ) as $v )
|
||||
{
|
||||
|
||||
@@ -11,29 +11,37 @@
|
||||
namespace IPS\GeoLocation\Maps;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\GeoLocation;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\Settings;
|
||||
use IPS\Theme;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapbox Maps
|
||||
*/
|
||||
class _Mapbox
|
||||
class Mapbox
|
||||
{
|
||||
/**
|
||||
* @brief GeoLocation
|
||||
*/
|
||||
public $geoLocation;
|
||||
public ?GeoLocation $geolocation = NULL;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \IPS\GeoLocation $geoLocation Location
|
||||
* @param GeoLocation $geoLocation Location
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( \IPS\GeoLocation $geoLocation )
|
||||
public function __construct( GeoLocation $geoLocation )
|
||||
{
|
||||
$this->geolocation = $geoLocation;
|
||||
}
|
||||
@@ -41,30 +49,41 @@ class _Mapbox
|
||||
/**
|
||||
* Render
|
||||
*
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|NULL $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|null $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @return string
|
||||
*/
|
||||
public function render( $width, $height, $zoom=NULL )
|
||||
public function render( int $width, int $height, float $zoom=NULL ): string
|
||||
{
|
||||
if( !($this->geolocation->long and $this->geolocation->lat ) )
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->staticMap( NULL, $this->mapUrl( $width, $height, $zoom ), $this->geolocation->lat, $this->geolocation->long, $width, $height );
|
||||
/* Check group permissions */
|
||||
if( Settings::i()->mapbox_groups != '*' and !Member::loggedIn()->inGroup( explode( ",", Settings::i()->mapbox_groups ) ) )
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
if( empty( $zoom ) )
|
||||
{
|
||||
$zoom = Settings::i()->mapbox_zoom ?: null;
|
||||
}
|
||||
|
||||
return Theme::i()->getTemplate( 'global', 'core', 'global' )->staticMap( NULL, $this->mapUrl( $width, $height, $zoom ), $this->geolocation->lat, $this->geolocation->long, $width, $height );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the map image URL
|
||||
*
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|NULL $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @return \IPS\Http\Url|NULL
|
||||
* @param int $width Width
|
||||
* @param int $height Height
|
||||
* @param float|null $zoom The zoom amount (a value between 0 being totally zoomed out view of the world, and 1 being as fully zoomed in as possible) or NULL to zoom automatically based on how much data is available
|
||||
* @return Url|NULL
|
||||
*/
|
||||
public function mapUrl( $width, $height, $zoom=NULL )
|
||||
public function mapUrl( int $width, int $height, float $zoom=NULL ): ?Url
|
||||
{
|
||||
if( !($this->geolocation->long and $this->geolocation->lat ) )
|
||||
{
|
||||
@@ -73,8 +92,9 @@ class _Mapbox
|
||||
|
||||
$location = str_replace( ',', '.', $this->geolocation->long ) . ',' . str_replace( ',', '.', $this->geolocation->lat );
|
||||
|
||||
return \IPS\Http\Url::external( "https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/pin-l-marker+f00({$location})/{$location},14,0,60/{$width}x{$height}@2x" )->setQueryString( array(
|
||||
'access_token' => \IPS\Settings::i()->mapbox_api_key,
|
||||
return Url::external( "https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/pin-l-marker+f00({$location})/{$location},14,0,60/{$width}x{$height}@2x" )->setQueryString( array(
|
||||
'access_token' => Settings::i()->mapbox_api_key,
|
||||
'zoom' => $zoom
|
||||
) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user