dodanie csrf, oauth 2, dockerfile

This commit is contained in:
Marcin Cieślikowski
2026-03-16 10:43:19 +01:00
parent 56c7ecff6d
commit df448ea7c1
26 changed files with 916 additions and 318 deletions
+41 -40
View File
@@ -1,62 +1,57 @@
<?php
// 1. Włączenie raportowania błędów (na czas testów)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('display_errors', '0');
ini_set('display_startup_errors', '0');
error_reporting(E_ALL);
// 2. Start sesji - musi być na samym początku, przed jakimkolwiek znakiem!
session_start();
// 3. Połączenie z bazą danych
require_once 'includes/db.php';
require_once 'includes/auth.php';
require_once 'includes/oauth_microsoft.php';
$error = '';
$logo_path = '';
// Pobranie ścieżki do logo z bazy danych
if (isset($_GET['oauth_error'])) {
$error = trim((string)$_GET['oauth_error']);
}
try {
$stmt = $pdo->query("SELECT setting_value FROM " . DB_PREFIX . "settings WHERE setting_key = 'logo_path'");
if ($stmt) {
$logo_path = $stmt->fetchColumn();
}
} catch (PDOException $e) {
// Ignorujemy błąd, jeśli tabela jeszcze nie istnieje itp.
error_log($e->getMessage());
}
// 4. Obsługa logowania
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = trim($_POST['username']);
$password = $_POST['password'];
if ($_SERVER["REQUEST_METHOD"] === "POST") {
requireCsrfToken();
if (!empty($username) && !empty($password)) {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if ($username !== '' && $password !== '') {
try {
// Pobieranie użytkownika z bazy (uwzględniając prefix)
$stmt = $pdo->prepare("SELECT * FROM " . DB_PREFIX . "users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user) {
// Weryfikacja hasła
if (password_verify($password, $user['password'])) {
// Logowanie udane - zapis do sesji
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
if ($user && password_verify($password, $user['password'])) {
session_regenerate_id(true);
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
// Przekierowanie do index.php
header("Location: index.php");
exit();
} else {
$error = "Błędne hasło. Upewnij się, że hasło jest poprawne).";
}
} else {
$error = "Użytkownik '$username' nie istnieje w bazie.";
header("Location: index.php");
exit();
}
$error = "Nieprawidlowy login lub haslo.";
} catch (PDOException $e) {
$error = "Błąd bazy danych: " . $e->getMessage();
error_log($e->getMessage());
$error = "Nie udalo sie zalogowac. Sprobuj ponownie pozniej.";
}
} else {
$error = "Proszę wypełnić oba pola.";
$error = "Prosze wypelnic oba pola.";
}
}
?>
@@ -66,7 +61,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Logowanie - <?php echo defined('APP_NAME') ? APP_NAME : 'System Zamówień'; ?></title>
<title>Logowanie - <?php echo e(defined('APP_NAME') ? APP_NAME : 'System Zamowien'); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f4f7f6; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
@@ -80,29 +75,35 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
<div class="login-card">
<div class="text-center mb-4">
<?php if ($logo_path && file_exists($logo_path)): ?>
<img src="<?php echo htmlspecialchars($logo_path); ?>" alt="Logo" class="login-logo">
<img src="<?php echo e($logo_path); ?>" alt="Logo" class="login-logo">
<?php else: ?>
<h3>💻 Logowanie</h3>
<h3>Logowanie</h3>
<?php endif; ?>
<p class="text-muted small">Wpisz dane, aby zarządzać zamówieniami</p>
<p class="text-muted small">Wpisz dane, aby zarzadzac zamowieniami</p>
</div>
<?php if ($error): ?>
<div class="alert alert-danger p-2 small"><?php echo $error; ?></div>
<div class="alert alert-danger p-2 small"><?php echo e($error); ?></div>
<?php endif; ?>
<form method="POST" action="login.php">
<?php echo csrfInput(); ?>
<div class="mb-3">
<label for="username" class="form-label small">Użytkownik</label>
<label for="username" class="form-label small">Uzytkownik</label>
<input type="text" name="username" id="username" class="form-control" required autofocus>
</div>
<div class="mb-3">
<label for="password" class="form-label small">Hasło</label>
<label for="password" class="form-label small">Haslo</label>
<input type="password" name="password" id="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary w-100">Zaloguj się</button>
<button type="submit" class="btn btn-primary w-100">Zaloguj sie</button>
</form>
<?php if (isMicrosoftOAuthConfigured()): ?>
<div class="text-center my-3 text-muted small">lub</div>
<a href="oauth_login.php" class="btn btn-outline-dark w-100">Zaloguj przez Microsoft 365</a>
<?php endif; ?>
<div class="mt-4 text-center border-top pt-3">
<p class="text-muted" style="font-size: 0.8rem;">
Aplikacja: <strong>goral.edu.pl/zeszyt</strong><br>