diff --git a/system/typemill/Controllers/ControllerApiSystemVersions.php b/system/typemill/Controllers/ControllerApiSystemVersions.php index a33e29d..48f4f23 100644 --- a/system/typemill/Controllers/ControllerApiSystemVersions.php +++ b/system/typemill/Controllers/ControllerApiSystemVersions.php @@ -7,6 +7,7 @@ use Psr\Http\Message\ResponseInterface as Response; use Typemill\Models\Validation; use Typemill\Models\License; use Typemill\Static\Translations; +use Typemill\Models\ApiCalls; class ControllerApiSystemVersions extends Controller { @@ -30,7 +31,6 @@ class ControllerApiSystemVersions extends Controller $type = $params['type']; $data = $params['data']; $url = 'https://typemill.net/api/v1/checkversion'; -# $url2 = 'http://localhost/typemillPlugins/api/v1/checkversion'; if($type == 'plugins') { @@ -63,72 +63,24 @@ class ControllerApiSystemVersions extends Controller return $response->withHeader('Content-Type', 'application/json')->withStatus(500); } + $authstring = hash('sha256', substr($authstring, 0, 50)); + $authHeader = "Authorization: " . $authstring; - if (function_exists('curl_version')) + $apiservice = new ApiCalls(); + $apiResponse = $apiservice->makeGetCall($url, $authHeader); + + if (!$apiResponse) { - $curl = curl_init(); + $response->getBody()->write(json_encode([ + 'message' => 'Could not make the call for the update check', + 'error' => $apiservice->getError() + ])); - if (defined('CURLSSLOPT_NATIVE_CA') && version_compare(curl_version()['version'], '7.71', '>=')) - { - curl_setopt($curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); - } - curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_TIMEOUT, 5); - curl_setopt($curl, CURLOPT_HTTPHEADER, [ - "Accept: application/json", - "Authorization: $authstring", - "Connection: close" - ]); - - $curl_response = curl_exec($curl); - - if (curl_errno($curl)) - { - $error = curl_error($curl); - } - else - { - $versions = json_decode($curl_response, true); - } - - curl_close($curl); - } - else - { - $opts = array( - 'http' => array( - 'method' =>"GET", - 'ignore_errors' => true, - 'timeout' => 5, - 'header' => - "Accept: application/json\r\n" . - "Authorization: $authstring\r\n" . - "Connection: close\r\n", - ) - ); - - $context = stream_context_create($opts); - $versions = file_get_contents($url, false, $context); - if ($versions === false) - { - $error = "file_get_contents error: failed to fetch data from $url"; - } - else - { - $versions = json_decode($versions, true); - } - } - - if($error) - { - $response->getBody()->write(json_encode([ - 'message' => $error - ])); - - return $response->withHeader('Content-Type', 'application/json')->withStatus(500); - } + return $response->withHeader('Content-Type', 'application/json')->withStatus(400); + } + + $versions = json_decode($apiResponse, true); $updateVersions = []; diff --git a/system/typemill/Models/ApiCalls.php b/system/typemill/Models/ApiCalls.php index 70cdf59..815bc61 100644 --- a/system/typemill/Models/ApiCalls.php +++ b/system/typemill/Models/ApiCalls.php @@ -13,7 +13,8 @@ class ApiCalls public function makePostCall(string $url, array $data, $authHeader = '') { - if (in_array('curl', get_loaded_extensions())) { + if (in_array('curl', get_loaded_extensions())) + { return $this->makeCurlCall($url, 'POST', $data, $authHeader); } @@ -22,7 +23,8 @@ class ApiCalls public function makeGetCall($url, $authHeader = '') { - if (in_array('curl', get_loaded_extensions())) { + if (in_array('curl', get_loaded_extensions())) + { return $this->makeCurlCall($url, 'GET', null, $authHeader); } @@ -37,16 +39,19 @@ class ApiCalls "Content-Type: application/json", ]; - if (!empty($authHeader)) { + if (!empty($authHeader)) + { $headers[] = $authHeader; } $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - if ($method === 'POST' && $data) { + if ($method === 'POST' && $data) + { $postdata = json_encode($data); - if ($postdata === false) { + if ($postdata === false) + { $this->error = "JSON encoding error: " . json_last_error_msg(); return false; } @@ -57,7 +62,8 @@ class ApiCalls $response = curl_exec($curl); - if ($response === false) { + if ($response === false) + { $this->error = curl_error($curl); } curl_close($curl); @@ -73,7 +79,8 @@ class ApiCalls "Content-Type: application/json" ]; - if (!empty($authHeader)) { + if (!empty($authHeader)) + { $headers[] = $authHeader; } @@ -85,9 +92,11 @@ class ApiCalls ] ]; - if ($method === 'POST' && $data !== null) { + if ($method === 'POST' && $data !== null) + { $postdata = json_encode($data); - if ($postdata === false) { + if ($postdata === false) + { $this->error = "JSON encoding error: " . json_last_error_msg(); return false; } @@ -97,7 +106,8 @@ class ApiCalls $context = stream_context_create($options); $response = file_get_contents($url, false, $context); - if ($response === false) { + if ($response === false) + { $this->error = 'file_get_contents failed for ' . $method . ' request.'; } diff --git a/system/typemill/Models/License.php b/system/typemill/Models/License.php index 0141ad2..9d1c74f 100644 --- a/system/typemill/Models/License.php +++ b/system/typemill/Models/License.php @@ -321,7 +321,7 @@ class License return false; } - } + } }