2012-02-09 21:10:12 -05:00
< ? php
/**
2012-02-19 08:28:43 -05:00
* Name : OpenStreetMap
2015-05-15 14:27:32 -04:00
* Description : Use OpenStreetMap for displaying locations . After activation the post location just beneath your avatar in your posts will link to OpenStreetMap .
* Version : 1.3 . 1
* Author : Fabio < http :// kirgroup . com /~ fabrixxm >
2012-02-09 21:10:12 -05:00
* Author : Mike Macgirvin < http :// macgirvin . com / profile / mike >
2012-02-19 08:28:43 -05:00
* Author : Klaus Weidenbach
2012-02-09 21:10:12 -05:00
*
*/
2019-02-03 16:22:03 -05:00
2022-06-23 01:16:22 -04:00
use Friendica\App ;
2021-10-23 04:49:26 -04:00
use Friendica\Core\Cache\Enum\Duration ;
2018-12-26 02:28:16 -05:00
use Friendica\Core\Hook ;
2018-10-29 19:40:18 -04:00
use Friendica\Core\Logger ;
2018-10-31 10:55:15 -04:00
use Friendica\Core\Renderer ;
2020-03-04 16:07:04 -05:00
use Friendica\DI ;
2023-01-01 14:21:56 -05:00
use Friendica\Core\Config\Util\ConfigFileManager ;
2018-11-08 11:45:19 -05:00
use Friendica\Util\Strings ;
2012-02-09 21:10:12 -05:00
2018-06-27 23:18:19 -04:00
const OSM_TMS = 'https://www.openstreetmap.org' ;
const OSM_NOM = 'https://nominatim.openstreetmap.org/search.php' ;
2018-03-20 02:43:03 -04:00
const OSM_ZOOM = 16 ;
const OSM_MARKER = 0 ;
2017-11-09 11:08:32 -05:00
function openstreetmap_install ()
{
2018-12-26 02:28:16 -05:00
Hook :: register ( 'load_config' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_load_config' );
Hook :: register ( 'render_location' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_location' );
Hook :: register ( 'generate_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_map' );
Hook :: register ( 'generate_named_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_named_map' );
Hook :: register ( 'Map::getCoordinates' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_get_coordinates' );
Hook :: register ( 'page_header' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_alterheader' );
2012-02-09 21:10:12 -05:00
2021-10-21 02:04:27 -04:00
Logger :: notice ( " installed openstreetmap " );
2012-02-09 21:10:12 -05:00
}
2023-01-01 14:21:56 -05:00
function openstreetmap_load_config ( App $a , ConfigFileManager $loader )
2018-06-27 23:18:19 -04:00
{
2022-11-20 12:44:53 -05:00
$a -> getConfigCache () -> load ( $loader -> loadAddonConfig ( 'openstreetmap' ), \Friendica\Core\Config\ValueObject\Cache :: SOURCE_STATIC );
2018-06-27 23:18:19 -04:00
}
2022-06-23 01:16:22 -04:00
function openstreetmap_alterheader ( App $a , & $navHtml )
2017-11-09 11:08:32 -05:00
{
2019-12-29 20:20:55 -05:00
$addScriptTag = '<script type="text/javascript" src="' . DI :: baseUrl () -> get () . '/addon/openstreetmap/openstreetmap.js"></script>' . " \r \n " ;
2019-12-30 15:53:43 -05:00
DI :: page ()[ 'htmlhead' ] .= $addScriptTag ;
2013-01-08 18:41:27 -05:00
}
2012-02-09 21:10:12 -05:00
2015-05-15 14:27:32 -04:00
/**
* @ brief Add link to a map for an item ' s set location / coordinates .
*
* If an item has coordinates add link to a tile map server , e . g . openstreetmap . org .
* If an item has a location open it with the help of OSM ' s Nominatim reverse geocode search .
2018-01-15 08:15:33 -05:00
*
2015-05-15 14:27:32 -04:00
* @ param mixed $a
* @ param array & $item
*/
2022-06-23 01:16:22 -04:00
function openstreetmap_location ( App $a , & $item )
2018-03-20 02:43:03 -04:00
{
if ( ! ( strlen ( $item [ 'location' ]) || strlen ( $item [ 'coord' ]))) {
2013-01-08 18:41:27 -05:00
return ;
2017-11-09 11:08:32 -05:00
}
2012-02-09 22:53:56 -05:00
2012-02-19 08:28:43 -05:00
/*
2015-05-15 14:27:32 -04:00
* Get the configuration variables from the config .
2015-06-30 01:31:27 -04:00
* @ todo Separate the tile map server from the text - string to map tile server
2015-05-15 14:27:32 -04:00
* since they apparently use different URL conventions .
* We use OSM ' s current convention of " #map=zoom/lat/lon " and optional
* ? mlat = lat & mlon = lon for markers .
*/
2020-01-19 15:21:12 -05:00
$tmsserver = DI :: config () -> get ( 'openstreetmap' , 'tmsserver' , OSM_TMS );
$nomserver = DI :: config () -> get ( 'openstreetmap' , 'nomserver' , OSM_NOM );
$zoom = DI :: config () -> get ( 'openstreetmap' , 'zoom' , OSM_ZOOM );
$marker = DI :: config () -> get ( 'openstreetmap' , 'marker' , OSM_MARKER );
2012-02-19 08:28:43 -05:00
2018-03-20 02:43:03 -04:00
// This is needed since we stored an empty string in the config in previous versions
if ( empty ( $nomserver )) {
$nomserver = OSM_NOM ;
2017-11-09 11:08:32 -05:00
}
2014-05-29 06:42:25 -04:00
2022-06-30 07:32:13 -04:00
if ( $item [ 'coord' ] != '' ) {
2012-02-09 21:10:12 -05:00
$coords = explode ( ' ' , $item [ 'coord' ]);
2022-09-08 00:16:17 -04:00
if (( count ( $coords ) > 1 ) && is_numeric ( $coords [ 0 ]) && is_numeric ( $coords [ 1 ])) {
2015-05-15 14:27:32 -04:00
$lat = urlencode ( round ( $coords [ 0 ], 5 ));
$lon = urlencode ( round ( $coords [ 1 ], 5 ));
2015-06-30 01:31:27 -04:00
$target = $tmsserver ;
2018-03-20 02:43:03 -04:00
if ( $marker > 0 ) {
$target .= '?mlat=' . $lat . '&mlon=' . $lon ;
}
2015-06-30 01:31:27 -04:00
$target .= '#map=' . intval ( $zoom ) . '/' . $lat . '/' . $lon ;
2012-02-09 21:10:12 -05:00
}
}
2015-06-30 01:31:27 -04:00
2018-07-10 08:30:23 -04:00
if ( empty ( $target )) {
2015-06-30 01:31:27 -04:00
$target = $nomserver . '?q=' . urlencode ( $item [ 'location' ]);
2017-11-09 11:08:32 -05:00
}
2015-06-30 01:31:27 -04:00
2022-06-30 07:32:13 -04:00
if ( $item [ 'location' ] != '' ) {
2015-06-30 01:31:27 -04:00
$title = $item [ 'location' ];
2017-11-09 11:08:32 -05:00
} else {
2015-06-30 01:31:27 -04:00
$title = $item [ 'coord' ];
2017-11-09 11:08:32 -05:00
}
2015-06-30 01:31:27 -04:00
2022-06-30 07:32:13 -04:00
$item [ 'html' ] = '<a target="map" title="' . $title . '" href= "' . $target . '">' . $title . '</a>' ;
2012-02-09 21:10:12 -05:00
}
2022-06-23 01:16:22 -04:00
function openstreetmap_get_coordinates ( App $a , array & $b )
2017-11-09 11:08:32 -05:00
{
2020-01-19 15:21:12 -05:00
$nomserver = DI :: config () -> get ( 'openstreetmap' , 'nomserver' , OSM_NOM );
2018-03-20 02:43:03 -04:00
// This is needed since we stored an empty string in the config in previous versions
if ( empty ( $nomserver )) {
$nomserver = OSM_NOM ;
}
2015-05-15 14:27:32 -04:00
$args = '?q=' . urlencode ( $b [ 'location' ]) . '&format=json' ;
2022-06-30 07:32:13 -04:00
$cachekey = 'openstreetmap:' . $b [ 'location' ];
2020-01-12 09:36:43 -05:00
$j = DI :: cache () -> get ( $cachekey );
2015-06-30 01:31:27 -04:00
2018-03-20 02:43:03 -04:00
if ( is_null ( $j )) {
2021-08-25 15:54:54 -04:00
$curlResult = DI :: httpClient () -> get ( $nomserver . $args );
2018-10-11 01:40:57 -04:00
if ( $curlResult -> isSuccess ()) {
$j = json_decode ( $curlResult -> getBody (), true );
2020-01-18 09:41:18 -05:00
DI :: cache () -> set ( $cachekey , $j , Duration :: MONTH );
2015-05-15 14:27:32 -04:00
}
}
2018-03-20 02:43:03 -04:00
if ( ! empty ( $j [ 0 ][ 'lat' ]) && ! empty ( $j [ 0 ][ 'lon' ])) {
$b [ 'lat' ] = $j [ 0 ][ 'lat' ];
$b [ 'lon' ] = $j [ 0 ][ 'lon' ];
}
2015-05-15 14:27:32 -04:00
}
2022-06-23 01:16:22 -04:00
function openstreetmap_generate_named_map ( App $a , array & $b )
2017-11-09 11:08:32 -05:00
{
2018-03-20 02:43:03 -04:00
openstreetmap_get_coordinates ( $a , $b );
if ( ! empty ( $b [ 'lat' ]) && ! empty ( $b [ 'lon' ])) {
openstreetmap_generate_map ( $a , $b );
2017-11-09 11:08:32 -05:00
}
2018-03-20 02:43:03 -04:00
}
2015-05-15 14:27:32 -04:00
2022-06-23 01:16:22 -04:00
function openstreetmap_generate_map ( App $a , array & $b )
2018-03-20 02:43:03 -04:00
{
2020-01-19 15:21:12 -05:00
$tmsserver = DI :: config () -> get ( 'openstreetmap' , 'tmsserver' , OSM_TMS );
2015-05-15 14:27:32 -04:00
2019-12-29 20:20:55 -05:00
if ( strpos ( DI :: baseUrl () -> get ( true ), 'https:' ) !== false ) {
2018-03-20 02:43:03 -04:00
$tmsserver = str_replace ( 'http:' , 'https:' , $tmsserver );
2017-11-09 11:08:32 -05:00
}
2015-05-15 14:27:32 -04:00
2020-01-19 15:21:12 -05:00
$zoom = DI :: config () -> get ( 'openstreetmap' , 'zoom' , OSM_ZOOM );
$marker = DI :: config () -> get ( 'openstreetmap' , 'marker' , OSM_MARKER );
2015-05-15 14:27:32 -04:00
$lat = $b [ 'lat' ]; // round($b['lat'], 5);
$lon = $b [ 'lon' ]; // round($b['lon'], 5);
2021-10-21 02:04:27 -04:00
Logger :: debug ( 'lat: ' . $lat );
Logger :: debug ( 'lon: ' . $lon );
2015-05-15 14:27:32 -04:00
2018-03-20 02:43:03 -04:00
$cardlink = '<a href="' . $tmsserver ;
2015-05-15 14:27:32 -04:00
2018-03-20 02:43:03 -04:00
if ( $marker > 0 ) {
$cardlink .= '?mlat=' . $lat . '&mlon=' . $lon ;
}
2012-02-19 17:26:41 -05:00
2021-05-31 01:44:17 -04:00
$cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ( $b [ 'location' ] ? ? 0 ? Strings :: escapeHtml ( $b [ 'location' ]) : DI :: l10n () -> t ( 'View Larger' )) . '</a>' ;
2018-03-20 02:43:03 -04:00
if ( empty ( $b [ 'mode' ])) {
$b [ 'html' ] = '<iframe style="width:100%; height:300px; border:1px solid #ccc" src="' . $tmsserver .
'/export/embed.html?bbox=' . ( $lon - 0.01 ) . '%2C' . ( $lat - 0.01 ) . '%2C' . ( $lon + 0.01 ) . '%2C' . ( $lat + 0.01 ) .
'&layer=mapnik&marker=' . $lat . '%2C' . $lon . '" style="border: 1px solid black"></iframe>' .
'<br/><small>' . $cardlink . '</small>' ;
} else {
$b [ 'html' ] .= '<br/>' . $cardlink ;
}
2015-05-15 14:27:32 -04:00
2021-10-21 02:04:27 -04:00
Logger :: debug ( 'generate_map: ' . $b [ 'html' ]);
2015-05-15 14:27:32 -04:00
}
2022-06-30 07:32:13 -04:00
function openstreetmap_addon_admin ( App $a , string & $o )
2017-11-09 11:08:32 -05:00
{
2022-06-30 07:32:13 -04:00
$t = Renderer :: getMarkupTemplate ( 'admin.tpl' , 'addon/openstreetmap/' );
2020-01-19 15:21:12 -05:00
$tmsserver = DI :: config () -> get ( 'openstreetmap' , 'tmsserver' , OSM_TMS );
$nomserver = DI :: config () -> get ( 'openstreetmap' , 'nomserver' , OSM_NOM );
$zoom = DI :: config () -> get ( 'openstreetmap' , 'zoom' , OSM_ZOOM );
$marker = DI :: config () -> get ( 'openstreetmap' , 'marker' , OSM_MARKER );
2018-03-20 02:43:03 -04:00
// This is needed since we stored an empty string in the config in previous versions
if ( empty ( $nomserver )) {
$nomserver = OSM_NOM ;
2017-11-09 11:08:32 -05:00
}
2015-05-15 14:27:32 -04:00
2018-10-31 10:55:15 -04:00
$o = Renderer :: replaceMacros ( $t , [
2020-01-18 14:52:33 -05:00
'$submit' => DI :: l10n () -> t ( 'Submit' ),
2020-02-24 20:08:43 -05:00
'$tmsserver' => [ 'tmsserver' , DI :: l10n () -> t ( 'Tile Server URL' ), $tmsserver , DI :: l10n () -> t ( 'A list of <a href="http://wiki.openstreetmap.org/wiki/TMS" target="_blank" rel="noopener noreferrer">public tile servers</a>' )],
'$nomserver' => [ 'nomserver' , DI :: l10n () -> t ( 'Nominatim (reverse geocoding) Server URL' ), $nomserver , DI :: l10n () -> t ( 'A list of <a href="http://wiki.openstreetmap.org/wiki/Nominatim" target="_blank" rel="noopener noreferrer">Nominatim servers</a>' )],
2020-01-18 14:52:33 -05:00
'$zoom' => [ 'zoom' , DI :: l10n () -> t ( 'Default zoom' ), $zoom , DI :: l10n () -> t ( 'The default zoom level. (1:world, 18:highest, also depends on tile server)' )],
'$marker' => [ 'marker' , DI :: l10n () -> t ( 'Include marker on map' ), $marker , DI :: l10n () -> t ( 'Include a marker on the map.' )],
2018-01-15 08:15:33 -05:00
]);
2012-02-19 08:28:43 -05:00
}
2017-11-09 11:08:32 -05:00
2022-06-23 01:16:22 -04:00
function openstreetmap_addon_admin_post ( App $a )
2017-11-09 11:08:32 -05:00
{
2022-06-30 07:32:13 -04:00
DI :: config () -> set ( 'openstreetmap' , 'tmsserver' , ( $_POST [ 'tmsserver' ] ? ? '' ) ? : OSM_TMS );
DI :: config () -> set ( 'openstreetmap' , 'nomserver' , ( $_POST [ 'nomserver' ] ? ? '' ) ? : OSM_NOM );
DI :: config () -> set ( 'openstreetmap' , 'zoom' , ( $_POST [ 'zoom' ] ? ? '' ) ? : OSM_ZOOM );
DI :: config () -> set ( 'openstreetmap' , 'marker' , ( $_POST [ 'marker' ] ? ? '' ) ? : OSM_MARKER );
2012-02-19 08:28:43 -05:00
}