From a345a225652053f850470b919fa9b3578f441fd2 Mon Sep 17 00:00:00 2001 From: adri Date: Wed, 2 Jan 2019 10:43:38 +0100 Subject: [PATCH] beautify --- index.php | 57 ++++---- post.php | 69 +++++----- script.js | 397 ++++++++++++++++++++++++++---------------------------- 3 files changed, 252 insertions(+), 271 deletions(-) diff --git a/index.php b/index.php index e2760b7..da30177 100755 --- a/index.php +++ b/index.php @@ -1,29 +1,31 @@ getMessage()); + die('Erreur : '.$e->getMessage()); } $reponse = $bdd->query('SELECT * FROM projetphp'); ?> + - + - + + @@ -38,29 +40,34 @@ $reponse = $bdd->query('SELECT * FROM projetphp'); fetch()) - { - ?> - - - - - - - - - +while ($data = $reponse->fetch()) { + ?> + + + + + + + + + - +
- - -
+ + + + + +
+ diff --git a/post.php b/post.php index 566d78e..5cff62e 100644 --- a/post.php +++ b/post.php @@ -2,50 +2,45 @@ $ret["return"] = false; $ret["pw"] = null; try { - $config = include('config.php'); - $bdd = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbName'].';charset=utf8', $config['username'], $config['pw']); + $config = include('config.php'); + $bdd = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbName'].';charset=utf8', $config['username'], $config['pw']); } catch (Exception $e) { - die('Erreur : '.$e->getMessage()); + die('Erreur : '.$e->getMessage()); } //DELETE FROM `projetphp` WHERE `projetphp`.`login` = \'caprout\' -if(isset($_POST["userToDel"])) -{ - $req = $bdd->prepare('DELETE FROM `projetphp` WHERE `projetphp`.`login`=:login'); - $req->execute(array( 'login' => htmlspecialchars($_POST["userToDel"]))); +if (isset($_POST["userToDel"])) { + $req = $bdd->prepare('DELETE FROM `projetphp` WHERE `projetphp`.`login`=:login'); + $req->execute(array( 'login' => htmlspecialchars($_POST["userToDel"]))); } //UPDATE `projetphp` SET `security_level` = '6' WHERE `projetphp`.`login` = 'aaazzze'; -if(isset($_POST["updatePW"], $_POST["datene"], $_POST["email"], $_POST["pw"], $_POST["sl"], $_POST["login"])) - if(preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $_POST["email"])) - if(htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "true" && preg_match('/^[a-z]{8,16}$/',$_POST["pw"])) - { - $req = $bdd->prepare('UPDATE `projetphp` SET `date_naissance` = :datene, `e_mail` = :email, `mot_de_passe` = :pw, `security_level` = :sl WHERE `projetphp`.`login` = :login'); - $req->execute(array( - 'datene' => htmlspecialchars($_POST["datene"], ENT_QUOTES, 'UTF-8'), - 'email' => htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8'), - 'pw' => md5(htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8')), - 'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'), - 'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'), - )); - $ret["return"] = true; - $ret["pw"] = md5(htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8')); - } - else if(htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "false") - { - $req = $bdd->prepare('UPDATE `projetphp` SET `date_naissance` = :datene, `e_mail` = :email, `security_level` = :sl WHERE `projetphp`.`login` = :login'); - $req->execute(array( - 'datene' => htmlspecialchars($_POST["datene"], ENT_QUOTES, 'UTF-8'), - 'email' => htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8'), - 'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'), - 'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'), - )); - $ret["return"] = true; - $ret["pw"] = htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8'); - } - else; -else; +if (isset($_POST["updatePW"], $_POST["datene"], $_POST["email"], $_POST["pw"], $_POST["sl"], $_POST["login"])) { + if (preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $_POST["email"])) { + if (htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "true" && preg_match('/^[a-z]{8,16}$/', $_POST["pw"])) { + $req = $bdd->prepare('UPDATE `projetphp` SET `date_naissance` = :datene, `e_mail` = :email, `mot_de_passe` = :pw, `security_level` = :sl WHERE `projetphp`.`login` = :login'); + $req->execute(array( +'datene' => htmlspecialchars($_POST["datene"], ENT_QUOTES, 'UTF-8'), +'email' => htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8'), +'pw' => md5(htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8')), +'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'), +'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'), +)); + $ret["return"] = true; + $ret["pw"] = md5(htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8')); + } elseif (htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "false") { + $req = $bdd->prepare('UPDATE `projetphp` SET `date_naissance` = :datene, `e_mail` = :email, `security_level` = :sl WHERE `projetphp`.`login` = :login'); + $req->execute(array( +'datene' => htmlspecialchars($_POST["datene"], ENT_QUOTES, 'UTF-8'), +'email' => htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8'), +'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'), +'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'), +)); + $ret["return"] = true; + $ret["pw"] = htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8'); + } else ; + } else ; +} echo json_encode((object)$ret); -?> diff --git a/script.js b/script.js index 7c6a7e3..91b735e 100644 --- a/script.js +++ b/script.js @@ -1,247 +1,226 @@ htmlAddUser = '\ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ '; -passwordPatern=/^[a-zA-Z0-9]{8,16}$/; -loginPatern=/^[a-z]{8,16}$/ -mailPatern=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;//https://emailregex.com/ +passwordPatern = /^[a-zA-Z0-9]{8,16}$/; +loginPatern = /^[a-z]{8,16}$/ +mailPatern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; //https://emailregex.com/ $(function() { -$("#allRecord").on("keyup", ".pw input", function(){ - if(passwordPatern.test($(this).val()) || $(this).val() == "") - { - $(this).css("background-color", ""); - $('div', $(this).parent()).css("display", "none"); - } - else - { - $(this).css("background-color", "red"); - $('div', $(this).parent()).css("display", "block"); - } -}); -$("#allRecord").on("keyup", ".email input", function(){ - if(mailPatern.test($(this).val())) - { - $(this).css("background-color", ""); - $('div', $(this).parent()).css("display", "none"); - } - else - { - $(this).css("background-color", "red"); - $('div', $(this).parent()).css("display", "block"); - } -}); -/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ -/*~~~~~~~~~~~~~~~~TR TO DEL~~~~~~~~~~~~~~~~*/ -/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ -$("#allRecord").on("click", ".delUser", function() { - newUser = false; - tr = $(this).parent().parent(); - var login = ""; - var sl = ""; - if(tr.find("i.editUser").length !== 0) - { - sl = ltrim($('.sl', tr).html()) - login = $('.login', tr).html(); - } - else if(tr.find("i.confirmEditUser").length !== 0) - { - sl = $('.sl select', tr).val(); - login = $('.login', tr).html(); - } - else - { - try{ - login = "New user "+$('.login input', tr).val(); - newUser = true; - } - catch(e) - { - swal("error : "+e); - return ; - } + $("#allRecord").on("keyup", ".pw input", function() { + if (passwordPatern.test($(this).val()) || $(this).val() == "") { + $(this).css("background-color", ""); + $('div', $(this).parent()).css("display", "none"); + } else { + $(this).css("background-color", "red"); + $('div', $(this).parent()).css("display", "block"); } - if(sl == "10") - { - swal( "Oops" , "You can't del user with sl 10" , "error" ); + }); + $("#allRecord").on("keyup", ".email input", function() { + if (mailPatern.test($(this).val())) { + $(this).css("background-color", ""); + $('div', $(this).parent()).css("display", "none"); + } else { + $(this).css("background-color", "red"); + $('div', $(this).parent()).css("display", "block"); + } + }); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + /*~~~~~~~~~~~~~~~~TR TO DEL~~~~~~~~~~~~~~~~*/ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + $("#allRecord").on("click", ".delUser", function() { + newUser = false; + tr = $(this).parent().parent(); + var login = ""; + var sl = ""; + if (tr.find("i.editUser").length !== 0) { + sl = ltrim($('.sl', tr).html()) + login = $('.login', tr).html(); + } else if (tr.find("i.confirmEditUser").length !== 0) { + sl = $('.sl select', tr).val(); + login = $('.login', tr).html(); + } else { + try { + login = "New user " + $('.login input', tr).val(); + newUser = true; + } catch (e) { + swal("error : " + e); + return; + } + } + if (sl == "10") { + swal("Oops", "You can't del user with sl 10", "error"); return; } login = ltrim(login); swal({ - title: "Are you sure to del "+login+" ?", - text: "Once deleted, you will not be able to recover this!", - icon: "warning", - buttons: true, - dangerMode: true, -}) -.then((willDelete) => { - if (willDelete) { - if(!newUser) - $.post("post.php", {userToDel: login}, function(){ - tr.remove(); - swal("Poof! "+login+" has been deleted!", { - icon: "success", - }); + title: "Are you sure to del " + login + " ?", + text: "Once deleted, you will not be able to recover this!", + icon: "warning", + buttons: true, + dangerMode: true, + }) + .then((willDelete) => { + if (willDelete) { + if (!newUser) + $.post("post.php", { + userToDel: login + }, function() { + tr.remove(); + swal("Poof! " + login + " has been deleted!", { + icon: "success", + }); + }); + else { + tr.remove(); + swal("Poof! " + login + " has been deleted!", { + icon: "success", + }); + } + } else { + + } }); - else { - tr.remove(); - swal("Poof! "+login+" has been deleted!", { - icon: "success", - }); - } - } else { - - } -}); -}); + }); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~TR TO EDIT~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - $("#allRecord").on("click", ".editUser", function() { + $("#allRecord").on("click", ".editUser", function() { $(this).toggleClass('fa-edit fa-check'); $(this).toggleClass('editUser confirmEditUser'); - for(i=1; i<$("td", $(this).parent().parent()).length-1;i++) - { - j= $($("td", $(this).parent().parent())[i]); + for (i = 1; i < $("td", $(this).parent().parent()).length - 1; i++) { + j = $($("td", $(this).parent().parent())[i]); className = j.attr('class'); - if(className == "sl") - { + if (className == "sl") { val = ltrim(j.html()); j.html('') - } - else - { +\ +\ +\ +') + } else { inputOptions = ''; - if(className=="pw") - inputOptions += 'type="password" data-oldpw="'+ltrim(j.html())+'" '; + if (className == "pw") + inputOptions += 'type="password" data-oldpw="' + ltrim(j.html()) + '" '; else if (j.attr('class') == "datene") inputOptions += 'type="date" ' - j.html(''); - if(className=="pw") - { + j.html(''); + if (className == "pw") { j.append("
le mot de passe comporte entre 8 et 16 caractères parmi a..z A..Z 0..9
") } } } - }); + }); - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - /*~~~~~~~~~~~~~~~~CONFIRM EDIT~~~~~~~~~~~~~~~~*/ - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - $("#allRecord").on("click", ".confirmEditUser", function() { - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - /*~~~~~~~~~~~~~~~~CALL BACK DE LA VERIF DE MOT DE PASSE~~~~~~~~~~~~~~~~*/ - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - function callBackVerif(data){ - var dataToSend = new Object(); //Objet contenant les données à envoyé - dataToSend["updatePW"] = (data===true?false:true); - if($(".pw input", tr).val() === data || data === true) //Verif du mot de passe si nécesaire - { - for(i=0; i"+e , "error" ); - } - console.log(data.return) - }, "json"); - } - else { - swal( "Oops" , "Password don't match !" , "error" ); - } - } - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - tds = $("td", $(this).parent().parent()); //Récupère les td concernant le click - tr = $(this).parent().parent(); //Récupère le tr concernant le click - that = this; - if(mailPatern.test($(".email input", tr).val()) && (passwordPatern.test($(".pw input", tr).val()) || $(".pw input", tr).val() =="")) - if ($(".pw input", tr).val() === "") { - callBackVerif(true); - } - else { - swal({ - closeOnClickOutside: false, - closeOnEsc: false, - content: { - element: "input", - attributes: { - placeholder: "Confirmer le mot de passe", - type: "password", - }, - }, - }).then(callBackVerif); - } - else - swal( "Oops" , "Input don't respect rules !" , "error" ); - }); - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - /*~~~~~~~~~~~~~~~~ADD USER~~~~~~~~~~~~~~~~*/ - /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - $("#allRecord").on("click", ".fa-user-plus", function(){ - $(this).parent().parent().before(htmlAddUser); - }); - $("#allRecord").on("click", ".confirmNewUser", function(){ + value = $('input,select', j).val(); + dataToSend[className] = value; + } + $.post("post.php", dataToSend, function(data) { + try { + if (data.return == true) { + for (i = 1; i < tds.length - 1; i++) //On change les input en pure html + { + j = $(tds[i]); + className = j.attr('class'); + if (className == "pw") + value = data.pw; + else + value = $('input,select', j).val(); + j.html(value); + } + swal("all done"); + $(that).toggleClass('fa-check fa-edit'); + $(that).toggleClass('confirmEditUser editUser'); + } else + swal(data); + } catch (e) { + swal("Oops", "Something went wrong!
" + e, "error"); + } + console.log(data.return) + }, "json"); + } else { + swal("Oops", "Password don't match !", "error"); + } + } + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + tds = $("td", $(this).parent().parent()); //Récupère les td concernant le click + tr = $(this).parent().parent(); //Récupère le tr concernant le click + that = this; + if (mailPatern.test($(".email input", tr).val()) && (passwordPatern.test($(".pw input", tr).val()) || $(".pw input", tr).val() == "")) + if ($(".pw input", tr).val() === "") { + callBackVerif(true); + } + else { + swal({ + closeOnClickOutside: false, + closeOnEsc: false, + content: { + element: "input", + attributes: { + placeholder: "Confirmer le mot de passe", + type: "password", + }, + }, + }).then(callBackVerif); + } else + swal("Oops", "Input don't respect rules !", "error"); + }); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + /*~~~~~~~~~~~~~~~~ADD USER~~~~~~~~~~~~~~~~*/ + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + $("#allRecord").on("click", ".fa-user-plus", function() { + $(this).parent().parent().before(htmlAddUser); + }); + $("#allRecord").on("click", ".confirmNewUser", function() { - }); - $("#allRecord").on("click", ".delNewUser", function(){ - tr = $(this).parent().parent(); - tr.remove(); - }); + }); + $("#allRecord").on("click", ".delNewUser", function() { + tr = $(this).parent().parent(); + tr.remove(); + }); }); //J'avais plein d'espace a droite en récupérent mon innerHTML //https://stackoverflow.com/questions/24282158/removing-the-white-space-at-the-start-of-the-string function ltrim(str) { - if(str == null) return str; + if (str == null) return str; return str.replace(/^\s+/g, ''); }