/** * Update the geoplace reference for this image * * @since Version 3.9.1 * @return void */ public function updateGeoPlace() { if (!filter_var($this->lat, FILTER_VALIDATE_FLOAT) || !filter_var($this->lat, FILTER_VALIDATE_FLOAT)) { return; } $timer = microtime(true); $GeoPlaceID = PlaceUtility::findGeoPlaceID($this->lat, $this->lon); #var_dump($GeoPlaceID);die; $data = ["geoplace" => $GeoPlaceID]; $where = ["id = ?" => $this->id]; $this->db->update("image", $data, $where); $this->Memcached->delete($this->mckey); $this->Redis->delete($this->mckey); Debug::logEvent(__METHOD__, $timer); Debug::LogCLI(__METHOD__, $timer); return; }
/** * Update the geoplace linked to this location * @since Version 3.9.1 * @return void */ private function updateGeoplace() { $id = PlaceUtility::findGeoPlaceID($this->lat, $this->lon); if ($id != $this->geoplace_id) { $data = ["geoplace" => $id]; $where = ["id = ?" => $this->id]; $this->db->update("location", $data, $where); $this->Memcached->delete($this->mckey); } return; }
/** * 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; }