Advertisement
Guest User

/app/Model/User.php

a guest
Apr 3rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.77 KB | None | 0 0
  1. <?php
  2. App::uses('CakeEvent', 'Event');
  3.  
  4. class User extends AppModel {
  5.  
  6.     private $userData;
  7.  
  8.     public $hasMany = array(
  9.         'Comment' => array(
  10.       'className' => 'Comment',
  11.       'foreignKey' => 'user_id',
  12.       'order' => 'Comment.created DESC',
  13.       'dependent' => true
  14.     ),
  15.         'Like' => array(
  16.       'className' => 'Like',
  17.       'foreignKey' => 'user_id',
  18.       'dependent' => true
  19.     )
  20.     );
  21.  
  22.     public function validRegister($data, $UtilComponent) {
  23.         if(preg_match('`^([a-zA-Z0-9_]{2,16})$`', $data['pseudo'])) {
  24.             $data['password'] = $UtilComponent->password($data['password'], $data['pseudo']);
  25.             $data['password_confirmation'] = $UtilComponent->password($data['password_confirmation'], $data['pseudo']);
  26.             if($data['password'] == $data['password_confirmation']) {
  27.                 if(filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
  28.                     $search_member_by_pseudo = $this->find('all', array('conditions' => array('pseudo' => $data['pseudo'])));
  29.                     $search_member_by_email = $this->find('all', array('conditions' => array('email' => $data['email'])));
  30.                     if(empty($search_member_by_pseudo)) {
  31.                         if(empty($search_member_by_email)) {
  32.                             return true;
  33.                         } else {
  34.                             return 'USER__ERROR_EMAIL_ALREADY_REGISTERED';
  35.                         }
  36.                     } else {
  37.                         return 'USER__ERROR_PSEUDO_ALREADY_REGISTERED';
  38.                     }
  39.                 } else {
  40.                     return 'USER__ERROR_EMAIL_NOT_VALID';
  41.                 }
  42.             } else {
  43.                 return 'USER__ERROR_PASSWORDS_NOT_SAME';
  44.             }
  45.         } else {
  46.             return 'USER__ERROR_PSEUDO_INVALID_FORMAT';
  47.         }
  48.     }
  49.  
  50.     public function register($data, $UtilComponent) {
  51.  
  52.         $data_to_save = array();
  53.  
  54.         $data_to_save['pseudo'] = before_display($data['pseudo']);
  55.         $data_to_save['email'] = before_display($data['email']);
  56.  
  57.         $data_to_save['ip'] = isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
  58.         $data_to_save['rank'] = 0;
  59.  
  60.         $data_to_save['password'] = $UtilComponent->password($data['password'], $data['pseudo']);
  61.  
  62.         $this->create();
  63.         $this->set($data_to_save);
  64.         $this->save();
  65.         return $this->getLastInsertId();
  66.     }
  67.  
  68.     public function login($data, $need_email_confirmed = false, $UtilComponent) {
  69.         $LoginRetryTable = ClassRegistry::init('LoginRetry');
  70.         $ip = isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
  71.         $findRetryWithIP = $LoginRetryTable->find('first', array('conditions' => array('ip' => $ip)));
  72.  
  73.         // si on trouve rien OU que il n'a pas encore essayé plus de 10 fois OU que la dernière date du retry est passé depuis 2h
  74.  
  75.         if(empty($findRetryWithIP) || $findRetryWithIP['LoginRetry']['count'] < 10 || strtotime('+2 hours', strtotime($findRetryWithIP['LoginRetry']['modified'])) < time()) {
  76.  
  77.             $search_user = $this->find('first', array('conditions' => array('pseudo' => $data['pseudo'], $data['pseudo']))));
  78.             if(!empty($search_user) && $UtilComponent->check($data['password'], $search_user['User']['password'])) {
  79.  
  80.                 if($need_email_confirmed && !empty($search_user['User']['confirmed']) && date('Y-m-d H:i:s', strtotime($search_user['User']['confirmed'])) != $search_user['User']['confirmed']) {
  81.                     // mail non confirmé
  82.                     return 'USER__MSG_NOT_CONFIRMED_EMAIL';
  83.                 }
  84.  
  85.                 return array('status' => true, 'session' => $search_user['User']['id']);
  86.  
  87.             } else {
  88.  
  89.                 if(!isset($findRetryWithIP['LoginRetry']) || strtotime('+2 hours', strtotime($findRetryWithIP['LoginRetry']['modified'])) < time()) { //on reset à 0
  90.                     $findRetryWithIP['LoginRetry']['count'] = 0;
  91.                 }
  92.  
  93.                 if(empty($findRetryWithIP) || !isset($findRetryWithIP['LoginRetry']['id'])) { // si il avais rien fail encore
  94.  
  95.                     $LoginRetryTable->create();
  96.                     $LoginRetryTable->set(array(
  97.                         'ip' => $ip,
  98.                         'count' => 1
  99.                     ));
  100.                     $LoginRetryTable->save();
  101.  
  102.                 } else {
  103.  
  104.                     $LoginRetryTable->read(null, $findRetryWithIP['LoginRetry']['id']);
  105.                     $LoginRetryTable->set(array(
  106.                         'ip' => $ip,
  107.                         'count' => ($findRetryWithIP['LoginRetry']['count']+1),
  108.                         'modified' => date('Y-m-d H:i:s')
  109.                     ));
  110.                     $LoginRetryTable->save();
  111.  
  112.                 }
  113.  
  114.                 return 'USER__ERROR_INVALID_CREDENTIALS';
  115.             }
  116.  
  117.         } else {
  118.             return 'LOGIN__BLOCKED';
  119.         }
  120.     }
  121.  
  122.     public function resetPass($data, $controller) {
  123.         $UtilComponent = $controller->Util;
  124.         if($data['password'] == $data['password2']) {
  125.             unset($data['password2']);
  126.             $search = $this->find('all', array('conditions' => array('email' => $data['email'])));
  127.             if(!empty($search)) {
  128.  
  129.                 $this->Lostpassword = ClassRegistry::init('Lostpassword');
  130.                 $Lostpassword = $this->Lostpassword->find('all', array('conditions' => array('email' => $data['email'], 'key' => $data['key'])));
  131.                 if(!empty($Lostpassword) && strtotime('+1 hour', strtotime($Lostpassword[0]['Lostpassword']['created'])) >= time()) {
  132.  
  133.                     $data_to_save['password'] = $UtilComponent->password($data['password'], $search['0']['User']['pseudo']);
  134.  
  135.                     $event = new CakeEvent('beforeResetPassword', $this, array('user_id' => $search[0]['User']['id'], 'new_password' => $data_to_save['password']));
  136.                     $controller->getEventManager()->dispatch($event);
  137.                     if($event->isStopped()) {
  138.                         return $event->result;
  139.                     }
  140.  
  141.                     $this->Lostpassword->delete($Lostpassword[0]['Lostpassword']['id']);
  142.  
  143.                     $this->read(null, $search['0']['User']['id']);
  144.                     $this->set($data_to_save);
  145.                     $this->save();
  146.  
  147.                     return array('status' => true, 'session' => $search[0]['User']['id']);
  148.  
  149.                 } else {
  150.                     return 'USER__PASSWORD_RESET_INVALID_KEY';
  151.                 }
  152.             } else {
  153.                 return 'ERROR__INTERNAL_ERROR';
  154.             }
  155.         } else {
  156.             return 'USER__ERROR_PASSWORDS_NOT_SAME';
  157.         }
  158.     }
  159.  
  160.     private function getDataBySession($session) {
  161.         if(empty($this->userData)) {
  162.             $this->userData = $this->find('first', array('conditions' => array('id' => $session)));
  163.         }
  164.         return $this->userData;
  165.     }
  166.  
  167.     public function isConnected() {
  168.         if(CakeSession::check('user') == false) {
  169.             return false;
  170.         } else {
  171.             // Je cherche si il la session est pas vide et si elle est dans la bdd
  172.             $user = $this->find('all', array(
  173.                 'conditions' => array(
  174.                     'id' => CakeSession::read('user'),
  175.                 )
  176.             ));
  177.             return (isset($user['0']['User']['id']));
  178.         }
  179.     }
  180.  
  181.     public function isAdmin() {
  182.         if(CakeSession::check('user') == false) {
  183.           return false;
  184.         } else {
  185.             // Je cherche si il la session est pas vide et si elle est dans la bdd
  186.             $user = $this->getDataBySession(CakeSession::read('user'));
  187.             return (isset($user['User']['id']) AND $user['User']['rank'] == 3 OR $user['User']['rank'] == 4);
  188.       }
  189.     }
  190.  
  191.     public function __makeCondition($search) {
  192.         if((string)(int)$search == $search) {
  193.             return array(
  194.                 'id' => intval($search)
  195.             );
  196.         } else {
  197.             return array(
  198.                 'pseudo' => $search
  199.             );
  200.         }
  201.     }
  202.  
  203.     public function exist($search) { //username || id
  204.         $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  205.     return (!empty($search_user));
  206.     }
  207.  
  208.     public function getKey($key) {
  209.     if(CakeSession::check('user')) {
  210.             $search_user = $this->getDataBySession(CakeSession::read('user'));
  211.             return ($search_user) ? $search_user['User'][$key] : '';
  212.     }
  213.     }
  214.  
  215.     public function setKey($key, $value) {
  216.     if(CakeSession::check('user')) {
  217.             $search_user = $this->getDataBySession(CakeSession::read('user'));
  218.             if($search_user) {
  219.             $this->id = $search_user['User']['id'];
  220.             $save = $this->saveField($key, $value);
  221.  
  222.                     // on reset les données
  223.                     $this->userData = null;
  224.  
  225.             return $save;
  226.             }
  227.     }
  228.     }
  229.  
  230.     public function getUsernameByID($id) {
  231.     $search_user = $this->find('first', array('conditons' => array('id' => $id)));
  232.     return (!empty($search_user)) ? $search_user['User']['pseudo'] : '';
  233.   }
  234.  
  235.     public function getFromUser($key, $search) {
  236.         $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  237.     return (!empty($search_user)) ? $search_user['User'][$key] : NULL;
  238.     }
  239.  
  240.     public function getAllFromCurrentUser() {
  241.     if(CakeSession::check('user')) {
  242.             $search_user = $this->getDataBySession(CakeSession::read('user'));
  243.             return ($search_user) ? $search_user['User'] : NULL;
  244.     }
  245.     }
  246.  
  247.     public function getAllFromUser($search = null) {
  248.         $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  249.     if(!empty($search_user)) {
  250.             return ($search_user) ? $search_user['User'] : NULL;
  251.     }
  252.         return array();
  253.     }
  254.  
  255.     public function setToUser($key, $value, $search) {
  256.     $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  257.     if(!empty($search_user)) {
  258.         $this->id = $search_user['User']['id'];
  259.         return $this->saveField($key, $value);
  260.     }
  261.     }
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement