-
Notifications
You must be signed in to change notification settings - Fork 53
/
Config.php
533 lines (498 loc) · 14.4 KB
/
Config.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* Config.php
* 08-Nov-2011
*
* PHP Version 5
*
* @category Services
* @package Services_Openstreetmap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: @package_version@
* @link Config.php
*/
/**
* Services_Openstreetmap_Config
*
* @category Services
* @package Services_Openstreetmap
* @author Ken Guest <kguest@php.net>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link Config.php
*/
class Services_Openstreetmap_Config
{
/**
* Minimum version of the OSM API that is supported.
* @var float
* @internal
*/
protected $minVersion = null;
/**
* Maximum version of the OSM API that is supported.
* @var float
* @internal
*/
protected $maxVersion = null;
/**
* timeout, in seconds.
* @var integer
* @internal
*/
protected $timeout = null;
/**
* number of elements allowed per changeset
* @var integer
* @internal
*/
protected $changeset_maximum_elements = null;
/**
* Maximum number of nodes per way.
* @var integer
* @internal
*/
protected $waynodes_maximum = null;
/**
* Number of tracepoints per way.
* @var integer
* @internal
*/
protected $tracepoints_per_page = null;
/**
* Max size of area that can be downloaded in one request.
* @var float
* @internal
*/
protected $area_maximum = null;
/**
* Default config settings
*
* @var array
* @see Services_Openstreetmap::getConfig
* @see Services_Openstreetmap::setConfig
*/
protected $config = array(
'adapter' => 'HTTP_Request2_Adapter_Socket',
'api_version' => '0.6',
'password' => null,
'passwordfile' => null,
'server' => 'http://api.openstreetmap.org/',
'User-Agent' => 'Services_Openstreetmap',
'user' => null,
'verbose' => false,
);
/**
* Version of the [OSM] API which communications will be over.
* @var string
* @internal
*/
protected $api_version = '0.6';
/**
* Server to connect to.
* @var string
* @internal
*/
protected $server = 'http://api.openstreetmap.org/';
/**
* Get the value of a configuration setting - if none is set all are
* returned.
*
* <code>
* $config = $osm->getConfig();
* </code>
*
* @param string $name name. optional.
*
* @return mixed value of $name parameter, array of all configuration
* parameters if $name is not given
* @throws Services_Openstreetmap_Exception If the parameter is unknown
*/
public function getValue($name = null)
{
if (is_null($name)) {
return $this->config;
} elseif (!array_key_exists($name, $this->config)) {
throw new Services_Openstreetmap_Exception(
"Unknown config parameter '$name'"
);
}
return $this->config[$name];
}
/**
* set at least one configuration variable.
*
* <pre>
* $osm->setConfig('user', 'fred@example.com');
* $osm->setConfig(array('user' => 'fred@example.com', 'password' => 'Simples'));
* $osm->setConfig('user' => 'f@example.com')->setConfig('password' => 'Sis');
* </pre>
*
* The following parameters are available:
* <ul>
* <li> 'adapter' - adapter to use (string)</li>
* <li> 'api_version' - Version of API to communicate via (string)</li>
* <li> 'password' - password (string, optional)</li>
* <li> 'passwordfile' - passwordfile (string, optional)</li>
* <li> 'server' - server to connect to (string)</li>
* <li> 'User-Agent' - User-Agent (string)</li>
* <li> 'user' - user (string, optional)</li>
* <li> 'verbose' - verbose (boolean, optional)</li>
* </ul>
*
* @param mixed $config array containing config settings
* @param mixed $value config value if $config is not an array
*
* @throws Services_Openstreetmap_Exception If the parameter is unknown
*
* @access public
* @return Services_Openstreetmap_Config
*/
public function setValue($config, $value = null)
{
if (is_array($config)) {
if (isset($config['adapter'])) {
$this->config['adapter'] = $config['adapter'];
}
foreach ($config as $key=>$value) {
if (!array_key_exists($key, $this->config)) {
throw new Services_Openstreetmap_Exception(
"Unknown config parameter '$key'"
);
}
switch($key) {
case 'server':
$this->setServer($value);
break;
case 'passwordfile':
$this->setPasswordfile($value);
break;
case 'api_version':
$this->config[$key] = $value;
$api = "Services_Openstreetmap_API_V" . str_replace(
'.',
'',
$value
);
$this->api = new $api;
break;
default:
$this->config[$key] = $value;
}
}
} else {
if (!array_key_exists($config, $this->config)) {
throw new Services_Openstreetmap_Exception(
"Unknown config parameter '$config'"
);
}
$this->config[$config] = $value;
if ($config == 'server') {
$this->setServer($this->server);
} elseif ($config == 'passwordfile') {
$this->setPasswordfile($value);
}
}
return $this;
}
/**
* Connect to specified server.
*
* @param string $server base server details, e.g. http://api.openstreetmap.org
*
* @access public
* @return Services_Openstreetmap
*/
public function setServer($server)
{
try {
$c = $this->getTransport()->getResponse($server . '/api/capabilities');
} catch (Exception $ex) {
throw new Services_Openstreetmap_Exception(
'Could not get a valid response from server',
$ex->getCode(),
$ex
);
}
$this->server = $server;
$capabilities = $c->getBody();
if (!$this->_checkCapabilities($capabilities)) {
throw new Services_Openstreetmap_Exception(
'Problem checking server capabilities'
);
}
$this->config['server'] = $server;
return $this;
}
/**
* Set and parse a password file, setting username and password as specified
* in the file.
*
* A password file is a ASCII text file, with username and passwords pairs
* on each line, seperated [delimited] by a semicolon.
* Lines starting with a hash [#] are comments.
* If only one non-commented line is present in the file, that username and
* password will be used for authentication.
* If more than one set of usernames and passwords are present, the
* username must be specified, and the matching password from the file will
* be used.
*
* <pre>
* # Example password file.
* fredfs@example.com:Wilma4evah
* barney@example.net:B3ttyRawks
* </pre>
*
* @param string $file file containing credentials
*
* @access public
* @return Services_Openstreetmap
*/
public function setPasswordfile($file)
{
if (is_null($file)) {
return $this;
}
$lines = @file($file);
if ($lines === false) {
throw new Services_Openstreetmap_Exception(
'Could not read password file'
);
}
$this->config['passwordfile'] = $file;
array_walk($lines, create_function('&$val', '$val = trim($val);'));
if (sizeof($lines) == 1) {
if (strpos($lines[0], '#') !== 0) {
list($this->config['user'], $this->config['password'])
= explode(':', $lines[0]);
}
} elseif (sizeof($lines) == 2) {
if (strpos($lines[0], '#') === 0) {
if (strpos($lines[1], '#') !== 0) {
list($this->config['user'], $this->config['password'])
= explode(':', $lines[1]);
}
}
} else {
foreach ($lines as $line) {
if (strpos($line, '#') === 0) {
continue;
}
list($user, $pwd) = explode(':', $line);
if ($user == $this->config['user']) {
$this->config['password'] = $pwd;
}
}
}
return $this;
}
/**
* Set the Transport instance.
*
* @param Services_Openstreetmap_Transport $transport Transport instance.
*
* @return Services_Openstreetmap_Config
*/
public function setTransport(Services_Openstreetmap_Transport $transport)
{
$this->transport = $transport;
return $this;
}
/**
* Retrieve the current Transport instance.
*
* @return Services_Openstreetmap_Transport.
*/
public function getTransport()
{
return $this->transport;
}
/**
* Return all config settings in an array.
*
* @return array
*/
public function asArray()
{
return $this->config;
}
/**
* Set various properties to describe the capabilities that the connected
* server supports.
*
* @param mixed $capabilities XML describing the capabilities of the server
*
* @see maxVersion
* @see minVersion
* @see timeout
*
* @return boolean
*
* @internal
* @throws Services_Openstreetmap_Exception If the API Version is not
* supported.
*/
private function _checkCapabilities($capabilities)
{
$xml = simplexml_load_string($capabilities);
if ($xml === false) {
return false;
}
$this->minVersion = (float) $this->getXMLValue($xml, 'version', 'minimum');
$this->maxVersion = (float) $this->getXMLValue($xml, 'version', 'maximum');
if (($this->minVersion > $this->api_version
|| $this->api_version > $this->maxVersion)
) {
throw new Services_Openstreetmap_Exception(
'Specified API Version ' . $this->api_version .' not supported.'
);
}
$this->timeout = (int) $this->getXMLValue($xml, 'timeout', 'seconds');
//changesets
$this->changeset_maximum_elements = (int) $this->getXMLValue(
$xml,
'changesets',
'maximum_elements'
);
// Maximum number of nodes per way.
$this->waynodes_maximum = (int) $this->getXMLValue(
$xml,
'waynodes',
'maximum'
);
// Number of tracepoints per way.
$this->tracepoints_per_page = (int) $this->getXMLValue(
$xml,
'tracepoints',
'per_page'
);
// Max size of area that can be downloaded in one request.
$this->area_maximum = (float) $this->getXMLValue(
$xml,
'area',
'maximum'
);
return true;
}
/**
* Max size of area that can be downloaded in one request.
*
* <code>
* $osm = new Services_Openstreetmap();
* $area_allowed = $osm->getMaxArea();
* </code>
*
* @return float
*/
public function getMaxArea()
{
return $this->area_maximum;
}
/**
* minVersion - min API version supported by connected server.
*
* <code>
* $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva');
* $osm = new Services_Openstreetmap($config);
* $min = $osm->getMinVersion();
* </code>
*
* @return float
*/
public function getMinVersion()
{
return $this->minVersion;
}
/**
* maxVersion - max API version supported by connected server.
*
* <code>
* $config = array('user' => 'fred@example.net', 'password' => 'wilma4eva');
* $osm = new Services_Openstreetmap($config);
* $max = $osm->getMaxVersion();
* </code>
*
* @return float
*/
public function getMaxVersion()
{
return $this->maxVersion;
}
/**
* Return the number of seconds that must elapse before a connection is
* considered to have timed-out.
*
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* Maximum number of tracepoints per page.
*
* <code>
* $osm = new Services_Openstreetmap();
* $tracepoints = $osm->getTracepointsPerPage();
* </code>
*
* @return float
*/
public function getTracepointsPerPage()
{
return $this->tracepoints_per_page;
}
/**
* Maximum number of nodes per way.
*
* Anymore than that and the way must be split.
*
* <code>
* $osm = new Services_Openstreetmap();
* $max = $osm->getMaxNodes();
* </code>
*
* @return float
*/
public function getMaxNodes()
{
return $this->waynodes_maximum;
}
/**
* Number of elements allowed per changeset
*
* <code>
* $osm = new Services_Openstreetmap();
* $max = $osm->getMaxElements();
* </code>
*
* @return float
*/
public function getMaxElements()
{
return $this->changeset_maximum_elements;
}
/**
* getXMLValue
*
* @param SimpleXMLElement $xml Object
* @param string $tag name of tag
* @param string $attribute name of attribute
* @param mixed $default default value
*
* @return string
*/
public function getXMLValue(
SimpleXMLElement $xml,
$tag,
$attribute,
$default = null
) {
$obj = $xml->xpath('//' . $tag);
if (empty($obj)) {
return $default;
}
return $obj[0]->attributes()->$attribute;
}
}
?>