예제 #1
0
 /**
  * Search for the given address in one of the geocoding services and update
  * its data.
  *
  * Data lat, lon, zip and city may get updated.
  *
  * @param array   &$address Address record from database
  * @param integer $service  Geocoding service to use
  *						  - 0: internal caching database table
  *						  - 1: geonames.org
  *						  - 2: nominatim.openstreetmap.org
  *
  * @return boolean True if the address got updated, false if not.
  */
 function searchAddress(&$address, $service = 0)
 {
     $config = tx_odsosm_div::getConfig(array('default_country', 'geo_service_email', 'geo_service_user'));
     $ll = false;
     $country = strtoupper(strlen($address['country']) == 2 ? $address['country'] : $config['default_country']);
     $email = t3lib_div::validEmail($config['geo_service_email']) ? $config['geo_service_email'] : $_SERVER['SERVER_ADMIN'];
     if (TYPO3_DLOG) {
         $service_names = array(0 => 'cache', 1 => 'geonames', 2 => 'nominatim');
         t3lib_div::devLog('Search address using ' . $service_names[$service], 'ods_osm', 0, $address);
     }
     switch ($service) {
         case 0:
             // cache
             $where = array();
             if ($country) {
                 $where[] = 'country=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($country, 'tx_odsosm_geocache');
             }
             if ($address['city']) {
                 $where[] = '(city=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['city'], 'tx_odsosm_geocache') . ' OR search_city=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['city'], 'tx_odsosm_geocache') . ')';
             }
             if ($address['zip']) {
                 $where[] = 'zip=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['zip'], 'tx_odsosm_geocache');
             }
             if ($address['street']) {
                 $where[] = 'street=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['street'], 'tx_odsosm_geocache');
             }
             if ($address['housenumber']) {
                 $where[] = 'housenumber=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($address['housenumber'], 'tx_odsosm_geocache');
             }
             if ($where) {
                 $where[] = 'deleted=0';
                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_odsosm_geocache', implode(' AND ', $where));
                 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                 if ($row) {
                     $ll = true;
                     $set = array('tstamp' => time(), 'cache_hit' => $row['cache_hit'] + 1);
                     $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_odsosm_geocache', 'uid=' . $row['uid'], $set);
                     $address['lat'] = $row['lat'];
                     $address['lon'] = $row['lon'];
                     if ($row['zip']) {
                         $address['zip'] = $row['zip'];
                     }
                     if ($row['city']) {
                         $address['city'] = $row['city'];
                     }
                     if ($row['state']) {
                         $address['state'] = $row['state'];
                     }
                     if (empty($address['country'])) {
                         $address['country'] = $row['country'];
                     }
                 }
             }
             break;
         case 1:
             // http://www.geonames.org/
             if ($country) {
                 $query['country'] = $country;
             }
             if ($address['city']) {
                 $query['placename'] = $address['city'];
             }
             if ($address['zip']) {
                 $query['postalcode'] = $address['zip'];
             }
             if ($query) {
                 $query['maxRows'] = 1;
                 $query['username'] = $config['geo_service_user'];
                 $xml = t3lib_div::getURL('http://api.geonames.org/postalCodeSearch?' . http_build_query($query, '', '&'), false, 'User-Agent: TYPO3 extension ods_osm/' . t3lib_extMgm::getExtensionVersion('ods_osm'));
                 if (TYPO3_DLOG && $xml === false) {
                     t3lib_div::devLog('t3lib_div::getURL failed', "ods_osm", 3);
                 }
                 if ($xml) {
                     $xmlobj = new SimpleXMLElement($xml);
                     if ($xmlobj->status) {
                         if (TYPO3_DLOG) {
                             t3lib_div::devLog('GeoNames message', 'ods_osm', 2, (array) $xmlobj->status->attributes());
                         }
                         $o_flashMessage = t3lib_div::makeInstance('t3lib_FlashMessage', (string) $xmlobj->status->attributes()->message, 'GeoNames message', t3lib_FlashMessage::WARNING);
                         t3lib_FlashMessageQueue::addMessage($o_flashMessage);
                     }
                     if ($xmlobj->code) {
                         $ll = true;
                         $address['lat'] = (string) $xmlobj->code->lat;
                         $address['lon'] = (string) $xmlobj->code->lng;
                         if ($xmlobj->code->postalcode) {
                             $address['zip'] = (string) $xmlobj->code->postalcode;
                         }
                         if ($xmlobj->code->name) {
                             $address['city'] = (string) $xmlobj->code->name;
                         }
                         if (empty($address['country'])) {
                             $address['country'] = (string) $xmlobj->code->countryCode;
                         }
                     }
                 }
             }
             break;
         case 2:
             // http://nominatim.openstreetmap.org/
             $query['country'] = $country;
             $query['email'] = $email;
             $query['addressdetails'] = 1;
             $query['format'] = 'xml';
             if ($this->address_type == 'structured') {
                 if ($address['city']) {
                     $query['city'] = $address['city'];
                 }
                 if ($address['zip']) {
                     $query['postalcode'] = $address['zip'];
                 }
                 if ($address['street']) {
                     $query['street'] = $address['street'];
                 }
                 if ($address['housenumber']) {
                     $query['street'] = $address['housenumber'] . ' ' . $query['street'];
                 }
                 if (TYPO3_DLOG) {
                     t3lib_div::devLog('Nominatim structured', 'ods_osm', -1, $query);
                 }
                 $ll = tx_odsosm_div::searchAddressNominatim($query, $address);
                 if (!$ll && $query['postalcode']) {
                     unset($query['postalcode']);
                     if (TYPO3_DLOG) {
                         t3lib_div::devLog('Nominatim retrying without zip', 'ods_osm', -1, $query);
                     }
                     $ll = tx_odsosm_div::searchAddressNominatim($query, $address);
                 }
             }
             if ($this->address_type == 'unstructured') {
                 $query['q'] = $address['address'];
                 if (TYPO3_DLOG) {
                     t3lib_div::devLog('Nominatim unstructured', 'ods_osm', -1, $query);
                 }
                 $ll = tx_odsosm_div::searchAddressNominatim($query, $address);
             }
             break;
     }
     if (TYPO3_DLOG) {
         if ($ll) {
             t3lib_div::devLog('Return address', 'ods_osm', 0, $address);
         } else {
             t3lib_div::devLog('No address found', 'ods_osm', 0);
         }
     }
     return $ll;
 }