Advertisement
Guest User

registerService

a guest
Aug 1st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.93 KB | None | 0 0
  1. <?php
  2. class registerService
  3. {
  4.     private $params;
  5.     private $error;
  6.     private $user;
  7.  
  8.     public function getParams() { return $this->params; }
  9.     public function setParams($params) { $this->params = $params; }
  10.    
  11.     public function getError() { return $this->error; }
  12.     public function setError($error) { $this->error = $error; }
  13.  
  14.     public function getUser(){ return $this->user; }
  15.     public function setUser($user){ $this->user = $user; }
  16.    
  17.     public function launchControls(){
  18.        
  19.         if(empty($this->params['username'])){
  20.             $this->error['username'] = 'Nom utilisateur manquant';
  21.         }
  22.         if(strlen($this->params['username']) < 8) {
  23.             $this->error['username'] = 'Nom utilisateur trop court 8 charactère minimum';
  24.         } else if(strlen($this->params['username']) > 16) {
  25.             $this->error['username'] = 'Nom utilisateur trop long 16 charactère maximum';
  26.         }
  27.  
  28.         if(empty($this->params['password'])){
  29.             $this->error['password'] = 'Mot de passe manquant';
  30.         }
  31.         if(strlen($this->params['password']) < 8) {
  32.             $this->error['password'] = 'Mot de passe trop court 8 charactère minimum';
  33.         } else if(strlen($this->params['password']) > 16) {
  34.             $this->error['password'] = 'Mot de passe trop long 16 charactère maximum';
  35.         }
  36.         if($this->params['password'] != $this->params['confirmPassword']) {
  37.             $this->error['confirmPassword'] = 'Les mot de passe ne sont pas identique';
  38.         }
  39.  
  40.         if (!filter_var($this->params['email'], FILTER_VALIDATE_EMAIL)) {
  41.             $this->error['email'] = 'Le format de l\'adresse mail n\'est pas valide';
  42.         }
  43.         if(empty($this->error)==false){
  44.             return $this->error;
  45.         }
  46.  
  47.         // $this->user = $this->checkUsernamePassword();
  48.         // if(empty($this->user)){
  49.         //     $this->error['identifiants'] = 'Le nom d\'utilisateur ou mot de passe incorrect';
  50.         //     return $this->error;
  51.         // }else{
  52.         //     return $this->user;
  53.         // }
  54.         $this->username = $this->checkUsernameExist();
  55.         if(empty($this->username)) {
  56.             return $this->error['username'] = 'Nom d\'utilisateur indisponible';
  57.         }else{
  58.             return $this->username;
  59.         }
  60.         $this->email = $this->checkEmailExist();
  61.         if(empty($this->email)) {
  62.             return $this->error['email'] = 'L\'adresse mail est déjà utiliser';
  63.         }else{
  64.             return $this->email;
  65.         }
  66.         $this->user = $this->checkUsernamePassword();
  67.         if(empty($this->user)){
  68.             $this->error['identifiants'] = 'Le nom d\'utilisateur ou mot de passe incorrect';
  69.             return $this->error;
  70.         }else{
  71.             return $this->user;
  72.         }
  73.     }
  74.     /***
  75.      * Il est évident que cette fonction ne va pas ici!!!!
  76.      */
  77.  
  78.     public function checkUsernameExist() {
  79.         $username = $this->params['username'];
  80.         $connexion = new PDO('mysql:host=localhost;dbname=blog;charset=UTF8','root','root');
  81.         $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  82.         $connexion->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  83.         $object = $connexion->prepare('SELECT username FROM users WHERE username=:username');
  84.         $object->execute(array(
  85.             'username'=>$username
  86.         ));
  87.         $user = $object->fetchAll(PDO::FETCH_ASSOC);
  88.         if(empty($user)==true){
  89.             return $user;
  90.         }
  91.         return true;
  92.     }
  93.     public function checkEmailExist() {
  94.         $email = $this->params['email'];
  95.         $connexion = new PDO('mysql:host=localhost;dbname=blog;charset=UTF8','root','root');
  96.         $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  97.         $connexion->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  98.         $object = $connexion->prepare('SELECT email FROM users WHERE email=:email');
  99.         $object->execute(array(
  100.             'email'=>$email
  101.         ));
  102.         $email = $object->fetchAll(PDO::FETCH_ASSOC);
  103.         if(empty($email)==true){
  104.             return $email;
  105.         }
  106.         return true;
  107.     }
  108.     public function checkUsernamePassword(){
  109.         $username = $this->params['username'];
  110.         $password = $this->params['password'];
  111.         $connexion = new PDO('mysql:host=localhost;dbname=blog;charset=UTF8','root','root');
  112.         $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  113.         $connexion->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  114.         $object = $connexion->prepare('SELECT id,username FROM users WHERE username=:username AND password=:password');
  115.         $object->execute(array(
  116.             'password'=>$password,
  117.             'username'=>$username
  118.         ));
  119.         $user = $object->fetchAll(PDO::FETCH_ASSOC);
  120.         if(empty($user)==false){
  121.             return $user;
  122.         }
  123.         return false;
  124.     }
  125. }
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement