Erant-OldApp/public/api/routes/game/details.php

71 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2022-09-08 07:19:43 +00:00
<?php
2022-10-19 08:19:43 +00:00
function remoteFileExists($url) {
$curl = curl_init($url);
//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
//do request
$result = curl_exec($curl);
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$ret = true;
2022-09-08 07:19:43 +00:00
}
2022-10-19 08:19:43 +00:00
}
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
curl_close($curl);
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
return $ret;
}
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
return function($data, $headers, $db) {
$imagesHostName = 'https://geohry.skolazdola.cz/';
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
if(!isset($data['gameurl']) || empty($data['gameurl'])) return [
2022-10-19 12:52:15 +00:00
"success" => false,
2022-10-19 08:19:43 +00:00
"data" => [
"message" => "Property 'gameurl' is not set."
]
];
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
$gameUrl = $data['gameurl'];
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
$gameDetails = $db->select('games4', '*', [
'url' => $gameUrl
]);
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
if(!$gameDetails) return [
2022-10-19 12:52:15 +00:00
"success" => false,
2022-10-19 08:19:43 +00:00
"data" => [
"message" => "Invalid 'gameurl'."
]
];
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
$gameDetails = $gameDetails[0];
$gameDetails["questions"] = $db->select('questions4', '*', [
'url' => $gameUrl
]);
$thumbnailPath = "{$imagesHostName}/games/{$gameUrl}/intro.jpg";
$gameDetails["thumbnail"] = remoteFileExists($thumbnailPath) ? $thumbnailPath : null;
2022-09-08 07:19:43 +00:00
2022-10-19 08:19:43 +00:00
foreach($gameDetails["questions"] as &$question) {
$questionThumbnailPath = "{$imagesHostName}/games/{$gameUrl}/" . $question["uniqid"] . ".jpg";
$question["thumbnail"] = remoteFileExists($questionThumbnailPath) ? $questionThumbnailPath : null;
2022-09-08 07:19:43 +00:00
}
2022-10-19 08:19:43 +00:00
return [
"success" => true,
"data" => $gameDetails
];
};