This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
EGMapClient.php
335 lines (303 loc) · 11.2 KB
/
EGMapClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
*
* EGMapClient Class
* A class to communicate with Google Maps
* Inspired on the work of Fabrice Bernhard
*
* @author Antonio Ramirez Cobos
* @link http://www.ramirezcobos.com
*
* @copyright
* info as this library is a modified version of Fabrice Bernhard
*
* Copyright (c) 2008 Fabrice Bernhard
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
class EGMapClient
{
/**
* The URL for the RESTful geocode API.
* @since 2011-03-23 Matt Cheale Updated URL from v2 to v3 of the API.
* @since 2011-04-21 Matt Cheale Removed the format option so it can be customised in the geocoding methods.
* @since 2011-12-19 Antonio Ramirez renamed to make use of more APIs
*/
const API_GEOCODE_URL = 'http://maps.googleapis.com/maps/api/geocode/';
/**
* The URL for the RESTful elevation API
*/
const API_ELEVATION_URL = 'http://maps.googleapis.com/maps/api/elevation/';
/**
* API key array
*
* @var EGMapApiKeyList $api_keys
*/
protected $api_keys = null;
/**
*
* Holds default domain
* domains specified active API key
* @var string
*/
private $_default_domain = 'localhost';
/**
*
* Constructor
* If $key parameter is set, it will try to add it
* to the collection. Array should be in the format of
* <pre>
* $gmapclient = new EGMapClient( array('domain'=>'googlekeyhere') );
* </pre>
* @param array $key
* @since 2011-04-21 Matt Cheale $key parameter deprecated
* @since 2011-09-18 Antonio Ramirez added $key parameter again
*/
public function __construct($key = array())
{
$this->api_keys = new EGMapApiKeyList();
if (!empty($key) && !is_scalar($key)) {
list($domain, $key) = each($key);
$this->setAPIKey($domain, $key);
}
}
/**
* Sets the Google Maps API key
* @param string $domain
* @param string $key
* @param bool $setAsDefault
* @since 2011-04-21 Matt Cheale Deprecated as API Keys are no longer required.
* @since 2011-09-18 Antonio Ramirez added $key support again
*/
public function setAPIKey($domain, $key, $setAsDefault = false)
{
if ($this->api_keys === null)
$this->api_keys = new EGMapApiKeyList();
$this->api_keys->addAPIKey($domain, $key);
if (true === $setAsDefault)
$this->setDomain($domain);
}
/**
*
* Sets default API key
* @param string $domain
* @since 2011-04-21 Matt Cheale Deprecated as API Keys are no longer required.
* @since 2011-09-18 Antonio Ramirez added support again
*/
public function setDomain($domain)
{
$this->_default_domain = $domain;
}
/**
* Gets the Google Maps API key
* @param string $domain the domain
* @since 2011-04-21 Matt Cheale Deprecated as API Keys are no longer required.
* @since 2011-09-18 Antonio Ramirez added support again
* @return string
*/
public function getAPIKey($domain = null)
{
$domain = (null === $domain ? $this->_default_domain : $domain);
return $this->api_keys->getAPIKeyByDomain($domain);
}
/**
* Guesses and sets default API Key
* @since 2011-04-21 Matt Cheale Deprecated as API Keys are no longer required.
* @since 2011-09-18 Antonio Ramirez added support again
*/
protected function guessAndSetAPIKey($key)
{
$this->setAPIKey($this->guessDomain(), $key, true);
}
/**
* Guesses the current domain
* @return string $domain
* @author Antonio Ramirez Cobos
* @since 2011-04-21 Matt Cheale Deprecated as API Keys are no longer required.
* @since 2011-09-18 Antonio Ramirez added support again
*/
public function guessDomain()
{
if (isset($_SERVER['SERVER_NAME']))
return $_SERVER['SERVER_NAME'];
else if (isset($_SERVER['HTTP_HOST']))
return $_SERVER['HTTP_HOST'];
// nothing found, return default
return $this->_default_domain;
}
/**
* Returns the collection of API keys
* @return EGMapApiKeyList
* @since 2011-04-21 Matt Cheale Deprecated as API Keys are no longer required.
* @since 2011-09-18 Antonio Ramirez added support again
*/
public function getAPIKeys()
{
return $this->api_keys;
}
/**
*
* Sets the API keys collection
* @param EGMapApiKeyList $api_keys
* @return false if $api_keys is not of class CMap
* @author Antonio Ramirez Cobos
* @since 2011-04-21 Matt Cheale Deprecated as API Keys are no longer required.
* @since 2011-09-18 Antonio Ramirez added support again
*/
public function setAPIKeys($api_keys)
{
if (!$api_keys instanceof EGMapApiKeyList)
return false;
$this->api_keys = $api_keys;
}
/**
*
* Changes default geocoding template
* Just in case google changes its API
* current is of default: {api}&output={format}&key={key}&q={address}
* @param string $template
* @author Antonio Ramirez Cobos
* @deprecated
* @since 2011-04-21 Matt Cheale Deprecated as latest code is not making any use of this.
*/
public function setGeoCodingTemplate($template)
{
}
/**
*
* Connection to Google Maps' API web service
*
* Modified to include a template for api
* just in case the url changes in future releases
* Includes template parsing and CURL calls
* @author Antonio Ramirez Cobos
* @since 2010-12-21
*
* @param string $address
* @param string $format 'csv' or 'xml' or 'json'
* @return string
* @author fabriceb
* @since 2009-06-17
* @since 2010-12-22 cUrl and Yii adaptation Antonio Ramirez
* @since 2011-04-21 Matt Cheale Updated to API V3 and moved HTTP call to another function.
*
*/
public function getGeocodingInfo($address, $format = 'json')
{
$apiURL = self::API_GEOCODE_URL . $format . '?address=' . urlencode($address) . '&sensor=false';
return $this->callApi($apiURL);
}
/**
* Reverse geocoding info
* @param $lat
* @param $lng
* @param string $format
* @return string
* @author Vincent Guillon <vincentg@theodo.fr>
* @since 2010-03-04
* @since 2010-12-22 modified by Antonio Ramirez (CUrl call)
* @since 2011-03-23 Matt Cheale Updated the query string to use v3 API variables.
* @since 2011-04-21 Matt Cheale Added format option and moved HTTP call to another function.
* @since 2011-12-19 Antonio Ramirez modified API call
*/
public function getReverseGeocodingInfo($lat, $lng, $format = 'json')
{
$apiURL = self::API_GEOCODE_URL . $format . '?latlng=' . $lat . ',' . $lng . '&sensor=false';
return $this->callApi($apiURL);
}
/**
* Elevation info request
*
* @param string $locations the coordinates array to get elevation info from
* @param string $format 'xml' or 'json'
* @return string
* @author Antonio Ramirez
*/
public function getElevationInfo($locations, $format = 'json')
{
$apiURL = self::API_ELEVATION_URL . $format . '?locations=' . $locations . '&sensor=false';
return $this->callApi($apiURL);
}
/**
* Takes the $apiURL and performs that HTTP request to Google, returning the
* raw data.
* @param string $apiUrl
* @return bool|mixed|string
* @author Matt Cheale
* @since 2011-04-21
* @since 2011-12-17 Modified to fix open_basedir restrictions
*/
private function callApi($apiUrl)
{
if (function_exists('curl_version')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
$raw_data = $this->_curl_exec_follow($ch);
curl_close($ch);
} else // no CUrl, try differently
$raw_data = file_get_contents($apiUrl);
return $raw_data;
}
/**
* This function handles redirections with CURL if safe_mode or open_basedir
* is enabled.
* @param resource $ch the curl handle
* @param integer $maxredirections
* @return bool|mixed
* @throws CHttpException
*/
private function _curl_exec_follow(&$ch, $maxredirections = 5)
{
if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $maxredirections > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $maxredirections);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
if ($maxredirections > 0) {
$new_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$rch = curl_copy_handle($ch);
curl_setopt($rch, CURLOPT_HEADER, true);
curl_setopt($rch, CURLOPT_NOBODY, true);
curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
do {
curl_setopt($rch, CURLOPT_URL, $new_url);
$header = curl_exec($rch);
if (curl_errno($rch))
$code = 0;
else {
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
if ($code == 301 || $code == 302) {
preg_match('/Location:(.*?)\n/', $header, $matches);
$new_url = trim(array_pop($matches));
} else
$code = 0;
}
} while ($code && --$maxredirections);
curl_close($rch);
if (!$maxredirections) {
if ($maxredirections === null)
throw new CHttpException(301, 'Too many redirects. When following redirects, libcurl hit the maximum amount.');
return false;
}
curl_setopt($ch, CURLOPT_URL, $new_url);
}
}
return curl_exec($ch);
}
}