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
+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()"
}
]
}