Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.43 KB | None | 0 0
  1. <?php
  2.  
  3. class UsersController extends AppController {
  4.  
  5.     var $name = 'Users';
  6.     var $obj_users_api;
  7.     var $obj_auth_api;
  8.     var $array_errors;
  9.        
  10.     function beforeFilter() {
  11.        
  12.         parent::beforeFilter();
  13.         App::import('Lib', 'users_api_class');
  14.         App::import('Lib', 'auth_api_class');
  15.         $this->obj_users_api = new Users_API;
  16.         $this->obj_auth_api = new Auth_API;
  17.     }
  18.    
  19.     // I. AFFICHAGE DES PAGES
  20.     // ----------------------------------------------------------------------------------------------------------------------------
  21.    
  22.     function index() {}
  23.    
  24.    
  25.     // II. VERIFICATIONS SUR LES SAISIES UTILISATEUR
  26.     // ----------------------------------------------------------------------------------------------------------------------------
  27.    
  28.     function check_signUp_inputs($data) {
  29.        
  30.         $this->autoRender = false;
  31.         $this->array_errors = array();
  32.    
  33.         // Si les données n'existent pas, redirection vers la page d'inscription
  34.         if (!isset($data)) {$this->redirect(array('controller' => 'users', 'action' => 'login'));}
  35.        
  36.         $bool_isError = false;
  37.         foreach ($data as $inputName => $inputValue) {
  38.            
  39.             switch($inputName) {
  40.                
  41.                 // VÉRIFICATION DU LOGIN
  42.                 case 'login':
  43.                     // 1. Est-ce que l'utilisateur a bien saisi un login ?
  44.                     if (empty($data[$inputName]) && $data[$inputName] != '0') {
  45.                         $this->array_errors['login'] = 'veuillez saisir un login';
  46.                         $bool_isError = true;
  47.                     }
  48.                     // 2. Le login dépasse t-il 25 caractères?
  49.                     elseif (strlen($inputValue) > 25) {
  50.                         $this->array_errors['login'] = 'le login ne doit pas dépasser pas 25 caractères';
  51.                         $bool_isError = true;
  52.                     }
  53.                     // 3. Est-ce que le login contient des caractères interdits ?
  54.                     else {
  55.                         $int_match_result = preg_match_all('/\w+|[-]/', $inputValue, $matches);
  56.                          
  57.                         // Si des occurrences sont trouvées
  58.                         if ($int_match_result > 0) {
  59.  
  60.                             // Comparaison de la concaténation des occurrences avec le login saisi
  61.                             if (implode('', $matches[0]) != $inputValue) {
  62.                                 $this->array_errors['login'] = 'le login ne doit contenir que des chiffres, des lettres non-accentuées, et les caractères suivants : _-';
  63.                                 $bool_isError = true;
  64.                             }
  65.                             // 4. Est-ce que le login est disponible ?
  66.                             else {
  67.                                 $str_post = '{ user : { login : "'.$inputValue.'" } }';
  68.                                 $bool_isAvailable = $this->check_login(false, $str_post);
  69.                                 if ($bool_isAvailable == false) {
  70.                                     $this->array_errors['login'] = 'le login saisi est indisponible';
  71.                                     $bool_isError = true;  
  72.                                 }
  73.                             }
  74.                         }
  75.                         // Si aucune occurrence n'est trouvée
  76.                         elseif ($int_match_result == 0) {
  77.                             $this->array_errors['login'] = 'le login ne doit contenir que des chiffres, des lettres non-accentuées, et les caractères suivants : _-';
  78.                             $bool_isError = true;
  79.                         }
  80.                     }
  81.                     break;
  82.                    
  83.                 // VÉRIFICATION DE L'EMAIL
  84.                 case 'email':
  85.                     // 1. Est-ce que l'utilisateur a sais un email ?
  86.                     if (empty($data[$inputName]) && $data[$inputName] != '0') {
  87.                         $this->array_errors['email'] = 'veuillez saisir un email';
  88.                         $bool_isError = true;
  89.                     }
  90.                     // 2. Est-ce que l'email saisi dépasse 255 caractères ?
  91.                     elseif (strlen($inputValue) > 255) {
  92.                         $this->array_errors['email'] = 'l\'email ne doit pas dépasser pas 255 caractères';
  93.                         $bool_isError = true;
  94.                     }
  95.                     // 3. Est-ce que le format de l'email saisi est valide ?
  96.                     elseif (preg_match('/^[\w-]+\.*[\w-]*@[a-z0-9-]+\.[a-z]+$/i', $inputValue) == 0) {
  97.                         $this->array_errors['email'] = 'l\'email doit avoir un format valide (exemple : mon@adresse.com)';
  98.                         $bool_isError = true;
  99.                     }
  100.                     // 4. Est-ce que l'email est disponible ?
  101.                     else {
  102.                         $str_post = '{ user : { email : "'.$inputValue.'" } }';
  103.                         $bool_isAvailable = $this->check_email(false, $str_post);
  104.                         if ($bool_isAvailable == false) {
  105.                             $this->array_errors['email'] = 'l\'email saisi est indisponible';
  106.                             $bool_isError = true;  
  107.                         }
  108.                     }
  109.                     break;
  110.                
  111.                 // VÉRIFICATION DU NOM
  112.                 case 'last_name':
  113.                    
  114.                     // 1. Est-ce que l'utilisateur a saisi un nom ?
  115.                     if (empty($data[$inputName]) && $data[$inputName] != '0') {
  116.                         $this->array_errors['last_name'] = 'veuillez saisir un nom';
  117.                         $bool_isError = true;
  118.                     }
  119.                     // 2. Est-ce que le nom dépasse 50 caractères ?
  120.                     elseif (strlen($inputValue) > 50) {
  121.                         $this->array_errors['last_name'] = 'le nom ne doit pas dépasser pas 50 caractères';
  122.                         $bool_isError = true;
  123.                     }
  124.                     break;
  125.                
  126.                 // VÉRIFICATION DU PRÉNOM    
  127.                 case 'first_name':
  128.                    
  129.                     // 1. Est-ce que l'utilisateur a saisi un prénom ?
  130.                     if (empty($data[$inputName]) && $data[$inputName] != '0') {
  131.                         $this->array_errors['first_name'] = 'veuillez saisir un prénom';
  132.                         $bool_isError = true;
  133.                     }
  134.                     // 2. Est-ce que le prénom dépasse 50 caractères ?
  135.                     elseif (strlen($inputValue) > 50) {
  136.                         $this->array_errors['first_name'] = 'le nom ne doit pas dépasser pas 50 caractères';
  137.                         $bool_isError = true;
  138.                     }
  139.                     break;
  140.                    
  141.                 default:
  142.                     break;
  143.             }
  144.         }
  145.         if ($bool_isError == false) { return true; } else { return false; }
  146.     }
  147.    
  148.        
  149.     function check_email($ajax = true, $data = null) {
  150.        
  151.         $this->autoRender = false;
  152.        
  153.         // Si cette méthode est appelée sans requête Ajax   
  154.         if ($ajax == false) {
  155.             $result = $this->obj_users_api->POST('check_email', $data);
  156.             if ($result['header'] == 200) { return true; } else { return false; }
  157.         }
  158.         // Si cette méthode est appelée avec requête Ajax
  159.         else {
  160.             $result = $this->obj_users_api->POST('check_email', $this->params['form']['ajaxData']);
  161.             if ($result['header'] == 200) { header('HTTP/1.1 200 OK', true, 200); }
  162.             elseif ($result['header'] == 409) { header('HTTP/1.1 409 CONFLICT', true, 409); }
  163.             else { header('HTTP/1.1 500 INTERNAL SERVER ERROR', true, 500); }
  164.         }
  165.     }
  166.    
  167.    
  168.     function check_login($ajax = true, $data = null) {
  169.        
  170.         $this->autoRender = false;
  171.        
  172.         if ($ajax == false) {      
  173.             $result = $this->obj_users_api->POST('check_login', $data);
  174.             if ($result['header'] == 200) { return true; } else { return false; }
  175.         }
  176.         else {
  177.             $result = $this->obj_users_api->POST('check_login', $this->params['form']['ajaxData']);
  178.             if ($result['header'] == 200) { header('HTTP/1.1 200 OK', true, 200); }
  179.             elseif ($result['header'] == 409) { header('HTTP/1.1 409 CONFLICT', true, 409); }
  180.             else { header('HTTP/1.1 500 INTERNAL SERVER ERROR', true, 500); }
  181.         }
  182.     }
  183.    
  184.    
  185.     // III. CONNEXION (EN ATTENTE)
  186.     // ----------------------------------------------------------------------------------------------------------------------------
  187.    
  188.     function login($signUp_data = null) {
  189.        
  190.         $this->autoRender = false;
  191.        
  192.         if (isset($signUp_data)) {
  193.            
  194.             // appel direct de session_auth
  195.            
  196.             echo 'login depuis inscription';
  197.            
  198.         } else {
  199.            
  200.             $match = $this->User->find('first', array('conditions' => array('User.login' => $this->data['User']['username'], 'User.password' => md5($this->data['User']['password']),)));
  201.            
  202.             if ($match != false) {
  203.            
  204.                 echo '<h2>User trouvé</h2>';
  205.                 $login = $match['User']['login'];
  206.                 $personal_key = $match['User']['personal_key'];
  207.                 $token = $this->session_auth_token($personal_key);
  208.                 $api_data = "{ credentials : { personal_key : $personal_key, token : $token } }";
  209.                 $cookie = $this->obj_auth_api->POST('session_auth', $api_data);
  210.                 echo '<pre>';
  211.                 var_dump($cookie);
  212.                 echo '<pre>';
  213.             }
  214.             else {
  215.                 echo 'Vos identifiants sont incorrects';
  216.             }      
  217.         }
  218.     }
  219.    
  220.     function logout() {exit('action logout');}
  221.    
  222.    
  223.     // IV. INSCRIPTION
  224.     // ----------------------------------------------------------------------------------------------------------------------------
  225.    
  226.     function create() {
  227.        
  228.         $this->autoRender = false;
  229.         $isDataOK = $this->check_signUp_inputs($this->data['User']);
  230.        
  231.         // Est-ce que les données sont correctes ?
  232.         if ($isDataOK == true) {
  233.             extract($this->data['User']);
  234.             $data = '{user : { login : "'.$login.'", first_name : "'.$first_name.'", last_name : "'.$last_name.'", email : "'.$email.'", birthdate : {year : '.(int)$year.', month : '.(int)$month.', day : '.(int)$day.' } } }';
  235.             $result = $this->obj_users_api->POST('create', $data);
  236.            
  237.             // Inscription réussie
  238.             if ($result['header'] == 201) {
  239.                
  240.                 $personal_key = json_decode($result['content'], true);
  241.                 $personal_key = $personal_key[1]["personal_key"]["personal_key"];
  242.                 $password = $this->random_password();
  243.                 $user = array('login' => $login, 'password' => md5($password), 'personal_key' => $personal_key);
  244.                
  245.                 $this->user_to_database($user); // 1. Sauvegarde dans la base de donnée
  246.                 $this->sendCredentials($login, $password); // 2. Envoi du mot de passe par email
  247.                 // 3. Fonction : Connexion
  248.             }
  249.             // Mauvais paramètres (API) -> les tests effectués avec check_signUp_inputs ne respectent plus les restrictions de la documentation
  250.             elseif ($result['header'] == 422) {$this->flash('[422] UNE ERREUR INTERNE EST SURVENUE, VEUILLEZ RÉESSAYER ULTÉRIEUREMENT', array('controller' => 'users', 'action' => 'index'), 3);}
  251.            
  252.             // Erreur interne (API)
  253.             else {$this->flash('[500] UNE ERREUR INTERNE EST SURVENUE, VEUILLEZ RÉESSAYER ULTÉRIEUREMENT', array('controller' => 'users', 'action' => 'index'), 3);}
  254.         }
  255.         else {
  256.             $this->Session->write(array('Errors.signUp' => $this->array_errors, 'SignUp.data' => $this->data['User']));
  257.             $this->redirect(array('controller' => 'users', 'action' => 'index'));
  258.         }
  259.     }
  260.    
  261.     // Création du deuxième paramètre pour la fonction session_auth de l'API
  262.     function session_auth_token($personal_key) {
  263.        
  264.         $this->autoRender = false;         
  265.         $delta = time() - (int)$this->obj_auth_api->GET('sync', 'content');
  266.         $time = (int)((time() + $delta) / 30);
  267.         $to_hash = $personal_key.'--'.$time;
  268.         $token = substr(hash('sha256', $to_hash), 0, 8);
  269.         return $token;
  270.     }
  271.    
  272.     function user_to_database($user) {
  273.         $this->autoRender = false; 
  274.         $this->User->save($user);
  275.     }
  276.    
  277.     function random_password($length = 8) {
  278.    
  279.         $this->autoRender = false; 
  280.         $hash = str_shuffle(str_shuffle('bcdfghjkmnpqrstvwxyz23456789%{&:[}]#-_^@)(=~+'));
  281.         $password = substr($hash, rand(0, (strlen($hash) - $length)), $length);
  282.         return $password;
  283.     }
  284.    
  285.     function sendCredentials ($login, $password) {
  286.        
  287.         $this->autoRender = false;
  288.         // Envoyer le mail avec les id
  289.         echo 'login : ' . $login;
  290.         echo '<br />';
  291.         echo 'pwd : ' . $password;
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement