geocoder-php /
Geocoder
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the Geocoder package. |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | * |
||
| 10 | * @license MIT License |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Geocoder\Provider\Nominatim; |
||
| 14 | |||
| 15 | use Geocoder\Collection; |
||
| 16 | use Geocoder\Exception\InvalidServerResponse; |
||
| 17 | use Geocoder\Exception\UnsupportedOperation; |
||
| 18 | use Geocoder\Location; |
||
| 19 | use Geocoder\Model\AddressBuilder; |
||
| 20 | use Geocoder\Model\AddressCollection; |
||
| 21 | use Geocoder\Query\GeocodeQuery; |
||
| 22 | use Geocoder\Query\ReverseQuery; |
||
| 23 | use Geocoder\Http\Provider\AbstractHttpProvider; |
||
| 24 | use Geocoder\Provider\Provider; |
||
| 25 | use Geocoder\Provider\Nominatim\Model\NominatimAddress; |
||
| 26 | use Http\Client\HttpClient; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @author Niklas Närhinen <[email protected]> |
||
| 30 | * @author Jonathan Beliën <[email protected]> |
||
| 31 | */ |
||
| 32 | final class Nominatim extends AbstractHttpProvider implements Provider |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $rootUrl; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param HttpClient $client |
||
| 41 | * @param string|null $locale |
||
|
0 ignored issues
–
show
|
|||
| 42 | * |
||
| 43 | * @return Nominatim |
||
| 44 | */ |
||
| 45 | public static function withOpenStreetMapServer(HttpClient $client) |
||
| 46 | { |
||
| 47 | return new self($client, 'https://nominatim.openstreetmap.org'); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param HttpClient $client an HTTP adapter |
||
| 52 | * @param string $rootUrl Root URL of the nominatim server |
||
| 53 | */ |
||
| 54 | public function __construct(HttpClient $client, $rootUrl) |
||
| 55 | { |
||
| 56 | parent::__construct($client); |
||
| 57 | |||
| 58 | $this->rootUrl = rtrim($rootUrl, '/'); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 65 | { |
||
| 66 | $address = $query->getText(); |
||
| 67 | // This API does not support IPv6 |
||
| 68 | if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
||
| 69 | throw new UnsupportedOperation('The Nominatim provider does not support IPv6 addresses.'); |
||
| 70 | } |
||
| 71 | |||
| 72 | if ('127.0.0.1' === $address) { |
||
| 73 | return new AddressCollection([$this->getLocationForLocalhost()]); |
||
| 74 | } |
||
| 75 | |||
| 76 | $url = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $query->getLimit()); |
||
| 77 | $content = $this->executeQuery($url, $query->getLocale()); |
||
| 78 | |||
| 79 | $doc = new \DOMDocument(); |
||
| 80 | if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')->item(0)) { |
||
| 81 | throw InvalidServerResponse::create($url); |
||
| 82 | } |
||
| 83 | |||
| 84 | $searchResult = $doc->getElementsByTagName('searchresults')->item(0); |
||
| 85 | $attribution = $searchResult->getAttribute('attribution'); |
||
| 86 | $places = $searchResult->getElementsByTagName('place'); |
||
| 87 | |||
| 88 | if (null === $places || 0 === $places->length) { |
||
| 89 | return new AddressCollection([]); |
||
| 90 | } |
||
| 91 | |||
| 92 | $results = []; |
||
| 93 | foreach ($places as $place) { |
||
| 94 | $results[] = $this->xmlResultToArray($place, $place, $attribution, false); |
||
| 95 | } |
||
| 96 | |||
| 97 | return new AddressCollection($results); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | public function reverseQuery(ReverseQuery $query): Collection |
||
| 104 | { |
||
| 105 | $coordinates = $query->getCoordinates(); |
||
| 106 | $longitude = $coordinates->getLongitude(); |
||
| 107 | $latitude = $coordinates->getLatitude(); |
||
| 108 | $url = sprintf($this->getReverseEndpointUrl(), $latitude, $longitude, $query->getData('zoom', 18)); |
||
| 109 | $content = $this->executeQuery($url, $query->getLocale()); |
||
| 110 | |||
| 111 | $doc = new \DOMDocument(); |
||
| 112 | if (!@$doc->loadXML($content) || $doc->getElementsByTagName('error')->length > 0) { |
||
| 113 | return new AddressCollection([]); |
||
| 114 | } |
||
| 115 | |||
| 116 | $searchResult = $doc->getElementsByTagName('reversegeocode')->item(0); |
||
| 117 | $attribution = $searchResult->getAttribute('attribution'); |
||
| 118 | $addressParts = $searchResult->getElementsByTagName('addressparts')->item(0); |
||
| 119 | $result = $searchResult->getElementsByTagName('result')->item(0); |
||
| 120 | |||
| 121 | return new AddressCollection([$this->xmlResultToArray($result, $addressParts, $attribution, true)]); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param \DOMElement $resultNode |
||
| 126 | * @param \DOMElement $addressNode |
||
| 127 | * |
||
| 128 | * @return Location |
||
| 129 | */ |
||
| 130 | private function xmlResultToArray(\DOMElement $resultNode, \DOMElement $addressNode, string $attribution, bool $reverse): Location |
||
| 131 | { |
||
| 132 | $builder = new AddressBuilder($this->getName()); |
||
| 133 | |||
| 134 | foreach (['state', 'county'] as $i => $tagName) { |
||
| 135 | if (null !== ($adminLevel = $this->getNodeValue($addressNode->getElementsByTagName($tagName)))) { |
||
| 136 | $builder->addAdminLevel($i + 1, $adminLevel, ''); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | // get the first postal-code when there are many |
||
| 141 | $postalCode = $this->getNodeValue($addressNode->getElementsByTagName('postcode')); |
||
| 142 | if (!empty($postalCode)) { |
||
| 143 | $postalCode = current(explode(';', $postalCode)); |
||
| 144 | } |
||
| 145 | $builder->setPostalCode($postalCode); |
||
| 146 | |||
| 147 | $localityFields = ['city', 'town', 'village', 'hamlet']; |
||
| 148 | foreach ($localityFields as $localityField) { |
||
| 149 | $localityFieldContent = $this->getNodeValue($addressNode->getElementsByTagName($localityField)); |
||
| 150 | if (!empty($localityFieldContent)) { |
||
| 151 | $builder->setLocality($localityFieldContent); |
||
| 152 | |||
| 153 | break; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $builder->setStreetName($this->getNodeValue($addressNode->getElementsByTagName('road')) ?: $this->getNodeValue($addressNode->getElementsByTagName('pedestrian'))); |
||
| 158 | $builder->setStreetNumber($this->getNodeValue($addressNode->getElementsByTagName('house_number'))); |
||
| 159 | $builder->setSubLocality($this->getNodeValue($addressNode->getElementsByTagName('suburb'))); |
||
| 160 | $builder->setCountry($this->getNodeValue($addressNode->getElementsByTagName('country'))); |
||
| 161 | |||
| 162 | $countryCode = $this->getNodeValue($addressNode->getElementsByTagName('country_code')); |
||
| 163 | if (!empty($countryCode)) { |
||
| 164 | $countryCode = strtoupper($countryCode); |
||
| 165 | } |
||
| 166 | $builder->setCountryCode($countryCode); |
||
| 167 | |||
| 168 | $builder->setCoordinates($resultNode->getAttribute('lat'), $resultNode->getAttribute('lon')); |
||
| 169 | |||
| 170 | $boundsAttr = $resultNode->getAttribute('boundingbox'); |
||
| 171 | if ($boundsAttr) { |
||
| 172 | $bounds = []; |
||
| 173 | list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = explode(',', $boundsAttr); |
||
| 174 | $builder->setBounds($bounds['south'], $bounds['west'], $bounds['north'], $bounds['east']); |
||
| 175 | } |
||
| 176 | |||
| 177 | $location = $builder->build(NominatimAddress::class); |
||
| 178 | $location = $location->withAttribution($attribution); |
||
| 179 | $location = $location->withOSMId(intval($resultNode->getAttribute('osm_id'))); |
||
| 180 | $location = $location->withOSMType($resultNode->getAttribute('osm_type')); |
||
| 181 | |||
| 182 | if (false === $reverse) { |
||
| 183 | $location = $location->withClass($resultNode->getAttribute('class')); |
||
| 184 | $location = $location->withDisplayName($resultNode->getAttribute('display_name')); |
||
| 185 | $location = $location->withType($resultNode->getAttribute('type')); |
||
| 186 | } else { |
||
| 187 | $location = $location->withDisplayName($resultNode->nodeValue); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $location; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function getName(): string |
||
| 197 | { |
||
| 198 | return 'nominatim'; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $url |
||
| 203 | * @param string|null $locale |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | private function executeQuery(string $url, string $locale = null): string |
||
| 208 | { |
||
| 209 | if (null !== $locale) { |
||
| 210 | $url = sprintf('%s&accept-language=%s', $url, $locale); |
||
| 211 | } |
||
| 212 | |||
| 213 | return $this->getUrlContents($url); |
||
| 214 | } |
||
| 215 | |||
| 216 | private function getGeocodeEndpointUrl(): string |
||
| 217 | { |
||
| 218 | return $this->rootUrl.'/search?q=%s&format=xml&addressdetails=1&limit=%d'; |
||
| 219 | } |
||
| 220 | |||
| 221 | private function getReverseEndpointUrl(): string |
||
| 222 | { |
||
| 223 | return $this->rootUrl.'/reverse?format=xml&lat=%F&lon=%F&addressdetails=1&zoom=%d'; |
||
| 224 | } |
||
| 225 | |||
| 226 | private function getNodeValue(\DOMNodeList $element) |
||
| 227 | { |
||
| 228 | return $element->length ? $element->item(0)->nodeValue : null; |
||
| 229 | } |
||
| 230 | } |
||
| 231 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.