Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions v1/firme/logo/delete/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@

if ($_SERVER['REQUEST_METHOD'] !== 'DELETE' && $_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(["error" => "Only DELETE method allowed"]);
echo json_encode(["error" => "Only DELETE or POST method allowed"]);
exit;
}

function postJson(string $url, string $payload, ?string $user = null, ?string $pass = null): array {
$headers = [];
function postJson(string $url, string $payload, string $id, string $logo, ?string $user = null, ?string $pass = null): array
{
if ($user && $pass) {
$headers[] = "Authorization: Basic " . base64_encode("$user:$pass");
}

$headers[] = "Content-Type: application/json";
$context = stream_context_create([
'http' => [
Expand All @@ -30,16 +31,26 @@ function postJson(string $url, string $payload, ?string $user = null, ?string $p
'timeout' => 10
]
]);

$data = @file_get_contents($url, false, $context);
if ($data === false) {
$err = error_get_last()['message'] ?? 'Unknown error';
throw new Exception("FETCH FAILED: $url | $err");
}

$json = json_decode($data, true);
if (!is_array($json)) {
throw new Exception("Invalid JSON response");
}
return $json;

if (($json['responseHeader']['status'] ?? 1) !== 0) {
throw new Exception("Solr update failed: " . json_encode($json));
}

return [
"message" => "Logo url << $logo >> deleted from id << $id >>",
"solr_response" => $json
];
}

try {
Expand All @@ -49,7 +60,7 @@ function postJson(string $url, string $payload, ?string $user = null, ?string $p

$input = json_decode(file_get_contents("php://input"), true);

if (!isset($input['id']) || !isset($input['logo'])) {
if (!isset($input['id']) || !isset($input['logo']) || !is_string($input['id']) || !is_string($input['logo'])) {
http_response_code(400);
echo json_encode(["error" => "Missing id or logo"]);
exit;
Expand All @@ -70,10 +81,9 @@ function postJson(string $url, string $payload, ?string $user = null, ?string $p

error_log("FIRME LOGO DELETE URL: $url");

$response = postJson($url, $payload, $SOLR_USER, $SOLR_PASS);
$response = postJson($url, $payload, $id, $logo, $SOLR_USER, $SOLR_PASS);

echo json_encode($response);

} catch (Exception $e) {
error_log("FIRME LOGO DELETE FAILED: " . $e->getMessage());
http_response_code(503);
Expand Down
Loading