Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.15 KB | None | 0 0
  1. <?php
  2.  
  3. require "assets/include/conf.inc.php";
  4.  
  5. function connectDb() {
  6.   try {
  7.     $db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PWD);
  8.     // set the PDO error mode to exception
  9.     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.         echo "Connection ok";
  11.   } catch(PDOException $e) {
  12.     die("Erreur de connection: " . $e->getMessage() );
  13.   }
  14.   return $db;
  15. }
  16.  
  17. function verifyInput($data) {
  18.     echo " verifyInput ";
  19.     $data = trim($data);
  20.     $data = stripslashes($data);
  21.     $data = htmlspecialchars($data);
  22.     return $data;
  23. }
  24.  
  25. function generateRandomCustomerCode($length = 10) {
  26.     $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  27.     $charactersLength = strlen($characters);
  28.     $randomCustomerCode = '';
  29.     for ($i = 0; $i < $length; $i++) {
  30.         $randomCustomerCode .= $characters[rand(0, $charactersLength - 1)];
  31.     }
  32.     // Vérifiez si le code existe déjà dans la BDD
  33.     $db = connectDb();
  34.     $query = $db->prepare("SELECT * FROM customers WHERE code_customer = :code_customer");
  35.     $query->execute([
  36.         "code_customer" => $randomCustomerCode
  37.     ]);
  38.     // Le code existe on doit en faire un autre
  39.     if(empty($query->fetch())) {
  40.         return $randomCustomerCode;
  41.     }
  42.     generateRandomCustomerCode($length);
  43. }
  44.  
  45. function registerCustomer(){
  46.  
  47.   $db = connectDb();
  48.     $randomCustomerCode = generateRandomCustomerCode();
  49.   $error = false;
  50.  
  51.   if ($_SERVER["REQUEST_METHOD"] == "POST") {
  52.     if (empty($_POST["name"])) {
  53.       $name_customer_Error = "Un prénom est requis";
  54.       $error = true;
  55.     } else {
  56.       $name_customer = verifyInput($_POST["name"]);
  57.       if(!ctype_alpha($name_customer)) {
  58.         $name_customer_Error = "Seules les lettres sont autorisés";
  59.         $error = true;
  60.       }
  61.     }
  62.  
  63.     if (empty($_POST["last-name"])) {
  64.       $last_name_customer_Error = "Un nom est requis";
  65.       $error = true;
  66.     } else {
  67.       $last_name_customer = verifyInput($_POST["last-name"]);
  68.       if(!ctype_alpha($last_name_customer)) {
  69.         $last_name_customer_Error = "Seules les lettres sont autorisés";
  70.         $error = true;
  71.       }
  72.     }
  73.  
  74.     if (empty($_POST["email"])) {
  75.       $email_customer_Error = "Un email est requis";
  76.       $error = true;
  77.     } else {
  78.       $email_customer = verifyInput($_POST["email"]);
  79.       if (!filter_var($email_customer, FILTER_VALIDATE_EMAIL)) {
  80.         $email_customer_Error = "Format d'email invalide";
  81.         $error = true;
  82.       }
  83.     }
  84.  
  85.     if (empty($_POST["tel"])) {
  86.       $phone_number_customer_Error = "Un téléphone est requis";
  87.       $error = true;
  88.     } else {
  89.       $phone_number_customer = verifyInput($_POST["tel"]);
  90.       if (!(strlen($_POST["tel"])==0 or (strlen($_POST["tel"])==10 and is_numeric($_POST["tel"])))) {
  91.         $phone_number_customer_Error = "Format de téléphone invalide";
  92.         $error = true;
  93.       }
  94.     }
  95.  
  96.     if (empty($_POST["pseudo"])) {
  97.       $pseudo_customer_Error = "Un pseudo est requis";
  98.       $error = true;
  99.     } else {
  100.       $pseudo_customer = verifyInput($_POST["pseudo"]);
  101.       if(!ctype_alnum($pseudo_customer)) {
  102.         $pseudo_customer_Error = "Seules les lettres et les chiffres sont autorisés";
  103.         $error = true;
  104.       }
  105.     }
  106.  
  107.     if (empty($_POST["password"])) {
  108.       $password_customer_Error = "Un mot de passe est requis";
  109.     } else {
  110.       $password_customer = verifyInput($_POST["password"]);
  111.       if(!ctype_alnum($password_customer)) {
  112.         $password_customer_Error = "Seules les lettres et les chiffres sont autorisés";
  113.         $error = true;
  114.       }
  115.       if (strlen($_POST["password"])<8 or strlen($_POST["password"])>20) {
  116.         $password_customer_Error = "Min: 8 - Max: 20";
  117.         $error = true;
  118.       }
  119.     }
  120.   }
  121.  
  122.     echo " En dehors de la condition error ";
  123.  
  124.   if(!$error) {
  125.         echo "Dans la condition error ";
  126.     // Préparation SQL et paramètres bind
  127.     $query = $db->prepare("INSERT INTO customers (name_customer, last_name_customer, email_customer, phone_number_customer, pseudo_customer, password_customer, code_customer, inside)
  128.    VALUES (:name_customer, :last_name_customer, :email_customer, :phone_number_customer, :pseudo_customer, :password_customer, :code_customer, :inside)");
  129.     $query->bindParam(':name_customer', $name_customer);
  130.     $query->bindParam(':last_name_customer', $last_name_customer);
  131.     $query->bindParam(':email_customer', $email_customer);
  132.     $query->bindParam(':phone_number_customer', $phone_number_customer);
  133.     $query->bindParam(':pseudo_customer', $pseudo_customer);
  134.     $query->bindParam(':password_customer', $password_customer);
  135.     $query->bindParam(':code_customer', $code_customer);
  136.     $query->bindParam(':inside', $inside);
  137.  
  138.     // Protection du mot de passe
  139.     $password_customer = password_hash($_POST["password"], PASSWORD_DEFAULT);
  140.  
  141.     // Insertion du client
  142.     $name_customer = $name_customer;
  143.     $last_name_customer = $last_name_customer;
  144.     $email_customer = $email_customer;
  145.     $phone_number_customer = $phone_number_customer;
  146.     $pseudo_customer = $pseudo_customer;
  147.     $password_customer = $password_customer;
  148.     $code_customer = $randomCustomerCode;
  149.     $inside = "0";
  150.     $query->execute();
  151.   }
  152. }
  153.  
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement