1 |
|
<?php |
2 |
|
/** |
3 |
|
* Config.php |
4 |
|
* 08-Nov-2011 |
5 |
|
* |
6 |
|
* PHP Version 5 |
7 |
|
* |
8 |
|
* @category Services |
9 |
|
* @package Services_OpenStreetMap |
10 |
|
* @author Ken Guest <kguest@php.net> |
11 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
12 |
|
* @version Release: @package_version@ |
13 |
|
* @link Config.php |
14 |
|
*/ |
15 |
|
|
16 |
|
/** |
17 |
|
* Services_OpenStreetMap_Config |
18 |
|
* |
19 |
|
* @category Services |
20 |
|
* @package Services_OpenStreetMap |
21 |
|
* @author Ken Guest <kguest@php.net> |
22 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
23 |
|
* @link Config.php |
24 |
|
*/ |
25 |
|
class Services_OpenStreetMap_Config |
26 |
|
{ |
27 |
|
|
28 |
|
/** |
29 |
|
* Minimum version of the OSM API that is supported. |
30 |
|
* @var float |
31 |
|
* @internal |
32 |
|
*/ |
33 |
|
protected $minVersion = null; |
34 |
|
|
35 |
|
/** |
36 |
|
* Maximum version of the OSM API that is supported. |
37 |
|
* @var float |
38 |
|
* @internal |
39 |
|
*/ |
40 |
|
protected $maxVersion = null; |
41 |
|
|
42 |
|
/** |
43 |
|
* timeout, in seconds. |
44 |
|
* @var integer |
45 |
|
* @internal |
46 |
|
*/ |
47 |
|
protected $timeout = null; |
48 |
|
|
49 |
|
/** |
50 |
|
* number of elements allowed per changeset |
51 |
|
* @var integer |
52 |
|
* @internal |
53 |
|
*/ |
54 |
|
protected $changesetMaximumElements = null; |
55 |
|
|
56 |
|
/** |
57 |
|
* Maximum number of nodes per way. |
58 |
|
* @var integer |
59 |
|
* @internal |
60 |
|
*/ |
61 |
|
protected $waynodesMaximum = null; |
62 |
|
|
63 |
|
/** |
64 |
|
* Number of tracepoints per way. |
65 |
|
* @var integer |
66 |
|
* @internal |
67 |
|
*/ |
68 |
|
protected $tracepointsPerPage = null; |
69 |
|
|
70 |
|
/** |
71 |
|
* Max size of area that can be downloaded in one request. |
72 |
|
* @var float |
73 |
|
* @internal |
74 |
|
*/ |
75 |
|
protected $areaMaximum = null; |
76 |
|
|
77 |
|
/** |
78 |
|
* Default config settings |
79 |
|
* |
80 |
|
* @var array |
81 |
|
* @see Services_OpenStreetMap::getConfig |
82 |
|
* @see Services_OpenStreetMap::setConfig |
83 |
|
*/ |
84 |
|
protected $config = array( |
85 |
|
'adapter' => 'HTTP_Request2_Adapter_Socket', |
86 |
|
'api_version' => '0.6', |
87 |
|
'password' => null, |
88 |
|
'passwordfile' => null, |
89 |
|
'server' => 'http://api.openstreetmap.org/', |
90 |
|
'User-Agent' => 'Services_OpenStreetMap', |
91 |
|
'user' => null, |
92 |
|
'verbose' => false, |
93 |
|
); |
94 |
|
|
95 |
|
/** |
96 |
|
* Version of the [OSM] API which communications will be over. |
97 |
|
* @var string |
98 |
|
* @internal |
99 |
|
*/ |
100 |
|
protected $api_version = '0.6'; |
101 |
|
|
102 |
|
/** |
103 |
|
* Server to connect to. |
104 |
|
* @var string |
105 |
|
* @internal |
106 |
|
*/ |
107 |
|
protected $server = 'http://api.openstreetmap.org/'; |
108 |
|
|
109 |
|
/** |
110 |
|
* Capabilities XML generated by... |
111 |
|
* |
112 |
|
* @var string |
113 |
|
* @internal |
114 |
|
*/ |
115 |
|
protected $generator = 'Generator'; |
116 |
|
|
117 |
|
/** |
118 |
|
* Get the value of a configuration setting - if none is set all are |
119 |
|
* returned. |
120 |
|
* |
121 |
|
* <code> |
122 |
|
* $config = $osm->getConfig(); |
123 |
|
* </code> |
124 |
|
* |
125 |
|
* @param string $name name. optional. |
126 |
|
* |
127 |
|
* @return mixed value of $name parameter, array of all configuration |
128 |
|
* parameters if $name is not given |
129 |
|
* @throws Services_OpenStreetMap_InvalidArgumentException If the parameter |
130 |
|
* is unknown |
131 |
|
*/ |
132 |
|
public function getValue($name = null) |
133 |
|
{ |
134 |
101 |
if (is_null($name)) { |
135 |
1 |
return $this->config; |
136 |
101 |
} elseif (!array_key_exists($name, $this->config)) { |
137 |
1 |
throw new Services_OpenStreetMap_InvalidArgumentException( |
138 |
1 |
"Unknown config parameter '$name'" |
139 |
1 |
); |
140 |
1 |
} |
141 |
101 |
return $this->config[$name]; |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
|
* set at least one configuration variable. |
146 |
|
* |
147 |
|
* <pre> |
148 |
|
* $osm->setConfig('user', 'fred@example.com'); |
149 |
|
* $osm->setConfig(array('user' => 'fred@example.com', 'password' => 'Simples')); |
150 |
|
* $osm->setConfig('user' => 'f@example.com')->setConfig('password' => 'Sis'); |
151 |
|
* </pre> |
152 |
|
* |
153 |
|
* The following parameters are available: |
154 |
|
* <ul> |
155 |
|
* <li> 'adapter' - adapter to use (string)</li> |
156 |
|
* <li> 'api_version' - Version of API to communicate via (string)</li> |
157 |
|
* <li> 'password' - password (string, optional)</li> |
158 |
|
* <li> 'passwordfile' - passwordfile (string, optional)</li> |
159 |
|
* <li> 'server' - server to connect to (string)</li> |
160 |
|
* <li> 'User-Agent' - User-Agent (string)</li> |
161 |
|
* <li> 'user' - user (string, optional)</li> |
162 |
|
* <li> 'verbose' - verbose (boolean, optional)</li> |
163 |
|
* </ul> |
164 |
|
* |
165 |
|
* @param mixed $config array containing config settings |
166 |
|
* @param mixed $value config value if $config is not an array |
167 |
|
* |
168 |
|
* @throws Services_OpenStreetMap_InvalidArgumentException If the parameter |
169 |
|
* is unknown |
170 |
|
* |
171 |
|
* @return Services_OpenStreetMap_Config |
172 |
|
*/ |
173 |
|
public function setValue($config, $value = null) |
174 |
|
{ |
175 |
102 |
if (is_array($config)) { |
176 |
102 |
if (isset($config['adapter'])) { |
177 |
94 |
$this->config['adapter'] = $config['adapter']; |
178 |
94 |
} |
179 |
102 |
foreach ($config as $key=>$value) { |
180 |
94 |
if (!array_key_exists($key, $this->config)) { |
181 |
1 |
throw new Services_OpenStreetMap_InvalidArgumentException( |
182 |
1 |
"Unknown config parameter '$key'" |
183 |
1 |
); |
184 |
1 |
} |
185 |
|
switch($key) { |
186 |
94 |
case 'server': |
187 |
68 |
$this->setServer($value); |
188 |
65 |
break; |
189 |
94 |
case 'passwordfile': |
190 |
10 |
$this->setPasswordfile($value); |
191 |
10 |
break; |
192 |
94 |
case 'api_version': |
193 |
2 |
$this->config[$key] = $value; |
194 |
2 |
$api = "Services_OpenStreetMap_API_V" . str_replace( |
195 |
2 |
'.', |
196 |
2 |
'', |
197 |
|
$value |
198 |
2 |
); |
199 |
2 |
$this->api = new $api; |
200 |
2 |
break; |
201 |
94 |
default: |
202 |
94 |
$this->config[$key] = $value; |
203 |
94 |
} |
204 |
102 |
} |
205 |
98 |
} else { |
206 |
9 |
if (!array_key_exists($config, $this->config)) { |
207 |
2 |
throw new Services_OpenStreetMap_InvalidArgumentException( |
208 |
2 |
"Unknown config parameter '$config'" |
209 |
2 |
); |
210 |
2 |
} |
211 |
7 |
$this->config[$config] = $value; |
212 |
7 |
if ($config == 'server') { |
213 |
1 |
$this->setServer($this->server); |
214 |
6 |
} elseif ($config == 'passwordfile') { |
215 |
5 |
$this->setPasswordfile($value); |
216 |
4 |
} |
217 |
|
} |
218 |
98 |
return $this; |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
|
* Connect to specified server. |
223 |
|
* |
224 |
|
* @param string $server base server details, e.g. http://api.openstreetmap.org |
225 |
|
* |
226 |
|
* @return Services_OpenStreetMap |
227 |
|
*/ |
228 |
|
public function setServer($server) |
229 |
|
{ |
230 |
|
try { |
231 |
70 |
$c = $this->getTransport()->getResponse($server . '/api/capabilities'); |
232 |
70 |
} catch (Exception $ex) { |
233 |
1 |
throw new Services_OpenStreetMap_Exception( |
234 |
1 |
'Could not get a valid response from server', |
235 |
1 |
$ex->getCode(), |
236 |
|
$ex |
237 |
1 |
); |
238 |
|
} |
239 |
69 |
$this->server = $server; |
240 |
69 |
$capabilities = $c->getBody(); |
241 |
69 |
if (!$this->_checkCapabilities($capabilities)) { |
242 |
1 |
throw new Services_OpenStreetMap_Exception( |
243 |
|
'Problem checking server capabilities' |
244 |
1 |
); |
245 |
1 |
} |
246 |
66 |
$this->config['server'] = $server; |
247 |
|
|
248 |
66 |
return $this; |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
|
* Set and parse a password file, setting username and password as specified |
253 |
|
* in the file. |
254 |
|
* |
255 |
|
* A password file is a ASCII text file, with username and passwords pairs |
256 |
|
* on each line, seperated [delimited] by a semicolon. |
257 |
|
* Lines starting with a hash [#] are comments. |
258 |
|
* If only one non-commented line is present in the file, that username and |
259 |
|
* password will be used for authentication. |
260 |
|
* If more than one set of usernames and passwords are present, the |
261 |
|
* username must be specified, and the matching password from the file will |
262 |
|
* be used. |
263 |
|
* |
264 |
|
* <pre> |
265 |
|
* # Example password file. |
266 |
|
* fredfs@example.com:Wilma4evah |
267 |
|
* barney@example.net:B3ttyRawks |
268 |
|
* </pre> |
269 |
|
* |
270 |
|
* @param string $file file containing credentials |
271 |
|
* |
272 |
|
* @return Services_OpenStreetMap |
273 |
|
*/ |
274 |
|
public function setPasswordfile($file) |
275 |
|
{ |
276 |
20 |
if (is_null($file)) { |
277 |
2 |
return $this; |
278 |
2 |
} |
279 |
18 |
$lines = @file($file); |
280 |
18 |
if ($lines === false) { |
281 |
2 |
throw new Services_OpenStreetMap_Exception( |
282 |
|
'Could not read password file' |
283 |
2 |
); |
284 |
2 |
} |
285 |
16 |
$this->config['passwordfile'] = $file; |
286 |
16 |
array_walk($lines, create_function('&$val', '$val = trim($val);')); |
287 |
16 |
if (sizeof($lines) == 1) { |
288 |
4 |
if (strpos($lines[0], '#') !== 0) { |
289 |
4 |
list($this->config['user'], $this->config['password']) |
290 |
4 |
= explode(':', $lines[0]); |
291 |
4 |
} |
292 |
16 |
} elseif (sizeof($lines) == 2) { |
293 |
8 |
if (strpos($lines[0], '#') === 0) { |
294 |
8 |
if (strpos($lines[1], '#') !== 0) { |
295 |
8 |
list($this->config['user'], $this->config['password']) |
296 |
8 |
= explode(':', $lines[1]); |
297 |
8 |
} |
298 |
8 |
} |
299 |
8 |
} else { |
300 |
4 |
foreach ($lines as $line) { |
301 |
2 |
if (strpos($line, '#') === 0) { |
302 |
2 |
continue; |
303 |
2 |
} |
304 |
2 |
list($user, $pwd) = explode(':', $line); |
305 |
2 |
if ($user == $this->config['user']) { |
306 |
2 |
$this->config['password'] = $pwd; |
307 |
2 |
} |
308 |
4 |
} |
309 |
|
} |
310 |
16 |
return $this; |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
|
* Set the Transport instance. |
315 |
|
* |
316 |
|
* @param Services_OpenStreetMap_Transport $transport Transport instance. |
317 |
|
* |
318 |
|
* @return Services_OpenStreetMap_Config |
319 |
|
*/ |
320 |
|
public function setTransport(Services_OpenStreetMap_Transport $transport) |
321 |
|
{ |
322 |
102 |
$this->transport = $transport; |
323 |
102 |
return $this; |
324 |
|
} |
325 |
|
|
326 |
|
/** |
327 |
|
* Retrieve the current Transport instance. |
328 |
|
* |
329 |
|
* @return Services_OpenStreetMap_Transport. |
330 |
|
*/ |
331 |
|
public function getTransport() |
332 |
|
{ |
333 |
70 |
return $this->transport; |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* Return all config settings in an array. |
338 |
|
* |
339 |
|
* @return array |
340 |
|
*/ |
341 |
|
public function asArray() |
342 |
|
{ |
343 |
32 |
return $this->config; |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
|
* Set various properties to describe the capabilities that the connected |
348 |
|
* server supports. |
349 |
|
* |
350 |
|
* @param mixed $capabilities XML describing the capabilities of the server |
351 |
|
* |
352 |
|
* @see maxVersion |
353 |
|
* @see minVersion |
354 |
|
* @see timeout |
355 |
|
* |
356 |
|
* @return boolean |
357 |
|
* |
358 |
|
* @internal |
359 |
|
* @throws Services_OpenStreetMap_Exception If the API Version is not |
360 |
|
* supported. |
361 |
|
*/ |
362 |
|
private function _checkCapabilities($capabilities) |
363 |
|
{ |
364 |
69 |
$xml = simplexml_load_string($capabilities); |
365 |
69 |
if ($xml === false) { |
366 |
1 |
return false; |
367 |
1 |
} |
368 |
|
|
369 |
68 |
$this->minVersion = (float) $this->getXmlValue($xml, 'version', 'minimum'); |
370 |
68 |
$this->maxVersion = (float) $this->getXmlValue($xml, 'version', 'maximum'); |
371 |
68 |
if (($this->minVersion > $this->api_version |
372 |
68 |
|| $this->api_version > $this->maxVersion) |
373 |
68 |
) { |
374 |
2 |
throw new Services_OpenStreetMap_Exception( |
375 |
2 |
'Specified API Version ' . $this->api_version .' not supported.' |
376 |
2 |
); |
377 |
2 |
} |
378 |
66 |
$this->timeout = (int) $this->getXmlValue($xml, 'timeout', 'seconds'); |
379 |
|
|
380 |
|
//changesets |
381 |
66 |
$this->changesetMaximumElements = (int) $this->getXmlValue( |
382 |
66 |
$xml, |
383 |
66 |
'changesets', |
384 |
|
'maximum_elements' |
385 |
66 |
); |
386 |
|
|
387 |
|
// Maximum number of nodes per way. |
388 |
66 |
$this->waynodesMaximum = (int) $this->getXmlValue( |
389 |
66 |
$xml, |
390 |
66 |
'waynodes', |
391 |
|
'maximum' |
392 |
66 |
); |
393 |
|
|
394 |
|
// Number of tracepoints per way. |
395 |
66 |
$this->tracepointsPerPage = (int) $this->getXmlValue( |
396 |
66 |
$xml, |
397 |
66 |
'tracepoints', |
398 |
|
'per_page' |
399 |
66 |
); |
400 |
|
|
401 |
|
// Max size of area that can be downloaded in one request. |
402 |
66 |
$this->areaMaximum = (float) $this->getXmlValue( |
403 |
66 |
$xml, |
404 |
66 |
'area', |
405 |
|
'maximum' |
406 |
66 |
); |
407 |
|
|
408 |
|
// What generated the XML. |
409 |
66 |
$this->generator = '' . $this->getXmlValue( |
410 |
66 |
$xml, |
411 |
66 |
'osm', |
412 |
66 |
'generator', |
413 |
|
'OpenStreetMap server' |
414 |
66 |
); |
415 |
|
|
416 |
66 |
return true; |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
|
* Max size of area that can be downloaded in one request. |
421 |
|
* |
422 |
|
* <code> |
423 |
|
* $osm = new Services_OpenStreetMap(); |
424 |
|
* $area_allowed = $osm->getMaxArea(); |
425 |
|
* </code> |
426 |
|
* |
427 |
|
* @return float |
428 |
|
*/ |
429 |
|
public function getMaxArea() |
430 |
|
{ |
431 |
1 |
return $this->areaMaximum; |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
|
* minVersion - min API version supported by connected server. |
436 |
|
* |
437 |
|
* <code> |
438 |
|
* $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva'); |
439 |
|
* $osm = new Services_OpenStreetMap($config); |
440 |
|
* $min = $osm->getMinVersion(); |
441 |
|
* </code> |
442 |
|
* |
443 |
|
* @return float |
444 |
|
*/ |
445 |
|
public function getMinVersion() |
446 |
|
{ |
447 |
1 |
return $this->minVersion; |
448 |
|
} |
449 |
|
|
450 |
|
/** |
451 |
|
* maxVersion - max API version supported by connected server. |
452 |
|
* |
453 |
|
* <code> |
454 |
|
* $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva'); |
455 |
|
* $osm = new Services_OpenStreetMap($config); |
456 |
|
* $max = $osm->getMaxVersion(); |
457 |
|
* </code> |
458 |
|
* |
459 |
|
* @return float |
460 |
|
*/ |
461 |
|
public function getMaxVersion() |
462 |
|
{ |
463 |
1 |
return $this->maxVersion; |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
|
* Return the number of seconds that must elapse before a connection is |
468 |
|
* considered to have timed-out. |
469 |
|
* |
470 |
|
* @return int |
471 |
|
*/ |
472 |
|
public function getTimeout() |
473 |
|
{ |
474 |
1 |
return $this->timeout; |
475 |
|
} |
476 |
|
|
477 |
|
/** |
478 |
|
* Maximum number of tracepoints per page. |
479 |
|
* |
480 |
|
* <code> |
481 |
|
* $osm = new Services_OpenStreetMap(); |
482 |
|
* $tracepoints = $osm->getTracepointsPerPage(); |
483 |
|
* </code> |
484 |
|
* |
485 |
|
* @return float |
486 |
|
*/ |
487 |
|
public function getTracepointsPerPage() |
488 |
|
{ |
489 |
1 |
return $this->tracepointsPerPage; |
490 |
|
} |
491 |
|
|
492 |
|
/** |
493 |
|
* Maximum number of nodes per way. |
494 |
|
* |
495 |
|
* Anymore than that and the way must be split. |
496 |
|
* |
497 |
|
* <code> |
498 |
|
* $osm = new Services_OpenStreetMap(); |
499 |
|
* $max = $osm->getMaxNodes(); |
500 |
|
* </code> |
501 |
|
* |
502 |
|
* @return float |
503 |
|
*/ |
504 |
|
public function getMaxNodes() |
505 |
|
{ |
506 |
1 |
return $this->waynodesMaximum; |
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
|
* Number of elements allowed per changeset |
511 |
|
* |
512 |
|
* <code> |
513 |
|
* $osm = new Services_OpenStreetMap(); |
514 |
|
* $max = $osm->getMaxElements(); |
515 |
|
* </code> |
516 |
|
* |
517 |
|
* @return float |
518 |
|
*/ |
519 |
|
public function getMaxElements() |
520 |
|
{ |
521 |
1 |
return $this->changesetMaximumElements; |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
|
* Name of what generated the Capabilities XML |
526 |
|
* |
527 |
|
* @return string |
528 |
|
*/ |
529 |
|
public function getGenerator() |
530 |
|
{ |
531 |
1 |
return $this->generator; |
532 |
|
} |
533 |
|
|
534 |
|
/** |
535 |
|
* getXmlValue |
536 |
|
* |
537 |
|
* @param SimpleXMLElement $xml Object |
538 |
|
* @param string $tag name of tag |
539 |
|
* @param string $attribute name of attribute |
540 |
|
* @param mixed $default default value |
541 |
|
* |
542 |
|
* @return string |
543 |
|
*/ |
544 |
|
public function getXmlValue( |
545 |
|
SimpleXMLElement $xml, |
546 |
|
$tag, |
547 |
|
$attribute, |
548 |
|
$default = null |
549 |
|
) { |
550 |
68 |
$obj = $xml->xpath('//' . $tag); |
551 |
68 |
if (empty($obj)) { |
552 |
|
return $default; |
553 |
|
} |
554 |
68 |
return $obj[0]->attributes()->$attribute; |
555 |
|
} |
556 |
|
|
557 |
|
} |
558 |
|
|
559 |
|
?> |