<?php
// Configuration: Path to the htpasswd file
$htpasswd_file = '/homez.298/alterinv/passwd/scpi';

function verify_password($password, $hash) {
    if (strpos($hash, '$apr1$') === 0) {
        // MD5-APR1 hash (Apache MD5 crypt)
        $parts = explode('$', $hash);
        if (count($parts) < 4) {
            return false; // malformed hash
        }
	$parts = explode('$', $hash);
	$salt = $parts[2]; // salt string from stored hash
	$crypt_salt = '$1$'. $salt . '$'; // correct salt format

        $new_hash = crypt($password, $crypt_salt);
	echo $new_hash;

        // Debug output
        // echo "Debug: chckpassword: " . (hash_equals($new_hash, $hash) ? 'true' : 'false') . "\n";

        return hash_equals($new_hash, $hash);
    } elseif (strpos($hash, '$2y$') === 0) {
        return password_verify($password, $hash);
    } elseif (strpos($hash, '{SHA}') === 0) {
        $sha_hash = '{SHA}' . base64_encode(sha1($password, true));
        return hash_equals($sha_hash, $hash);
    } else {
        return hash_equals($password, $hash);
    }
}



// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    // Read the htpasswd file
    if (file_exists($htpasswd_file)) {
        $lines = file($htpasswd_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $authenticated = false;
        foreach ($lines as $line) {
            list($user, $hash) = explode(':', $line, 2);
            if ($user === $username && verify_password($password, $hash)) {
                $authenticated = true;
                break;
            }
        }

        if ($authenticated) {
            // Start a session or redirect to another page
            session_start();
            $_SESSION['username'] = $username;
	    echo "pouet";
            exit;
        } else {
            $error = "Identifiant ou mot de passe incorrect";
        }
    } else {
        $error = "Fichier d'authentification non trouvé";
    }
}
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Le Couvent des Augustins - La Rochelle</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Georgia, 'Times New Roman', serif;
            background: linear-gradient(135deg, #f6f8fb 0%, #e6edf5 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background: #ffffff;
            border-radius: 18px;
            box-shadow: 0 12px 28px rgba(18, 34, 58, 0.08);
            max-width: 480px;
            width: 100%;
            padding: 40px;
            position: relative;
            overflow: hidden;
        }

        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #3B6A97 0%, #E08A64 50%, #3B6A97 100%);
        }

        .logo-container {
            text-align: center;
            margin-bottom: 32px;
        }

        .logo {
            display: inline-block;
            margin-bottom: 20px;
        }

        h1 {
            font-size: 24px;
            font-weight: 700;
            color: #3B6A97;
            letter-spacing: 1.2px;
            margin-bottom: 8px;
        }

        .subtitle {
            font-size: 16px;
            font-weight: 600;
            color: #E08A64;
            letter-spacing: 2.5px;
            font-variant: small-caps;
            margin-bottom: 24px;
        }

        .welcome-text {
            text-align: center;
            font-size: 15px;
            color: #6b7e90;
            margin-bottom: 32px;
            line-height: 1.6;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            font-size: 14px;
            font-weight: 600;
            color: #1e2a36;
            margin-bottom: 8px;
            letter-spacing: 0.3px;
        }

        input {
            width: 100%;
            padding: 12px 14px;
            border: 1px solid #d7e1ec;
            border-radius: 12px;
            font-size: 15px;
            font-family: 'Segoe UI', Arial, sans-serif;
            color: #1e2a36;
            background: #fff;
            transition: all 0.2s;
        }

        input:focus {
            outline: none;
            border-color: #E08A64;
            box-shadow: 0 0 0 3px rgba(224, 138, 100, 0.18);
        }

        .btn-login {
            width: 100%;
            padding: 14px;
            background: #E08A64;
            color: #fff;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 700;
            letter-spacing: 0.5px;
            cursor: pointer;
            margin-top: 28px;
            box-shadow: 0 8px 18px rgba(224, 138, 100, 0.25);
            transition: all 0.2s;
            font-family: Georgia, 'Times New Roman', serif;
        }

        .btn-login:hover {
            filter: brightness(1.05);
            transform: translateY(-1px);
            box-shadow: 0 10px 22px rgba(224, 138, 100, 0.3);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .footer-text {
            text-align: center;
            margin-top: 24px;
            font-size: 12px;
            color: #6b7e90;
        }

        .footer-text a:hover {
            color: #E08A64;
            transition: color 0.2s;
        }

        .badge {
            display: inline-block;
            background: #fff7f2;
            border: 1px solid rgba(224, 138, 100, 0.35);
            border-radius: 12px;
            padding: 8px 14px;
            font-size: 12px;
            color: #E08A64;
            font-weight: 700;
            letter-spacing: 0.5px;
            margin-top: 20px;
        }

        @media (max-width: 600px) {
            .container {
                padding: 30px 24px;
            }

            h1 {
                font-size: 20px;
            }

            .subtitle {
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo-container">
            <div class="logo">
                <svg viewBox="0 0 380 120" xmlns="http://www.w3.org/2000/svg" width="320" height="100">
                    <defs>
                        <linearGradient id="shadowGrad" x1="0%" y1="100%" x2="0%" y2="0%">
                            <stop offset="0%" style="stop-color:#3B6A97;stop-opacity:0.25" />
                            <stop offset="100%" style="stop-color:#3B6A97;stop-opacity:0" />
                        </linearGradient>
                        <linearGradient id="buildingGrad" x1="0%" y1="0%" x2="0%" y2="100%">
                            <stop offset="0%" style="stop-color:#6B94BD;stop-opacity:0.85" />
                            <stop offset="100%" style="stop-color:#5A82A8;stop-opacity:0.85" />
                        </linearGradient>
                        <linearGradient id="accentGrad" x1="0%" y1="0%" x2="100%" y2="0%">
                            <stop offset="0%" style="stop-color:#E08A64;stop-opacity:1" />
                            <stop offset="100%" style="stop-color:#F5A47B;stop-opacity:1" />
                        </linearGradient>
                        <filter id="shadow" x="-50%" y="-50%" width="200%" height="200%">
                            <feDropShadow dx="0" dy="4" stdDeviation="6" flood-color="#3B6A97" flood-opacity="0.35"/>
                        </filter>
                    </defs>
                    <rect width="380" height="120" rx="10" fill="#ffffff"/>
                    <g transform="translate(190, 60)">
                        <g transform="translate(-150, 0)">
                            <path d="M 5,28 L 85,28 L 114,-45 L 34,-45 Z" fill="url(#shadowGrad)"/>
                            <g filter="url(#shadow)">
                                <rect x="5" y="-22" width="80" height="50" rx="2" fill="url(#buildingGrad)"/>
                                <rect x="39" y="-35" width="12" height="13" rx="1" fill="url(#buildingGrad)"/>
                                <path d="M 38,-35 L 45,-42 L 52,-35 Z" fill="#E08A64" opacity="0.8"/>
                                <line x1="45" y1="-42" x2="45" y2="-47" stroke="#E08A64" stroke-width="2" stroke-linecap="round"/>
                                <line x1="42.5" y1="-45" x2="47.5" y2="-45" stroke="#E08A64" stroke-width="2" stroke-linecap="round"/>
                                <path d="M 12,-12 L 12,-8 Q 14,-10 16,-8 L 16,-12 Z" fill="#E08A64"/>
                                <path d="M 23,-12 L 23,-8 Q 25,-10 27,-8 L 27,-12 Z" fill="#E08A64"/>
                                <path d="M 34,-12 L 34,-8 Q 36,-10 38,-8 L 38,-12 Z" fill="#E08A64"/>
                                <path d="M 45,-12 L 45,-8 Q 47,-10 49,-8 L 49,-12 Z" fill="#E08A64"/>
                                <path d="M 56,-12 L 56,-8 Q 58,-10 60,-8 L 60,-12 Z" fill="#E08A64"/>
                                <path d="M 67,-12 L 67,-8 Q 69,-10 71,-8 L 71,-12 Z" fill="#E08A64"/>
                                <path d="M 78,-12 L 78,-8 Q 80,-10 82,-8 L 82,-12 Z" fill="#E08A64"/>
                                <path d="M 12,8 L 12,12 Q 14,10 16,12 L 16,8 Z" fill="#E08A64"/>
                                <path d="M 23,8 L 23,12 Q 25,10 27,12 L 27,8 Z" fill="#E08A64"/>
                                <path d="M 34,8 L 34,12 Q 36,10 38,12 L 38,8 Z" fill="#E08A64"/>
                                <path d="M 45,8 L 45,12 Q 47,10 49,12 L 49,8 Z" fill="#E08A64"/>
                                <path d="M 56,8 L 56,12 Q 58,10 60,12 L 60,8 Z" fill="#E08A64"/>
                                <path d="M 67,8 L 67,12 Q 69,10 71,12 L 71,8 Z" fill="#E08A64"/>
                                <path d="M 78,8 L 78,12 Q 80,10 82,12 L 82,8 Z" fill="#E08A64"/>
                                <rect x="40" y="18" width="10" height="10" fill="#E08A64"/>
                                <path d="M 40,18 Q 45,14 50,18" fill="#E08A64"/>
                                <line x1="3" y1="-23" x2="87" y2="-23" stroke="#E08A64" stroke-width="4" stroke-linecap="round"/>
                            </g>
                        </g>
                        <g transform="translate(-44, 0)">
                            <text x="0" y="-2" font-family="Georgia, 'Times New Roman', serif" font-size="15" font-weight="700" fill="#3B6A97" text-anchor="start" letter-spacing="0.7">
                                Le Couvent des Augustins
                            </text>
                            <text x="0" y="13" font-family="Georgia, 'Times New Roman', serif" font-size="11" font-weight="600" fill="#E08A64" text-anchor="start" letter-spacing="1.8" style="font-variant: small-caps;">
                                La Rochelle
                            </text>
                        </g>
                    </g>
                </svg>
            </div>

            <div class="badge">ESPACE DOCUMENTS ET SIMULATIONS</div>
        </div>

        <p class="welcome-text">
            Bienvenue sur votre espace privé. Veuillez vous identifier pour accéder aux informations.
        </p>

	<form id="loginForm" method='POST'>
	    <div class="form-group">
		<label for="username">Identifiant</label>
		<input
		    type="text"
		    id="username"
		    name="username"
		    required
		>

	    </div>

	    <div class="form-group">
	     <label for="password">Mot de passe</label>
		<input
		    type="password"
		    id="password"
		    name="password"
		    required
		>
	    </div>

	    <button type="submit" class="btn-login">
		Se connecter
	    </button>
	</form>

	<p class="footer-text">
	    Site géré par <a href="https://www.alter-invest.com/CouventdesAugustins" target="_blank" style="color: #3B6A97; font-weight: 700; text-decoration: none;">Alter Invest</a>
	</p>
    </div>
</body>
</html>

