Advertisement
Guest User

Untitled

a guest
Jan 30th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Players extends CI_Model {
  5.  
  6. /**
  7. * Tries to get a player by the specified email.
  8. *
  9. * @param $email {string} The email of the player
  10. * @returns The player object, or null if the player isn't found
  11. */
  12. public function get_player_by_email($email)
  13. {
  14. $query_obj = $this->db->get_where('players', array('email' => $email), 1);
  15.  
  16. return $query_obj->row();
  17. }
  18.  
  19. public function sign_out($email)
  20. {
  21. $this->session->unset_userdata($email);
  22. }
  23.  
  24. /**
  25. * Checks if a username is taken.
  26. *
  27. * @param $username {string} The username to check on
  28. * @returns True if the username is taken, false otherwise
  29. */
  30. public function username_taken($username)
  31. {
  32. return $this->db->from('players')->where(array('username' => $username))->limit(1)->count_all_results() > 0;
  33. }
  34.  
  35. /**
  36. * Tries to login
  37. */
  38. public function try_login()
  39. {
  40. $email = $this->input->post('_email', true);
  41. $password = $this->input->post('_password', true);
  42. $remember_me = $this->input->post('_remember_me', true);
  43. $pin = $this->input->post('_pin', true);
  44.  
  45. if (!trim($email) || !trim($password) || !trim($pin))
  46. {
  47. $this->session->set_flashdata('error', 'Please fill in all fields!');
  48. }
  49. else if (!valid_email($email))
  50. {
  51. $this->session->set_flashdata('Please enter a valid email!');
  52. }
  53. else if (!is_numeric($pin) || strlen($pin) !== 4)
  54. {
  55. $this->session->set_flashdata('Please enter your 4-digit pin!');
  56. }
  57. else
  58. {
  59. $success = false;
  60. $player = $this->get_player_by_email($email);
  61.  
  62. if ($player)
  63. {
  64. $success = password_verify($password, $player->password) && $player->pin == $pin;
  65. }
  66.  
  67. if ($success)
  68. {
  69. $this->session->set_userdata('email', $email);
  70.  
  71. header('Refresh:0;url=/start');
  72. return;
  73. }
  74. else
  75. {
  76. $this->session->set_flashdata('error', 'Wrong email, password and/or pin!');
  77. }
  78. }
  79. }
  80.  
  81. public function register()
  82. {
  83. $email = $this->input->post('email', true);
  84. $username = $this->input->post('username', true);
  85. $password = $this->input->post('password', true);
  86. $password_repeat = $this->input->post('password_repeat', true);
  87. $accept_tos = $this->input->post('accept_tos', true);
  88. $pin = $this->input->post('pin', true);
  89.  
  90. $special_characters = array('-', '_');
  91.  
  92. $errors = [];
  93.  
  94. if (!trim($email) || !trim($username) || !trim($password) || !trim($password_repeat) || !trim($pin))
  95. {
  96. $errors[] = 'Please fill in all fields!';
  97. }
  98. if (!empty($this->Players->get_player_by_email($email)))
  99. {
  100. $errors[] = 'Email already in use!';
  101. }
  102. if ($this->Players->username_taken($username))
  103. {
  104. $errors[] = 'Username already in use!';
  105. }
  106. if(!(strlen($username) >= 4))
  107. {
  108. $errors[] = 'Username has to be atleast 4 characters long!';
  109. }
  110. if(!ctype_alnum(str_replace($special_characters, '', $username)))
  111. {
  112. $errors[] = 'Username can\'t contain any special characters!';
  113. }
  114. if (strlen($password) < 8)
  115. {
  116. $errors[] = 'Password has to be at least 8 characters long!';
  117. }
  118. if ($password !== $password_repeat)
  119. {
  120. $errors[] = 'Passwords don\'t match!';
  121. }
  122. if (!$accept_tos && $accept_tos !== 'on')
  123. {
  124. $errors[] = 'You have to accept the ToS and privacy policy!';
  125. }
  126. if (!is_numeric($pin) || strlen($pin) !== 4)
  127. {
  128. $errors[] = 'Pin have to be 4 numbers!';
  129. }
  130.  
  131. if (count($errors) > 0)
  132. {
  133. $this->session->set_flashdata('errors', $errors);
  134. }
  135. else
  136. {
  137. $this->db->insert('players', array(
  138. 'username' => $username,
  139. 'email' => $email,
  140. 'password' => password_hash($password, PASSWORD_BCRYPT),
  141. 'pin' => $pin
  142. ));
  143.  
  144. $this->session->set_userdata('email', $email);
  145.  
  146. header('Refresh:0;url=/');
  147. return;
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement