PluginProbe
Leaflet Map / 3.4.0
Leaflet Map v3.4.0
3.4.7 3.4.6 trunk 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.11.5 2.12.0 2.13.0 2.14.0 2.15.0 2.16.0 2.16.1 2.16.2 2.17.0 2.17.1 2.17.2 2.17.3 2.18.0 2.19.0 2.19.1 All 49 releases
leaflet-map / class.geocoder.php

class.geocoder.php in Leaflet Map 3.4.0, at class.geocoder.php

201 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Geocoder
4 *
5 * calls the specific geocoder function (chosen in admin or default: google_geocode)
6 *
7 */
8
9 class Leaflet_Geocoder {
10 /**
11 * Geocoder should return this on error/not found
12 * @var array $not_found
13 */
14 private $not_found = array('lat' => 0, 'lng' => 0);
15 /**
16 * Latitude
17 * @var float $lat
18 */
19 public $lat = 0;
20 /**
21 * Longitude
22 * @var float $lng
23 */
24 public $lng = 0;
25
26 /**
27 * new Geocoder from address
28 *
29 * handles url encoding and caching
30 *
31 * @param string $address the requested address to look up
32 * @return NOTHING
33 */
34 public function __construct ($address) {
35 $settings = Leaflet_Map_Plugin_Settings::init();
36 // trim all quotes (even smart) from address
37 $address = trim($address, '\'"”');
38 $address = urlencode( $address );
39
40 $geocoder = $settings->get('geocoder');
41
42 $cached_address = 'leaflet_' . $geocoder . '_' . $address;
43
44 /* retrieve cached geocoded location */
45 $found_cache = get_option( $cached_address );
46
47 if ( $found_cache ) {
48 $location = $found_cache;
49 } else {
50 // try geocoding
51 $geocoding_method = $geocoder . '_geocode';
52
53 try {
54 $location = (Object) $this->$geocoding_method( $address );
55
56 /* add location */
57 add_option($cached_address, $location);
58
59 /* add option key to locations for clean up purposes */
60 $locations = get_option('leaflet_geocoded_locations', array());
61 array_push($locations, $cached_address);
62 update_option('leaflet_geocoded_locations', $locations);
63 } catch (Exception $e) {
64 // failed
65 $location = $this->not_found;
66 }
67 }
68
69 if (isset($location->lat) && isset($location->lng)) {
70 $this->lat = $location->lat;
71 $this->lng = $location->lng;
72 }
73 }
74
75 /**
76 * Removes location caches
77 */
78 public static function remove_caches () {
79 $addresses = get_option('leaflet_geocoded_locations', array());
80 foreach ($addresses as $address) {
81 delete_option($address);
82 }
83 delete_option('leaflet_geocoded_locations');
84 }
85
86 /**
87 * Used by geocoders to make requests via curl or file_get_contents
88 *
89 * includes a try/catch
90 *
91 * @param string $url the urlencoded request url
92 * @return varies object from API or null (failed)
93 */
94 private function get_url( $url ) {
95 $referer = get_site_url();
96
97 if (in_array('curl', get_loaded_extensions())) {
98 /* try curl */
99 $ch = curl_init();
100
101 curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
102 curl_setopt($ch, CURLOPT_HEADER, 0);
103 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
104 curl_setopt($ch, CURLOPT_REFERER, $referer);
105 curl_setopt($ch, CURLOPT_URL, $url);
106 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
107
108 $data = curl_exec($ch);
109 curl_close($ch);
110
111 return $data;
112 } else if (ini_get('allow_url_fopen')) {
113 /* try file get contents */
114
115 $opts = array(
116 'http' => array(
117 'header' => array("Referer: $referer\r\n")
118 )
119 );
120 $context = stream_context_create($opts);
121
122 return file_get_contents($url, false, $context);
123 }
124
125 $error_msg = 'Could not get url: ' . $url;
126 throw new Exception( $error_msg );
127 }
128
129 /**
130 * Google geocoder (https://developers.google.com/maps/documentation/geocoding/start)
131 *
132 * @param string $address the urlencoded address to look up
133 * @return varies object from API or null (failed)
134 */
135
136 private function google_geocode ( $address ) {
137 // Leaflet_Map_Plugin_Settings
138 $settings = Leaflet_Map_Plugin_Settings::init();
139 $key = $settings->get('google_appkey');
140
141 $geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s';
142 $geocode_url = sprintf($geocode_url, $address, $key);
143
144 $json = $this->get_url($geocode_url);
145 $json = json_decode($json);
146
147 /* found location */
148 if ($json->status == 'OK') {
149
150 $location = $json->results[0]->geometry->location;
151
152 return (Object) $location;
153 }
154
155 throw new Exception('No Address Found');
156 }
157
158 /**
159 * OpenStreetMap geocoder Nominatim (https://nominatim.openstreetmap.org/)
160 *
161 * @param string $address the urlencoded address to look up
162 * @return varies object from API or null (failed)
163 */
164
165 private function osm_geocode ( $address ) {
166 $geocode_url = 'https://nominatim.openstreetmap.org/?format=json&limit=1&q=';
167 $geocode_url .= $address;
168 $json = $this->get_url($geocode_url);
169 $json = json_decode($json);
170
171 if (isset($json[0]->lat) && isset($json[0]->lon)) {
172 return (Object) array(
173 'lat' => $json[0]->lat,
174 'lng' => $json[0]->lon,
175 );
176 } else {
177 return false;
178 }
179 }
180
181 /**
182 * TODO: does this still work?
183 * Danish Addresses Web Application
184 * (https://dawa.aws.dk)
185 *
186 * @param string $address the urlencoded address to look up
187 * @return varies object from API or null (failed)
188 */
189 private function dawa_geocode ( $address ) {
190 $geocode_url = 'https://dawa.aws.dk/adresser?format=json&q=';
191 $geocode_url .= $address;
192 $json = $this->get_url($geocode_url);
193 $json = json_decode($json);
194
195 /* found location */
196 return (Object) array(
197 'lat' => $json[0]->adgangsadresse->adgangspunkt->koordinater[1],
198 'lng' => $json[0]->adgangsadresse->adgangspunkt->koordinater[0]
199 );
200 }
201 }