1
0
mirror of https://github.com/typemill/typemill.git synced 2025-07-25 00:02:28 +02:00

switch to apiservice for update check

This commit is contained in:
trendschau
2025-03-29 03:27:29 +01:00
parent 0c6b93933e
commit c913575e9c
3 changed files with 36 additions and 74 deletions

View File

@@ -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 = [];

View File

@@ -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.';
}

View File

@@ -321,7 +321,7 @@ class License
return false;
}
}
}
}