2012-02-10 03:10:12 +01:00
< ? php
/**
2012-02-19 14:28:43 +01:00
* Name : OpenStreetMap
2015-05-15 20:27:32 +02: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-10 03:10:12 +01:00
* Author : Mike Macgirvin < http :// macgirvin . com / profile / mike >
2012-02-19 14:28:43 +01:00
* Author : Klaus Weidenbach
2012-02-10 03:10:12 +01:00
*
*/
2019-02-03 22:22:03 +01:00
2017-11-09 17:08:32 +01:00
use Friendica\Core\Cache ;
2017-11-07 00:55:24 +01:00
use Friendica\Core\Config ;
2018-12-26 08:28:16 +01:00
use Friendica\Core\Hook ;
2018-01-22 20:03:11 +01:00
use Friendica\Core\L10n ;
2018-10-30 00:40:18 +01:00
use Friendica\Core\Logger ;
2018-10-31 15:55:15 +01:00
use Friendica\Core\Renderer ;
2019-06-23 19:56:21 +02:00
use Friendica\Util\ConfigFileLoader ;
2018-01-27 14:52:02 +01:00
use Friendica\Util\Network ;
2018-11-08 17:45:19 +01:00
use Friendica\Util\Strings ;
2012-02-10 03:10:12 +01:00
2018-06-28 05:18:19 +02:00
const OSM_TMS = 'https://www.openstreetmap.org' ;
const OSM_NOM = 'https://nominatim.openstreetmap.org/search.php' ;
2018-03-20 07:43:03 +01:00
const OSM_ZOOM = 16 ;
const OSM_MARKER = 0 ;
2017-11-09 17:08:32 +01:00
function openstreetmap_install ()
{
2018-12-26 08:28:16 +01: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-10 03:10:12 +01:00
2018-10-30 00:40:18 +01:00
Logger :: log ( " installed openstreetmap " );
2012-02-10 03:10:12 +01:00
}
2017-11-09 17:08:32 +01:00
function openstreetmap_uninstall ()
{
2018-12-26 08:28:16 +01:00
Hook :: unregister ( 'load_config' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_load_config' );
Hook :: unregister ( 'render_location' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_location' );
Hook :: unregister ( 'generate_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_map' );
Hook :: unregister ( 'generate_named_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_named_map' );
Hook :: unregister ( 'Map::getCoordinates' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_get_coordinates' );
Hook :: unregister ( 'page_header' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_alterheader' );
2012-02-10 03:10:12 +01:00
2018-10-30 00:40:18 +01:00
Logger :: log ( " removed openstreetmap " );
2012-02-10 03:10:12 +01:00
}
2019-03-24 12:54:26 +01:00
function openstreetmap_load_config ( \Friendica\App $a , ConfigFileLoader $loader )
2018-06-28 05:18:19 +02:00
{
2019-02-10 20:10:59 +01:00
$a -> getConfigCache () -> load ( $loader -> loadAddonConfig ( 'openstreetmap' ));
2018-06-28 05:18:19 +02:00
}
2017-11-09 17:08:32 +01:00
function openstreetmap_alterheader ( $a , & $navHtml )
{
2018-10-09 20:13:22 +02:00
$addScriptTag = '<script type="text/javascript" src="' . $a -> getBaseURL () . '/addon/openstreetmap/openstreetmap.js"></script>' . " \r \n " ;
2013-01-09 00:41:27 +01:00
$a -> page [ 'htmlhead' ] .= $addScriptTag ;
}
2012-02-10 03:10:12 +01:00
2015-05-15 20:27:32 +02: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 14:15:33 +01:00
*
2015-05-15 20:27:32 +02:00
* @ param mixed $a
* @ param array & $item
*/
2018-03-20 07:43:03 +01:00
function openstreetmap_location ( $a , & $item )
{
if ( ! ( strlen ( $item [ 'location' ]) || strlen ( $item [ 'coord' ]))) {
2013-01-09 00:41:27 +01:00
return ;
2017-11-09 17:08:32 +01:00
}
2012-02-10 04:53:56 +01:00
2012-02-19 14:28:43 +01:00
/*
2015-05-15 20:27:32 +02:00
* Get the configuration variables from the config .
2015-06-30 07:31:27 +02:00
* @ todo Separate the tile map server from the text - string to map tile server
2015-05-15 20:27:32 +02: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 .
*/
2018-03-20 07:43:03 +01:00
$tmsserver = Config :: get ( 'openstreetmap' , 'tmsserver' , OSM_TMS );
$nomserver = Config :: get ( 'openstreetmap' , 'nomserver' , OSM_NOM );
$zoom = Config :: get ( 'openstreetmap' , 'zoom' , OSM_ZOOM );
$marker = Config :: get ( 'openstreetmap' , 'marker' , OSM_MARKER );
2012-02-19 14:28:43 +01:00
2018-03-20 07:43:03 +01: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 17:08:32 +01:00
}
2014-05-29 12:42:25 +02:00
2015-06-30 07:31:27 +02:00
if ( $item [ 'coord' ] != " " ) {
2012-02-10 03:10:12 +01:00
$coords = explode ( ' ' , $item [ 'coord' ]);
2018-03-20 07:43:03 +01:00
if ( count ( $coords ) > 1 ) {
2015-05-15 20:27:32 +02:00
$lat = urlencode ( round ( $coords [ 0 ], 5 ));
$lon = urlencode ( round ( $coords [ 1 ], 5 ));
2015-06-30 07:31:27 +02:00
$target = $tmsserver ;
2018-03-20 07:43:03 +01:00
if ( $marker > 0 ) {
$target .= '?mlat=' . $lat . '&mlon=' . $lon ;
}
2015-06-30 07:31:27 +02:00
$target .= '#map=' . intval ( $zoom ) . '/' . $lat . '/' . $lon ;
2012-02-10 03:10:12 +01:00
}
}
2015-06-30 07:31:27 +02:00
2018-07-10 14:30:23 +02:00
if ( empty ( $target )) {
2015-06-30 07:31:27 +02:00
$target = $nomserver . '?q=' . urlencode ( $item [ 'location' ]);
2017-11-09 17:08:32 +01:00
}
2015-06-30 07:31:27 +02:00
2017-11-09 17:08:32 +01:00
if ( $item [ 'location' ] != " " ) {
2015-06-30 07:31:27 +02:00
$title = $item [ 'location' ];
2017-11-09 17:08:32 +01:00
} else {
2015-06-30 07:31:27 +02:00
$title = $item [ 'coord' ];
2017-11-09 17:08:32 +01:00
}
2015-06-30 07:31:27 +02:00
$item [ 'html' ] = '<a target="map" title="' . $title . '" href= "' . $target . '">' . $title . '</a>' ;
2012-02-10 03:10:12 +01:00
}
2018-03-20 07:43:03 +01:00
function openstreetmap_get_coordinates ( $a , & $b )
2017-11-09 17:08:32 +01:00
{
2018-03-20 07:43:03 +01:00
$nomserver = Config :: get ( 'openstreetmap' , 'nomserver' , OSM_NOM );
// This is needed since we stored an empty string in the config in previous versions
if ( empty ( $nomserver )) {
$nomserver = OSM_NOM ;
}
2015-05-15 20:27:32 +02:00
$args = '?q=' . urlencode ( $b [ 'location' ]) . '&format=json' ;
2018-03-20 07:43:03 +01:00
$cachekey = " openstreetmap: " . $b [ 'location' ];
$j = Cache :: get ( $cachekey );
2015-06-30 07:31:27 +02:00
2018-03-20 07:43:03 +01:00
if ( is_null ( $j )) {
2018-10-11 07:40:57 +02:00
$curlResult = Network :: curl ( $nomserver . $args );
if ( $curlResult -> isSuccess ()) {
$j = json_decode ( $curlResult -> getBody (), true );
2018-10-20 18:22:13 +02:00
Cache :: set ( $cachekey , $j , Cache :: MONTH );
2015-05-15 20:27:32 +02:00
}
}
2018-03-20 07:43:03 +01:00
if ( ! empty ( $j [ 0 ][ 'lat' ]) && ! empty ( $j [ 0 ][ 'lon' ])) {
$b [ 'lat' ] = $j [ 0 ][ 'lat' ];
$b [ 'lon' ] = $j [ 0 ][ 'lon' ];
}
2015-05-15 20:27:32 +02:00
}
2018-03-20 07:43:03 +01:00
function openstreetmap_generate_named_map ( & $a , & $b )
2017-11-09 17:08:32 +01:00
{
2018-03-20 07:43:03 +01:00
openstreetmap_get_coordinates ( $a , $b );
if ( ! empty ( $b [ 'lat' ]) && ! empty ( $b [ 'lon' ])) {
openstreetmap_generate_map ( $a , $b );
2017-11-09 17:08:32 +01:00
}
2018-03-20 07:43:03 +01:00
}
2015-05-15 20:27:32 +02:00
2018-03-20 07:43:03 +01:00
function openstreetmap_generate_map ( & $a , & $b )
{
$tmsserver = Config :: get ( 'openstreetmap' , 'tmsserver' , OSM_TMS );
2015-05-15 20:27:32 +02:00
2018-10-13 19:07:22 +02:00
if ( strpos ( $a -> getBaseURL ( true ), 'https:' ) !== false ) {
2018-03-20 07:43:03 +01:00
$tmsserver = str_replace ( 'http:' , 'https:' , $tmsserver );
2017-11-09 17:08:32 +01:00
}
2015-05-15 20:27:32 +02:00
2018-03-20 07:43:03 +01:00
$zoom = Config :: get ( 'openstreetmap' , 'zoom' , OSM_ZOOM );
$marker = Config :: get ( 'openstreetmap' , 'marker' , OSM_MARKER );
2015-05-15 20:27:32 +02:00
$lat = $b [ 'lat' ]; // round($b['lat'], 5);
$lon = $b [ 'lon' ]; // round($b['lon'], 5);
2018-10-30 14:48:09 +01:00
Logger :: log ( 'lat: ' . $lat , Logger :: DATA );
Logger :: log ( 'lon: ' . $lon , Logger :: DATA );
2015-05-15 20:27:32 +02:00
2018-03-20 07:43:03 +01:00
$cardlink = '<a href="' . $tmsserver ;
2015-05-15 20:27:32 +02:00
2018-03-20 07:43:03 +01:00
if ( $marker > 0 ) {
$cardlink .= '?mlat=' . $lat . '&mlon=' . $lon ;
}
2012-02-19 23:26:41 +01:00
2018-11-09 19:32:45 +01:00
$cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ( $b [ 'location' ] ? Strings :: escapeHtml ( $b [ 'location' ]) : L10n :: t ( 'View Larger' )) . '</a>' ;
2018-03-20 07:43:03 +01: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 20:27:32 +02:00
2018-10-30 14:48:09 +01:00
Logger :: log ( 'generate_map: ' . $b [ 'html' ], Logger :: DATA );
2015-05-15 20:27:32 +02:00
}
2018-01-20 14:57:41 +01:00
function openstreetmap_addon_admin ( & $a , & $o )
2017-11-09 17:08:32 +01:00
{
2018-10-31 15:55:15 +01:00
$t = Renderer :: getMarkupTemplate ( " admin.tpl " , " addon/openstreetmap/ " );
2018-03-20 07:43:03 +01:00
$tmsserver = Config :: get ( 'openstreetmap' , 'tmsserver' , OSM_TMS );
$nomserver = Config :: get ( 'openstreetmap' , 'nomserver' , OSM_NOM );
$zoom = Config :: get ( 'openstreetmap' , 'zoom' , OSM_ZOOM );
$marker = Config :: get ( 'openstreetmap' , 'marker' , OSM_MARKER );
// This is needed since we stored an empty string in the config in previous versions
if ( empty ( $nomserver )) {
$nomserver = OSM_NOM ;
2017-11-09 17:08:32 +01:00
}
2015-05-15 20:27:32 +02:00
2018-10-31 15:55:15 +01:00
$o = Renderer :: replaceMacros ( $t , [
2018-01-22 20:03:11 +01:00
'$submit' => L10n :: t ( 'Submit' ),
'$tmsserver' => [ 'tmsserver' , L10n :: t ( 'Tile Server URL' ), $tmsserver , L10n :: t ( 'A list of <a href="http://wiki.openstreetmap.org/wiki/TMS" target="_blank">public tile servers</a>' )],
'$nomserver' => [ 'nomserver' , L10n :: t ( 'Nominatim (reverse geocoding) Server URL' ), $nomserver , L10n :: t ( 'A list of <a href="http://wiki.openstreetmap.org/wiki/Nominatim" target="_blank">Nominatim servers</a>' )],
'$zoom' => [ 'zoom' , L10n :: t ( 'Default zoom' ), $zoom , L10n :: t ( 'The default zoom level. (1:world, 18:highest, also depends on tile server)' )],
'$marker' => [ 'marker' , L10n :: t ( 'Include marker on map' ), $marker , L10n :: t ( 'Include a marker on the map.' )],
2018-01-15 14:15:33 +01:00
]);
2012-02-19 14:28:43 +01:00
}
2017-11-09 17:08:32 +01:00
2018-01-20 14:57:41 +01:00
function openstreetmap_addon_admin_post ( & $a )
2017-11-09 17:08:32 +01:00
{
2018-03-20 07:43:03 +01:00
$urltms = defaults ( $_POST , 'tmsserver' , OSM_TMS );
$urlnom = defaults ( $_POST , 'nomserver' , OSM_NOM );
$zoom = defaults ( $_POST , 'zoom' , OSM_ZOOM );
$marker = defaults ( $_POST , 'marker' , OSM_MARKER );
2017-11-07 00:55:24 +01:00
Config :: set ( 'openstreetmap' , 'tmsserver' , $urltms );
Config :: set ( 'openstreetmap' , 'nomserver' , $urlnom );
Config :: set ( 'openstreetmap' , 'zoom' , $zoom );
Config :: set ( 'openstreetmap' , 'marker' , $marker );
2018-03-20 07:43:03 +01:00
2018-01-22 20:03:11 +01:00
info ( L10n :: t ( 'Settings updated.' ) . EOL );
2012-02-19 14:28:43 +01:00
}