Skip to content

Commit 1b09d11

Browse files
Use Namespaces to make project clear.
1 parent 1730c07 commit 1b09d11

29 files changed

+264
-76
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,5 @@ includes/db.php
150150
.idea/php-blog.iml
151151
.idea/sqldialects.xml
152152
.idea/vcs.xml
153+
154+
/vendor/

.idea/php-blog.iml

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

admin/include/totalCalculator.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
2-
require_once __DIR__ . "/../../class/Database.php";
2+
3+
use Database\Mysql;
4+
5+
require_once __DIR__ . "/../../vendor/autoload.php";
6+
37
// Create database connection
4-
$db = new Database();
8+
$db = new Mysql();
59
// Send request to database
610
$db->query("SELECT `id` FROM `posts`");
711
$db->execute();

admin/include/users/getUsers.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2-
require_once(__DIR__ . "/../../../class/Database.php");
2+
3+
use Database\Mysql;
4+
5+
require_once(__DIR__ . "/../../../vendor/autoload.php");
36

47
// Create database connection
5-
$db = new Database();
8+
$db = new Mysql();
69
// Send query to database
710
$db->query("SELECT `id`, `username`, `email` FROM `users`");
811
$db->execute();

admin/posts.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
2-
require_once __DIR__ . "/../class/Post.php";
3-
require_once __DIR__ . "/class/Posts.php";
42

5-
$posts = new Post();
6-
$postManager = new Posts();
3+
use Post\{Fetch, Management};
4+
5+
require_once __DIR__ . "/../vendor/autoload.php";
6+
7+
$posts = new Fetch();
8+
$postManager = new Management();
79
?>
810
<!DOCTYPE html>
911
<html lang="en">

admin/posts/delete.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
2-
require_once __DIR__ . "/../class/Posts.php";
32

4-
$postManager = new Posts();
3+
use Post\Management as PostManagement;
4+
5+
require_once __DIR__ . "/../../vendor/autoload.php";
6+
7+
$postManager = new PostManagement();
58

69
if (isset($_GET["id"])) {
710
$postId = $_GET["id"];

admin/posts/edit.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2-
require_once __DIR__ . "/../class/Posts.php";
3-
require_once(__DIR__ . "/../include/posts/editPost.php");
42

5-
$postManager = new Posts();
3+
use Post\Management as PostManagement;
4+
5+
require_once __DIR__ . "/../../vendor/autoload.php";
6+
7+
8+
$postManager = new PostManagement();
69
if (isset($_GET["id"])) {
710
$postId = $_GET["id"];
811
$postManager->getPost($postId);

admin/posts/new.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
2-
require_once __DIR__ . "/../class/Posts.php";
32

4-
$postManager = new Posts();
3+
use Post\Management as PostManagement;
4+
5+
require_once __DIR__ . "/../../vendor/autoload.php";
6+
7+
$postManager = new PostManagement();
58

69
if (isset($_POST["save"])) {
710
$postTitle = trim($_POST["postName"]);

admin/settings.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
2-
require_once __DIR__ . "/class/Settings.php";
2+
3+
use Blog\Settings;
4+
5+
require_once __DIR__ . "/../vendor/autoload.php";
36
$settings = new Settings();
47
if (isset($_POST["update"])) {
58
$blogTitle = $_POST["blogName"];

admin/sidebar.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
2-
require_once __DIR__ . "/../class/Permission.php";
3-
$permission = new Permission();
2+
3+
use Permission\AdminPermission;
4+
5+
require_once __DIR__ . "/../vendor/autoload.php";
6+
7+
$permission = new AdminPermission();
48
$permission->permissionAdmin();
59
?>
610
<!-- Sidebar -->

admin/users/addUser.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
2-
require_once __DIR__ . "/../../class/Account/Register.php";
2+
3+
use Account\Register;
4+
5+
require_once __DIR__ . "/../../vendor/autoload.php";
36

47
if (isset($_POST["register"])) {
58
$username = $_POST["username"];

admin/users/deleteUser.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?php
2-
require_once(__DIR__ . "/../../class/Database.php");
2+
3+
use Database\Mysql;
4+
5+
require_once(__DIR__ . "/../../vendor/autoload.php");
6+
37
// Check the request method
48
if ($_SERVER["REQUEST_METHOD"] == "GET") {
59
// Create database connection
6-
$db = new Database();
10+
$db = new Mysql();
711
// Store the needed data
812
$userID = intval($_GET["id"]);
913
// Send the delete request to database

class/Account/Login.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
2-
require_once __DIR__ . "/../Database.php";
2+
3+
namespace Account;
4+
5+
require_once __DIR__ . "/../../vendor/autoload.php";
6+
7+
use Database\Mysql;
8+
use PDO;
39

410
/**
511
* Class Login
@@ -58,7 +64,7 @@ private function verifyStandard()
5864
private function verifyPassword()
5965
{
6066
// Create database connection
61-
$db = new Database();
67+
$db = new Mysql();
6268
// Send query
6369
$db->query("SELECT `id`, `password`, `is_admin` FROM `users` WHERE username=:username");
6470
$db->bind(":username", $this->username, PDO::PARAM_STR);

class/Account/Register.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
2-
require_once __DIR__ . "/../Database.php";
2+
3+
namespace Account;
4+
5+
use Database\Mysql;
6+
use PDO;
7+
8+
require_once __DIR__ . "/../../vendor/autoload.php";
39

410
/**
511
* Class Register
@@ -108,7 +114,7 @@ private function verifyStandard()
108114
private function verifyDatabase()
109115
{
110116
// Create database connection
111-
$db = new Database();
117+
$db = new Mysql();
112118
// Send query to database
113119
$db->query("SELECT * FROM `users` WHERE username=:username");
114120
$db->bind(":username", $this->username, PDO::PARAM_STR);
@@ -128,7 +134,7 @@ private function verifyDatabase()
128134
private function registerUserInDatabase(): bool
129135
{
130136
// Create database connection
131-
$db = new Database();
137+
$db = new Mysql();
132138
// Send query to database
133139
$db->query("INSERT INTO `users`(`username`, `password`) VALUES (:username, :password)");
134140
$db->bind(":username", $this->username, PDO::PARAM_STR);

class/Blog.php renamed to class/Blog/Info.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<?php
2-
require_once __DIR__ . "/Database.php";
2+
3+
namespace Blog;
4+
5+
use Database\Mysql;
6+
7+
require_once __DIR__ . "/../../vendor/autoload.php";
38

49
/**
510
* Class Blog
611
*/
7-
class Blog
12+
class Info
813
{
914
public $blogTitle;
1015
public $blogAuthor;
1116
public $blogAuthorInfo;
1217

1318
public function __construct()
1419
{
15-
$db = new Database();
20+
$db = new Mysql();
1621
$db->query("SELECT `blogTitle`, `blogAuthor`, `blogAuthorInfo` FROM `settings`");
1722
$db->execute();
1823
$result = $db->fetch();

admin/class/Settings.php renamed to class/Blog/Settings.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
2-
require_once __DIR__ . "/../../class/Database.php";
2+
3+
namespace Blog;
4+
5+
use Database\Mysql;
6+
use PDO;
7+
8+
require_once __DIR__ . "/../../vendor/autoload.php";
39

410
/**
511
* Class Settings
@@ -18,7 +24,7 @@ class Settings
1824
public function __construct()
1925
{
2026
// Create database connection
21-
$this->db = new Database();
27+
$this->db = new Mysql();
2228
// Get blog settings from database
2329
$this->db->query("SELECT `blogTitle`,`blogAuthor`,`blogAuthorInfo` FROM `settings`");
2430
$this->db->execute();

class/Database.php renamed to class/Database/Mysql.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<?php
22

3+
namespace Database;
4+
5+
use PDO;
6+
use PDOException;
7+
38
/**
49
* Class Database
510
*/
6-
class Database
11+
class Mysql
712
{
813
private const DB_HOST = "localhost";
914
private const DB_NAME = "blog";
@@ -93,4 +98,4 @@ public function __destruct()
9398
$this->stmt = null;
9499
}
95100
}
96-
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3+
namespace Permission;
4+
35
/**
4-
* Class Permission
6+
* Class Admin Permission
57
*/
6-
class Permission
8+
class AdminPermission
79
{
810
public function __construct()
911
{
@@ -20,15 +22,4 @@ public function permissionAdmin()
2022
die();
2123
}
2224
}
23-
24-
/**
25-
* User panel permission manager
26-
*/
27-
public function permissionUser()
28-
{
29-
if ($_SESSION["isLogged"]) {
30-
header("location: /");
31-
die();
32-
}
33-
}
3425
}

class/Permission/UserPermission.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Permission;
4+
5+
/**
6+
* Class User Permission
7+
*/
8+
class UserPermission
9+
{
10+
private bool $isLogged;
11+
12+
public function __construct()
13+
{
14+
session_start();
15+
if (isset($_SESSION["isLogged"])) {
16+
$this->isLogged = $_SESSION["isLogged"];
17+
} else {
18+
$this->isLogged = false;
19+
}
20+
}
21+
22+
/**
23+
* User panel permission manager
24+
*/
25+
public function permissionUser()
26+
{
27+
if ($this->isLogged) {
28+
header("location: /");
29+
die();
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)