From 518852ac9eeb3d3c00f74f20589a19017e8b600f Mon Sep 17 00:00:00 2001 From: iazaran Date: Sun, 17 Oct 2021 21:46:53 +0300 Subject: [PATCH 1/2] Show message from cookie if needed --- public/js/main.js | 57 +++++++++++++++++++++++++++++++++++++++---- public/js/main.min.js | 2 +- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index 18ebb62..4bd81b6 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -13,7 +13,17 @@ $(document).ready(function () { /** * Enable bootstrap toast with options */ - $(".toast").toast({delay: 4000}); + const toastElement = $(".toast"); + toastElement.toast({delay: 4000}); + + /** + * Check if message cookie exist to show it + */ + const message = getCookie("message"); + if (message !== "") { + toastElement.toast("show"); + $(".toast-body").text(decodeURI(message)); + } /** * Enable tooltips everywhere @@ -63,13 +73,13 @@ $(document).ready(function () { if (result["status"] === "OK") { window.location.replace("/"); } else { - $(".toast").toast("show"); + toastElement.toast("show"); $(".toast-body").text(result["message"]); } }, error(xhr, status, error) { // alert("responseText: " + xhr.responseText); - $(".toast").toast("show"); + toastElement.toast("show"); $(".toast-body").text(result["message"]); } }); @@ -106,15 +116,52 @@ $(document).ready(function () { if (result["status"] === "OK") { window.location.replace("/"); } else { - $(".toast").toast("show"); + toastElement.toast("show"); $(".toast-body").text(result["message"]); } }, error(xhr, status, error) { // alert("responseText: " + xhr.responseText); - $(".toast").toast("show"); + toastElement.toast("show"); $(".toast-body").text(result["message"]); } }); }); }); + +/** + * Set cookie + * + * @param name + * @param value + * @param expiresDay + */ +function setCookie(name, value, expiresDay) { + const d = new Date(); + d.setTime(d.getTime() + (expiresDay * 24 * 60 * 60 * 1000)); + let expires = "expires="+d.toUTCString(); + + document.cookie = name + "=" + value + ";" + expires + ";path=/"; +} + +/** + * Get cookie + * + * @param name + * @returns {string} + */ +function getCookie(name) { + let cookieName = name + "="; + let ca = document.cookie.split(";"); + for(let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) === " ") { + c = c.substring(1); + } + if (c.indexOf(cookieName) === 0) { + return c.substring(cookieName.length, c.length); + } + } + + return ""; +} diff --git a/public/js/main.min.js b/public/js/main.min.js index 726675d..69660a3 100644 --- a/public/js/main.min.js +++ b/public/js/main.min.js @@ -1 +1 @@ -$(document).ready(function(){const summernoteBody=$("#body");if(summernoteBody.length){summernoteBody.summernote({tabsize:4,height:100})}$(".toast").toast({delay:4000});$(function(){$("[data-toggle='tooltip']").tooltip()});const body=$("body");body.on("click",".form-button",function(event){let elementId=$(this).attr("id");elementId=elementId.replace("-submit","");let formData=new FormData($("form").get(0));$.ajax({url:apiAddress+"/"+elementId.replace("-","/"),data:formData,type:"POST",dataType:"JSON",cache:false,processData:false,contentType:false,beforeSend(){$(".progress").css("top","56px")},complete(){$(".progress").css("top","51px")},success(result){if(result.status==="OK"){window.location.replace("/")}else{$(".toast").toast("show");$(".toast-body").text(result.message)}},error(xhr,status,error){$(".toast").toast("show");$(".toast-body").text(result.message)}})});body.on("keypress","form",function(event){if(event.key==='Enter')$(".form-button").click()});body.on("click",".form-delete-button",function(event){let elementId=$(this).attr("id");$.ajax({url:apiAddress+"/blog/delete/"+elementId,type:"DELETE",dataType:"JSON",beforeSend(){$(".progress").css("top","56px")},complete(){$(".progress").css("top","51px")},success(result){if(result.status==="OK"){window.location.replace("/")}else{$(".toast").toast("show");$(".toast-body").text(result.message)}},error(xhr,status,error){$(".toast").toast("show");$(".toast-body").text(result.message)}})})}); \ No newline at end of file +$(document).ready(function(){const summernoteBody=$("#body");if(summernoteBody.length){summernoteBody.summernote({tabsize:4,height:100})}const toastElement=$(".toast");toastElement.toast({delay:4000});const message=getCookie("message");if(message!==""){toastElement.toast("show");$(".toast-body").text(decodeURI(message))}$(function(){$("[data-toggle='tooltip']").tooltip()});const body=$("body");body.on("click",".form-button",function(event){let elementId=$(this).attr("id");elementId=elementId.replace("-submit","");let formData=new FormData($("form").get(0));$.ajax({url:apiAddress+"/"+elementId.replace("-","/"),data:formData,type:"POST",dataType:"JSON",cache:false,processData:false,contentType:false,beforeSend(){$(".progress").css("top","56px")},complete(){$(".progress").css("top","51px")},success(result){if(result.status==="OK"){window.location.replace("/")}else{toastElement.toast("show");$(".toast-body").text(result.message)}},error(xhr,status,error){toastElement.toast("show");$(".toast-body").text(result.message)}})});body.on("keypress","form",function(event){if(event.key==='Enter')$(".form-button").click()});body.on("click",".form-delete-button",function(event){let elementId=$(this).attr("id");$.ajax({url:apiAddress+"/blog/delete/"+elementId,type:"DELETE",dataType:"JSON",beforeSend(){$(".progress").css("top","56px")},complete(){$(".progress").css("top","51px")},success(result){if(result.status==="OK"){window.location.replace("/")}else{toastElement.toast("show");$(".toast-body").text(result.message)}},error(xhr,status,error){toastElement.toast("show");$(".toast-body").text(result.message)}})})});function setCookie(name,value,expiresDay){const d=new Date();d.setTime(d.getTime()+(expiresDay*24*60*60*1000));let expires="expires="+d.toUTCString();document.cookie=name+"="+value+";"+expires+";path=/"}function getCookie(name){let cookieName=name+"=";let ca=document.cookie.split(";");for(let i=0;i Date: Sun, 17 Oct 2021 21:47:36 +0300 Subject: [PATCH 2/2] Verification email process bugs fixed --- src/Controllers/AuthController.php | 30 +++++++++-------- src/Controllers/BlogController.php | 52 ++++++++++++++++-------------- src/Models/Auth.php | 2 +- 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index c81d3fe..1a7c07e 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -54,19 +54,21 @@ public function register() [$request->tagline, 'required', 'Please enter a tagline to introduce yourself!'], ]); - if ($request->password1 !== $request->password2) { - $output['status'] = 'ERROR'; - $output['message'] = 'Please repeat password in confirmation field!'; - } elseif (Auth::existed($request->email)) { - $output['status'] = 'ERROR'; - $output['message'] = 'This Email registered before!'; - } elseif ($output['status'] == 'OK' && Helper::csrf($request->token) && Auth::register($request)) { - Helper::mailto($request->email, 'Welcome to PHPMVC! Email Verification', '

Hi dear friend,


Please click on this link to verify your email


Good luck,

Verify your email at PHPMVC

'); - - setcookie('message', 'Verification has been sent to your email, please check your inbox.', time() + 60); - } else { - $output['status'] = 'ERROR'; - $output['message'] = 'There is an error! Please try again.'; + if ($output['status'] == 'OK') { + if ($request->password1 !== $request->password2) { + $output['status'] = 'ERROR'; + $output['message'] = 'Please repeat password in confirmation field!'; + } elseif (Auth::existed($request->email)) { + $output['status'] = 'ERROR'; + $output['message'] = 'This Email registered before!'; + } elseif (Helper::csrf($request->token) && Auth::register($request)) { + Helper::mailto($request->email, 'Welcome to PHPMVC! Email Verification', '

Hi dear friend,


Please click on this link to verify your email


Good luck,

Verify your email at PHPMVC

'); + + setcookie('message', 'Verification has been sent to your email, please check your inbox.', time() + 60); + } else { + $output['status'] = 'ERROR'; + $output['message'] = 'There is an error! Please try again.'; + } } unset($_POST); @@ -80,7 +82,7 @@ public function register() */ public function verify() { - $request = json_decode(json_encode($_POST)); + $request = json_decode(json_encode($_GET)); if (Auth::verify($request) && $secret = Auth::getSecret($request)) { Helper::mailto($request->email, 'PHPMVC! Your API secret key', '

Hi dear friend,


This is your API secret key to access authenticated API routes:

' . $secret . '

Please keep it in a safe place.


Good luck,

PHPMVC

'); diff --git a/src/Controllers/BlogController.php b/src/Controllers/BlogController.php index b0e2f4b..f9dc348 100644 --- a/src/Controllers/BlogController.php +++ b/src/Controllers/BlogController.php @@ -100,17 +100,19 @@ public function store() [$request->body, 'required', 'Please enter a body for the post!'], ]); - if ($output['status'] == 'OK' && Helper::csrf($request->token) && Blog::store($request)) { - if (isset($_FILES['image']['type'])) { - HandleForm::upload($_FILES['image'], ['jpeg', 'jpg','png'], 5000000, '../public/assets/images/', 85, Helper::slug($request->title, '-', false)); + if ($output['status'] == 'OK') { + if (Helper::csrf($request->token) && Blog::store($request)) { + if (isset($_FILES['image']['type'])) { + HandleForm::upload($_FILES['image'], ['jpeg', 'jpg', 'png'], 5000000, '../public/assets/images/', 85, Helper::slug($request->title, '-', false)); + } + + unset($_POST); + XmlGenerator::feed(); + Cache::clearCache(['index', 'blog.index', 'api.index']); + } else { + $output['status'] = 'ERROR'; + $output['message'] = 'There is an error! Please try again.'; } - - unset($_POST); - XmlGenerator::feed(); - Cache::clearCache(['index', 'blog.index', 'api.index']); - } else { - $output['status'] = 'ERROR'; - $output['message'] = 'There is an error! Please try again.'; } echo json_encode($output); @@ -162,23 +164,25 @@ public function update() [$request->body, 'required', 'Please enter a body for the post!'], ]); - if ($output['status'] == 'OK' && Helper::csrf($request->token) && Blog::update($request)) { - Database::query("SELECT * FROM posts WHERE id = :id"); - Database::bind(':id', $request->id); + if ($output['status'] == 'OK') { + if ($output['status'] == 'OK' && Helper::csrf($request->token) && Blog::update($request)) { + Database::query("SELECT * FROM posts WHERE id = :id"); + Database::bind(':id', $request->id); - $currentPost = Database::fetch(); + $currentPost = Database::fetch(); - if (isset($_FILES['image']['type'])) { - HandleForm::upload($_FILES['image'], ['jpeg', 'jpg','png'], 5000000, '../public/assets/images/', 85, substr($currentPost['slug'], 0, -11)); - } + if (isset($_FILES['image']['type'])) { + HandleForm::upload($_FILES['image'], ['jpeg', 'jpg', 'png'], 5000000, '../public/assets/images/', 85, substr($currentPost['slug'], 0, -11)); + } - unset($_POST); - XmlGenerator::feed(); - Cache::clearCache('blog.show.' . $currentPost['slug']); - Cache::clearCache(['index', 'blog.index', 'api.index']); - } else { - $output['status'] = 'ERROR'; - $output['message'] = 'There is an error! Please try again.'; + unset($_POST); + XmlGenerator::feed(); + Cache::clearCache('blog.show.' . $currentPost['slug']); + Cache::clearCache(['index', 'blog.index', 'api.index']); + } else { + $output['status'] = 'ERROR'; + $output['message'] = 'There is an error! Please try again.'; + } } echo json_encode($output); diff --git a/src/Models/Auth.php b/src/Models/Auth.php index 3631fe8..0cbad6d 100644 --- a/src/Models/Auth.php +++ b/src/Models/Auth.php @@ -24,7 +24,7 @@ public static function register(object $request): bool `secret`, `user_token`, `tagline` - ) VALUES (:email, :password, :secret, :tagline)"); + ) VALUES (:email, :password, :secret, :user_token, :tagline)"); Database::bind([ ':email' => $request->email, ':password' => password_hash($request->password1, PASSWORD_DEFAULT),