1
0
Fork 0
chickadee/blog_log.php

102 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2023-12-19 23:55:46 +00:00
<?php
include "pass_hash.php";
const PW_FILE = "pass_hash.php";
2024-01-18 22:09:17 +00:00
session_start();
$user = $_POST["user"] ?? null;
$pass = $_POST["password"] ?? null;
2024-01-18 22:09:17 +00:00
if ( isset( $_SESSION["user"] ) ) {
header("Location: /admin.php");
die();
}
if ( $pass && $user ) {
2024-01-06 04:22:45 +00:00
$secret = hash( "sha256", $user . $pass );
2023-12-19 23:55:46 +00:00
if ( $secret ) {
if ( BLOG_HASH ) {
error_log( $secret );
error_log( BLOG_HASH );
if ( $secret == BLOG_HASH ) {
2024-01-18 22:09:17 +00:00
$_SESSION["user"] = $user;
header("Location: /admin.php");
2023-12-19 23:55:46 +00:00
die();
} else {
$invalid = true;
}
} else {
$template = <<<'PHP'
<?php
if ( __FILE__ == $_SERVER['SCRIPT_FILENAME'] ) {
header("Location: 404.php");
die();
}
const BLOG_HASH = '%s';
PHP;
2024-01-06 04:46:25 +00:00
if (!file_exists('./posts')) {
mkdir('posts', 0775, true);
}
if (!file_exists('./media')) {
mkdir('posts', 0775, true);
}
$success = file_put_contents( PW_FILE, sprintf( $template, $secret ) );
2023-12-19 23:55:46 +00:00
if ( !$success ) die( "Internal server error" );
2024-01-18 22:09:17 +00:00
$_SESSION["user"] = $user;
header("Location: /admin.php");
2023-12-19 23:55:46 +00:00
die();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>A secret opens the door</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
form{width:400px;max-width:80%;margin:2em auto}
.slant{width:500px;max-width:90%;margin:2em auto;border-top:1em solid #333;transform:rotateZ(-2deg)}
input{width:calc(100% - 8px);border:2px solid #999;border-radius:2px;background:white;color:#333;font-size:1.2em}
input[type=submit]{width:100%;background:#333;color:white;font-weight:bold;padding:5px 0;border-color:#333}
input:focus{border-color:#333}
.logo{width:300px;max-width:60%;margin:3em auto 0em}
img{width:100%}
h1{text-align:center;font-size:1.5rem}
#error-message{font-weight:bold;color:red;text-align:center}
</style>
2023-12-19 23:55:46 +00:00
</head>
<body>
2024-01-18 22:09:17 +00:00
<header>
<a href="/">Home</a>
</header>
<div class="logo">
<img src="chickadee.svg">
</div>
2023-12-19 23:55:46 +00:00
<div>
<?php if ( $invalid ): ?>
<p id="error-message">
An invalid username or password was given.
2023-12-19 23:55:46 +00:00
</p>
<?php endif; ?>
<div class="slant"></div>
2023-12-21 22:55:05 +00:00
<form action="blog_log.php" method="post">
<h1>Log In</h1>
<p>
<label>Username<br><input type="text" required name="user" <?php echo $invalid ? 'aria-describedby="error-message"' : ''; ?>></label>
</p>
<p>
<label>Password<br><input type="password" required name="password" <?php echo $invalid ? 'aria-describedby="error-message"' : ''; ?>></label>
</p>
<p>
<input type="submit" value="Submit">
</p>
2023-12-19 23:55:46 +00:00
</form>
<div class="slant"></div>
2023-12-19 23:55:46 +00:00
</div>
</body>
</html>