Advertisement
Guest User

Untitled

a guest
Dec 1st, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 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['username'] = before_display($data['pseudo']);
  56. $data_to_save['email'] = before_display($data['email']);
  57.  
  58. $data_to_save['ip'] = isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
  59. $data_to_save['rank'] = 0;
  60.  
  61. $data_to_save['password'] = $UtilComponent->password($data['password'], $data['pseudo']);
  62.  
  63. $this->create();
  64. $this->set($data_to_save);
  65. $this->save();
  66. return $this->getLastInsertId();
  67. }
  68.  
  69. public function login($data, $need_email_confirmed = false, $UtilComponent) {
  70. $LoginRetryTable = ClassRegistry::init('LoginRetry');
  71. $ip = isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
  72. $findRetryWithIP = $LoginRetryTable->find('first', array('conditions' => array('ip' => $ip)));
  73.  
  74. // 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
  75.  
  76. if(empty($findRetryWithIP) || $findRetryWithIP['LoginRetry']['count'] < 10 || strtotime('+2 hours', strtotime($findRetryWithIP['LoginRetry']['modified'])) < time()) {
  77.  
  78. $search_user = $this->find('first', array('conditions' => array('pseudo' => $data['pseudo'], 'password' => $UtilComponent->password($data['password'], $data['pseudo']))));
  79. if(!empty($search_user)) {
  80.  
  81. 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']) {
  82. // mail non confirmé
  83. return 'USER__MSG_NOT_CONFIRMED_EMAIL';
  84. }
  85.  
  86. return array('status' => true, 'session' => $search_user['User']['id']);
  87.  
  88. } else {
  89.  
  90. if(!isset($findRetryWithIP['LoginRetry']) || strtotime('+2 hours', strtotime($findRetryWithIP['LoginRetry']['modified'])) < time()) { //on reset à 0
  91. $findRetryWithIP['LoginRetry']['count'] = 0;
  92. }
  93.  
  94. if(empty($findRetryWithIP) || !isset($findRetryWithIP['LoginRetry']['id'])) { // si il avais rien fail encore
  95.  
  96. $LoginRetryTable->create();
  97. $LoginRetryTable->set(array(
  98. 'ip' => $ip,
  99. 'count' => 1
  100. ));
  101. $LoginRetryTable->save();
  102.  
  103. } else {
  104.  
  105. $LoginRetryTable->read(null, $findRetryWithIP['LoginRetry']['id']);
  106. $LoginRetryTable->set(array(
  107. 'ip' => $ip,
  108. 'count' => ($findRetryWithIP['LoginRetry']['count']+1),
  109. 'modified' => date('Y-m-d H:i:s')
  110. ));
  111. $LoginRetryTable->save();
  112.  
  113. }
  114.  
  115. return 'USER__ERROR_INVALID_CREDENTIALS';
  116. }
  117.  
  118. } else {
  119. return 'LOGIN__BLOCKED';
  120. }
  121. }
  122.  
  123. public function resetPass($data, $controller) {
  124. $UtilComponent = $controller->Util;
  125. if($data['password'] == $data['password2']) {
  126. unset($data['password2']);
  127. $search = $this->find('all', array('conditions' => array('email' => $data['email'])));
  128. if(!empty($search)) {
  129.  
  130. $this->Lostpassword = ClassRegistry::init('Lostpassword');
  131. $Lostpassword = $this->Lostpassword->find('all', array('conditions' => array('email' => $data['email'], 'key' => $data['key'])));
  132. if(!empty($Lostpassword) && strtotime('+1 hour', strtotime($Lostpassword[0]['Lostpassword']['created'])) >= time()) {
  133.  
  134. $data_to_save['password'] = $UtilComponent->password($data['password'], $search['0']['User']['pseudo']);
  135.  
  136. $event = new CakeEvent('beforeResetPassword', $this, array('user_id' => $search[0]['User']['id'], 'new_password' => $data_to_save['password']));
  137. $controller->getEventManager()->dispatch($event);
  138. if($event->isStopped()) {
  139. return $event->result;
  140. }
  141.  
  142. $this->Lostpassword->delete($Lostpassword[0]['Lostpassword']['id']);
  143.  
  144. $this->read(null, $search['0']['User']['id']);
  145. $this->set($data_to_save);
  146. $this->save();
  147.  
  148. return array('status' => true, 'session' => $search[0]['User']['id']);
  149.  
  150. } else {
  151. return 'USER__PASSWORD_RESET_INVALID_KEY';
  152. }
  153. } else {
  154. return 'ERROR__INTERNAL_ERROR';
  155. }
  156. } else {
  157. return 'USER__ERROR_PASSWORDS_NOT_SAME';
  158. }
  159. }
  160.  
  161. private function getDataBySession($session) {
  162. if(empty($this->userData)) {
  163. $this->userData = $this->find('first', array('conditions' => array('id' => $session)));
  164. }
  165. return $this->userData;
  166. }
  167.  
  168. public function isConnected() {
  169. if(CakeSession::check('user') == false) {
  170. return false;
  171. } else {
  172. // Je cherche si il la session est pas vide et si elle est dans la bdd
  173. $user = $this->find('all', array(
  174. 'conditions' => array(
  175. 'id' => CakeSession::read('user'),
  176. )
  177. ));
  178. return (isset($user['0']['User']['id']));
  179. }
  180. }
  181.  
  182. public function isAdmin() {
  183. if(CakeSession::check('user') == false) {
  184. return false;
  185. } else {
  186. // Je cherche si il la session est pas vide et si elle est dans la bdd
  187. $user = $this->getDataBySession(CakeSession::read('user'));
  188. return (isset($user['User']['id']) AND $user['User']['rank'] == 3 OR $user['User']['rank'] == 4);
  189. }
  190. }
  191.  
  192. public function __makeCondition($search) {
  193. if((string)(int)$search == $search) {
  194. return array(
  195. 'id' => intval($search)
  196. );
  197. } else {
  198. return array(
  199. 'pseudo' => $search
  200. );
  201. }
  202. }
  203.  
  204. public function exist($search) { //username || id
  205. $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  206. return (!empty($search_user));
  207. }
  208.  
  209. public function getKey($key) {
  210. if(CakeSession::check('user')) {
  211. $search_user = $this->getDataBySession(CakeSession::read('user'));
  212. return ($search_user) ? $search_user['User'][$key] : '';
  213. }
  214. }
  215.  
  216. public function setKey($key, $value) {
  217. if(CakeSession::check('user')) {
  218. $search_user = $this->getDataBySession(CakeSession::read('user'));
  219. if($search_user) {
  220. $this->id = $search_user['User']['id'];
  221. $save = $this->saveField($key, $value);
  222.  
  223. // on reset les données
  224. $this->userData = null;
  225.  
  226. return $save;
  227. }
  228. }
  229. }
  230.  
  231. public function getUsernameByID($id) {
  232. $search_user = $this->find('first', array('conditons' => array('id' => $id)));
  233. return (!empty($search_user)) ? $search_user['User']['pseudo'] : '';
  234. }
  235.  
  236. public function getFromUser($key, $search) {
  237. $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  238. return (!empty($search_user)) ? $search_user['User'][$key] : NULL;
  239. }
  240.  
  241. public function getAllFromCurrentUser() {
  242. if(CakeSession::check('user')) {
  243. $search_user = $this->getDataBySession(CakeSession::read('user'));
  244. return ($search_user) ? $search_user['User'] : NULL;
  245. }
  246. }
  247.  
  248. public function getAllFromUser($search = null) {
  249. $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  250. if(!empty($search_user)) {
  251. return ($search_user) ? $search_user['User'] : NULL;
  252. }
  253. return array();
  254. }
  255.  
  256. public function setToUser($key, $value, $search) {
  257. $search_user = $this->find('first', array('conditions' => $this->__makeCondition($search)));
  258. if(!empty($search_user)) {
  259. $this->id = $search_user['User']['id'];
  260. return $this->saveField($key, $value);
  261. }
  262. }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement