Реструктура стилей, страница логина, уведомления

This commit is contained in:
Shr3dd3r 2024-01-13 03:24:42 +03:00
parent 6958b75414
commit 4e1c36d670
29 changed files with 612 additions and 94 deletions

View File

@ -1,4 +1,7 @@
<?php
// Things related to authentication
// Includes
if ($IS_FRONTEND)
@ -9,7 +12,7 @@ else
// End currently active session
function EndSession () {
function AUTH_EndSession () {
session_unset();
session_destroy();
if (isset($_COOKIE["PHPSESSID"])) {
@ -35,8 +38,7 @@ if (session_status() === PHP_SESSION_ACTIVE && isset($_SESSION["userid"])) { //
$s->bind_param("s", $_SESSION["userid"]);
$s->execute();
if (!(bool)$s->get_result()->fetch_assoc()) { // If not, then destroy session
EndSession();
echo "user id does not exist";
AUTH_EndSession();
die("user id used in session does not exist");
}
$LOGGED_IN = true;
@ -48,7 +50,7 @@ if (session_status() === PHP_SESSION_ACTIVE && isset($_SESSION["userid"])) { //
if ($Config["debug"] && isset($_REQUEST["debug"])) { // If there are not any session and debug mode is on
// ATTENTION: FOR DEBUG PURPOSES ONLY!
if ($_REQUEST["debug"] == "drop") {
EndSession();
AUTH_EndSession();
die("session discarded");
}
$_SESSION["userid"] = intval($_REQUEST["debug"]);

View File

@ -1,4 +1,7 @@
<?php // Parsing configuration file
<?php
// Parsing configuration file
$Config = array();
$Config_FileName = "config.json";
@ -31,7 +34,13 @@ function CreateDirIfNotExist ($path) {
mkdir($path, 0755, true);
}
CreateDirIfNotExist("../" . $Config["media"]["pics_path"]); // TODO: treat path as absolute
// Creating dirs at correct path
if ($IS_FRONTEND) {
CreateDirIfNotExist($Config["media"]["pics_path"]);
CreateDirIfNotExist($Config["media"]["prevs_path"]);
} else {
CreateDirIfNotExist("../" . $Config["media"]["pics_path"]);
CreateDirIfNotExist("../" . $Config["media"]["prevs_path"]);
}
?>

View File

@ -1,4 +1,7 @@
<?php // Database setup
<?php
// Database setup
// Includes
if ($IS_FRONTEND)

View File

@ -1,4 +1,5 @@
<?php // All existing errors
<?php
// All existing errors
@ -23,6 +24,7 @@ const E_AUT_ALRLOGIN = 301; // User is already logged in
const E_AUT_REGCLOSED = 302; // Registrations are closed
const E_AUT_PWD2WEAK = 303; // Password is too weak
const E_AUT_NOTAUTHED = 304; // Not authenticated
const E_AUT_WRONGCREDS = 305; // User with that credentials does not exist
// Access errors
const E_ACS_PERMDENIED = 401; // Permission to object denied
const E_ACS_INSUFROLE = 402; // Insufficient role
@ -55,6 +57,7 @@ $Errors_Enum = array(
array("aut.regclosed", E_AUT_REGCLOSED, "registrations are closed"),
array("aut.pwd2weak", E_AUT_PWD2WEAK, "password is too weak"),
array("aut.notauthed", E_AUT_NOTAUTHED, "not authenticated"),
array("aut.wrongcreds", E_AUT_WRONGCREDS, "no such user name and/or password"),
// Access errors
array("acs.permdenied", E_ACS_PERMDENIED, "permission denied"),
array("acs.insufrole", E_ACS_INSUFROLE, "insufficient role"),

View File

@ -1,4 +1,7 @@
<?php // JSON-related functions
<?php
// JSON-related functions
// Includes
if ($IS_FRONTEND)

View File

@ -1,4 +1,7 @@
<?php // Necessary functions, types and other stuff
<?php
// Necessary functions, types and other stuff
// Includes
if ($IS_FRONTEND) {

View File

@ -1,4 +1,5 @@
<?php // Utility functions
<?php
// Utility functions

View File

@ -1,10 +1,22 @@
<?php // Get all comments from comment section by ID and base methods for managing comment sections
<?php
// Get all comments from comment section by ID and base methods for managing comment sections
// Includes
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/_types.php");
require_once("api/user/index.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
require_once("../user/index.php");
}

View File

@ -1,11 +1,22 @@
<?php // Create new post
<?php
// Create new post
// Includes
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/_types.php");
require_once("api/user/index.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
require_once("../user/index.php");
}

View File

@ -1,4 +1,7 @@
<?php // Get single post by ID
<?php
// Get single post by ID
// Includes
if ($IS_FRONTEND) {

View File

@ -1,6 +1,12 @@
<?php // Start session as any user
<?php
// Start session as any user
// ATTENTION: FOR DEBUG PURPOSES ONLY!
if ($IS_FRONTEND)
die("this file must not be included!");
// Includes
require_once("../_auth.php");
require_once("../_utils.php");
@ -17,9 +23,6 @@ if (Utils_ThisFileIsRequested(__FILE__)) {
if (!isset($_REQUEST["id"]))
JSON_ReturnError(code: E_UIN_INSUFARGS, desc: "valid id must be specified");
if (!isset($_SESSION["userid"]))
session_start();
$_SESSION["userid"] = intval($_REQUEST["id"]);
JSON_ReturnData($_SESSION);
}

View File

@ -1,11 +1,22 @@
<?php // Creating account
<?php
// Creating account
// Includes
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/_types.php");
require_once("api/user/index.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
require_once("index.php");
require_once("./index.php");
}

View File

@ -1,10 +1,20 @@
<?php // Deleting existing account
<?php
// Deleting existing account
// Includes
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/user/index.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("./index.php");
}
@ -73,7 +83,7 @@ if (Utils_ThisFileIsRequested(__FILE__)) {
} else {
// If it was self-deletion
if ($id === $THIS_USER)
EndSession();
AUTH_EndSession();
JSON_ReturnData(["success" => $result->GetData()]);
}
}

View File

@ -1,4 +1,7 @@
<?php // Viewing account data
<?php
// Viewing account data
// Includes
if ($IS_FRONTEND) {
@ -86,7 +89,7 @@ function User_IsMod (int $id): ReturnT {
* Get user information from DB
*/
function User_GetInfoByID (int $id): ReturnT {
global $db, $THIS_USER;
global $db, $THIS_USER, $LOGGED_IN;
$result = array();
@ -104,9 +107,15 @@ function User_GetInfoByID (int $id): ReturnT {
$result["avatar_path"] = $d["avatar_path"];
$result["role"] = $d["role"];
$result["banned"] = $d["banned"];
if (($id === $THIS_USER) || User_IsMod($THIS_USER)->GetData()) { // User himself and mods can see additional info
// User himself and mods can see additional info
if ($id === $THIS_USER) {
$result["email"] = $d["email"];
$result["invite_id"] = $d["invite_id"];
} elseif ($LOGGED_IN) {
if (User_IsMod($THIS_USER)->GetData()) {
$result["email"] = $d["email"];
$result["invite_id"] = $d["invite_id"];
}
}
return new ReturnT(data: $result);

89
api/user/login.php Normal file
View File

@ -0,0 +1,89 @@
<?php
// Logging into account
// Includes
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/_types.php");
require_once("api/user/index.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
require_once("./index.php");
}
// Methods
/*
* METHOD
* Log into existing user account
*/
function User_Login_Method (array $req): ReturnT {
global $db, $LOGGED_IN, $THIS_USER;
$login = $req["login"];
$password = $req["password"];
// Input sanity checks
// If already logged in
if ($LOGGED_IN)
return new ReturnT(err_code: E_AUT_ALRLOGIN, err_desc: "you are already logged in");
// If no password or login supplied
if (!isset($login) || !isset($password))
return new ReturnT(err_code: E_AUT_WRONGCREDS, err_desc: "you must supply both login and password");
// Checking if password is correct
$s = $db->prepare("SELECT * FROM users WHERE login = ?");
$s->bind_param("s", $login);
$s->execute();
$d = $s->get_result()->fetch_assoc();
// Wrong login
if (!(bool)$d)
return new ReturnT(err_code: E_AUT_WRONGCREDS, err_desc: "wrong login or password");
$suppl_pwd_hash = hash("sha256", $password . $d["salt"], true);
$real_pwd_hash = $d["password_hash"];
// Wrong password
if ($suppl_pwd_hash !== $real_pwd_hash)
return new ReturnT(err_code: E_AUT_WRONGCREDS, err_desc: "wrong login or password");
// Actions
$_SESSION["userid"] = $d["id"];
$THIS_USER = $d["id"];
return new ReturnT(data: true);
}
if (Utils_ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
// HACK: for debugging purposes. Will be removed later
if ($Config["debug"])
$_POST = $_REQUEST;
// Log into account
$result = User_Login_Method($_POST);
// Checking result
if ($result->IsError())
$result->ThrowJSONError();
else
JSON_ReturnData(["success" => $result->GetData()]);
}
?>

View File

@ -1,9 +1,9 @@
<div class="visualbox footer">
<div class="quicklinks">
<p>
<a title="Contacts" href="./static/contact">Contacts</a> |
<a title="Terms of service" href="./static/terms_of_service">Terms of service</a> |
<a title="Privacy policy" href="./static/privacy">Privacy</a>
<a title="Contacts" href="./?do=show_contacts">Contacts</a> |
<a title="Terms of service" href="./?do=show_tos">Terms of service</a> |
<a title="Privacy policy" href="./?do=there_are_my_data">Privacy</a>
</p>
</div>
<div class="description">

View File

@ -12,7 +12,14 @@ echo "<title>E949: $PAGE_TITLE</title>\n";
require_once("favicon.html");
?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="front/styles/base.css">
<link rel="stylesheet" href="front/styles/footer.css">
<link rel="stylesheet" href="front/styles/index.css"> <!--TODO: insert different additional styles on different pages-->
<?php
// Include custom page style, if exists
if (isset($PAGE_STYLE)) {
echo "<link rel=\"stylesheet\" href=\"$PAGE_STYLE\">";
}
?>

33
front/notifications.php Normal file
View File

@ -0,0 +1,33 @@
<?php
// Notifications
// Notices stack
$NTFY_NoticesStack = array();
// Add new notice with selected type
function NTFY_AddNotice (string $text, string $type = "fail") {
global $NTFY_NoticesStack;
switch ($type) {
case "fail":
$NTFY_NoticesStack[] = "<div class=\"notification_fail\"><p>$text</p></div>";
break;
default:
die("invalid notification type: $type");
}
}
// Echo all notifications
function NTFY_EchoAllNotices () {
global $NTFY_NoticesStack;
foreach ($NTFY_NoticesStack as $notice) {
echo "$notice\n";
}
}
?>

View File

@ -1,11 +1,20 @@
<?php
// Main page posts counter
if (!$IS_FRONTEND) {
http_response_code(500);
die();
}
// Includes
require_once("api/post/index.php");
$totalPostsAmount = Post_GetPostsAmount();
$totalPostsAmount = strval($totalPostsAmount);

View File

@ -0,0 +1,5 @@
<?php
require_once("front/pages/index/random_meme.php");
require_once("front/pages/index/searchbox.php");
require_once("front/pages/index/counter.php");
?>

View File

@ -2,5 +2,5 @@
// TODO: picking random meme
?>
<div class="visualbox">
<img src="test.png">
<img style="max-width: 90%; max-height: 240px;" src="test.png">
</div>

View File

@ -1,11 +1,20 @@
<?php
// Main page search box
if (!$IS_FRONTEND) {
http_response_code(500);
die();
}
// Includes
require_once("api/user/index.php");
?>
<div class="visualbox searchbox">
<a class="title" href=".">E949</a>
@ -17,25 +26,25 @@ if (!$IS_FRONTEND) {
if ($res->IsError())
$res->ThrowJSONError();
$uname = $res->GetData()["login"];
echo "<a class=\"useraccount\" title=\"Account page\" href=\"./?do=view_user&id=$THIS_USER\">$uname</a>";
echo "<a class=\"useraccount\" title=\"Account page\" href=\"./?do=user_info&id=$THIS_USER\">$uname</a>";
unset($res);
} else { // If user is NOT logged in
?>
<a title="Login in existing account" href="./?do=login">Login</a>
<a title="Create new account" href="./?do=signup">Signup</a>
<a title="Create new account" href="./?do=register">Register</a>
<?php
}
?>
<a title="A paginated list of every post" href="./?do=view_all_posts">Posts</a>
<a title="A paginated list of every tag" href="./?do=view_all_tags">Tags</a>
<a title="A paginated list of every post" href="./?do=search_posts">Posts</a>
<a title="A paginated list of every tag" href="./?do=view_tags">Tags</a>
<a title="Statistics of current instance" href="./?do=view_stats">Statistics</a>
<a title="A site map" href="./?do=view_sitemap">Site map</a>
</div>
<div>
<form action="." accept-charset="UTF-8" method="get">
<input type="hidden" name="do" value="search_posts">
<input type="text" name="tags" id="tags" value="" size="36" autofocus="autofocus" autocomplete="on"><br>
<input type="submit" value="Search">
<input type="hidden" name="do" value="search_posts">
<!-- TODO: JS
<input type="button" value="Show random meme" id="random-meme">
-->

View File

@ -0,0 +1,48 @@
<?php
// Login page
// Includes
require_once("api/user/login.php");
require_once("front/pages/main_nav.php");
require_once("front/notifications.php");
// If there is attempt to login in
if (isset($_POST["login"]) || isset($_POST["password"])) {
$result = User_Login_Method($_POST);
if ($result->IsError()) {
NTFY_AddNotice("Failed to log into account! Check your credentials and try again.<br>" . $result->GetError());
} else {
header("Location: index.php");
exit();
}
}
NTFY_EchoAllNotices();
?>
<div class="visualbox">
<h1>Login</h1>
<form class="login" action="./?do=login" accept-charset="UTF-8" method="post">
<div>
<label for="login">Username</label><br>
<input type="text" name="login" id="login">
</div>
<div>
<label for="password">Password</label><br>
<input type="password" name="password" id="password">
<a href="./?do=reset_password">Reset</a>
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
<div class="loginmisc">
<p>Don&#39;t have an account? <a href="./?do=register">Register here</a></p>
</div>
</div>

53
front/pages/main_nav.php Normal file
View File

@ -0,0 +1,53 @@
<?php
// Main navigation bar
$MARKUP_CURRENT_PAGE = "class=\"current\"";
$MARKUP_CURRENT_PAGE_LOGIN = "";
if ($WHAT_PAGE_IS_CURRENT["login"])
$MARKUP_CURRENT_PAGE_LOGIN = $MARKUP_CURRENT_PAGE;
?>
<nav class="main">
<ul>
<li <?php echo $MARKUP_CURRENT_PAGE_LOGIN; ?>>
<p><a title="Login in existing account" href="./?do=login">Login</p></a>
</li>
<span>|</span>
<li>
<p><a title="Create new account" href="./?do=register">Register</p></a>
</li>
<span>|</span>
<li>
<p><a title="A paginated list of every post" href="./?do=search_posts">Posts</p></a>
</li>
<span>|</span>
<li>
<p><a title="A paginated list of every tag" href="./?do=view_tags">Tags</p></a>
</li>
<span>|</span>
<li>
<p><a title="Statistics of current instance" href="./?do=view_stats">Stats</p></a>
</li>
<span>|</span>
<li>
<p><a title="Contacts" href="./?do=show_contacts">Contacts</p></a>
</li>
<span>|</span>
<li>
<p><a title="Terms of service" href="./?do=show_tos">ToS</p></a>
</li>
<span>|</span>
<li>
<p><a title="Privacy policy" href="./?do=there_are_my_data">Privacy</p></a>
</li>
<span>|</span>
<li>
<p><a title="A site map" href="./?do=view_sitemap">Site map</p></a>
</li>
</ul>
</nav>

View File

@ -1,4 +1,12 @@
/* Common blocks */
/* The most base style, used everywhere */
/* Common */
*:focus {
outline: 2px dotted #49f49f;
}
body {
background-color: #094e59;
@ -12,13 +20,14 @@ body, div, p, a {
}
/* Fonts */
h1, h2, h3, h4, h5, h6, p, ul, li, dd, dt {
h1, h2, h3, h4, h5, h6, p, ul, li, dd, dt, label {
font-family: Verdana, Sans-Serif;
}
h1, h2, h3, h4, h5, h6, p, a {
h1, h2, h3, h4, h5, h6, p, a, label {
color: #00c07c;
text-decoration: none;
}
@ -31,15 +40,89 @@ a {
a:hover {
color: #00c07c;
cursor: pointer;
}
/* Custom blocks */
div.wrapper {
margin: auto;
}
div.visualbox {
margin: 10px;
padding: 10px;
border-radius: 5px;
box-shadow: 0 0 5px #000;
text-shadow: 0 0 2px black, 0 0 6px black;
backdrop-filter: blur(6px);
}
div.notification_fail {
margin: 10px;
padding: 10px;
border-radius: 5px;
box-shadow: 0 0 5px red;
text-shadow: 0 0 2px black, 0 0 6px black;
backdrop-filter: blur(6px);
background-color: #f003;
}
div.notification_fail p {
color: red;
}
/* Input */
/* Text input */
input[type=text], input[type=password] {
color: #00c07c;
background-color: transparent;
border: 2px solid #009049;
border-radius: 3px;
font-family: Verdana, Sans-Serif;
font-size: 16px;
text-shadow: 0 0 6px black;
transition: all 0.25s;
}
input[type=text]:hover, input[type=password]:hover {
border: 2px solid transparent;
border-bottom: 2px solid #009049;
}
input[type=text]:focus, input[type=password]:focus {
border: 2px solid transparent;
border-bottom: 2px solid #49f49f;
outline: none;
}
/* Submit button */
input[type=submit] {
background-color: transparent;
border: 2px solid #009049;
border-radius: 3px;
color: #00c07c;
text-shadow: 0 0 6px #000a;
font-family: Verdana, Sans-Serif;
font-size: 16px;
cursor: pointer;
transition: all 0.2s;
}
input[type=submit]:hover {
border: 2px solid transparent;
border-bottom: 2px solid #009049;
background-color: #009049a0;
color: #49f49f;
}
input[type=submit]:focus {
border: 2px solid #49f49f;
outline: none;
}
/* Checkbox */
input[type=checkbox] {
cursor: pointer;
background-color: red;
}

View File

@ -1,3 +1,7 @@
/* Style specifically for footer */
div.footer div.quicklinks {
padding: 0 0 4px 0;
}

View File

@ -1,12 +1,16 @@
/* Index wrapper */
/* Stylesheet for index page */
/* Custom wrapper */
div.wrapper {
text-align: center;
margin: auto;
width: 65%;
}
/* Index fonts */
div.searchbox a.title {
@ -21,45 +25,20 @@ div.searchbox a.title:hover {
color: #009049;
}
/* Index search box */
div.searchbox input[type=text] {
margin-top: 2px;
margin-bottom: 4px;
color: #00c07c;
background-color: transparent;
border: 2px solid #009049;
border-radius: 3px;
font-family: Verdana, Sans-Serif;
font-size: 16px;
text-shadow: 0 0 6px black;
transition: all 0.25s;
}
div.searchbox input[type=text]:focus {
border: 2px solid transparent;
border-bottom: 2px solid #009049;
outline: none;
}
div.searchbox input[type=submit] {
margin-top: 4px;
background-color: transparent;
border: 2px solid #009049;
border-radius: 3px;
color: #00c07c;
text-shadow: 0 0 6px #000a;
font-family: Verdana, Sans-Serif;
font-size: 16px;
padding: 5px 10px;
cursor: pointer;
}
div.searchbox input[type=submit]:focus {
border: 2px solid transparent;
border-bottom: 2px solid #009049;
background-color: #009049a0;
}
/* Index navigation */

83
front/styles/main.css Normal file
View File

@ -0,0 +1,83 @@
/* Common stylesheet for most of the site */
/* Adjusting wrapper */
div.wrapper {
padding-top: 26pt;
}
/* Navigation block */
nav.main {
background-color: transparent;
position: fixed;
top: 0;
left: 0;
z-index: 999;
width: 100%;
}
nav.main ul {
position: relative;
margin: 0;
margin-left: 10px;
margin-right: 10px;
padding: 7px;
backdrop-filter: blur(6px);
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
box-shadow: 0 0 5px #000;
text-shadow: 0 0 2px black, 0 0 6px black;
text-align: center;
}
nav.main ul li {
display: inline-block;
margin-left: 1.75vw;
margin-right: 1.75vw;
}
nav.main ul li a {
color: #00c07c;
text-decoration: none;
transition: all 0.2s;
}
nav.main ul li a:hover {
color: #49f49f;
}
nav.main ul li.current a {
font-weight: bold;
text-decoration: underline;
}
nav.main ul span {
color: #00c07c;
padding: 0;
margin: 0;
}
/* Other */
form.login div {
margin-bottom: 14px;
}
form.login a {
font-size: 80%;
}
div.loginmisc {
margin-top: 4px;
margin-bottom: 4px;
}
div.loginmisc p {
font-style: italic;
font-size: 80%;
}

View File

@ -1,16 +1,52 @@
<?php
// Main page
$IS_FRONTEND = true;
// Includes
require_once("api/_auth.php");
require_once("api/user/index.php");
require_once("api/post/index.php");
require_once("front/notifications.php");
$PAGE_TITLE = "Index"; // TODO
$PAGE_TITLE = null; // String that will be showed as "E949: $PAGE_TITLE"
$PAGE_STYLE = null; // Path to file with style that will be included
$PAGE_FILE = null; // Path to main body file that will be included
$PICKED_PAGE = null;
if (isset($_GET["do"]))
$PICKED_PAGE = $_GET["do"];
else
$PICKED_PAGE = "";
$WHAT_PAGE_IS_CURRENT = array(
"main" => false,
"login" => false
);
// Picking current page
switch ($PICKED_PAGE) {
// Login page
case "login":
$WHAT_PAGE_IS_CURRENT["login"] = true;
$PAGE_TITLE = "Login";
$PAGE_STYLE = "front/styles/main.css";
$PAGE_FILE = "front/pages/login/page.php";
break;
// Main page
case "index":
case "main":
default:
$WHAT_PAGE_IS_CURRENT["main"] = true;
$PAGE_TITLE = "Index";
$PAGE_STYLE = "front/styles/index.css";
$PAGE_FILE = "front/pages/index/page.php";
}
@ -23,10 +59,7 @@ $PAGE_TITLE = "Index"; // TODO
<body>
<div class="wrapper">
<?php
// TODO: different pages
require_once("front/pages/index/random_meme.php");
require_once("front/pages/index/searchbox.php");
require_once("front/pages/index/counter.php");
require_once($PAGE_FILE);
require_once("front/footer.php");
?>
</div>