Files
Atomcms-edit/app/Http/Controllers/Community/RadioLeaderboardController.php
T
root f29ba72591 Fix security, performance, and code quality issues across CMS
Security:
- Replace unescaped {!! !!} with Purify::clean() in 15+ Blade templates (XSS)
- Add rate limiting to register (3/hr), upload (10/min), SSE (6/min)
- Add max:5000 validation on article comments
- Remove duplicate exception handler callback

Hardcoded paths:
- Replace ~44 /var/www/ hardcoded paths with env() configs
- CatalogService (13), AutoDetectService (18), Commandocentrum (11), AppServiceProvider (2)

Performance:
- Add 10 missing database indexes (radio_song_requests, help_center_tickets, etc.)
- Replace Cache::flush() with targeted Cache::forget() in RadioSettings
- Cache getCachedCategories() in TicketController (N+1 fix)
- Remove redundant top-3 leaderboard query

Bug fixes:
- Fix undefined $enabled variable → $isOnline in radio index view
- Add getAvatarAttribute() accessor for non-existent avatar column
- Fix User::guilds() from wrong HasMany to HasManyThrough

Code quality:
- Replace file_get_contents with Http::timeout(10) in TraxService
- Remove commented Echo/Pusher boilerplate in bootstrap.js
- Remove TODO/FIXME comments from logo-generator templates
- Replace hardcoded Turnstile CDN URL with config()
- Restore QUEUE_CONNECTION=redis in .env.example files
2026-06-29 18:28:19 +02:00

81 lines
2.3 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Community;
use App\Http\Controllers\Controller;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
class RadioLeaderboardController extends Controller
{
public function __invoke(Request $request): View
{
$period = $request->get('period', 'all');
$users = match ($period) {
'weekly' => $this->getWeeklyLeaderboard(),
'monthly' => $this->getMonthlyLeaderboard(),
default => $this->getAllTimeLeaderboard(),
};
return view('community.radio.leaderboard', ['users' => $users, 'period' => $period]);
}
private function getAllTimeLeaderboard(): array
{
return User::where('radio_points', '>', 0)
->orderBy('radio_points', 'desc')
->limit(100)
->get()
->map(fn ($user, $index) => [
'rank' => $index + 1,
'id' => $user->id,
'username' => $user->username,
'avatar' => $user->avatar,
'points' => $user->radio_points,
])
->toArray();
}
private function getWeeklyLeaderboard(): array
{
return $this->getPointsLeaderboard(now()->subWeek());
}
private function getMonthlyLeaderboard(): array
{
return $this->getPointsLeaderboard(now()->subMonth());
}
private function getPointsLeaderboard(Carbon $since): array
{
$points = DB::table('radio_listener_points')
->where('earned_at', '>=', $since)
->select('user_id', DB::raw('SUM(points) as total_points'))
->groupBy('user_id')
->orderBy('total_points', 'desc')
->limit(100)
->get();
$userIds = $points->pluck('user_id')->toArray();
$users = User::whereIn('id', $userIds)->get()->keyBy('id');
return $points->map(function ($item, $index) use ($users) {
$user = $users->get($item->user_id);
return [
'rank' => $index + 1,
'id' => $item->user_id,
'username' => $user?->username ?? 'Onbekend',
'avatar' => $user?->avatar,
'points' => (int) $item->total_points,
];
})->toArray();
}
}