🆙 Fix code in somephp files an refactored and deleted deprecated code 🆙

This commit is contained in:
Remco
2026-01-19 17:25:31 +01:00
parent 3238cdb8e7
commit 6736e8fe60
9 changed files with 145 additions and 92 deletions
@@ -4,6 +4,8 @@ namespace App\Observers;
use App\Models\WebsiteDrawBadge;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
class WebsiteDrawBadgeObserver
{
@@ -21,7 +23,6 @@ class WebsiteDrawBadgeObserver
->where('badge_code', $badgeCode)
->delete();
// Remove from JSON
$this->updateExternalTexts(false, $badgeCode);
return;
@@ -40,30 +41,63 @@ class WebsiteDrawBadgeObserver
]);
}
// Add to JSON
$this->updateExternalTexts(true, $badgeCode, $websiteDrawBadge->badge_name, $websiteDrawBadge->badge_desc);
}
protected function updateExternalTexts(bool $add, string $badgeCode, ?string $name = null, ?string $desc = null): void
{
$filePath = DB::table('website_settings')->where('key', 'nitro_external_texts_file')->value('value');
try {
$filePath = DB::table('website_settings')->where('key', 'nitro_external_texts_file')->value('value');
if (! $filePath || ! file_exists($filePath) || ! is_writable($filePath)) {
return;
}
if (! $filePath) {
return;
}
$json = json_decode(file_get_contents($filePath), true);
$filePath = str_replace(['../', '..\\'], '', $filePath);
if ($add) {
$json = array_merge($json, [
"badge_name_{$badgeCode}" => $name,
"badge_desc_{$badgeCode}" => $desc,
if (! file_exists($filePath) || ! is_file($filePath) || ! is_writable($filePath)) {
return;
}
$realPath = realpath($filePath);
if ($realPath === false) {
return;
}
$content = file_get_contents($realPath);
if ($content === false) {
Log::warning("Failed to read external texts file: {$realPath}");
return;
}
$json = json_decode($content, true);
if (! is_array($json)) {
Log::error("Invalid JSON in external texts file: {$realPath}");
return;
}
if ($add) {
$json["badge_name_{$badgeCode}"] = $name;
$json["badge_desc_{$badgeCode}"] = $desc;
} else {
unset($json["badge_name_{$badgeCode}"]);
unset($json["badge_desc_{$badgeCode}"]);
}
$jsonContent = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($jsonContent === false) {
Log::error("Failed to encode JSON for external texts file: {$realPath}");
return;
}
if (file_put_contents($realPath, $jsonContent, LOCK_EX) === false) {
Log::error("Failed to write external texts file: {$realPath}");
}
} catch (\Throwable $e) {
Log::error("Error updating external texts: {$e->getMessage()}", [
'badge_code' => $badgeCode,
'exception' => $e,
]);
} else {
unset($json["badge_name_{$badgeCode}"]);
unset($json["badge_desc_{$badgeCode}"]);
}
file_put_contents($filePath, json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}