2012-02-10 02:10:12 +00:00
< ? php
/**
2012-02-19 13:28:43 +00:00
* Name : OpenStreetMap
2015-05-15 18:27:32 +00: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 02:10:12 +00:00
* Author : Mike Macgirvin < http :// macgirvin . com / profile / mike >
2012-02-19 13:28:43 +00:00
* Author : Klaus Weidenbach
2012-02-10 02:10:12 +00:00
*
*/
2018-01-15 13:15:33 +00:00
2017-11-09 16:08:32 +00:00
use Friendica\Core\Cache ;
2017-11-06 23:55:24 +00:00
use Friendica\Core\Config ;
2012-02-10 02:10:12 +00:00
2017-11-09 16:08:32 +00:00
function openstreetmap_install ()
{
2012-02-10 02:10:12 +00:00
register_hook ( 'render_location' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_location' );
2015-05-15 18:27:32 +00:00
register_hook ( 'generate_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_map' );
register_hook ( 'generate_named_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_named_map' );
2014-05-29 10:42:25 +00:00
register_hook ( 'page_header' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_alterheader' );
2012-02-10 02:10:12 +00:00
logger ( " installed openstreetmap " );
}
2017-11-09 16:08:32 +00:00
function openstreetmap_uninstall ()
{
2012-02-10 02:10:12 +00:00
unregister_hook ( 'render_location' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_location' );
2015-05-15 18:27:32 +00:00
unregister_hook ( 'generate_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_map' );
unregister_hook ( 'generate_named_map' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_generate_named_map' );
2013-01-08 23:41:27 +00:00
unregister_hook ( 'page_header' , 'addon/openstreetmap/openstreetmap.php' , 'openstreetmap_alterheader' );
2012-02-10 02:10:12 +00:00
logger ( " removed openstreetmap " );
}
2017-11-09 16:08:32 +00:00
function openstreetmap_alterheader ( $a , & $navHtml )
{
2015-05-15 18:27:32 +00:00
$addScriptTag = '<script type="text/javascript" src="' . $a -> get_baseurl () . '/addon/openstreetmap/openstreetmap.js"></script>' . " \r \n " ;
2013-01-08 23:41:27 +00:00
$a -> page [ 'htmlhead' ] .= $addScriptTag ;
}
2012-02-10 02:10:12 +00:00
2015-05-15 18:27:32 +00: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 13:15:33 +00:00
*
2015-05-15 18:27:32 +00:00
* @ param mixed $a
* @ param array & $item
*/
2012-02-10 02:10:12 +00:00
function openstreetmap_location ( $a , & $item ) {
2013-01-08 23:41:27 +00:00
2017-11-09 16:08:32 +00:00
if ( ! ( strlen ( $item [ 'location' ]) || strlen ( $item [ 'coord' ]))) {
2013-01-08 23:41:27 +00:00
return ;
2017-11-09 16:08:32 +00:00
}
2012-02-10 03:53:56 +00:00
2012-02-19 13:28:43 +00:00
/*
2015-05-15 18:27:32 +00:00
* Get the configuration variables from the config .
2015-06-30 05:31:27 +00:00
* @ todo Separate the tile map server from the text - string to map tile server
2015-05-15 18:27:32 +00: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 .
*/
2017-11-06 23:55:24 +00:00
$tmsserver = Config :: get ( 'openstreetmap' , 'tmsserver' );
2017-11-09 16:08:32 +00:00
if ( ! $tmsserver ) {
2015-05-15 18:27:32 +00:00
$tmsserver = 'http://www.openstreetmap.org' ;
2017-11-09 16:08:32 +00:00
}
2012-02-19 13:28:43 +00:00
2017-11-06 23:55:24 +00:00
$nomserver = Config :: get ( 'openstreetmap' , 'nomserver' );
2017-11-09 16:08:32 +00:00
if ( ! $nomserver ) {
2015-05-15 18:27:32 +00:00
$nomserver = 'http://nominatim.openstreetmap.org/search.php' ;
2017-11-09 16:08:32 +00:00
}
2012-02-10 02:10:12 +00:00
2017-11-06 23:55:24 +00:00
$zoom = Config :: get ( 'openstreetmap' , 'zoom' );
2017-11-09 16:08:32 +00:00
if ( ! $zoom ) {
2015-05-15 18:27:32 +00:00
$zoom = 16 ;
2017-11-09 16:08:32 +00:00
}
2014-05-29 10:42:25 +00:00
2017-11-06 23:55:24 +00:00
$marker = Config :: get ( 'openstreetmap' , 'marker' );
2017-11-09 16:08:32 +00:00
if ( ! $marker ) {
2015-05-15 18:27:32 +00:00
$marker = 0 ;
2017-11-09 16:08:32 +00:00
}
2014-05-29 10:42:25 +00:00
2015-06-30 05:31:27 +00:00
if ( $item [ 'coord' ] != " " ) {
2012-02-10 02:10:12 +00:00
$coords = explode ( ' ' , $item [ 'coord' ]);
if ( count ( $coords ) > 1 ) {
2015-05-15 18:27:32 +00:00
$lat = urlencode ( round ( $coords [ 0 ], 5 ));
$lon = urlencode ( round ( $coords [ 1 ], 5 ));
2015-06-30 05:31:27 +00:00
$target = $tmsserver ;
2015-05-15 18:27:32 +00:00
if ( $marker > 0 )
2015-06-30 05:31:27 +00:00
$target .= '?mlat=' . $lat . '&mlon=' . $lon ;
$target .= '#map=' . intval ( $zoom ) . '/' . $lat . '/' . $lon ;
2012-02-10 02:10:12 +00:00
}
}
2015-06-30 05:31:27 +00:00
2017-11-09 16:08:32 +00:00
if ( $target == " " ) {
2015-06-30 05:31:27 +00:00
$target = $nomserver . '?q=' . urlencode ( $item [ 'location' ]);
2017-11-09 16:08:32 +00:00
}
2015-06-30 05:31:27 +00:00
2017-11-09 16:08:32 +00:00
if ( $item [ 'location' ] != " " ) {
2015-06-30 05:31:27 +00:00
$title = $item [ 'location' ];
2017-11-09 16:08:32 +00:00
} else {
2015-06-30 05:31:27 +00:00
$title = $item [ 'coord' ];
2017-11-09 16:08:32 +00:00
}
2015-06-30 05:31:27 +00:00
$item [ 'html' ] = '<a target="map" title="' . $title . '" href= "' . $target . '">' . $title . '</a>' ;
2012-02-10 02:10:12 +00:00
}
2012-02-19 13:28:43 +00:00
2017-11-09 16:08:32 +00:00
function openstreetmap_generate_named_map ( & $a , & $b )
{
2017-11-06 23:55:24 +00:00
$nomserver = Config :: get ( 'openstreetmap' , 'nomserver' );
2015-05-15 18:27:32 +00:00
if ( ! $nomserver )
$nomserver = 'http://nominatim.openstreetmap.org/search.php' ;
$args = '?q=' . urlencode ( $b [ 'location' ]) . '&format=json' ;
$x = z_fetch_url ( $nomserver . $args );
if ( $x [ 'success' ]) {
$j = json_decode ( $x [ 'body' ], true );
2015-06-30 05:31:27 +00:00
2015-05-15 18:27:32 +00:00
if ( $j && is_array ( $j ) && $j [ 0 ][ 'lat' ] && $j [ 0 ][ 'lon' ]) {
2018-01-15 13:15:33 +00:00
$arr = [ 'lat' => $j [ 0 ][ 'lat' ], 'lon' => $j [ 0 ][ 'lon' ], 'location' => $b [ 'location' ], 'html' => '' ];
2015-05-15 18:27:32 +00:00
openstreetmap_generate_map ( $a , $arr );
$b [ 'html' ] = $arr [ 'html' ];
}
}
}
2017-11-09 16:08:32 +00:00
function openstreetmap_generate_map ( & $a , & $b )
{
2017-11-06 23:55:24 +00:00
$tmsserver = Config :: get ( 'openstreetmap' , 'tmsserver' );
2017-11-09 16:08:32 +00:00
if ( ! $tmsserver ) {
2015-05-15 18:27:32 +00:00
$tmsserver = 'http://www.openstreetmap.org' ;
2017-11-09 16:08:32 +00:00
}
if ( strpos ( z_root (), 'https:' ) !== false ) {
2015-05-15 18:27:32 +00:00
$tmsserver = str_replace ( 'http:' , 'https:' , $tmsserver );
2017-11-09 16:08:32 +00:00
}
2015-05-15 18:27:32 +00:00
2017-11-06 23:55:24 +00:00
$zoom = Config :: get ( 'openstreetmap' , 'zoom' );
2017-11-09 16:08:32 +00:00
if ( ! $zoom ) {
2015-05-15 18:27:32 +00:00
$zoom = 16 ;
2017-11-09 16:08:32 +00:00
}
2015-05-15 18:27:32 +00:00
2017-11-06 23:55:24 +00:00
$marker = Config :: get ( 'openstreetmap' , 'marker' );
2017-11-09 16:08:32 +00:00
if ( ! $marker ) {
2015-05-15 18:27:32 +00:00
$marker = 0 ;
2017-11-09 16:08:32 +00:00
}
2015-05-15 18:27:32 +00:00
$lat = $b [ 'lat' ]; // round($b['lat'], 5);
$lon = $b [ 'lon' ]; // round($b['lon'], 5);
logger ( 'lat: ' . $lat , LOGGER_DATA );
logger ( 'lon: ' . $lon , LOGGER_DATA );
$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 ) ;
2012-02-19 22:26:41 +00:00
2015-05-15 18:27:32 +00:00
$b [ 'html' ] .= '&layer=mapnik&marker=' . $lat . '%2C' . $lon . '" style="border: 1px solid black"></iframe><br/><small><a href="' . $tmsserver . '/?mlat=' . $lat . '&mlon=' . $lon . '#map=16/' . $lat . '/' . $lon . '">' . (( $b [ 'location' ]) ? escape_tags ( $b [ 'location' ]) : t ( 'View Larger' )) . '</a></small>' ;
logger ( 'generate_map: ' . $b [ 'html' ], LOGGER_DATA );
}
2017-11-09 16:08:32 +00:00
function openstreetmap_plugin_admin ( & $a , & $o )
{
2015-05-15 18:27:32 +00:00
$t = get_markup_template ( " admin.tpl " , " addon/openstreetmap/ " );
2017-11-06 23:55:24 +00:00
$tmsserver = Config :: get ( 'openstreetmap' , 'tmsserver' );
2017-11-09 16:08:32 +00:00
if ( ! $tmsserver ) {
2015-05-15 18:27:32 +00:00
$tmsserver = 'http://www.openstreetmap.org' ;
2017-11-09 16:08:32 +00:00
}
2017-11-06 23:55:24 +00:00
$nomserver = Config :: get ( 'openstreetmap' , 'nomserver' );
2017-11-09 16:08:32 +00:00
if ( ! $nomserver ) {
2015-05-15 18:27:32 +00:00
$nomserver = 'http://nominatim.openstreetmap.org/search.php' ;
2017-11-09 16:08:32 +00:00
}
2017-11-06 23:55:24 +00:00
$zoom = Config :: get ( 'openstreetmap' , 'zoom' );
2017-11-09 16:08:32 +00:00
if ( ! $zoom ) {
2015-05-15 18:27:32 +00:00
$zoom = 16 ;
2017-11-09 16:08:32 +00:00
}
2017-11-06 23:55:24 +00:00
$marker = Config :: get ( 'openstreetmap' , 'marker' );
2017-11-09 16:08:32 +00:00
if ( ! $marker ) {
2015-05-15 18:27:32 +00:00
$marker = 0 ;
2017-11-09 16:08:32 +00:00
}
2015-05-15 18:27:32 +00:00
2018-01-15 13:15:33 +00:00
$o = replace_macros ( $t , [
2015-05-15 18:27:32 +00:00
'$submit' => t ( 'Submit' ),
2018-01-15 13:15:33 +00:00
'$tmsserver' => [ 'tmsserver' , t ( 'Tile Server URL' ), $tmsserver , t ( 'A list of <a href="http://wiki.openstreetmap.org/wiki/TMS" target="_blank">public tile servers</a>' )],
'$nomserver' => [ 'nomserver' , t ( 'Nominatim (reverse geocoding) Server URL' ), $nomserver , t ( 'A list of <a href="http://wiki.openstreetmap.org/wiki/Nominatim" target="_blank">Nominatim servers</a>' )],
'$zoom' => [ 'zoom' , t ( 'Default zoom' ), $zoom , t ( 'The default zoom level. (1:world, 18:highest, also depends on tile server)' )],
'$marker' => [ 'marker' , t ( 'Include marker on map' ), $marker , t ( 'Include a marker on the map.' )],
]);
2012-02-19 13:28:43 +00:00
}
2017-11-09 16:08:32 +00:00
function openstreetmap_plugin_admin_post ( & $a )
{
2015-05-15 18:27:32 +00:00
$urltms = (( x ( $_POST , 'tmsserver' )) ? notags ( trim ( $_POST [ 'tmsserver' ])) : '' );
$urlnom = (( x ( $_POST , 'nomserver' )) ? notags ( trim ( $_POST [ 'nomserver' ])) : '' );
$zoom = (( x ( $_POST , 'zoom' )) ? intval ( trim ( $_POST [ 'zoom' ])) : '16' );
$marker = (( x ( $_POST , 'marker' )) ? intval ( trim ( $_POST [ 'marker' ])) : '0' );
2017-11-06 23:55:24 +00:00
Config :: set ( 'openstreetmap' , 'tmsserver' , $urltms );
Config :: set ( 'openstreetmap' , 'nomserver' , $urlnom );
Config :: set ( 'openstreetmap' , 'zoom' , $zoom );
Config :: set ( 'openstreetmap' , 'marker' , $marker );
2015-05-15 18:27:32 +00:00
info ( t ( 'Settings updated.' ) . EOL );
2012-02-19 13:28:43 +00:00
}