Example #1
0
 /**
  * Set the geocode data for this location
  * @since Version 3.9.1
  * @return void
  */
 private function geocode()
 {
     if (!empty($this->region) && !empty($this->country) && !empty($this->locality)) {
         #return;
     }
     $woe = PlaceUtility::LatLonWoELookup($this->lat, $this->lon);
     $woe = PlaceUtility::formatWoE($woe);
     $this->country = $woe['country_code'];
     $this->region = $woe['region_code'];
     $this->locality = $woe['neighbourhood'];
     $this->neighbourhood = null;
     // $this->neighbourhood is ignored
     if (empty($this->region)) {
         $this->region = $woe['region'];
     }
     /**
      * Shit's a little bit broken, it's ok
      */
     if (empty($this->region) || empty($this->country)) {
         $woe = PlaceUtility::LatLonWoELookup($this->lat, $this->lon, true);
         // force lookup, ignore any cached data
         $woe = PlaceUtility::formatWoE($woe);
         $this->region = $woe['region_code'];
         $this->country = $woe['country_code'];
     }
     if (empty($this->region)) {
         $this->region = strtoupper($woe['region_name']);
     }
     #printArray($this->region);die;
     return;
     /*
     
     // Fetch geodata and populate the vars
     //$url  = "http://maps.google.com/maps/geo?q=".$this->lat.",".$this->lon."&output=json&sensor=false";
     $url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" . $this->lat . "," . $this->lon . "&sensor=false";
     $ch     = curl_init();
     
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
     $data       = curl_exec($ch);
     $jsondata   = json_decode($data, true); 
     curl_close($ch);
     
     if (isset($jsondata['results'][0]['address_components'])) {
         $row = $jsondata['results'][0]['address_components']; 
     }
     
     if (isset($jsondata['Placemark']) && $area = $jsondata['Placemark'][0]['AddressDetails']['Country']) {
         $this->country          = $area['CountryNameCode']; 
         $this->region           = isset($area['AdministrativeArea']['AdministrativeAreaName']) ? $area['AdministrativeArea']['AdministrativeAreaName'] : NULL; 
         $this->locality         = isset($area['AdministrativeArea']['Locality']['LocalityName']) ? $area['AdministrativeArea']['Locality']['LocalityName'] : NULL; 
         $this->neighbourhood    = isset($area['CountryNameCode']['someotherarea']) ? $area['CountryNameCode']['someotherarea'] : NULL; 
     } elseif (isset($row)) {
         // Loop through the results and try to populate the object
         foreach ($row as $area) {
             if ($area['types'][0] == "country") {
                 $this->country = $area['short_name']; 
             }
             
             if ($area['types'][0] == "administrative_area_level_2") {
                 $this->region = $area['long_name']; 
             }
             
             if ($area['types'][0] == "administrative_area_level_3") {
                 $this->neighbourhood = $area['long_name']; 
             }
             
             if ($area['types'][0] == "locality") {
                 $this->locality = $area['long_name']; 
             }
         }
     }
     
     if (empty($this->country) || empty($this->region) || empty($this->locality)) {
         // Google doesn't give data in a consistent bloody format - go here instead
         $url    = sprintf("http://www.geoplugin.net/extras/location.gp?lat=%s&long=%s&format=php", $this->lat, $this->lon);
         $ch     = curl_init();
         
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         
         $geodata    = curl_exec($ch);
         $geodata    = unserialize($geodata); 
         curl_close($ch);
         
         $this->country          = $geodata['geoplugin_countryCode']; 
         $this->region           = $geodata['geoplugin_region']; 
         $this->locality         = $geodata['geoplugin_place']; 
         $this->neighbourhood    = NULL;
     }
     
     return;
     */
 }
Example #2
0
 /**
  * Get WOE (Where On Earth) data from Yahoo's GeoPlanet API
  *
  * Ported from [master]/includes/functions.php
  * @since Version 3.8.7
  * @param string $lookup
  * @param array $types Yahoo Woe types to lookup
  * @return array
  */
 public static function getWOEData($lookup = false, $types = false)
 {
     if ($lookup === false) {
         return false;
     }
     $return = array();
     $expiry = strtotime("+1 year");
     $mckey = "railpage:woe=" . $lookup;
     if ($types) {
         $mckey .= ";types=" . implode(",", $types);
     }
     $Cache = AppCore::getRedis();
     $Cache = AppCore::getMemcached();
     /**
      * Try and get the WoE data from Memcached or Redis
      */
     if ($return = $Cache->fetch($mckey)) {
         /**
          * Convert JSON back to an array if required
          */
         if (!is_array($return) && is_string($return)) {
             $return = json_decode($return, true);
         }
         return $return;
     }
     /**
      * Try and get the WoE data from the database
      */
     $Database = (new AppCore())->getDatabaseConnection();
     $query = "SELECT response FROM cache_woe WHERE hash = ?";
     if ($return = $Database->fetchOne($query, md5($mckey))) {
         $return = json_decode($return, true);
         $Cache->save($mckey, $return, $expiry);
         return $return;
     }
     /**
      * Nothing found in our cache - look it up
      */
     $Config = AppCore::getConfig();
     $latlng = $lookup;
     if (preg_match("@[a-zA-Z]+@", $lookup) || strpos($lookup, ",")) {
         $lookup = sprintf("places.q('%s')", $lookup);
     } else {
         $lookup = sprintf("place/%s", $lookup);
     }
     if ($types === false) {
         $url = sprintf("http://where.yahooapis.com/v1/%s?lang=en&appid=%s&format=json", $lookup, $Config->Yahoo->ApplicationID);
     } else {
         $url = sprintf("http://where.yahooapis.com/v1/places\$and(.q('%s'),.type(%s))?lang=en&appid=%s&format=json", $latlng, implode(",", $types), $Config->Yahoo->ApplicationID);
     }
     /**
      * Attempt to fetch the WoE data from our local cache
      */
     if (strpos($lookup, ",") !== false) {
         $tmp = str_replace("places.q('", "", str_replace("')", "", $lookup));
         $tmp = explode(",", $tmp);
         $return = PlaceUtility::LatLonWoELookup($tmp[0], $tmp[1]);
         $Cache->save($mckey, $return, strtotime("+1 hour"));
         return $return;
     }
     /**
      * Try and fetch using GuzzleHTTP from the web service
      */
     try {
         $GuzzleClient = new Client();
         $response = $GuzzleClient->get($url);
     } catch (RequestException $e) {
         switch ($e->getResponse()->getStatusCode()) {
             case 503:
                 throw new Exception("Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.");
                 break;
             case 403:
                 throw new Exception("Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.");
                 break;
             case 400:
                 if (!($return = PlaceUtility::getViaCurl($url))) {
                     throw new Exception(sprintf("Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means:  Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML/JSON response. The URL sent was: %s\n\n%s", $url, json_decode($e->getResponse()->getBody())));
                 }
                 break;
             default:
                 throw new Exception("Your call to Yahoo Web Services returned an unexpected HTTP status of: " . $e->getResponse()->getStatusCode());
         }
     }
     if (!$return && isset($response) && $response->getStatusCode() == 200) {
         $return = json_decode($response->getBody(), true);
     }
     $return['url'] = $url;
     /**
      * Attempt to cache this data
      */
     if ($return !== false) {
         /**
          * Save it in MariaDB
          */
         $data = ["hash" => md5($mckey), "response" => json_encode($return), "expiry" => date("Y-m-d H:i:s", $expiry)];
         $Database->insert("cache_woe", $data);
         $rs = $Cache->save($mckey, $return, $expiry);
         /**
          * Verify that it actually saved in the cache handler. It's being a turd lately
          */
         if (!$rs || json_encode($return) != json_encode($Cache->fetch($mckey))) {
             $Cache->save($mckey, json_encode($return), $expiry);
         }
     }
     return $return;
 }