Проект ещё жив!

This commit is contained in:
Shr3dd3r 2023-12-20 06:08:13 +03:00
parent a573faf5a1
commit 8d74a51937
23 changed files with 358 additions and 143 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
_temp/
front/styles/bg_pattern_peace_old.png
test.png
TODO.md
TODO.md
front/images/*
front/images/counter/*

View File

@ -1,7 +1,10 @@
<?php
// Includes
require_once("_db.php");
if ($IS_FRONTEND)
require_once("api/_db.php");
else
require_once("_db.php");

View File

@ -2,7 +2,7 @@
$Config = array();
$Config_FileName = "config.json";
$Config_PossiblePaths = array(
$Config_PossiblePaths = array( // TODO: remake with flag $IS_FRONTEND
"./" . $Config_FileName,
"../" . $Config_FileName,
"../../" . $Config_FileName,

View File

@ -1,6 +1,10 @@
<?php // Database setup
require_once("_config.php");
// Includes
if ($IS_FRONTEND)
require_once("api/_config.php");
else
require_once("_config.php");

View File

@ -1,7 +1,10 @@
<?php // JSON-related functions
// Includes
require_once("_errorslist.php");
if ($IS_FRONTEND)
require_once("api/_errorslist.php");
else
require_once("_errorslist.php");

View File

@ -1,8 +1,13 @@
<?php // Necessary functions, types and other stuff
// Includes
require_once("_errorslist.php");
require_once("_json.php");
if ($IS_FRONTEND) {
require_once("api/_errorslist.php");
require_once("api/_json.php");
} else {
require_once("_errorslist.php");
require_once("_json.php");
}

View File

@ -2,46 +2,127 @@
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
require_once("../user/index.php");
// Get comments from range of selected comment section
function ComSec_Get ($sec_id, $ts_from, $ts_to) {
global $db;
// Functions
/*
* FUNCTION
* Get comments from range of selected comment section
*/
function Comments_GetSectionRange (int $sec_id, int $ts_from = 0, int $ts_to = 0xffffffff): ReturnT {
global $db, $LOGGED_IN, $THIS_USER;
$result = array();
$s = $db->prepare("SELECT * FROM posts WHERE id = ?");
$s->bind_param("s", $id);
$s = $db->prepare("SELECT * FROM comments WHERE comment_section_id=? AND created_at>=? AND created_at<=? ORDER BY created_at");
$s->bind_param("sss", $sec_id, date("Y-m-d H:i:s", $ts_from), date("Y-m-d H:i:s", $ts_to));
$s->execute();
$d = $s->get_result()->fetch_assoc();
$d = $s->get_result();
if (!(bool)$d) {
return null;
if (!(bool)$d)
return new ReturnT(data: $result);
$isAdmin = false;
if ($LOGGED_IN && User_HasRole($THIS_USER, "admin")->GetData())
$isAdmin = true;
while ($row = $d->fetch_array()) {
if (!$isAdmin && $row["needs_check"])
continue;
$newResultRow = array(
"id" => $row["id"],
"author_id" => $row["author_id"],
"created_at" => $row["created_at"],
"contents" => $row["contents"]
);
if ($isAdmin)
$newResultRow["needs_check"] = (bool)$row["needs_check"];
$result[] = $newResultRow;
}
return new ReturnT(data: $result);
}
if (ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
// Methods
/*
* METHOD
* Get comments from range of selected comment section
*/
function Comments_GetSectionRange_Method (array $req): ReturnT {
// Input sanity checks
$SectionID = null;
$TSFrom = 0;
$TSTo = 0xffffffff;
if (isset($_REQUEST["id"])) {
if (!ctype_digit($_REQUEST["id"]))
ReturnJSONError($Err_RDP_InvalidID, "id must be numeric");
$SectionID = intval($_REQUEST["id"]);
if (isset($req["id"])) {
if (!ctype_digit($req["id"]))
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "id must be numeric");
$SectionID = intval($req["id"]);
} else {
ReturnJSONError($Err_RDP_InvalidID, "id must be specified");
return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "id must be specified");
}
/*
$ResponseData = ComSec_GetComms($SectionID);
if ($ResponseData)
ReturnJSONData($ResponseData);
if (isset($req["ts_from"])) {
$TSFrom = $req["ts_from"];
if (strlen($TSFrom) > 24)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be this long");
if (!ctype_digit($TSFrom))
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp must be numeric");
$TSFrom = intval($TSFrom);
if ($TSFrom > 0xffffffff)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be bigger than INT32_MAX");
}
if (isset($req["ts_to"])) {
$TSTo = $req["ts_to"];
if (strlen($TSTo) > 24)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be this long");
if (!ctype_digit($TSTo))
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp must be numeric");
$TSTo = intval($TSTo);
if ($TSTo > 0xffffffff)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be bigger than INT32_MAX");
}
if ($TSTo < $TSFrom)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be bigger than end timestamp");
// Actions
return Comments_GetSectionRange($SectionID, $TSFrom, $TSTo);
}
if (Utils_ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
$result = Comments_GetSectionRange_Method($_REQUEST);
if ($result->IsError())
$result->ThrowJSONError();
else
ReturnJSONError($Err_DP_IDNotFound, "wrong id");
*/
JSON_ReturnData($result->GetData());
}
?>

View File

@ -88,6 +88,8 @@ function Post_Create (
bool $comms_enabled = false,
bool $edit_lock = false
): ReturnT {
global $db;
$result = null;
// Author ID should exist
@ -113,7 +115,7 @@ function Post_Create (
* Create single publication
*/
function Post_Create_Method (array $req, array $files): ReturnT {
global $db, $Config, $LOGGED_IN, $THIS_USER;
global $Config, $LOGGED_IN, $THIS_USER;
$author_id = $THIS_USER;
$tags = null;
@ -168,7 +170,7 @@ function Post_Create_Method (array $req, array $files): ReturnT {
if (ThisFileIsRequested(__FILE__)) {
if (Utils_ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
// TODO: cleanup if bad result

View File

@ -1,14 +1,39 @@
<?php // Get single post by ID
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
// Includes
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/_types.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
}
// Functions
/*
* FUNCTION
* Get total amount of posts
*/
function Post_GetPostsAmount (): int {
global $db;
$s = $db->prepare("SELECT id FROM posts"); // NOTICE: very naive and will impact performance when many posts are exist
$s->execute();
$d = $s->get_result()->fetch_assoc();
if ($d)
return count($d);
else
return 0;
}
/*
* FUNCTION
* Increment number of views for post
@ -79,10 +104,10 @@ function Post_GetByID_Method (array $req) {
$PostID = null;
if (isset($req["id"])) {
if (!ctype_digit($req["id"]))
return new ReturnT(err_code: E_UIN_BADARGS, "id must be numeric");
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "id must be numeric");
$PostID = intval($req["id"]);
} else {
return new ReturnT(err_code: E_UIN_INSUFARGS, "id must be specified");
return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "id must be specified");
}
// Actions

View File

@ -15,7 +15,7 @@ if (Utils_ThisFileIsRequested(__FILE__)) {
JSON_ReturnError(code: E_UNS_INTERNAL, desc: "you need to enable debug mode in configuration file first");
if (!isset($_REQUEST["id"]))
JSON_ReturnError(code: E_UIN_WRONGID, desc: "valid id must be specified");
JSON_ReturnError(code: E_UIN_INSUFARGS, desc: "valid id must be specified");
if (!isset($_SESSION["userid"]))
session_start();

View File

@ -16,13 +16,15 @@ require_once("index.php");
* Create new user account
*/
function User_Create (string $login, string $password, ?string $email = null, ?string $invite_id = null, ?string $avatar_path = null): ReturnT {
global $db;
$salt = Utils_GenerateRandomString(8);
$pwd_hash = hash("sha256", $password . $salt, true);
// TODO: process invite
$s = $db->prepare("INSERT INTO users (login,email,password_hash,salt,avatar_path,role,invite_id) VALUES (?,?,?,?,?,?,?)");
$role = "newbie"; // TODO: make decision from config
$role = "newbie"; // TODO: make decision from config or supply by argument
$s->bind_param("sssssss", $login, $email, $pwd_hash, $salt, $avatar_path, $role, $invite_id);
if ($s->execute() === false)
@ -40,7 +42,7 @@ function User_Create (string $login, string $password, ?string $email = null, ?s
* Create new user account
*/
function User_Create_Method (array $req): ReturnT {
global $db, $Config, $LOGGED_IN;
global $Config, $LOGGED_IN;
$login = null;
$password = null;

View File

@ -15,6 +15,8 @@ require_once("./index.php");
* Delete existing account
*/
function User_Delete (int $id): ReturnT {
global $db;
$s = $db->prepare("delete from users where id = ?");
$s->bind_param("s", $id);
@ -30,7 +32,7 @@ function User_Delete (int $id): ReturnT {
* Delete existing account
*/
function User_Delete_Method (array $req): ReturnT {
global $db, $LOGGED_IN, $THIS_USER;
global $LOGGED_IN, $THIS_USER;
$id = null;
@ -47,7 +49,7 @@ function User_Delete_Method (array $req): ReturnT {
}
// If its attempt to delete other account
if (!User_HasRole($THIS_USER, "admin").GetData() && $THIS_USER !== $id)
if (!User_HasRole($THIS_USER, "admin")->GetData() && $THIS_USER !== $id)
return new ReturnT(err_code: E_ACS_INSUFROLE, err_desc: "you must be admin to delete other accounts");
// Actions
@ -63,7 +65,7 @@ if (Utils_ThisFileIsRequested(__FILE__)) {
// HACK: for debugging purposes. Will be removed later
if ($Config["debug"])
$_POST = $_REQUEST;
$result = User_Delete_Method($_POST);
if ($result->IsError()) {

View File

@ -1,10 +1,17 @@
<?php // Viewing account data
// Includes
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/_types.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
}
@ -14,7 +21,7 @@ require_once("../_types.php");
* FUNCTION
* Check if user with supplied login exists
*/
function User_LoginExist ($login): bool {
function User_LoginExist (string $login): bool {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE login = ?");
@ -28,7 +35,7 @@ function User_LoginExist ($login): bool {
* FUNCTION
* Check if user with supplied ID exists
*/
function User_IDExist ($id): bool {
function User_IDExist (int $id): bool {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
@ -42,7 +49,7 @@ function User_IDExist ($id): bool {
* FUNCTION
* Check if user has specified role
*/
function User_HasRole ($id, $role): ReturnT {
function User_HasRole (int $id, string $role): ReturnT {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
@ -60,7 +67,7 @@ function User_HasRole ($id, $role): ReturnT {
* FUNCTION
* Check if user is moderator (or higher)
*/
function User_IsMod ($id): ReturnT {
function User_IsMod (int $id): ReturnT {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
@ -79,7 +86,7 @@ function User_IsMod ($id): ReturnT {
* Get user information from DB
*/
function User_GetInfoByID (int $id): ReturnT {
global $THIS_USER;
global $db, $THIS_USER;
$result = array();
@ -97,7 +104,7 @@ 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
if (($id === $THIS_USER) || User_IsMod($THIS_USER)->GetData()) { // User himself and mods can see additional info
$result["email"] = $d["email"];
$result["invite_id"] = $d["invite_id"];
}
@ -114,7 +121,7 @@ function User_GetInfoByID (int $id): ReturnT {
* Get user information from DB
*/
function User_GetInfoByID_Method (array $req): ReturnT {
global $db, $THIS_USER, $LOGGED_IN;
global $THIS_USER, $LOGGED_IN;
// Input sanity checks
@ -127,7 +134,7 @@ function User_GetInfoByID_Method (array $req): ReturnT {
if ($LOGGED_IN)
$UserID = $THIS_USER;
else
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "id must be specified or valid session must be provided");
return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "id must be specified or valid session must be provided");
}
// Actions

View File

@ -4,10 +4,14 @@
We are using MariaDB, but any MySQL-compatible database should be enough. There are instructions how to setup it for using with E949.
1. Login to your SQL database with admin account:
```bash
mysql -u root -p
```
2. Setup new user and database:
```mysql
CREATE USER e949@localhost IDENTIFIED BY 'password';
CREATE DATABASE e949 CHARACTER SET = 'utf8';
@ -16,10 +20,14 @@ FLUSH PRIVILEGES;
EXIT
```
3. Login with new account:
```bash
mysql -u e949 -p
```
4. Create tables:
```mysql
USE e949;
CREATE TABLE users (
@ -72,4 +80,6 @@ CREATE TABLE invites (
CREATE TABLE approved_tags (
value VARCHAR(255) NOT NULL COMMENT 'The tag itself'
);
```
```
5. Profit!!!

24
front/counter.php Normal file
View File

@ -0,0 +1,24 @@
<?php
// Main page posts counter
if (!$IS_FRONTEND) {
http_response_code(500);
die();
}
$totalPostsAmount = Post_GetPostsAmount();
$totalPostsAmount = strval($totalPostsAmount);
?>
<div class="nibbabox notsearchbox">
<?php
$allNumbers = array();
for ($i = 0; $i < strlen($totalPostsAmount); ++$i)
$allNumbers[] = "<img src=\"front/images/counter/" . $totalPostsAmount[$i] . ".png\">";
while (count($allNumbers) < 7)
$allNumbers[] = "<img src=\"front/images/counter/0.png\">";
foreach ($allNumbers as $numberImg)
echo $numberImg;
?>
</div>

3
front/favicon.html Normal file
View File

@ -0,0 +1,3 @@
<link rel="shortcut icon" href="front/images/favicon.ico" type="image/x-icon">
<link rel="icon" type="image/png" sizes="32x32" href="front/images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="front/images/favicon-16x16.png">

16
front/head.php Normal file
View File

@ -0,0 +1,16 @@
<meta charset="UTF-8">
<?php
// <head> ... </head>
if (!isset($PAGE_TITLE)) {
http_response_code(500);
die("\$PAGE_TITLE not set");
}
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/default.css">

42
front/searchbox.php Normal file
View File

@ -0,0 +1,42 @@
<?php
// Main page search box
if (!$IS_FRONTEND) {
http_response_code(500);
die();
}
?>
<div class="nibbabox searchbox">
<h1 class="title"><a href=".">E949</a></h1>
<div class="nav">
<?php
// If user is logged in
if ($LOGGED_IN) {
$res = User_GetInfoByID($THIS_USER);
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>";
} 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>
<?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="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="text" name="tags" id="tags" value="" size="36" autofocus="autofocus" autocomplete="on"><br>
<input type="submit" value="Search">
<!-- TODO: JS
<input type="button" value="Show random meme" id="random-meme">
-->
</form>
</div>
</div>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 807 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B

View File

@ -1,5 +1,7 @@
body, div, h1, h2, h3, h4, h5, h6, p, ul, li, dd, dt {
font-family: Verdana, Sans-Serif;
body {
background-color: #094e59;
background-image: url("../images/bg_pattern_peace.png");
background-repeat: repeat;
}
body, div, p, a {
@ -7,37 +9,11 @@ body, div, p, a {
margin: 0;
}
h1, h2, h3, h4, h5, h6, p, a {
color: #00c07c;
text-decoration: none;
}
a {
color: #009049;
}
body {
background-color: #094e59;
background-image: url("./bg_pattern_peace.png");
background-repeat: repeat;
}
div.wrapper {
text-align: center;
margin: auto;
}
/*div.wrapper img {
width: 60%;
height: 60%;
}*/
h1.title {
font-size: 4em;
padding: 0;
margin: 0;
}
div.nav {
margin-bottom: 0.25rem;
}
@ -63,6 +39,30 @@ div.notsearchbox {
font-size: 80%;
}
h1, h2, h3, h4, h5, h6, p, ul, li, dd, dt {
font-family: Verdana, Sans-Serif;
}
h1, h2, h3, h4, h5, h6, p, a {
color: #00c07c;
text-decoration: none;
}
a {
color: #009049;
}
div.nav a.useraccount {
color: orange;
text-decoration: underline;
}
h1.title {
font-size: 4em;
padding: 0;
margin: 0;
}
@media only screen and (max-height: 600px) {
div.wrapper {
top: 25vh;

View File

@ -1,60 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>E949: Index</title>
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon">
<link rel="icon" type="image/png" sizes="32x32" href="./favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./favicon-16x16.png">
<!-- <meta name="theme-color" content="#00549e"> -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--TODO-->
<link rel="stylesheet" href="./front/styles/default.css">
<!-- <script src="./front/scripts/some_script.js" type="text/javascript" integrity="1234"></script> -->
</head>
<body>
<div class="wrapper">
<div class="nibbabox notsearchbox">
<img src="./test.png">
</div>
<div class="nibbabox searchbox">
<h1 class="title"><a href="./">E949</a></h1>
<div class="nav">
<a title="Login in existing account" href="./account/login.php">Login</a>
<a title="Create new account" href="./account/create.php">Signup</a>
<!-- <a title="Account page" href="./account/">Username123</a> -->
<a title="A paginated list of every post" href="./posts/">Posts</a>
<a title="A paginated list of every tag" href="./tags/">Tags</a>
<a title="Statistics of current instance" href="./stats.php">Statistics</a>
<a title="A site map" href="./site_map.php">Site map</a>
</div>
<div>
<form action="./posts" accept-charset="UTF-8" method="get">
<input type="text" name="tags" id="tags" value="" size="30" autofocus="autofocus" data-autocomplete="tag-query"><br>
<input type="submit" value="Search">
<input type="button" value="Change Mascot" id="change-mascot">
</form>
</div>
</div>
<div class="nibbabox notsearchbox">
<img src="./images/counter/3.png">
<img src="./images/counter/4.png">
<img src="./images/counter/5.png">
<img src="./images/counter/1.png">
<img src="./images/counter/9.png">
<img src="./images/counter/5.png">
<img src="./images/counter/3.png">
</div>
<div class="nibbabox notsearchbox">
<p>
Serving 3,451,953 posts<br>
<a title="Takedown Information" href="./static/takedown">Takedown Policy and Process</a> |
<a title="Contact Us" href="./static/contact">Contact Us</a> |
<a title="Advertising with Us" href="./help/advertising">Advertising</a> |
<a title="Terms of Service" href="./static/terms_of_service">Terms of Service</a> |
<a title="Privacy Policy" href="./static/privacy">Privacy</a>
</p>
</div>
</div>
</body>
</html>

44
index.php Normal file
View File

@ -0,0 +1,44 @@
<?php
// Main page
$IS_FRONTEND = true;
// Includes
require_once("api/_auth.php");
require_once("api/user/index.php");
require_once("api/post/index.php");
$PAGE_TITLE = "Index"; // TODO
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php require_once("front/head.php"); ?>
</head>
<body>
<div class="wrapper">
<div class="nibbabox notsearchbox">
<img src="test.png">
</div>
<?php
require_once("front/searchbox.php");
require_once("front/counter.php");
?>
<div class="nibbabox notsearchbox">
<p>
Serving 3,451,953 posts<br>
<a title="Takedown Information" href="./static/takedown">Takedown Policy and Process</a> |
<a title="Contact Us" href="./static/contact">Contact Us</a> |
<a title="Advertising with Us" href="./help/advertising">Advertising</a> |
<a title="Terms of Service" href="./static/terms_of_service">Terms of Service</a> |
<a title="Privacy Policy" href="./static/privacy">Privacy</a>
</p>
</div>
</div>
</body>
</html>