I see Light of the Internet!

This commit is contained in:
Krzysztof 'KrzysiekSiemv' Smaga
2023-04-20 21:45:24 +02:00
commit 41fa19344b
814 changed files with 165027 additions and 0 deletions
@@ -0,0 +1,53 @@
<?php
namespace BlogEngine\Authentication {
require "vendor/blog-engine/php/Database/DatabaseController.php";
use BlogEngine\Database\DatabaseController;
class AuthController extends DatabaseController{
public function Authenticate($login, $password, $remember) : bool|string {
$status = false;
$srv = $this->Connection();
$check_auth = "SELECT login_token FROM users WHERE login = '$login' AND password = PASSWORD('$password');";
$res = mysqli_query($srv, $check_auth);
if(mysqli_num_rows($res) > 0){
if($row = mysqli_fetch_row($res)){
if($remember)
setcookie("uToken_BE", $row[0], time()+24*60*60*7, "/");
if($_SESSION['uToken_BE'] = $row[0])
$status = true;
else
$status = "Błąd przy dodawaniu do sesji! (Authentication\AuthController.php:18)";
} else
$status = mysqli_error($row);
} else
$status = "Nie ma takiego użytkownika";
$this->Close($srv);
return $status;
}
public function CheckRole($login_token) : string{
$srv = $this->Connection();
$check_role = "SELECT role FROM users WHERE login_token = '$login_token';";
$res = mysqli_query($srv, $check_role);
if(mysqli_num_rows($res) > 0){
if($row = mysqli_fetch_row($res)){
return $row[0];
} else
return mysqli_error($res);
} else
return "Nie ma użytkownika o takim tokenie";
$this->Close($srv);
}
public function NewUser($login, $password, $email, $role = 'user', $display_name = 'Użytkownik', $fname = '', $lname = '') : bool|string{
$status = false;
$srv = $this->Connection();
}
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace BlogEngine {
class Configuration {
public function SendData($data) {
$template = file_get_contents(__DIR__ . "/template.txt");
for($i = 0; $i < sizeof(array_keys($data)); $i++){
$key = array_keys($data)[$i];
$value = $data[$key];
$template = str_replace("{{$key}}", $value, $template);
}
$this->CreateConfigFile($template);
}
public function CreateConfigFile($data){
$config = fopen("_config.php", "w");
fwrite($config, $data);
fclose($config);
}
}
}
+198
View File
@@ -0,0 +1,198 @@
<?php
namespace BlogEngine\Database {
use BlogEngine\Configuration;
use BlogEngine\Database\Tables;
use PHPMailer\PHPMailer\PHPMailer;
class DatabaseController {
public function Connection(){
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Jeżeli połączenie nie przejdzie pomyślnie, wyrzuć błąd
if(!$conn){
error_log("Błąd połączenia z bazą: " . mysqli_connect_error());
}
return $conn;
}
public function Close($conn) { mysqli_close($conn); }
function Insert($into, $values){
$conn = $this->Connection();
mysqli_query($conn, "INSERT INTO {$into} VALUES({$values})");
$this->Close($conn);
}
function CreateTable($name, $columns){
$conn = $this->Connection();
if(mysqli_query($conn, "CREATE TABLE IF NOT EXISTS {$name}({$columns});")){
} else {
echo "Utworzenie tabeli {$name} nie przeszło pomyślnie!";
}
$this->Close($conn);
}
function CreateDatabase(){
$srv = $this->Connection();
// Ładowanie struktury bazy
if($configs = scandir(__DIR__."/Tables")){
if(sizeof($configs) > 0){
foreach($configs as $config){
if(pathinfo($config, PATHINFO_EXTENSION) == "json"){
$tables = array();
$table_content = file_get_contents(__DIR__."/Tables/" . $config);
$structure = json_decode($table_content, true);
foreach($structure as $name=>$table){
$query = "";
foreach ($table as $column){
$column_data = [
"column_name" => "", "datatype" => "", "length" => 0, "enum_data" => [],
"primary_key" => false, "unique" => false,
"auto_increment" => false, "not_null" => false, "default" => ["value" => "", "is_string" => false], "foreign_key" => ["references" => "", "column" => "", "column_name" => ""]
];
if(array_key_exists("column_name", $column))
$column_data['column_name'] = $column['column_name'];
if(array_key_exists("datatype", $column))
$column_data['datatype'] = $column['datatype'];
if(array_key_exists("length", $column))
$column_data['length'] = $column['length'];
if(array_key_exists("enum_data", $column))
$column_data['enum_data'] = $column['enum_data'];
if(array_key_exists("primary_key", $column))
$column_data['primary_key'] = $column['primary_key'];
if(array_key_exists("foreign_key", $column)){
$column_data['foreign_key']['references'] = $column['foreign_key']['references'];
$column_data['foreign_key']['column'] = $column['foreign_key']['column'];
$column_data['foreign_key']['column_name'] = $column['column_name'];
}
if(array_key_exists("auto_increment", $column))
$column_data['auto_increment'] = $column['auto_increment'];
if(array_key_exists("not_null", $column))
$column_data['not_null'] = $column['not_null'];
if(array_key_exists("unique", $column))
$column_data['unique'] = $column['unique'];
if(array_key_exists("default", $column)) {
$column_data['default']['value'] = $column['default']['value'];
$column_data['default']['is_string'] = $column['default']['is_string'];
}
foreach ($column_data as $key=>$new_column){
if($key == "foreign_key") {
if($new_column['references'] != "" && $new_column['column'] != "")
$query .= ", FOREIGN KEY ({$new_column['column_name']}) REFERENCES {$new_column['references']}({$new_column['column']}) ";
} else if($key == "default"){
if($new_column['value'] != "") {
if ($new_column['is_string'])
$query .= "DEFAULT \"{$new_column['value']}\" ";
else
$query .= "DEFAULT {$new_column['value']} ";
}
} else {
if(gettype($new_column) == "string"){
if($new_column != ""){
$query .= $new_column . " ";
}
} else if(gettype($new_column) == "integer"){
if($new_column != 0){
$query .= "({$new_column}) ";
}
} else if(gettype($new_column) == "boolean"){
if($new_column){
$query .= strtoupper(($key != "auto_increment"?str_replace("_", " ", $key):$key)) . " ";
}
} else if(gettype($new_column) == "array"){
if(sizeof($new_column) > 0){
$elements = "";
foreach ($new_column as $element){
$elements .= "'{$element}', ";
}
$elements = substr($elements, 0, strlen($elements) - 2);
$query .= "({$elements}) ";
}
}
}
}
$query .= ", ";
}
$query = substr($query, 0, strlen($query) - 3);
array_push($tables, [
"Nazwa" => $name,
"Struktura" => $query
]);
}
// Dodawanie do bazy tabel
foreach ($tables as $table){
$this->CreateTable($table['Nazwa'], $table['Struktura']);
}
// Dodawanie do tabeli "users" administratora
$this->Insert("users", "1, NULL, '', NULL, '{$_POST['ADMIN_USER']}', '{$_POST['BLOG_AUTHOR']}', PASSWORD('{$_POST['ADMIN_PASS']}'), '{$_POST['ADMIN_EMAIL']}', 'administrator', UUID(), NOW(), NULL");
// Dodawanie do bazy domyślnych danych
if($data = scandir(__DIR__ . "/Values")) {
if(sizeof($data) > 0){
foreach ($data as $datum){
if(pathinfo($datum, PATHINFO_EXTENSION) == "json"){
$value_content = file_get_contents(__DIR__ . "/Values/" . $datum);
$value_structure = json_decode($value_content, true);
//print_r($value_structure);
$i = 0;
foreach ($value_structure as $table) {
foreach ($table as $row=>$columns){
$to_table = array_keys($value_structure)[$i];
$insert_columns = "";
$insert_values = "";
foreach ($columns as $column=>$value){
$insert_columns .= $column . ", ";
if($value == "NULL")
$insert_values .= "NULL, ";
else if($value == "NOW()")
$insert_values .= "NOW(), ";
else if(is_string($value))
$insert_values .= "\"$value\", ";
else if(is_int($value) || is_float($value) || is_double($value))
$insert_values .= "$value, ";
else if(is_bool($value)){
if($value)
$insert_values .= "true, ";
else
$insert_values .= "false, ";
}
}
$insert_columns = trim($insert_columns, ", ");
$insert_values = trim($insert_values, ", ");
$query = "INSERT INTO $to_table($insert_columns) VALUES ($insert_values);";
mysqli_query($srv, $query);
}
$i++;
}
}
}
}
}
}
}
} else {
error_log("Folder \"Tables\" jest pusty. Zatrzymywanie tworzenia bazy!");
}
} else {
error_log("Nie ma dostępnego folderu \"Tables\". Zatrzymywanie tworzenia bazy!");
}
}
}
}
+88
View File
@@ -0,0 +1,88 @@
{
"table_name": [
{
"column_name": "NAZWA TABELI", // WYMAGANE!
"datatype": "TYP KOLUMNY", // WYMAGANE!
"length": DŁUGOŚĆ/WIELKOŚĆ WARTOŚCI (0-255), // WYMAGANE DLA DANYCH TYPÓW
"enum_data": [ZBIÓR DANYCH (np. "Jabłko", "Kiwi")], // WYMAGANE DLA ENUM/SET
"primary_key": true/false, // Czy jest to klucz podstawowy
"unique": true/false, // Czy musi być unikalne
"foreign_key": {
"references": "TABELA",
"column": "KOLUMNA Z KTÓREJ POBIERA INDEKS"
},
"auto_increment": true/false, // Czy ma dawać wartości liczbowe rosnące
"not_null": true/false, // Czy kolumna nie może być pusta
"default": {
"value": "DOMYŚLNIE" // Wartość domyślna dla kolumny
"is_string": true/false // WYMAGANE GDY DODAWANE DEFAULT! Czy wartość domyślna jest Stringiem czy czymś innym
}
}
]
// Przykład na dwóch tabelach z relacją 1..n
"users": [
{
"column_name": "id_user",
"datatype": "INT",
"length": 10,
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "name",
"datatype": "VARCHAR",
"length": 24,
"not_null": true
},
{
"column_name": "lastName",
"datatype": "VARCHAR",
"length": 48
},
{
"column_name": "description",
"datatype": "TEXT"
},
{
"column_name": "sex",
"datatype": "ENUM",
"enum_data": ['male', 'female'];
}
],
"posts": [
{
"column_name": "id_post",
"datatype": "INT",
"length": 10,
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "id_user",
"datatype": "INT",
"length": 10,
"foreign_key": {
"references": "users",
"column": "id_user"
},
"not_null": true
},
{
"column_name": "added_at",
"datatype": "DATETIME",
"not_null": true,
"default": {
"value": "NOW()",
"is_string": false
}
},
{
"column_name": "content",
"datatype": "TEXT",
"not_null": true
}
]
}
+423
View File
@@ -0,0 +1,423 @@
{
"users": [
{
"column_name": "id_user",
"datatype": "INT",
"length": 11,
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "avatar_link",
"datatype": "VARCHAR",
"length": 96
},
{
"column_name": "fname",
"datatype": "VARCHAR",
"length": 28,
"not_null": true
},
{
"column_name": "lname",
"datatype": "VARCHAR",
"length": 40
},
{
"column_name": "login",
"datatype": "VARCHAR",
"length": 32,
"not_null": true
},
{
"column_name": "display_name",
"datatype": "VARCHAR",
"length": 32,
"not_null": true
},
{
"column_name": "password",
"datatype": "TEXT",
"not_null": true
},
{
"column_name": "email",
"datatype": "VARCHAR",
"length": 96,
"not_null": true
},
{
"column_name": "role",
"datatype": "ENUM",
"enum_data": ["administrator", "moderator", "user"],
"not_null": true,
"default": {
"value": "user",
"is_string": true
}
},
{
"column_name": "login_token",
"datatype": "TEXT",
"unique": true,
"not_null": true,
"default": {
"value": "UUID()",
"is_string": false
}
},
{
"column_name": "created_at",
"datatype": "DATETIME",
"length": 0,
"not_null": true,
"default": {
"value": "NOW()",
"is_string": false
}
},
{
"column_name": "modified_at",
"datatype": "DATETIME"
}
],
"posts": [
{
"column_name": "id_post",
"datatype": "INT",
"length": 11,
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "id_user",
"datatype": "INT",
"length": 11,
"foreign_key": {
"references": "users",
"column": "id_user"
},
"not_null": true
},
{
"column_name": "name",
"datatype": "VARCHAR",
"length": 60,
"not_null": true
},
{
"column_name": "title",
"datatype": "VARCHAR",
"length": 60,
"not_null": true
},
{
"column_name": "content",
"datatype": "LONGTEXT",
"not_null": true
},
{
"column_name": "status",
"datatype": "ENUM",
"enum_data": ["public", "draft"],
"not_null": true,
"default": {
"value": "draft",
"is_string": true
}
},
{
"column_name": "comments",
"datatype": "ENUM",
"enum_data": ["open", "registered", "closed"],
"not_null": true,
"default": {
"value": "open",
"is_string": true
}
},
{
"column_name": "views",
"datatype": "INT",
"length": 11,
"not_null": true,
"default": {
"value": 0,
"is_string": false
}
},
{
"column_name": "added_at",
"datatype": "DATETIME",
"not_null": true,
"default": {
"value": "NOW()",
"is_string": false
}
},
{
"column_name": "updated_at",
"datatype": "DATETIME"
}
],
"comments": [
{
"column_name": "id_comment",
"datatype": "INT",
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "id_post",
"datatype": "INT",
"foreign_key": {
"references": "posts",
"column": "id_post"
},
"not_null": true
},
{
"column_name": "id_author",
"datatype": "INT",
"foreign_key": {
"references": "users",
"column": "id_user"
}
},
{
"column_name": "author",
"datatype": "VARCHAR",
"length": 32
},
{
"column_name": "content",
"datatype": "TEXT",
"not_null": true
},
{
"column_name": "author_ip",
"datatype": "VARCHAR",
"length": 15,
"not_null": true
},
{
"column_name": "added_at",
"datatype": "DATETIME",
"not_null": true,
"default": {
"value": "NOW()",
"is_string": false
}
}
],
"tags": [
{
"column_name": "id_tag",
"datatype": "INT",
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "show_name",
"datatype": "VARCHAR",
"length": 32,
"not_null": true
},
{
"column_name": "slug",
"datatype": "VARCHAR",
"length": 32,
"not_null": true
}
],
"options": [
{
"column_name": "id_option",
"datatype": "INT",
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "name",
"datatype": "VARCHAR",
"length": 48,
"not_null": true
},
{
"column_name": "value",
"datatype": "TEXT",
"not_null": true
},
{
"column_name": "autoload",
"datatype": "TINYINT",
"length": 1,
"not_null": true,
"default": {
"value": 1,
"is_string": false
}
}
],
"meta_users": [
{
"column_name": "id_umeta",
"datatype": "INT",
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "id_user",
"datatype": "INT",
"foreign_key": {
"references": "users",
"column": "id_user"
},
"not_null": true
},
{
"column_name": "name",
"datatype": "VARCHAR",
"length": 48,
"not_null": true
},
{
"column_name": "value",
"datatype": "TEXT",
"not_null": true
}
],
"meta_posts": [
{
"column_name": "id_pmeta",
"datatype": "INT",
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "id_post",
"datatype": "INT",
"foreign_key": {
"references": "posts",
"column": "id_post"
},
"not_null": true
},
{
"column_name": "name",
"datatype": "VARCHAR",
"length": 48,
"not_null": true
},
{
"column_name": "value",
"datatype": "TEXT",
"not_null": true
}
],
"meta_comments": [
{
"column_name": "id_cmeta",
"datatype": "INT",
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "id_comment",
"datatype": "INT",
"foreign_key": {
"references": "comments",
"column": "id_comment"
},
"not_null": true
},
{
"column_name": "name",
"datatype": "VARCHAR",
"length": 48,
"not_null": true
},
{
"column_name": "value",
"datatype": "TEXT",
"not_null": true
}
],
"meta_tags": [
{
"column_name": "id_tmeta",
"datatype": "INT",
"primary_key": true,
"auto_increment": true,
"not_null": true
},
{
"column_name": "id_tag",
"datatype": "INT",
"foreign_key": {
"references": "tags",
"column": "id_tag"
},
"not_null": true
},
{
"column_name": "name",
"datatype": "VARCHAR",
"length": 48,
"not_null": true
},
{
"column_name": "value",
"datatype": "TEXT",
"not_null": true
}
],
"tag_to_post": [
{
"column_name": "id_tag",
"datatype": "INT",
"foreign_key": {
"references": "tags",
"column": "id_tag"
},
"not_null": true
},
{
"column_name": "id_post",
"datatype": "INT",
"foreign_key": {
"references": "posts",
"column": "id_post"
},
"not_null": true
}
],
"statistics": [
{
"column_name": "count_date",
"datatype": "DATE",
"not_null": true,
"default": {
"value": "NOW()",
"is_string": false
}
},
{
"column_name": "views",
"datatype": "INT",
"not_null": false,
"default": {
"value": 0,
"is_string": false
}
}
]
}
+39
View File
@@ -0,0 +1,39 @@
{
"posts": [
{
"id_post": "NULL",
"id_user": 1,
"name": "hello-world",
"title": "Witaj świecie!",
"content": "Jestem pierwszym postem na nowo postawionym blogu! Ciesze się, że mogę tutaj być!",
"status": "public",
"comments": "open",
"views": 0,
"added_at": "NOW()",
"updated_at": "NULL"
},
{
"id_post": "NULL",
"id_user": 1,
"name": "lipsum",
"title": "Lorem Ipsum!",
"content": "Drugi post, zawierający komentarze",
"status": "public",
"comments": "open",
"views": 0,
"added_at": "NOW()",
"updated_at": "NULL"
}
],
"comments": [
{
"id_comment": "NULL",
"id_post": 2,
"id_author": 1,
"content": "Jestem komentarzem! :D",
"author": "Administrator",
"author_ip": "127.0.0.1",
"added_at": "NOW()"
}
]
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace BlogEngine {
class Dictionary {
public $page_tags = [
"posts" => "{POSTS}",
"links" => "{LINKS}",
"tags" => "{TAGS}",
"title" => "{TITLE}",
"author" => "{AUTHOR}",
"description" => "{DESCRIPTION}"
];
}
}
+15
View File
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Witaj świecie!</title>
<link rel="stylesheet" href="vendor/blog-engine/blog.css"/>
<script type="module" src="vendor/blog-engine/blog.js" defer></script>
</head>
<body>
<h1>Witaj świecie!</h1>
<form method="POST" action="">
</form>
</body>
</html>
+26
View File
@@ -0,0 +1,26 @@
<?php
$display_name = "";
$conn = $auth->Connection();
$res = mysqli_query($conn, "SELECT display_name FROM users WHERE login_token = '{$_SESSION['uToken_BE']}';") ;
while($row = mysqli_fetch_row($res)){
$display_name = $row[0];
}
?>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container-fluid">
<a href="#" class="navbar-brand">Witaj, <?php echo $display_name ?></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="#">Wejdź do panelu</a></li>
<li class="nav-item"><a class="nav-link" href="#">Dodaj nowy wpis</a></li>
<li class="nav-item"><a class="nav-link" href="#">Moderuj komentarze</a></li>
<li class="nav-item"><a class="nav-link" href="#">Zobacz statystyki</a></li>
<li class="nav-item"><a class="nav-link" href="#">Wyloguj z panelu</a> </li>
</ul>
</div>
</div>
</nav>
+77
View File
@@ -0,0 +1,77 @@
<?php
/*
* KONFIGURACJA BLOGA
*/
// Czy blog przeszedł już pierwsze uruchomienie
const BLOG_RAN = true;
// Tytuł bloga
const BLOG_TITLE = "{BLOG_TITLE}";
// Opis bloga
const BLOG_DESC = "{BLOG_DESC}";
// Autor bloga/Nazwa wyświetlana dla głównego administratora bloga
const BLOG_AUTHOR = "{BLOG_AUTHOR}";
// Tagi bloga
const BLOG_TAGS = "{BLOG_TAGS}";
// Domena bloga
const BLOG_DOMAIN = "{BLOG_DOMAIN}";
// Ikona bloga (format .PNG)
const BLOG_ICON = "{BLOG_ICON}";
// Nazwa pliku strony głównej
const BLOG_INDEX = "{BLOG_INDEX}";
/*
* KONFIGURACJA BAZY DANYCH
*/
// Adres IP do serwera bazy danych MySQL/MariaDB
const DB_HOST = "{DB_HOST}";
// Nazwa użytkownika do serwera bazy danych MySQL/MariaDB
const DB_USER = "{DB_USER}";
// Hasło do użytkownika serwera bazy danych MySQL/MariaDB
const DB_PASS = "{DB_PASS}";
// Baza danych dla bloga
const DB_NAME = "{DB_NAME}";
/*
* KONFIGURACJA KONTA ADMINISTRATORA
*/
// Nazwa użytkownika dla głównego administratora bloga
const ADMIN_USER = "{ADMIN_USER}";
// Adres mailowy kontaktowy do głównego administratora bloga
const ADMIN_EMAIL = "{ADMIN_EMAIL}";
// Hasło dla głównego administratora bloga
const ADMIN_PASS = "{ADMIN_PASS}";
/*
* KONFIGURACJA POCZTY, DLA NEWSLETTERÓW
*/
// Adres e-mail, z którego będą wysyłane wiadomości mailowe
const MAIL_NAME = "{MAIL_NAME}";
// Hasło do poczty
const MAIL_PASS = "{MAIL_PASS}";
// Serwer wychodzący poczty e-mail
const MAIL_SRV = "{MAIL_SRV}";
// Port do serwera wychodzącego poczty e-mail
const MAIL_PORT = "{MAIL_PORT}";
// Wymaga SSL'a
const MAIL_SSL = {MAIL_SSL};