You've already forked Epicnabbo-Catalogus-Updated-Daily
104 lines
3.3 KiB
PHP
104 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Models\WebsiteDrawBadge;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class WebsiteDrawBadgeObserver
|
|
{
|
|
public function updated(WebsiteDrawBadge $websiteDrawBadge): void
|
|
{
|
|
if (! $websiteDrawBadge->wasChanged() || ! $websiteDrawBadge->badge_path) {
|
|
return;
|
|
}
|
|
|
|
$badgeCode = pathinfo($websiteDrawBadge->badge_path, PATHINFO_FILENAME);
|
|
|
|
if (! $websiteDrawBadge->published) {
|
|
DB::table('users_badges')
|
|
->where('user_id', $websiteDrawBadge->user_id)
|
|
->where('badge_code', $badgeCode)
|
|
->delete();
|
|
|
|
$this->updateExternalTexts(false, $badgeCode);
|
|
|
|
return;
|
|
}
|
|
|
|
$exists = DB::table('users_badges')
|
|
->where('user_id', $websiteDrawBadge->user_id)
|
|
->where('badge_code', $badgeCode)
|
|
->exists();
|
|
|
|
if (! $exists) {
|
|
DB::table('users_badges')->insert([
|
|
'user_id' => $websiteDrawBadge->user_id,
|
|
'slot_id' => 0,
|
|
'badge_code' => $badgeCode,
|
|
]);
|
|
}
|
|
|
|
$this->updateExternalTexts(true, $badgeCode, $websiteDrawBadge->badge_name, $websiteDrawBadge->badge_desc);
|
|
}
|
|
|
|
protected function updateExternalTexts(bool $add, string $badgeCode, ?string $name = null, ?string $desc = null): void
|
|
{
|
|
try {
|
|
$filePath = DB::table('website_settings')->where('key', 'nitro_external_texts_file')->value('value');
|
|
|
|
if (! is_string($filePath) || $filePath === '') {
|
|
return;
|
|
}
|
|
|
|
$filePath = str_replace(['../', '..\\'], '', (string) $filePath);
|
|
|
|
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,
|
|
]);
|
|
}
|
|
}
|
|
}
|