Guest User

Untitled

a guest
Mar 2nd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. <?php
  2.  
  3. class User extends ActiveRecord\Model
  4. {
  5. static $has_many = array(
  6. array('posts'),
  7. array('pictures'),
  8. array('messages'),
  9. array('recipients', 'foreign_key' => 'recipient_id', 'class_name' => 'Message'),
  10. array('senders', 'foreign_key' => 'sender_id', 'class_name' => 'Message'),
  11. );
  12.  
  13. static $belongs_to = array(
  14. array('picture'),
  15. );
  16.  
  17. static $validates_presence_of = array(
  18. array('username'),
  19. array('hashed_password'),
  20. array('first_name'),
  21. array('last_name'),
  22. array('email'),
  23. );
  24.  
  25. static $validates_length_of = array(
  26. array('username', 'minimum' => '4', 'too_short' => 'is too short.'),
  27. array('username', 'maximum' => '32', 'too_long' => 'is too long.'),
  28. array('first_name', 'maximum' => '32', 'too_long' => 'is too long.'),
  29. array('last_name', 'maximum' => '32', 'too_long' => 'is too long.'),
  30. );
  31.  
  32. static $validates_format_of = array(
  33. //array('email', 'with' => '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/'),
  34. );
  35.  
  36. static $validates_uniqueness_of = array(
  37. array('email', 'message' => 'is already in use.'),
  38. array('username', 'message' => 'is already in use.'),
  39. );
  40.  
  41.  
  42. function set_password($plaintext)
  43. {
  44. $this->hashed_password = $this->hash_password($plaintext);
  45. }
  46.  
  47. private function hash_password($password)
  48. {
  49. $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
  50. $hash = hash('sha256', $salt . $password);
  51.  
  52. return $salt . $hash;
  53. }
  54.  
  55. private function validate_password($password)
  56. {
  57. $salt = substr($this->hashed_password, 0, 64);
  58. $hash = substr($this->hashed_password, 64, 64);
  59.  
  60. $password_hash = hash('sha256', $salt . $password);
  61.  
  62. return $password_hash == $hash;
  63. }
  64.  
  65. public static function validate_login($username, $password)
  66. {
  67. $user = User::find_by_username($username);
  68.  
  69. if($user && $user->validate_password($password))
  70. {
  71. User::login($user->id);
  72. return $user;
  73. }
  74. else
  75. return FALSE;
  76. }
  77.  
  78. public static function login($user_id)
  79. {
  80. $CI =& get_instance();
  81. $CI->session->set_userdata('user_id', $user_id);
  82. }
  83.  
  84. public static function logout()
  85. {
  86. $CI =& get_instance();
  87. $CI->session->sess_destroy();
  88. }
  89.  
  90. function register($data)
  91. {
  92. $user = User::create(array(
  93. 'username' => $data['username'],
  94. 'hashed_password' => User::hash_password($data['password']),
  95. 'first_name' => $data['first_name'],
  96. 'last_name' => $data['last_name'],
  97. 'email' => $data['email'],
  98. ));
  99.  
  100. if($user)
  101. return $user;
  102. else
  103. return FALSE;
  104. }
  105.  
  106. }
Add Comment
Please, Sign In to add comment