[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins / gis / action / gis_geocoder_rechercher.php
1 <?php
2
3 if (!defined('_ECRIRE_INC_VERSION')) {
4 return;
5 }
6
7 include_spip('inc/distant');
8
9 /**
10 * Proxy vers le service du geocoder
11 *
12 * Cette fonction permet de transmettre une requete auprès du service
13 * de recherche d'adresse d'OpenStreetMap (Nomatim ou Photon).
14 *
15 * Seuls les arguments spécifiques au service sont transmis.
16 */
17 function action_gis_geocoder_rechercher_dist() {
18 include_spip('inc/modifier');
19
20 $mode = _request('mode');
21 if (!$mode || !in_array($mode, array('search', 'reverse'))) {
22 return;
23 }
24
25 /* On filtre les arguments à renvoyer à Nomatim (liste blanche) */
26 $arguments = collecter_requests(array('format', 'q', 'limit', 'addressdetails', 'accept-language', 'lat', 'lon'), array());
27
28 $geocoder = defined('_GIS_GEOCODER') ? _GIS_GEOCODER : 'photon';
29
30 if (!empty($arguments) && in_array($geocoder, array('photon','nominatim'))) {
31 header('Content-Type: application/json; charset=UTF-8');
32 if ($geocoder == 'photon') {
33 if (isset($arguments['accept-language'])) {
34 // ne garder que les deux premiers caractères du code de langue, car les variantes spipiennes comme fr_fem posent problème
35 $arguments['lang'] = substr($arguments['accept-language'], 0, 2);
36 unset($arguments['accept-language']);
37 }
38 if ($mode == 'search') {
39 $mode = 'api/';
40 } else {
41 $mode = 'reverse';
42 }
43 $url = 'http://photon.komoot.de/';
44 } else {
45 $url = 'http://nominatim.openstreetmap.org/';
46 }
47
48 $url = defined('_GIS_GEOCODER_URL') ? _GIS_GEOCODER_URL : $url;
49 $data = recuperer_page("{$url}{$mode}?" . http_build_query($arguments));
50 echo $data;
51 }
52 }