Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Tweak statistics module to not require curl
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Nov 3, 2014
1 parent f48bec7 commit b41c50b
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions modules/statistics/models/base/IpLocationModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ abstract public function getByIp($ip);
*/
public function performGeolocation($apiKey)
{
if (function_exists('curl_init') == false) {
throw new Exception('Curl must be enabled on the server');
}

if (empty($apiKey)) {
return 'Empty API key. No geolocations performed';
}
Expand All @@ -71,15 +67,25 @@ public function performGeolocation($apiKey)
$location->setLatitude('0');
$location->setLongitude('0'); // only try geolocation once per ip
if ($location->getIp()) {
$url = 'http://api.ipinfodb.com/v3/ip-city/?key='.$apiKey.'&ip='.$location->getIp().'&format=json';
$url = 'https://api.ipinfodb.com/v3/ip-city/?key='.$apiKey.'&ip='.$location->getIp().'&format=json';

if (extension_loaded('curl')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_PORT, 443);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
if ($status != 200) {
$response = false;
}
} else {
$response = file_get_contents($url, false);
}

if ($resp && !empty($resp)) {
$answer = json_decode($resp);
if ($status !== false) {
$answer = json_decode($response);
if ($answer && strtoupper($answer->statusCode) == 'OK') {
$location->setLatitude($answer->latitude);
$location->setLongitude($answer->longitude);
Expand Down

0 comments on commit b41c50b

Please sign in to comment.