2026-10-03 15:56:22 +02:00
< ? php
2026-10-03 16:27:22 +02:00
// Lade den minimalistischen Autoloader
require_once __DIR__ . '/../src/autoload.php' ;
2026-10-04 08:11:52 +02:00
// Auth-System initialisieren
App\Core\Auth :: start ();
2026-10-03 16:27:22 +02:00
use App\Core\Database ;
2026-10-04 08:11:52 +02:00
use App\Core\Auth ;
2026-10-03 16:27:22 +02:00
use App\Models\Game ;
use App\Models\Tipp ;
2026-10-04 08:11:52 +02:00
use App\Models\User ;
2026-10-03 16:43:00 +02:00
use App\Models\Round ;
2026-10-03 16:49:52 +02:00
use App\Models\Season ;
2026-10-03 17:35:26 +02:00
use App\Models\Ranking ;
2026-10-04 17:40:59 +02:00
use App\Models\Payment ;
2026-10-04 18:14:46 +02:00
use App\Models\Quota ;
2026-10-03 15:56:22 +02:00
$uri = parse_url ( $_SERVER [ 'REQUEST_URI' ], PHP_URL_PATH );
2026-10-04 08:11:52 +02:00
$page = $_GET [ 'page' ] ? ? null ;
// Home-Seite erkennen
if (( $uri === '/' || $uri === '/index.php' ) && $page === null ) {
$page = 'home' ;
}
2026-10-03 15:56:22 +02:00
2026-10-04 08:11:52 +02:00
// Routing basierend auf Query-Parametern
if ( $page === 'login' ) {
2026-10-04 09:14:19 +02:00
// Wenn bereits eingeloggt, zur Rangliste weiterleiten
if ( Auth :: check ()) {
header ( 'Location: /index.php?page=ranking' );
exit ;
}
2026-10-04 08:11:52 +02:00
if ( $_SERVER [ 'REQUEST_METHOD' ] === 'POST' ) {
2026-10-04 11:18:26 +02:00
$firstname = $_POST [ 'firstname' ] ? ? '' ;
$lastname = $_POST [ 'lastname' ] ? ? '' ;
2026-10-04 08:11:52 +02:00
$password = $_POST [ 'password' ] ? ? '' ;
2026-10-04 11:18:26 +02:00
if ( Auth :: login ( $firstname , $lastname , $password )) {
2026-10-04 08:11:52 +02:00
header ( 'Location: /index.php?page=ranking' );
exit ;
} else {
$error = 'Ungültige Anmeldedaten.' ;
require __DIR__ . '/../src/Views/login.php' ;
}
} else {
require __DIR__ . '/../src/Views/login.php' ;
}
2026-10-04 17:50:19 +02:00
} elseif ( $page === 'register' ) {
if ( Auth :: check ()) {
header ( 'Location: /index.php?page=ranking' );
exit ;
}
if ( $_SERVER [ 'REQUEST_METHOD' ] === 'POST' ) {
$data = [
'firstname' => $_POST [ 'firstname' ] ? ? '' ,
'lastname' => $_POST [ 'lastname' ] ? ? '' ,
'email' => $_POST [ 'email' ] ? ? '' ,
'password' => $_POST [ 'password' ] ? ? '' ,
];
$password_confirm = $_POST [ 'password_confirm' ] ? ? '' ;
$error = null ;
if ( empty ( $data [ 'firstname' ]) || empty ( $data [ 'lastname' ]) || empty ( $data [ 'email' ]) || empty ( $data [ 'password' ])) {
$error = 'Alle Felder sind erforderlich.' ;
} elseif ( $data [ 'password' ] !== $password_confirm ) {
$error = 'Die Passwörter stimmen nicht überein.' ;
} elseif ( User :: findByEmail ( $data [ 'email' ])) {
$error = 'Diese E-Mail-Adresse ist bereits registriert.' ;
} else {
try {
if ( User :: create ( $data )) {
// Automatically log in the new user
if ( Auth :: login ( $data [ 'firstname' ], $data [ 'lastname' ], $data [ 'password' ])) {
header ( 'Location: /index.php?page=ranking' );
exit ;
} else {
// This should not happen if creation was successful
$error = 'Anmeldung nach Registrierung fehlgeschlagen. Bitte versuchen Sie, sich manuell anzumelden.' ;
require __DIR__ . '/../src/Views/login.php' ;
exit ;
}
} else {
$error = 'Ein Fehler ist bei der Registrierung aufgetreten.' ;
}
} catch ( \Throwable $e ) {
// Could be a unique constraint violation from DB
$error = 'Ein Datenbankfehler ist aufgetreten. Die E-Mail könnte bereits vergeben sein.' ;
}
}
// Show form again with error
require __DIR__ . '/../src/Views/register.php' ;
} else {
// Show registration form
require __DIR__ . '/../src/Views/register.php' ;
}
2026-10-04 08:11:52 +02:00
} elseif ( $page === 'logout' ) {
Auth :: logout ();
header ( 'Location: /index.php?page=login' );
exit ;
2026-10-04 11:27:46 +02:00
} elseif ( $page === 'admin' ) {
Auth :: requireLogin ();
Auth :: requireAdmin ();
require __DIR__ . '/../src/Views/admin.php' ;
2026-10-04 17:55:58 +02:00
} elseif ( $page === 'admin_seasons' ) {
Auth :: requireLogin ();
Auth :: requireAdmin ();
$message = null ;
$error = null ;
if ( $_SERVER [ 'REQUEST_METHOD' ] === 'POST' ) {
$action = $_POST [ 'action' ] ? ? '' ;
$seasonId = $_POST [ 'id' ] ? ? null ;
$redirect = '/index.php?page=admin_seasons' ;
try {
if ( $action === 'create' || $action === 'update' ) {
$data = [
'name' => $_POST [ 'name' ],
'rounds' => $_POST [ 'rounds' ],
];
2026-10-04 18:19:51 +02:00
$stake = $_POST [ 'stake' ] ? ? '0' ;
if ( empty ( $data [ 'name' ]) || ! is_numeric ( $data [ 'rounds' ]) || ! is_numeric ( $stake )) {
2026-10-04 18:14:46 +02:00
throw new \Exception ( 'Name, Runden und Einsatz sind erforderlich.' );
2026-10-04 17:55:58 +02:00
}
2026-10-04 18:19:51 +02:00
$quotasToSave = [[ 'name' => 'Einsatz' , 'prc' => $stake ]];
if ( isset ( $_POST [ 'quotas' ]) && is_array ( $_POST [ 'quotas' ])) {
$quotasToSave = array_merge ( $quotasToSave , $_POST [ 'quotas' ]);
}
2026-10-04 17:55:58 +02:00
if ( $action === 'create' ) {
2026-10-04 18:04:20 +02:00
$newSeasonId = Season :: create ( $data );
if ( $newSeasonId ) {
2026-10-04 18:19:51 +02:00
Quota :: updateForSeason ( $newSeasonId , $quotasToSave );
2026-10-04 18:04:20 +02:00
Season :: setCurrent ( $newSeasonId );
$_SESSION [ 'admin_message' ] = " Saison erfolgreich erstellt und als aktiv gesetzt. " ;
} else {
throw new \Exception ( " Saison konnte nicht erstellt werden. " );
}
2026-10-04 17:55:58 +02:00
} elseif ( $seasonId ) {
Season :: update ( $seasonId , $data );
2026-10-04 18:19:51 +02:00
Quota :: updateForSeason ( $seasonId , $quotasToSave );
2026-10-04 17:55:58 +02:00
$_SESSION [ 'admin_message' ] = " Saison erfolgreich aktualisiert. " ;
}
} elseif ( $action === 'set_current' && $seasonId ) {
Season :: setCurrent ( $seasonId );
$_SESSION [ 'admin_message' ] = " Aktive Saison wurde geändert. " ;
}
} catch ( \Throwable $e ) {
$_SESSION [ 'admin_error' ] = " Fehler: " . $e -> getMessage ();
if ( $action === 'update' && $seasonId ) {
$redirect = '/index.php?page=admin_seasons&edit_id=' . $seasonId ;
}
}
header ( 'Location: ' . $redirect );
exit ;
}
if ( isset ( $_SESSION [ 'admin_message' ])) {
$message = $_SESSION [ 'admin_message' ];
unset ( $_SESSION [ 'admin_message' ]);
}
if ( isset ( $_SESSION [ 'admin_error' ])) {
$error = $_SESSION [ 'admin_error' ];
unset ( $_SESSION [ 'admin_error' ]);
}
$seasons = Season :: getAll ();
$currentSeason = Season :: getCurrent ();
$editSeason = null ;
2026-10-04 18:19:51 +02:00
$quotasForForm = [];
$stakeValue = '25.00' ; // Default
2026-10-04 17:55:58 +02:00
if ( isset ( $_GET [ 'edit_id' ])) {
$editSeason = Season :: findById (( int ) $_GET [ 'edit_id' ]);
2026-10-04 18:02:53 +02:00
if ( $editSeason ) {
2026-10-04 18:19:51 +02:00
$allQuotas = Quota :: getBySeason ( $editSeason [ 'id' ]);
foreach ( $allQuotas as $quota ) {
if ( $quota [ 'name' ] === 'Einsatz' ) {
$stakeValue = $quota [ 'prc' ];
} else {
$quotasForForm [] = $quota ;
}
}
2026-10-04 18:02:53 +02:00
}
2026-10-04 17:55:58 +02:00
}
require __DIR__ . '/../src/Views/admin/seasons.php' ;
2026-10-04 17:34:43 +02:00
} elseif ( $page === 'admin_users' ) {
Auth :: requireLogin ();
Auth :: requireAdmin ();
2026-10-04 17:40:59 +02:00
$currentSeason = Season :: getCurrent ();
2026-10-04 17:34:43 +02:00
$message = null ;
$error = null ;
if ( $_SERVER [ 'REQUEST_METHOD' ] === 'POST' ) {
$action = $_POST [ 'action' ] ? ? '' ;
$userId = $_POST [ 'id' ] ? ? null ;
$redirect = '/index.php?page=admin_users' ;
try {
if ( $action === 'create' || $action === 'update' ) {
$data = [
'firstname' => $_POST [ 'firstname' ],
'lastname' => $_POST [ 'lastname' ],
'email' => $_POST [ 'email' ],
'password' => $_POST [ 'password' ],
'admin' => isset ( $_POST [ 'admin' ]) ? 1 : 0 ,
];
if ( $action === 'create' ) {
if ( empty ( $data [ 'password' ])) {
throw new \Exception ( 'Passwort ist erforderlich.' );
}
User :: create ( $data );
$_SESSION [ 'admin_message' ] = " Benutzer erfolgreich erstellt. " ;
} elseif ( $userId ) {
User :: update ( $userId , $data );
2026-10-04 17:40:59 +02:00
if ( $currentSeason ) {
Payment :: setPaidStatus ( $userId , $currentSeason [ 'id' ], isset ( $_POST [ 'has_paid' ]));
}
2026-10-04 17:34:43 +02:00
$_SESSION [ 'admin_message' ] = " Benutzer erfolgreich aktualisiert. " ;
}
} elseif ( $action === 'delete' && $userId ) {
User :: delete ( $userId );
$_SESSION [ 'admin_message' ] = " Benutzer erfolgreich gelöscht. " ;
}
} catch ( \Throwable $e ) {
$_SESSION [ 'admin_error' ] = " Fehler: " . $e -> getMessage ();
if ( $action === 'update' && $userId ) {
$redirect = '/index.php?page=admin_users&edit_id=' . $userId ;
}
}
header ( 'Location: ' . $redirect );
exit ;
}
if ( isset ( $_SESSION [ 'admin_message' ])) {
$message = $_SESSION [ 'admin_message' ];
unset ( $_SESSION [ 'admin_message' ]);
}
if ( isset ( $_SESSION [ 'admin_error' ])) {
$error = $_SESSION [ 'admin_error' ];
unset ( $_SESSION [ 'admin_error' ]);
}
$users = User :: getAll ();
2026-10-04 17:40:59 +02:00
$payments = [];
if ( $currentSeason ) {
$payments = Payment :: getPaymentsBySeason ( $currentSeason [ 'id' ]);
}
2026-10-04 17:34:43 +02:00
$editUser = null ;
if ( isset ( $_GET [ 'edit_id' ])) {
$editUser = User :: findById (( int ) $_GET [ 'edit_id' ]);
}
require __DIR__ . '/../src/Views/admin/users.php' ;
2026-10-04 08:11:52 +02:00
} elseif ( $page === 'round' && isset ( $_GET [ 'id' ])) {
Auth :: requireLogin ();
2026-10-03 17:05:24 +02:00
try {
$roundId = ( int ) $_GET [ 'id' ];
2026-10-03 17:09:57 +02:00
$selectedRound = Round :: getById ( $roundId );
2026-10-03 17:05:24 +02:00
2026-10-03 17:09:57 +02:00
if ( ! $selectedRound ) {
2026-10-03 17:05:24 +02:00
http_response_code ( 404 );
echo " 404 Not Found: Runde nicht gefunden. " ;
exit ;
}
2026-10-03 17:09:57 +02:00
// Alle Runden der Saison holen
$allRoundsInSeason = Round :: getBySeason ( $selectedRound [ 'season_id' ]);
$roundsToShow = [];
2026-10-04 06:11:02 +02:00
$all_game_ids = [];
2026-10-03 17:09:57 +02:00
foreach ( $allRoundsInSeason as $round ) {
if ( $round [ 'id' ] <= $selectedRound [ 'id' ]) {
2026-10-04 06:11:02 +02:00
$gamesOfRound = Game :: getGamesByRound ( $round [ 'id' ]);
$round [ 'games' ] = $gamesOfRound ;
2026-10-03 17:09:57 +02:00
$roundsToShow [] = $round ;
2026-10-04 06:11:02 +02:00
if ( ! empty ( $gamesOfRound )) {
$all_game_ids = array_merge ( $all_game_ids , array_column ( $gamesOfRound , 'id' ));
}
}
}
// Hole alle zahlenden User der Saison
$db = Database :: getConnection ();
2026-10-04 17:46:45 +02:00
$stmt_users = $db -> prepare ( " SELECT u.id, u.name FROM users u JOIN payments p ON u.id = p.user_id WHERE p.season_id = ? AND u.deleted IS NULL AND p.paid_date IS NOT NULL ORDER BY u.name ASC " );
2026-10-04 06:11:02 +02:00
$stmt_users -> execute ([ $selectedRound [ 'season_id' ]]);
$users = $stmt_users -> fetchAll ( \PDO :: FETCH_ASSOC );
// Hole alle relevanten Tipps
$tipps_map = [];
if ( ! empty ( $all_game_ids ) && ! empty ( $users )) {
$user_ids = array_column ( $users , 'id' );
$game_placeholders = implode ( ',' , array_fill ( 0 , count ( $all_game_ids ), '?' ));
$user_placeholders = implode ( ',' , array_fill ( 0 , count ( $user_ids ), '?' ));
$stmt_tipps = $db -> prepare ( "
SELECT user_id , game_id , score1 AS tipp1 , score2 AS tipp2 , joker
FROM tipps
WHERE game_id IN ( $game_placeholders ) AND user_id IN ( $user_placeholders )
" );
$stmt_tipps -> execute ( array_merge ( $all_game_ids , $user_ids ));
foreach ( $stmt_tipps -> fetchAll ( \PDO :: FETCH_ASSOC ) as $tipp ) {
$tipps_map [ $tipp [ 'user_id' ]][ $tipp [ 'game_id' ]] = $tipp ;
2026-10-03 17:09:57 +02:00
}
}
2026-10-03 17:05:24 +02:00
// Lade das View-Template für die Rundenansicht
require __DIR__ . '/../src/Views/round.php' ;
} catch ( Throwable $e ) {
http_response_code ( 500 );
echo " <p style='color:red;'>Fehler: " . htmlspecialchars ( $e -> getMessage ()) . " </p> " ;
}
2026-10-04 08:11:52 +02:00
} elseif ( $page === 'ranking' ) {
Auth :: requireLogin ();
2026-10-03 17:35:26 +02:00
try {
$currentSeason = Season :: getCurrent ();
if ( ! $currentSeason ) {
throw new \Exception ( " Keine aktuelle Saison gefunden. " );
}
2026-10-04 06:04:25 +02:00
$selectedRoundId = isset ( $_GET [ 'round_id' ]) ? ( int ) $_GET [ 'round_id' ] : null ;
$data = Ranking :: getRanking ( $currentSeason [ 'id' ], $selectedRoundId );
$ranking = $data [ 'ranking' ];
$games = $data [ 'games' ];
$allRounds = $data [ 'allRounds' ];
$selectedRound = $data [ 'selectedRound' ];
$dailyWinners = $data [ 'dailyWinners' ];
2026-10-03 17:35:26 +02:00
require __DIR__ . '/../src/Views/ranking.php' ;
} catch ( Throwable $e ) {
http_response_code ( 500 );
echo " <p style='color:red;'>Fehler: " . htmlspecialchars ( $e -> getMessage ()) . " </p> " ;
}
2026-10-04 08:11:52 +02:00
} elseif ( $page === 'home' ) {
2026-10-04 08:56:45 +02:00
Auth :: requireLogin ();
2026-10-03 16:27:22 +02:00
try {
$db = Database :: getConnection ();
$userCount = $db -> query ( " SELECT COUNT(*) FROM users " ) -> fetchColumn ();
2026-10-03 16:49:52 +02:00
$currentSeason = Season :: getCurrent ();
if ( $currentSeason ) {
$rounds = Round :: getBySeason ( $currentSeason [ 'id' ]);
} else {
$rounds = [];
}
2026-10-03 16:45:14 +02:00
2026-10-03 17:09:57 +02:00
// Testdaten für die Startseite
$scoreTest = Tipp :: getScoreLocal ( 2 , 1 , 2 , 1 , true );
2026-10-03 16:27:22 +02:00
$lastRoundId = $db -> query ( " SELECT round_id FROM games WHERE score1 > -1 ORDER BY id DESC LIMIT 1 " ) -> fetchColumn ();
if ( $lastRoundId ) {
$games = Game :: getGamesByRound ( $lastRoundId );
} else {
2026-10-03 17:09:57 +02:00
$games = [];
2026-10-03 16:27:22 +02:00
}
2026-10-03 17:09:57 +02:00
require __DIR__ . '/../src/Views/home.php' ;
2026-10-03 17:02:11 +02:00
} catch ( Throwable $e ) {
echo " <p style='color:red;'>Fehler: " . $e -> getMessage () . " </p> " ;
2026-10-03 16:27:22 +02:00
}
2026-10-03 15:56:22 +02:00
} elseif ( $uri === '/api/status' ) {
header ( 'Content-Type: application/json' );
echo json_encode ([ 'status' => 'ok' , 'version' => '2027.1' ]);
} else {
http_response_code ( 404 );
echo " 404 Not Found " ;
}