🆙 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
-41
View File
@@ -1,41 +0,0 @@
APP_NAME=Atom
APP_ENV=testing
APP_KEY=base64:GENERATE_NEW_KEY_HERE
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mariadb
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=docker
DB_PASSWORD=password
BROADCAST_DRIVER=log
CACHE_DRIVER=array
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=array
SESSION_LIFETIME=120
MAIL_MAILER=array
# Testing specific settings
BCRYPT_ROUNDS=4
TELESCOPE_ENABLED=false
# Disable CAPTCHA for testing
GOOGLE_RECAPTCHA_ENABLED=0
CLOUDFLARE_TURNSTILE_ENABLED=0
# Cloudflare Turnstile (test keys)
TURNSTILE_SITE_KEY=1x00000000000000000000AA
TURNSTILE_SECRET_KEY=1x0000000000000000000000000000000AA
# Google reCAPTCHA (dummy values for testing)
GOOGLE_RECAPTCHA_SITE_KEY=dummy_site_key
GOOGLE_RECAPTCHA_SECRET_KEY=dummy_secret_key
+26 -5
View File
@@ -37,12 +37,33 @@ if (! function_exists('strLimit')) {
if (! function_exists('findMigration')) { if (! function_exists('findMigration')) {
function findMigration(string $tableName): string function findMigration(string $tableName): string
{ {
foreach (glob(database_path('migrations/*.php')) as $filename) { static $cache = [];
$content = file_get_contents($filename);
if ($content !== false && str_contains($content, "Schema::create('$tableName'")) { if (isset($cache[$tableName])) {
return basename($filename); return $cache[$tableName];
}
} }
$migrationsPath = database_path('migrations');
if (! is_dir($migrationsPath)) {
return '';
}
try {
$files = new \DirectoryIterator($migrationsPath);
foreach ($files as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$content = @file_get_contents($file->getPathname());
if ($content !== false && str_contains($content, "Schema::create('$tableName'")) {
$cache[$tableName] = $file->getFilename();
return $file->getFilename();
}
}
}
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::error("Error finding migration for table {$tableName}: {$e->getMessage()}");
}
$cache[$tableName] = '';
return ''; return '';
} }
} }
@@ -18,7 +18,7 @@ class BadgeController extends Controller
private const int MAX_BADGE_SIZE_BYTES = 40960; private const int MAX_BADGE_SIZE_BYTES = 40960;
public function show(SettingsService $settingsService) public function show(SettingsService $settingsService): \Illuminate\Contracts\View\View
{ {
$cost = (int) $settingsService->getOrDefault('drawbadge_currency_value', 150); $cost = (int) $settingsService->getOrDefault('drawbadge_currency_value', 150);
$currencyType = $settingsService->getOrDefault('drawbadge_currency_type', 'credits'); $currencyType = $settingsService->getOrDefault('drawbadge_currency_type', 'credits');
@@ -41,7 +41,7 @@ class BadgeController extends Controller
return view('draw-badge', ['cost' => $cost, 'currencyType' => $currencyType, 'folderError' => $folderError, 'errorMessage' => $errorMessage]); return view('draw-badge', ['cost' => $cost, 'currencyType' => $currencyType, 'folderError' => $folderError, 'errorMessage' => $errorMessage]);
} }
public function buy(Request $request, SendCurrency $sendCurrency, SettingsService $settingsService) public function buy(Request $request, SendCurrency $sendCurrency, SettingsService $settingsService): \Illuminate\Http\JsonResponse
{ {
$user = Auth::user(); $user = Auth::user();
$cost = (int) $settingsService->getOrDefault('drawbadge_currency_value', 150); $cost = (int) $settingsService->getOrDefault('drawbadge_currency_value', 150);
@@ -6,10 +6,12 @@ use App\Http\Controllers\Controller;
use App\Models\Game\Guild\GuildMember; use App\Models\Game\Guild\GuildMember;
use App\Models\Game\Player\MessengerFriendship; use App\Models\Game\Player\MessengerFriendship;
use App\Models\User; use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
class ProfileController extends Controller class ProfileController extends Controller
{ {
public function __invoke(User $user) public function __invoke(User $user): View
{ {
$user = $this->loadUserRelations($user); $user = $this->loadUserRelations($user);
@@ -41,7 +43,7 @@ class ProfileController extends Controller
]); ]);
} }
private function getUserFriends(int $userId) private function getUserFriends(int $userId): Collection
{ {
return MessengerFriendship::select('user_two_id') return MessengerFriendship::select('user_two_id')
->where('user_one_id', '=', $userId) ->where('user_one_id', '=', $userId)
@@ -52,7 +54,7 @@ class ProfileController extends Controller
->get(); ->get();
} }
private function getUserGroups(int $userId) private function getUserGroups(int $userId): Collection
{ {
return GuildMember::query() return GuildMember::query()
->select(['guilds_members.id', 'guilds_members.guild_id', 'guilds_members.user_id', 'guilds.name', 'guilds.badge']) ->select(['guilds_members.id', 'guilds_members.guild_id', 'guilds_members.user_id', 'guilds.name', 'guilds.badge'])
@@ -4,6 +4,8 @@ namespace App\Observers;
use App\Models\WebsiteDrawBadge; use App\Models\WebsiteDrawBadge;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
class WebsiteDrawBadgeObserver class WebsiteDrawBadgeObserver
{ {
@@ -21,7 +23,6 @@ class WebsiteDrawBadgeObserver
->where('badge_code', $badgeCode) ->where('badge_code', $badgeCode)
->delete(); ->delete();
// Remove from JSON
$this->updateExternalTexts(false, $badgeCode); $this->updateExternalTexts(false, $badgeCode);
return; return;
@@ -40,30 +41,63 @@ class WebsiteDrawBadgeObserver
]); ]);
} }
// Add to JSON
$this->updateExternalTexts(true, $badgeCode, $websiteDrawBadge->badge_name, $websiteDrawBadge->badge_desc); $this->updateExternalTexts(true, $badgeCode, $websiteDrawBadge->badge_name, $websiteDrawBadge->badge_desc);
} }
protected function updateExternalTexts(bool $add, string $badgeCode, ?string $name = null, ?string $desc = null): void 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)) { if (! $filePath) {
return; return;
} }
$json = json_decode(file_get_contents($filePath), true); $filePath = str_replace(['../', '..\\'], '', $filePath);
if ($add) { if (! file_exists($filePath) || ! is_file($filePath) || ! is_writable($filePath)) {
$json = array_merge($json, [ return;
"badge_name_{$badgeCode}" => $name, }
"badge_desc_{$badgeCode}" => $desc,
$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));
} }
} }
@@ -12,17 +12,34 @@ class PermissionsService
public function __construct() public function __construct()
{ {
Cache::remember('website_permissions', now()->addMinutes(30), fn () => WebsitePermission::all()->pluck('min_rank', 'permission')); $this->permissions = Cache::remember(
'website_permissions',
$this->permissions = Cache::get('website_permissions'); now()->addMinutes(30),
fn () => WebsitePermission::all()->pluck('min_rank', 'permission')
);
} }
public function getOrDefault(string $permissionName, bool $default = false): bool public function getOrDefault(string $permissionName, bool $default = false): bool
{ {
if (! array_key_exists($permissionName, $this->permissions->toArray())) { if (! $this->permissions instanceof Collection || ! $this->permissions->has($permissionName)) {
return $default; return $default;
} }
return auth()->check() && auth()->user()->rank >= (int) $this->permissions->get($permissionName); if (! auth()->check()) {
return false;
}
return auth()->user()->rank >= (int) $this->permissions->get($permissionName);
}
public function refresh(): void
{
Cache::forget('website_permissions');
$this->permissions = Cache::remember(
'website_permissions',
now()->addMinutes(30),
fn () => WebsitePermission::all()->pluck('min_rank', 'permission')
);
} }
} }
+17 -14
View File
@@ -32,16 +32,19 @@ class RconService
if (! $this->socket) { if (! $this->socket) {
$error = socket_strerror(socket_last_error()); $error = socket_strerror(socket_last_error());
Log::error("RCON initialization failed: $error"); Log::error("RCON initialization failed: {$error}");
$this->closeConnection(); $this->closeConnection();
return; return;
} }
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 5, 'usec' => 0]);
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => 5, 'usec' => 0]);
if (! @socket_connect($this->socket, $this->config['ip'], $this->config['port'])) { if (! @socket_connect($this->socket, $this->config['ip'], $this->config['port'])) {
$error = socket_strerror(socket_last_error()); $error = socket_strerror(socket_last_error());
Log::error("RCON connection failed: $error"); Log::error("RCON connection failed: {$error}");
$this->closeConnection(); $this->closeConnection();
@@ -53,7 +56,7 @@ class RconService
private function closeConnection(): void private function closeConnection(): void
{ {
if ($this->socket instanceof \Socket) { if ($this->socket instanceof Socket) {
socket_close($this->socket); socket_close($this->socket);
} }
@@ -70,29 +73,24 @@ class RconService
* @throws RconConnectionException * @throws RconConnectionException
* @throws JsonException * @throws JsonException
*/ */
public function sendCommand(string $command, ?array $data = null) public function sendCommand(string $command, ?array $data = null): bool
{ {
if (! $this->isConnected) { if (! $this->isConnected) {
$error = 'RCON command failed: Not connected'; Log::error('RCON command failed: Not connected');
Log::error($error);
$this->closeConnection(); $this->closeConnection();
return false;
return $this->isConnected;
} }
$payload = json_encode(['key' => $command, 'data' => $data], JSON_THROW_ON_ERROR); $payload = json_encode(['key' => $command, 'data' => $data], JSON_THROW_ON_ERROR);
if (! @socket_write($this->socket, $payload, strlen($payload))) { if (! @socket_write($this->socket, $payload, strlen($payload))) {
$error = socket_strerror(socket_last_error($this->socket)); $error = socket_strerror(socket_last_error($this->socket));
Log::error("RCON command ($command) failed: $error"); Log::error("RCON command ({$command}) failed: {$error}");
$this->closeConnection(); $this->closeConnection();
return false;
return $this->isConnected;
} }
return $this->isConnected; return true;
} }
/** /**
@@ -254,4 +252,9 @@ class RconService
'command' => $command, 'command' => $command,
]); ]);
} }
public function __destruct()
{
$this->closeConnection();
}
} }
+21 -4
View File
@@ -15,9 +15,11 @@ class SettingsService
public function __construct() public function __construct()
{ {
try { try {
Cache::remember('website_settings', now()->addMinutes(5), fn () => Schema::hasTable('website_settings') ? WebsiteSetting::all()->pluck('value', 'key') : collect()); $this->settings = Cache::remember(
'website_settings',
$this->settings = Cache::get('website_settings'); now()->addMinutes(5),
fn () => Schema::hasTable('website_settings') ? WebsiteSetting::all()->pluck('value', 'key') : collect()
);
} catch (Throwable) { } catch (Throwable) {
$this->settings = collect(); $this->settings = collect();
} }
@@ -25,10 +27,25 @@ class SettingsService
public function getOrDefault(string $settingName, ?string $default = null): string public function getOrDefault(string $settingName, ?string $default = null): string
{ {
if (! $this->settings instanceof \Illuminate\Support\Collection) { if (! $this->settings instanceof Collection) {
return (string) $default; return (string) $default;
} }
return (string) $this->settings->get($settingName, $default); return (string) $this->settings->get($settingName, $default);
} }
public function refresh(): void
{
Cache::forget('website_settings');
try {
$this->settings = Cache::remember(
'website_settings',
now()->addMinutes(5),
fn () => Schema::hasTable('website_settings') ? WebsiteSetting::all()->pluck('value', 'key') : collect()
);
} catch (Throwable) {
$this->settings = collect();
}
}
} }
+2 -2
View File
@@ -9,7 +9,7 @@ return [
| It will assign the default active theme to be used if one is not set during | It will assign the default active theme to be used if one is not set during
| runtime. | runtime.
*/ */
'active' => 'atom', 'active' => env('THEME_ACTIVE', 'atom'),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -21,7 +21,7 @@ return [
| file is not found in the currently active theme, then it will look for it | file is not found in the currently active theme, then it will look for it
| in the parent theme. | in the parent theme.
*/ */
'parent' => 'atom', 'parent' => env('THEME_PARENT', 'atom'),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------