X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=openstreetmap%2Fopenstreetmap.php;h=61fe1efd4856c9d1d257d1ecf10b2b19a5d48121;hb=09bb49eef5acf3b12ab6fae4687f7eec06c40eee;hp=e805632a51a1e6dbda32e2a3539b1a1965b4c142;hpb=27211133de08e1568d791a8193a7c4c3e7e22c38;p=friendica-addons.git
diff --git a/openstreetmap/openstreetmap.php b/openstreetmap/openstreetmap.php
index e805632a..61fe1efd 100644
--- a/openstreetmap/openstreetmap.php
+++ b/openstreetmap/openstreetmap.php
@@ -8,31 +8,54 @@
* Author: Klaus Weidenbach
*
*/
-
-require_once('include/cache.php');
+use Friendica\DI;
+use Friendica\Core\Cache\Duration;
+use Friendica\Core\Hook;
+use Friendica\Core\Logger;
+use Friendica\Core\Renderer;
+use Friendica\Util\ConfigFileLoader;
+use Friendica\Util\Network;
+use Friendica\Util\Strings;
+
+const OSM_TMS = 'https://www.openstreetmap.org';
+const OSM_NOM = 'https://nominatim.openstreetmap.org/search.php';
+const OSM_ZOOM = 16;
+const OSM_MARKER = 0;
+
+function openstreetmap_install()
+{
+ 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');
+
+ Logger::log("installed openstreetmap");
+}
-function openstreetmap_install() {
- register_hook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
- register_hook('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
- register_hook('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
- register_hook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
+function openstreetmap_uninstall()
+{
+ 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');
- logger("installed openstreetmap");
+ Logger::log("removed openstreetmap");
}
-function openstreetmap_uninstall() {
- unregister_hook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
- unregister_hook('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
- unregister_hook('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
- unregister_hook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
-
- logger("removed openstreetmap");
+function openstreetmap_load_config(\Friendica\App $a, ConfigFileLoader $loader)
+{
+ $a->getConfigCache()->load($loader->loadAddonConfig('openstreetmap'));
}
-function openstreetmap_alterheader($a, &$navHtml) {
- $addScriptTag = '' . "\r\n";
- $a->page['htmlhead'] .= $addScriptTag;
+function openstreetmap_alterheader($a, &$navHtml)
+{
+ $addScriptTag = '' . "\r\n";
+ DI::page()['htmlhead'] .= $addScriptTag;
}
/**
@@ -40,154 +63,166 @@ function openstreetmap_alterheader($a, &$navHtml) {
*
* 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.
- *
+ *
* @param mixed $a
* @param array& $item
*/
-function openstreetmap_location($a, &$item) {
-
- if(! (strlen($item['location']) || strlen($item['coord'])))
+function openstreetmap_location($a, &$item)
+{
+ if (!(strlen($item['location']) || strlen($item['coord']))) {
return;
+ }
/*
* Get the configuration variables from the config.
- * @todo Separate the tile map server from the text-string to map tile server
+ * @todo Separate the tile map server from the text-string to map tile server
* 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.
*/
- $tmsserver = get_config('openstreetmap', 'tmsserver');
- if(! $tmsserver)
- $tmsserver = 'http://www.openstreetmap.org';
-
- $nomserver = get_config('openstreetmap', 'nomserver');
- if(! $nomserver)
- $nomserver = 'http://nominatim.openstreetmap.org/search.php';
-
- $zoom = get_config('openstreetmap', 'zoom');
- if(! $zoom)
- $zoom = 16;
-
- $marker = get_config('openstreetmap', 'marker');
- if(! $marker)
- $marker = 0;
-
- $location = '';
- $coord = '';
+ $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);
- $location = $item['location'];
-
- $location = (($location && (! $item['coord'])) ? '' . $item['location'] . '' : $location);
+ // This is needed since we stored an empty string in the config in previous versions
+ if (empty($nomserver)) {
+ $nomserver = OSM_NOM;
+ }
- if($item['coord']) {
+ if ($item['coord'] != "") {
$coords = explode(' ', $item['coord']);
- if(count($coords) > 1) {
+ if (count($coords) > 1) {
$lat = urlencode(round($coords[0], 5));
$lon = urlencode(round($coords[1], 5));
- $coord = 'Map';
+ $target = $tmsserver;
+ if ($marker > 0) {
+ $target .= '?mlat=' . $lat . '&mlon=' . $lon;
+ }
+ $target .= '#map='.intval($zoom).'/'.$lat.'/'.$lon;
}
}
- if(strlen($coord)) {
- if($location)
- $location .= ' (' . $coord . ')';
- else
- $location = '' . $coord . '';
+
+ if (empty($target)) {
+ $target = $nomserver.'?q='.urlencode($item['location']);
}
- $item['html'] = $location;
-}
+ if ($item['location'] != "") {
+ $title = $item['location'];
+ } else {
+ $title = $item['coord'];
+ }
+
+ $item['html'] = ''.$title.'';
+}
-function openstreetmap_generate_named_map(&$a,&$b) {
+function openstreetmap_get_coordinates($a, &$b)
+{
+ $nomserver = DI::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;
+ }
- $nomserver = get_config('openstreetmap', 'nomserver');
- 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);
-
- if($j && is_array($j) && $j[0]['lat'] && $j[0]['lon']) {
- $arr = array('lat' => $j[0]['lat'],'lon' => $j[0]['lon'],'location' => $b['location'], 'html' => '');
- openstreetmap_generate_map($a,$arr);
- $b['html'] = $arr['html'];
+ $cachekey = "openstreetmap:" . $b['location'];
+ $j = DI::cache()->get($cachekey);
+
+ if (is_null($j)) {
+ $curlResult = Network::curl($nomserver . $args);
+ if ($curlResult->isSuccess()) {
+ $j = json_decode($curlResult->getBody(), true);
+ DI::cache()->set($cachekey, $j, Duration::MONTH);
}
}
-
-
+
+ if (!empty($j[0]['lat']) && !empty($j[0]['lon'])) {
+ $b['lat'] = $j[0]['lat'];
+ $b['lon'] = $j[0]['lon'];
+ }
}
-function openstreetmap_generate_map(&$a,&$b) {
+function openstreetmap_generate_named_map(&$a, &$b)
+{
+ openstreetmap_get_coordinates($a, $b);
- $tmsserver = get_config('openstreetmap', 'tmsserver');
- if(! $tmsserver)
- $tmsserver = 'http://www.openstreetmap.org';
- if(strpos(z_root(),'https:') !== false)
- $tmsserver = str_replace('http:','https:',$tmsserver);
+ if (!empty($b['lat']) && !empty($b['lon'])) {
+ openstreetmap_generate_map($a, $b);
+ }
+}
+function openstreetmap_generate_map(&$a, &$b)
+{
+ $tmsserver = DI::config()->get('openstreetmap', 'tmsserver', OSM_TMS);
- $zoom = get_config('openstreetmap', 'zoom');
- if(! $zoom)
- $zoom = 16;
+ if (strpos(DI::baseUrl()->get(true), 'https:') !== false) {
+ $tmsserver = str_replace('http:','https:',$tmsserver);
+ }
- $marker = get_config('openstreetmap', 'marker');
- if(! $marker)
- $marker = 0;
+ $zoom = DI::config()->get('openstreetmap', 'zoom', OSM_ZOOM);
+ $marker = DI::config()->get('openstreetmap', 'marker', OSM_MARKER);
$lat = $b['lat']; // round($b['lat'], 5);
$lon = $b['lon']; // round($b['lon'], 5);
- logger('lat: ' . $lat, LOGGER_DATA);
- logger('lon: ' . $lon, LOGGER_DATA);
+ Logger::log('lat: ' . $lat, Logger::DATA);
+ Logger::log('lon: ' . $lon, Logger::DATA);
+ $cardlink = '
' . (($b['location']) ? escape_tags($b['location']) : t('View Larger')) . '';
+ if ($marker > 0) {
+ $cardlink .= '?mlat=' . $lat . '&mlon=' . $lon;
+ }
- logger('generate_map: ' . $b['html'], LOGGER_DATA);
+ $cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ($b['location'] ? Strings::escapeHtml($b['location']) : DI::l10n()->t('View Larger')) . '';
+ if (empty($b['mode'])) {
+ $b['html'] = '' .
+ '
' . $cardlink . '';
+ } else {
+ $b['html'] .= '
' . $cardlink;
+ }
+ Logger::log('generate_map: ' . $b['html'], Logger::DATA);
}
-function openstreetmap_plugin_admin(&$a, &$o) {
- $t = get_markup_template("admin.tpl", "addon/openstreetmap/");
- $tmsserver = get_config('openstreetmap', 'tmsserver');
- if(! $tmsserver)
- $tmsserver = 'http://www.openstreetmap.org';
- $nomserver = get_config('openstreetmap', 'nomserver');
- if(! $nomserver)
- $nomserver = 'http://nominatim.openstreetmap.org/search.php';
- $zoom = get_config('openstreetmap', 'zoom');
- if(! $zoom)
- $zoom = 16;
- $marker = get_config('openstreetmap', 'marker');
- if(! $marker)
- $marker = 0;
-
- $o = replace_macros($t, array(
- '$submit' => t('Submit'),
- '$tmsserver' => array('tmsserver', t('Tile Server URL'), $tmsserver, t('A list of public tile servers')),
- '$nomserver' => array('nomserver', t('Nominatim (reverse geocoding) Server URL'), $nomserver, t('A list of Nominatim servers')),
- '$zoom' => array('zoom', t('Default zoom'), $zoom, t('The default zoom level. (1:world, 18:highest, also depends on tile server)')),
- '$marker' => array('marker', t('Include marker on map'), $marker, t('Include a marker on the map.')),
- ));
-}
-function openstreetmap_plugin_admin_post(&$a) {
- $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');
- set_config('openstreetmap', 'tmsserver', $urltms);
- set_config('openstreetmap', 'nomserver', $urlnom);
- set_config('openstreetmap', 'zoom', $zoom);
- set_config('openstreetmap', 'marker', $marker);
- info( t('Settings updated.') . EOL);
+function openstreetmap_addon_admin(&$a, &$o)
+{
+ $t = Renderer::getMarkupTemplate("admin.tpl", "addon/openstreetmap/");
+ $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);
+
+ // This is needed since we stored an empty string in the config in previous versions
+ if (empty($nomserver)) {
+ $nomserver = OSM_NOM;
+ }
+
+ $o = Renderer::replaceMacros($t, [
+ '$submit' => DI::l10n()->t('Submit'),
+ '$tmsserver' => ['tmsserver', DI::l10n()->t('Tile Server URL'), $tmsserver, DI::l10n()->t('A list of public tile servers')],
+ '$nomserver' => ['nomserver', DI::l10n()->t('Nominatim (reverse geocoding) Server URL'), $nomserver, DI::l10n()->t('A list of Nominatim servers')],
+ '$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.')],
+ ]);
}
+function openstreetmap_addon_admin_post(&$a)
+{
+ $urltms = ($_POST['tmsserver'] ?? '') ?: OSM_TMS;
+ $urlnom = ($_POST['nomserver'] ?? '') ?: OSM_NOM;
+ $zoom = ($_POST['zoom'] ?? '') ?: OSM_ZOOM;
+ $marker = ($_POST['marker'] ?? '') ?: OSM_MARKER;
+ DI::config()->set('openstreetmap', 'tmsserver', $urltms);
+ DI::config()->set('openstreetmap', 'nomserver', $urlnom);
+ DI::config()->set('openstreetmap', 'zoom', $zoom);
+ DI::config()->set('openstreetmap', 'marker', $marker);
+
+ info(DI::l10n()->t('Settings updated.') . EOL);
+}