| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Transport.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 http://pear.php.net/package/Services_Openstreetmap |
| 14 |
|
*/ |
| 15 |
|
|
| 16 |
|
/** |
| 17 |
|
* Services_Openstreetmap_Transport |
| 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 Transport.php |
| 24 |
|
*/ |
| 25 |
|
class Services_Openstreetmap_Transport |
| 26 |
|
{ |
| 27 |
|
/**#@+ |
| 28 |
|
* @link http://tools.ietf.org/html/rfc2616 |
| 29 |
|
*/ |
| 30 |
|
/** |
| 31 |
|
* Ok |
| 32 |
|
*/ |
| 33 |
|
const OK = 200; |
| 34 |
|
/** |
| 35 |
|
* Unauthorised, e.g. login credentials wrong. |
| 36 |
|
*/ |
| 37 |
|
const UNAUTHORISED = 401; |
| 38 |
|
/** |
| 39 |
|
* Resource not found. |
| 40 |
|
*/ |
| 41 |
|
const NOT_FOUND = 404; |
| 42 |
|
/** |
| 43 |
|
* Method not allowed. |
| 44 |
|
*/ |
| 45 |
|
const METHOD_NOT_ALLOWED = 405; |
| 46 |
|
/** |
| 47 |
|
* Conflict. |
| 48 |
|
*/ |
| 49 |
|
const CONFLICT = 409; |
| 50 |
|
/** |
| 51 |
|
* Resource no longer available. |
| 52 |
|
*/ |
| 53 |
|
const GONE = 410; |
| 54 |
|
/** |
| 55 |
|
* Precondition failed. |
| 56 |
|
*/ |
| 57 |
|
const PRECONDITION_FAILED = 412; |
| 58 |
|
/** |
| 59 |
|
* Internal server error. |
| 60 |
|
*/ |
| 61 |
|
const INTERNAL_SERVER_ERROR = 500; |
| 62 |
|
/** |
| 63 |
|
* Service unavailable. |
| 64 |
|
*/ |
| 65 |
|
const SERVICE_UNAVAILABLE = 503; |
| 66 |
|
/** |
| 67 |
|
* Bandwidth limited exceeded. |
| 68 |
|
* @link http://wiki.openstreetmap.org/wiki/API_v0.6 |
| 69 |
|
*/ |
| 70 |
|
const BANDWIDTH_LIMIT_EXCEEDED = 509; |
| 71 |
|
/**#@-*/ |
| 72 |
|
|
| 73 |
|
/** |
| 74 |
|
* The HTTP_Request2 instance. |
| 75 |
|
* |
| 76 |
|
* Customise this for proxy settings etc with the getRequest/setRequest |
| 77 |
|
* methods if necessary. |
| 78 |
|
* |
| 79 |
|
* @var HTTP_Request2 $request |
| 80 |
|
* @internal |
| 81 |
|
* @see Services_Openstreetmap::getRequest |
| 82 |
|
* @see Services_Openstreetmap::setRequest |
| 83 |
|
*/ |
| 84 |
|
protected $request = null; |
| 85 |
|
|
| 86 |
|
protected $config = null; |
| 87 |
|
|
| 88 |
|
/** |
| 89 |
|
* Send request to OSM server and return the response. |
| 90 |
|
* |
| 91 |
|
* @param string $url URL |
| 92 |
|
* @param string $method GET (default)/POST/PUT |
| 93 |
|
* @param string $user user (optional for read-only actions) |
| 94 |
|
* @param string $password password (optional for read-only actions) |
| 95 |
|
* @param string $body body (optional) |
| 96 |
|
* @param array $post_data (optional) |
| 97 |
|
* @param array $headers (optional) |
| 98 |
|
* |
| 99 |
|
* @return HTTP_Request2_Response |
| 100 |
|
* @throws Services_Openstreetmap_Exception If something unexpected has |
| 101 |
|
* happened while conversing with |
| 102 |
|
* the server. |
| 103 |
|
*/ |
| 104 |
|
function getResponse( |
| 105 |
|
$url, |
| 106 |
|
$method = HTTP_Request2::METHOD_GET, |
| 107 |
|
$user = null, |
| 108 |
|
$password = null, |
| 109 |
|
$body = null, |
| 110 |
|
array $post_data = null, |
| 111 |
|
array $headers = null |
| 112 |
|
) { |
| 113 |
67 |
$response = null; |
| 114 |
67 |
$eMsg = null; |
| 115 |
|
|
| 116 |
67 |
if ($this->getConfig()->getValue('verbose')) { |
| 117 |
|
echo $url, "\n"; |
| 118 |
|
} |
| 119 |
67 |
$request = $this->getRequest(); |
| 120 |
67 |
$request->setUrl($url); |
| 121 |
67 |
$request->setMethod($method); |
| 122 |
67 |
$request->setAdapter($this->getConfig()->getValue('adapter')); |
| 123 |
|
|
| 124 |
|
|
| 125 |
67 |
$request->setHeader( |
| 126 |
67 |
'User-Agent', |
| 127 |
67 |
$this->getConfig()->getValue('User-Agent') |
| 128 |
67 |
); |
| 129 |
67 |
if (!is_null($user) && !is_null($password)) { |
| 130 |
10 |
$request->setAuth($user, $password); |
| 131 |
10 |
} |
| 132 |
67 |
if ($post_data != array()) { |
| 133 |
|
$request->setMethod(HTTP_Request2::METHOD_POST); |
| 134 |
|
foreach ($post_data as $key => $value) { |
| 135 |
|
$request->addPostParameter($key, $value); |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
67 |
if ($headers != array()) { |
| 139 |
7 |
foreach ($headers as $header) { |
| 140 |
7 |
$request->setHeader($header[0], $header[1], $header[2]); |
| 141 |
7 |
} |
| 142 |
7 |
} |
| 143 |
67 |
if (!is_null($body)) { |
| 144 |
7 |
$request->setBody($body); |
| 145 |
7 |
} |
| 146 |
67 |
$code = 0; |
| 147 |
|
try { |
| 148 |
67 |
$response = $request->send(); |
| 149 |
67 |
$code = $response->getStatus(); |
| 150 |
|
|
| 151 |
67 |
if ($this->getConfig()->getValue('verbose')) { |
| 152 |
|
var_dump($response->getHeader()); |
| 153 |
|
var_dump($response->getBody()); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
67 |
if (Services_Openstreetmap_Transport::OK == $code) { |
| 157 |
66 |
return $response; |
| 158 |
66 |
} else { |
| 159 |
|
$eMsg = 'Unexpected HTTP status: ' |
| 160 |
11 |
. $code . ' ' |
| 161 |
11 |
. $response->getReasonPhrase(); |
| 162 |
11 |
$error = $response->getHeader('error'); |
| 163 |
11 |
if (!is_null($error)) { |
| 164 |
|
$eMsg .= " ($error)"; |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
} |
| 168 |
11 |
} catch (HTTP_Request2_Exception $e) { |
| 169 |
|
throw new Services_Openstreetmap_Exception( |
| 170 |
|
$e->getMessage(), |
| 171 |
|
$code, |
| 172 |
|
$e |
| 173 |
|
); |
| 174 |
|
} |
| 175 |
11 |
if ($eMsg != null) { |
| 176 |
11 |
throw new Services_Openstreetmap_Exception($eMsg, $code); |
| 177 |
11 |
} |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
/** |
| 181 |
|
* Get HTTP_Request2 instance. |
| 182 |
|
* |
| 183 |
|
* @return HTTP_Request2 |
| 184 |
|
*/ |
| 185 |
|
function getRequest() |
| 186 |
|
{ |
| 187 |
67 |
if (is_null($this->request)) { |
| 188 |
67 |
$this->request = new HTTP_Request2(); |
| 189 |
67 |
} |
| 190 |
67 |
return $this->request; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
/** |
| 194 |
|
* Set the HTTP_Request2 instance and return the Services_Openstreetmap |
| 195 |
|
* instance. |
| 196 |
|
* |
| 197 |
|
* Use this to inject a specific HTTP_Request2 instance. |
| 198 |
|
* |
| 199 |
|
* @param HTTP_Request2 $request The HTTP_Request2 instance to set. |
| 200 |
|
* |
| 201 |
|
* @return Services_Openstreetmap |
| 202 |
|
*/ |
| 203 |
|
public function setRequest(HTTP_Request2 $request) |
| 204 |
|
{ |
| 205 |
|
$this->request = $request; |
| 206 |
|
return $this; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
/** |
| 210 |
|
* getObject |
| 211 |
|
* |
| 212 |
|
* Returns false if the object is not found |
| 213 |
|
* |
| 214 |
|
* @param string $type object type |
| 215 |
|
* @param mixed $id id of object to retrieve |
| 216 |
|
* @param mixed $version version of object |
| 217 |
|
* |
| 218 |
|
* @return object |
| 219 |
|
* @throws Services_Openstreetmap_Exception |
| 220 |
|
*/ |
| 221 |
|
public function getObject($type, $id, $version = null) |
| 222 |
|
{ |
| 223 |
|
/* |
| 224 |
|
if (!in_array($type, $this->elements)) { |
| 225 |
|
throw new Services_Openstreetmap_Exception( |
| 226 |
|
sprintf("Invalid Element Type '%s'", $type) |
| 227 |
|
); |
| 228 |
|
}*/ |
| 229 |
|
|
| 230 |
24 |
$config = $this->getConfig()->asArray(); |
| 231 |
24 |
$url = $config['server'] |
| 232 |
|
. 'api/' |
| 233 |
24 |
. $config['api_version'] |
| 234 |
24 |
. '/' . $type . '/' |
| 235 |
24 |
. $id; |
| 236 |
24 |
if (!is_null($version)) { |
| 237 |
1 |
$url .= "/$version"; |
| 238 |
1 |
} |
| 239 |
|
try { |
| 240 |
24 |
$response = $this->getResponse($url); |
| 241 |
24 |
} catch (Services_Openstreetmap_Exception $ex) { |
| 242 |
3 |
$code = $ex->getCode(); |
| 243 |
3 |
if (Services_Openstreetmap_Transport::NOT_FOUND == $code) { |
| 244 |
1 |
return false; |
| 245 |
2 |
} elseif (Services_Openstreetmap_Transport::GONE == $code) { |
| 246 |
1 |
return false; |
| 247 |
1 |
} else { |
| 248 |
1 |
throw $ex; |
| 249 |
|
} |
| 250 |
|
} |
| 251 |
21 |
$class = 'Services_Openstreetmap_' . ucfirst(strtolower($type)); |
| 252 |
21 |
$obj = new $class(); |
| 253 |
21 |
$obj->setXml(simplexml_load_string($response->getBody())); |
| 254 |
21 |
return $obj; |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
/** |
| 258 |
|
* getObjects |
| 259 |
|
* |
| 260 |
|
* @param string $type object type |
| 261 |
|
* @param array $ids ids of objects to retrieve |
| 262 |
|
* |
| 263 |
|
* @return void |
| 264 |
|
* |
| 265 |
|
*/ |
| 266 |
|
public function getObjects($type, array $ids) |
| 267 |
|
{ |
| 268 |
|
/* |
| 269 |
|
if (!in_array($type, $this->elements)) { |
| 270 |
|
throw new Services_Openstreetmap_Exception('Invalid Element Type'); |
| 271 |
|
} |
| 272 |
|
*/ |
| 273 |
11 |
$config = $this->getConfig(); |
| 274 |
11 |
$url = $config->getValue('server') |
| 275 |
|
. 'api/' |
| 276 |
11 |
. $config->getValue('api_version') |
| 277 |
11 |
. '/' . $type . 's?' . $type . 's=' |
| 278 |
11 |
. implode(',', $ids); |
| 279 |
|
try { |
| 280 |
11 |
$response = $this->getResponse($url); |
| 281 |
11 |
} catch (Services_Openstreetmap_Exception $ex) { |
| 282 |
4 |
switch ($ex->getCode()) { |
| 283 |
4 |
case Services_Openstreetmap_Transport::NOT_FOUND: |
| 284 |
4 |
case Services_Openstreetmap_Transport::UNAUTHORISED: |
| 285 |
4 |
case Services_Openstreetmap_Transport::GONE: |
| 286 |
3 |
return false; |
| 287 |
1 |
default: |
| 288 |
1 |
throw $ex; |
| 289 |
1 |
} |
| 290 |
|
} |
| 291 |
|
|
| 292 |
7 |
$class = 'Services_Openstreetmap_' . ucfirst(strtolower($type)) . 's'; |
| 293 |
7 |
$obj = new $class(); |
| 294 |
7 |
if (!is_null($config)) { |
| 295 |
7 |
$obj->setConfig($config); |
| 296 |
7 |
} |
| 297 |
7 |
$obj->setTransport($this); |
| 298 |
7 |
$sxe = @simplexml_load_string($response->getBody()); |
| 299 |
7 |
if ($sxe === false) { |
| 300 |
1 |
$obj->setVal(trim($response->getBody())); |
| 301 |
1 |
} else { |
| 302 |
6 |
$obj->setXml($sxe); |
| 303 |
|
} |
| 304 |
7 |
return $obj; |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
/** |
| 308 |
|
* Set Config object |
| 309 |
|
* |
| 310 |
|
* @param Services_Openstreetmap_Config $config Config settings. |
| 311 |
|
* |
| 312 |
|
* @return Services_Openstreetmap_API_V06 |
| 313 |
|
*/ |
| 314 |
|
public function setConfig(Services_Openstreetmap_Config $config) |
| 315 |
|
{ |
| 316 |
84 |
$this->config = $config; |
| 317 |
|
} |
| 318 |
|
|
| 319 |
|
/** |
| 320 |
|
* Get current Config object |
| 321 |
|
* |
| 322 |
|
* @return Services_Openstreetmap_Config |
| 323 |
|
*/ |
| 324 |
|
public function getConfig() |
| 325 |
|
{ |
| 326 |
67 |
return $this->config; |
| 327 |
|
} |
| 328 |
|
|
| 329 |
|
/** |
| 330 |
|
* searchObjects |
| 331 |
|
* |
| 332 |
|
* @param string $type object type (e.g. changeset) |
| 333 |
|
* @param array $criteria array of criterion objects. |
| 334 |
|
* |
| 335 |
|
* @return Services_Openstreetmap_Objects |
| 336 |
|
*/ |
| 337 |
|
public function searchObjects($type, array $criteria) |
| 338 |
|
{ |
| 339 |
5 |
$query = array(); |
| 340 |
5 |
foreach ($criteria as $criterion) { |
| 341 |
5 |
$query[] = $criterion->query(); |
| 342 |
5 |
} |
| 343 |
5 |
$config = $this->getConfig(); |
| 344 |
5 |
$url = $config->getValue('server') |
| 345 |
|
. 'api/' |
| 346 |
5 |
. $config->getValue('api_version') |
| 347 |
5 |
. '/' . $type . 's?' . implode('&', $query); |
| 348 |
|
try { |
| 349 |
5 |
$response = $this->getResponse($url); |
| 350 |
5 |
} catch (Services_Openstreetmap_Exception $ex) { |
| 351 |
|
switch ($ex->getCode()) { |
| 352 |
|
case Services_Openstreetmap_Transport::NOT_FOUND: |
| 353 |
|
case Services_Openstreetmap_Transport::UNAUTHORISED: |
| 354 |
|
case Services_Openstreetmap_Transport::GONE: |
| 355 |
|
return false; |
| 356 |
|
default: |
| 357 |
|
throw $ex; |
| 358 |
|
} |
| 359 |
|
} |
| 360 |
5 |
$class = 'Services_Openstreetmap_' . ucfirst(strtolower($type)) . 's'; |
| 361 |
5 |
$obj = new $class(); |
| 362 |
5 |
$sxe = @simplexml_load_string($response->getBody()); |
| 363 |
5 |
if ($sxe === false) { |
| 364 |
|
$obj->setVal(trim($response->getBody())); |
| 365 |
|
} else { |
| 366 |
5 |
$obj->setXml($sxe); |
| 367 |
|
} |
| 368 |
5 |
return $obj; |
| 369 |
|
} |
| 370 |
|
} |
| 371 |
|
// vim:set et ts=4 sw=4: |
| 372 |
|
?> |