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

Commit b41c50b

Browse files
author
Jamie Snape
committed
Tweak statistics module to not require curl
1 parent f48bec7 commit b41c50b

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

modules/statistics/models/base/IpLocationModelBase.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ abstract public function getByIp($ip);
5757
*/
5858
public function performGeolocation($apiKey)
5959
{
60-
if (function_exists('curl_init') == false) {
61-
throw new Exception('Curl must be enabled on the server');
62-
}
63-
6460
if (empty($apiKey)) {
6561
return 'Empty API key. No geolocations performed';
6662
}
@@ -71,15 +67,25 @@ public function performGeolocation($apiKey)
7167
$location->setLatitude('0');
7268
$location->setLongitude('0'); // only try geolocation once per ip
7369
if ($location->getIp()) {
74-
$url = 'http://api.ipinfodb.com/v3/ip-city/?key='.$apiKey.'&ip='.$location->getIp().'&format=json';
70+
$url = 'https://api.ipinfodb.com/v3/ip-city/?key='.$apiKey.'&ip='.$location->getIp().'&format=json';
71+
72+
if (extension_loaded('curl')) {
73+
$curl = curl_init();
74+
curl_setopt($curl, CURLOPT_URL, $url);
75+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
76+
curl_setopt($curl, CURLOPT_PORT, 443);
77+
$response = curl_exec($curl);
78+
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
7579

76-
$curl = curl_init();
77-
curl_setopt($curl, CURLOPT_URL, $url);
78-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
79-
$resp = curl_exec($curl);
80+
if ($status != 200) {
81+
$response = false;
82+
}
83+
} else {
84+
$response = file_get_contents($url, false);
85+
}
8086

81-
if ($resp && !empty($resp)) {
82-
$answer = json_decode($resp);
87+
if ($status !== false) {
88+
$answer = json_decode($response);
8389
if ($answer && strtoupper($answer->statusCode) == 'OK') {
8490
$location->setLatitude($answer->latitude);
8591
$location->setLongitude($answer->longitude);

0 commit comments

Comments
 (0)