From e895f6f1ff8ce8b3e0e2f3691947bc6e05a54407 Mon Sep 17 00:00:00 2001
From: Tan Nguyen
diff --git a/admin.php b/admin.php index ea8a87c..734adbe 100644 --- a/admin.php +++ b/admin.php @@ -1,5 +1,12 @@ load(); + session_start(); require_once('lib/model.php'); diff --git a/index.php b/index.php index 361a8ba..7fbc9d3 100644 --- a/index.php +++ b/index.php @@ -5,6 +5,13 @@ * Github: https://github.com/TanHongIT */ +use Dotenv\Dotenv; + +require __DIR__ . '/vendor/autoload.php'; + +$dotenv = Dotenv::createImmutable(__DIR__); +$dotenv->load(); + session_start(); require_once('lib/model.php'); diff --git a/lib/config/config.php b/lib/config/config.php index f67558c..a81668a 100644 --- a/lib/config/config.php +++ b/lib/config/config.php @@ -1,6 +1,7 @@ Date: Thu, 4 Jan 2024 03:09:54 +0700 Subject: [PATCH 2/4] fix: clean code and remove redundant code --- admin/models/feedbacks.php | 1 - admin/models/users.php | 3 -- content/controllers/category/index.php | 8 ++-- content/controllers/comment/index.php | 40 ++++++++----------- content/controllers/feedback/index.php | 2 + content/controllers/forgot-password/index.php | 4 +- .../controllers/forgot-password/request.php | 1 - .../result-change-password.php | 1 - content/controllers/register/index.php | 1 - content/controllers/register/resend.php | 1 - content/views/comment/index.php | 4 +- content/views/feedback/result.php | 4 +- 12 files changed, 33 insertions(+), 37 deletions(-) diff --git a/admin/models/feedbacks.php b/admin/models/feedbacks.php index e760c2f..1711d88 100644 --- a/admin/models/feedbacks.php +++ b/admin/models/feedbacks.php @@ -60,7 +60,6 @@ function updateFeedback(): void function feedbackReplyMail($html, $email) { //sendmail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $mail = new PHPMailer(true); diff --git a/admin/models/users.php b/admin/models/users.php index 9e7c2f4..55595eb 100644 --- a/admin/models/users.php +++ b/admin/models/users.php @@ -69,7 +69,6 @@ function changePassword($id, $newpassword, $currentPassword) ]; save('users', $options); //sendmail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $mail = new PHPMailer(true); $user = getRecord('users', $id); @@ -169,7 +168,6 @@ function user_update() $user_edited = getRecord('users', $userId); if ($user_edited['user_email'] != $currentEmail) { //send mail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $email = $user_edited['user_email']; $mail = new PHPMailer(true); @@ -271,7 +269,6 @@ function user_add() save('users', $user_add); } //send mail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $mail = new PHPMailer(true); diff --git a/content/controllers/category/index.php b/content/controllers/category/index.php index fdd8b2c..62c319b 100644 --- a/content/controllers/category/index.php +++ b/content/controllers/category/index.php @@ -1,14 +1,16 @@ 'id, subcategory_name', 'order_by' => 'id ASC', diff --git a/content/controllers/comment/index.php b/content/controllers/comment/index.php index bad6798..de29a9e 100644 --- a/content/controllers/comment/index.php +++ b/content/controllers/comment/index.php @@ -2,32 +2,26 @@ if (!empty($_POST)) { global $userNav; + + $commentData = [ + 'id' => 0, + 'product_id' => intval($_POST['product_id']), + 'email' => escape($_POST['email']), + 'author' => escape($_POST['author']), + 'content' => escape($_POST['content']), + 'createDate' => gmdate('Y-m-d H:i:s', time() + 7 * 3600), + 'user_id' => intval($_POST['user_id']), + ]; + if (isset($userNav)) { - $comment_add = [ - 'id' => 0, - 'product_id' => intval($_POST['product_id']), - 'email' => escape($_POST['email']), - 'author' => escape($_POST['author']), - 'content' => escape($_POST['content']), - 'createDate' => gmdate('Y-m-d H:i:s', time() + 7 * 3600), - 'user_id' => intval($_POST['user_id']), - 'link_image' => escape($_POST['link_image']), - ]; - } else { - $comment_add = [ - 'id' => 0, - 'product_id' => intval($_POST['product_id']), - 'email' => escape($_POST['email']), - 'author' => escape($_POST['author']), - 'content' => escape($_POST['content']), - 'createDate' => gmdate('Y-m-d H:i:s', time() + 7 * 3600), - 'user_id' => intval($_POST['user_id']), - ]; + $commentData['link_image'] = escape($_POST['link_image']); } - save('comments', $comment_add); + + save('comments', $commentData); echo "
"; } require('content/views/comment/index.php'); + // $input = json_decode(file_get_contents('php://input'), true); // $productID = $input['productID']; @@ -37,7 +31,7 @@ // $author = $input['author']; // $userID = $input['email']; -// $comment_add = array( +// $commentData = array( // 'id' => 0, // 'product_id' => intval($productID), // 'email' => escape($email), @@ -47,7 +41,7 @@ // 'user_id' => intval($userID), // 'link_image' => escape($link_image) // ); -// save('comments', $comment_add); +// save('comments', $commentData); // $option = array('product_id' => intval($productID)); diff --git a/content/controllers/feedback/index.php b/content/controllers/feedback/index.php index ac8776a..c27e9c9 100644 --- a/content/controllers/feedback/index.php +++ b/content/controllers/feedback/index.php @@ -4,11 +4,13 @@ if (!empty($_POST)) { feedback_add(); } + if (isset($_GET['product_id'])) { $productId = intval($_GET['product_id']); } else { $productId = 0; } + $product = getRecord('products', $productId); if (isset($userNav)) { $user_action = getRecord('users', $userNav); diff --git a/content/controllers/forgot-password/index.php b/content/controllers/forgot-password/index.php index afa687b..0613cde 100644 --- a/content/controllers/forgot-password/index.php +++ b/content/controllers/forgot-password/index.php @@ -1 +1,3 @@ - Date: Thu, 4 Jan 2024 09:39:26 +0700 Subject: [PATCH 3/4] docs: update readme and config --- .env.example | 8 ++-- .php-cs-fixer.dist.php | 1 + README.md | 82 ++++++++++++++++++++++++++++------------- lib/config/config.php | 4 +- lib/config/database.php | 2 +- 5 files changed, 65 insertions(+), 32 deletions(-) diff --git a/.env.example b/.env.example index c201d5d..b0f435e 100644 --- a/.env.example +++ b/.env.example @@ -1,12 +1,14 @@ +# if you don't want to set the value you can remove the line or comment it out with a hash (#) + PATH_URL=/ -PATH_URL_IMG=/public/upload/images/ -PATH_URL_IMG_PRODUCT=/public/upload/products/ +PATH_URL_IMG=public/upload/images/ +PATH_URL_IMG_PRODUCT=public/upload/products/ DB_HOST=db_server DB_PORT=3306 DB_USER=root DB_PASS=root -DB_NAME=chikoi +DB_NAME=new_mvc_shop_db SMTP_HOST=smtp.gmail.com SMTP_PORT=465 diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 58121af..bda1b6d 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,4 +1,5 @@ in([ diff --git a/README.md b/README.md index 27a9610..a8d72e8 100644 --- a/README.md +++ b/README.md @@ -59,37 +59,62 @@ Create a new database on **PHPMyAdmin** at your server, then import the .sql fil # 5. Request configuration -After a clone my repository to the local computer, you need to edit some code to be able to connect to the database and help the site works. +Clone the project to your computer: -### 5.1 Edit Config +```bash +git clone https://github.com/tanhongit/new-mvc-shop.git +``` -You need to change the path in the '**config.php**' file to match the location of this source code on your server and must match the domain you registered. +Run composer install: -Path: [`/lib/config/config.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config) +```bash +composer install +``` -```php - **Note:** +> +> The path of the config file that is using these environment variables is located at: [`/lib/config/config.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config/config.php) -```php -define('DB_HOST', 'localhost'); -define('DB_USER', 'root'); -define('DB_PASS', ''); -define('DB_NAME', 'chikoi'); +## 5.2 Edit Connect Database + +You need to change the connection information and import sql file to the database after you have cloned my repository so that the website can work. + +This is the path to the sql file for you to import to your database: +[`/admin/database/***.sql`](https://github.com/TanHongIT/new-mvc-shop/tree/master/admin/database) + +And change the connection information to match your database in .env file: + +```dotenv +DB_HOST=db_server +DB_PORT=3306 +DB_USER=root +DB_PASS=root +DB_NAME=chikoi ``` -### 5.3 Edit .htaccess +> **Note:** +> +> The path of the database config file that is using these environment variables is located at: [`/lib/config/database.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config/database.php) + +## 5.3 Edit .htaccess Change RewriteBase - Recommend the path that matches your host address. @@ -116,11 +141,13 @@ RewriteBase / > The third thing: You need to change the information about **SMTP Mail** to be able to use some functions about user account authentication, change passwords, notify users, ... -```php -define('SMTP_HOST','smtp.gmail.com'); -define('SMTP_PORT','465'); -define('SMTP_UNAME','add_your_mail'); -define('SMTP_PWORD','add_your_application_password_from_your_mail'); +Update the following information in the **.env** file: + +```dotenv +SMTP_HOST=smtp.gmail.com +SMTP_PORT=465 +SMTP_UNAME=add_your_mail +SMTP_PWORD=add_your_application_password_from_your_mail ``` Change the value of the constant **SMTP_UNAME** and **SMTP_PWORD** to match the configuration you added on your Gmail. @@ -129,7 +156,10 @@ Tips: https://support.google.com/accounts/answer/185833?hl=en **Where SMTP_PWORD is the application password for your _gmail.com_ account.** -Path: [`/lib/config/sendmail.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config) +> **Note:** +> +> The path of the email config file that is using these environment variables is located at: [`/lib/config/sendmail.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config/sendmail.php) + # 6. Demo diff --git a/lib/config/config.php b/lib/config/config.php index a81668a..b016cc6 100644 --- a/lib/config/config.php +++ b/lib/config/config.php @@ -2,6 +2,6 @@ define("PATH_URL", $_ENV['PATH_URL'] ?? '/'); -define("PATH_URL_IMG", $_ENV['PATH_URL_IMG'] ?? '/public/upload/images/'); +define("PATH_URL_IMG", PATH_URL . ($_ENV['PATH_URL_IMG'] ?? 'public/upload/images/')); -define("PATH_URL_IMG_PRODUCT", $_ENV['PATH_URL_IMG_PRODUCT'] ?? '/public/upload/products/'); +define("PATH_URL_IMG_PRODUCT", PATH_URL . ($_ENV['PATH_URL_IMG_PRODUCT'] ?? 'public/upload/product/')); diff --git a/lib/config/database.php b/lib/config/database.php index 7e9a47f..bc8b5eb 100644 --- a/lib/config/database.php +++ b/lib/config/database.php @@ -4,7 +4,7 @@ define("DB_PORT", $_ENV['DB_PORT'] ?? '3306'); define("DB_USER", $_ENV['DB_USER'] ?? 'root'); define("DB_PASS", $_ENV['DB_PASS'] ?? 'root'); -define("DB_NAME", $_ENV['DB_NAME'] ?? 'chikoi'); +define("DB_NAME", $_ENV['DB_NAME'] ?? 'new_mvc_shop_db'); if (isset($_SESSION['user'])) { $userNav = $_SESSION['user']['id']; From 0bf10acf85939f9908b4f28bf17671bca993a1e6 Mon Sep 17 00:00:00 2001 From: Tan Nguyen