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

+57 -34
View File
@@ -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;
}
}
}