<?php
@session_start();

$config_ini = parse_ini_file('./config.ini');

$db = new SQLite3('./api/.db.db');
$db->exec('CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY,username TEXT ,password TEXT)');

$log_check = $db->query('SELECT * FROM users WHERE id=\'1\'');
$roe = $log_check->fetchArray();
$loggedinuser = @$roe['username'];

if (isset($_SESSION['name']) === $loggedinuser) {
    header('location: dns.php');
}

$rows = $db->query('SELECT COUNT(*) as count FROM users');
$row = $rows->fetchArray();
$numRows = $row['count'];

if ($numRows == 0) {
    $db->exec('INSERT INTO users(id ,username, password) VALUES(\'1\' ,\'admin\', \'admin\')');
    $db->close();
}

if (isset($_POST['login'])) {
    if (!$db) {
        echo $db->lastErrorMsg();
    }

    $sql = 'SELECT * from users where username="' . $_POST['username'] . '";';
    $ret = $db->query($sql);

    while ($row = $ret->fetchArray()) {
        $id = $row['id'];
        $username = $row['username'];
        $password = $row['password'];
    }

    if ($id != '') {
        if ($password == $_POST['password']) {
            session_regenerate_id();
            $_SESSION['loggedin'] = true;
            $_SESSION['name'] = $_POST['username'];

            if ($_POST['username'] == 'admin') {
                header('Location: user.php');
            } else {
                header('Location: dns.php');
            }
        } else {
            header('Location: ./api/index.php');
        }
    } else {
        header('Location: ./api/index.php');
    }

    $db->close();
}

echo '<!DOCTYPE html>' . "\r\n";
echo '<html lang="en">' . "\r\n";
echo '<head>' . "\r\n";
echo '    <meta charset="utf-8">' . "\r\n";
echo '    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">' . "\r\n";
echo '    <meta name="author" content="GLOBAL WEB.">' . "\r\n";
echo '    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">' . "\r\n";
echo '    <title>IBO 26 TEMAS</title>' . "\r\n";
echo '</head>' . "\r\n";
echo '<style>' . "\r\n";
echo '    body {' . "\r\n";
echo '        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);' . "\r\n";
echo '        min-height: 100vh;' . "\r\n";
echo '        display: flex;' . "\r\n";
echo '        align-items: center;' . "\r\n";
echo '        justify-content: center;' . "\r\n";
echo '        font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;' . "\r\n";
echo '    }' . "\r\n";
echo '    .login-container {' . "\r\n";
echo '        background: rgba(255, 255, 255, 0.95);' . "\r\n";
echo '        padding: 40px;' . "\r\n";
echo '        border-radius: 15px;' . "\r\n";
echo '        box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);' . "\r\n";
echo '        backdrop-filter: blur(10px);' . "\r\n";
echo '        width: 100%;' . "\r\n";
echo '        max-width: 400px;' . "\r\n";
echo '    }' . "\r\n";
echo '    .logo {' . "\r\n";
echo '        text-align: center;' . "\r\n";
echo '        margin-bottom: 30px;' . "\r\n";
echo '    }' . "\r\n";
echo '    .logo img {' . "\r\n";
echo '        max-width: 150px;' . "\r\n";
echo '        height: auto;' . "\r\n";
echo '        border-radius: 10px;' . "\r\n";
echo '    }' . "\r\n";
echo '    .form-control {' . "\r\n";
echo '        border: 2px solid #e9ecef;' . "\r\n";
echo '        border-radius: 10px;' . "\r\n";
echo '        padding: 15px;' . "\r\n";
echo '        font-size: 16px;' . "\r\n";
echo '        transition: all 0.3s ease;' . "\r\n";
echo '        margin-bottom: 20px;' . "\r\n";
echo '    }' . "\r\n";
echo '    .form-control:focus {' . "\r\n";
echo '        border-color: #667eea;' . "\r\n";
echo '        box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);' . "\r\n";
echo '    }' . "\r\n";
echo '    .btn-login {' . "\r\n";
echo '        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);' . "\r\n";
echo '        border: none;' . "\r\n";
echo '        border-radius: 10px;' . "\r\n";
echo '        padding: 15px;' . "\r\n";
echo '        font-size: 16px;' . "\r\n";
echo '        font-weight: 600;' . "\r\n";
echo '        color: white;' . "\r\n";
echo '        width: 100%;' . "\r\n";
echo '        transition: all 0.3s ease;' . "\r\n";
echo '        text-transform: uppercase;' . "\r\n";
echo '        letter-spacing: 1px;' . "\r\n";
echo '    }' . "\r\n";
echo '    .btn-login:hover {' . "\r\n";
echo '        transform: translateY(-2px);' . "\r\n";
echo '        box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);' . "\r\n";
echo '        color: white;' . "\r\n";
echo '    }' . "\r\n";
echo '    .developer-link {' . "\r\n";
echo '        text-align: center;' . "\r\n";
echo '        margin-top: 30px;' . "\r\n";
echo '    }' . "\r\n";
echo '    .developer-link a {' . "\r\n";
echo '        color: #667eea;' . "\r\n";
echo '        text-decoration: none;' . "\r\n";
echo '        font-size: 14px;' . "\r\n";
echo '        transition: all 0.3s ease;' . "\r\n";
echo '    }' . "\r\n";
echo '    .developer-link a:hover {' . "\r\n";
echo '        color: #764ba2;' . "\r\n";
echo '        text-decoration: underline;' . "\r\n";
echo '    }' . "\r\n";
echo '    .brand-link {' . "\r\n";
echo '        text-align: center;' . "\r\n";
echo '        margin-top: 20px;' . "\r\n";
echo '    }' . "\r\n";
echo '    .brand-link a {' . "\r\n";
echo '        color: #667eea;' . "\r\n";
echo '        text-decoration: none;' . "\r\n";
echo '        font-weight: 500;' . "\r\n";
echo '        transition: all 0.3s ease;' . "\r\n";
echo '    }' . "\r\n";
echo '    .brand-link a:hover {' . "\r\n";
echo '        color: #764ba2;' . "\r\n";
echo '    }' . "\r\n";
echo '</style>' . "\r\n";
echo '<body>' . "\r\n";
echo '<div class="container">' . "\r\n";
echo '    <div class="login-container">' . "\r\n";
echo '        <div class="logo">' . "\r\n";
echo '            <img src="./img/logo.png" alt="Logo">' . "\r\n";
echo '        </div>' . "\r\n";
echo '        <form method="post">' . "\r\n";
echo '            <div class="form-group">' . "\r\n";
echo '                <input type="text" class="form-control" placeholder="Username" name="username" required autofocus>' . "\r\n";
echo '            </div>' . "\r\n";
echo '            <div class="form-group">' . "\r\n";
echo '                <input type="password" class="form-control" placeholder="Password" name="password" required>' . "\r\n";
echo '            </div>' . "\r\n";
echo '            <input type="submit" class="btn btn-login" value="ENTRAR" name="login">' . "\r\n";
echo '        </form>' . "\r\n";
echo '        <div class="developer-link">' . "\r\n";
echo '            <a href="https://appsrebrand.com/">desenvolvedor</a>' . "\r\n";
echo '        </div>' . "\r\n";
echo '        <div class="brand-link">' . "\r\n";
echo '            <a href="';
echo $config_ini['contact'];
echo '">';
echo $config_ini['brand_name'];
echo '</a>' . "\r\n";
echo '        </div>' . "\r\n";
echo '    </div>' . "\r\n";
echo '</div>' . "\r\n";
echo '</body>' . "\r\n";
echo '</html>';
?>
