Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User extends ActiveRecord\Model
- {
- static $has_many = array(
- array('posts'),
- array('pictures'),
- array('messages'),
- array('recipients', 'foreign_key' => 'recipient_id', 'class_name' => 'Message'),
- array('senders', 'foreign_key' => 'sender_id', 'class_name' => 'Message'),
- );
- static $belongs_to = array(
- array('picture'),
- );
- static $validates_presence_of = array(
- array('username'),
- array('hashed_password'),
- array('first_name'),
- array('last_name'),
- array('email'),
- );
- static $validates_length_of = array(
- array('username', 'minimum' => '4', 'too_short' => 'is too short.'),
- array('username', 'maximum' => '32', 'too_long' => 'is too long.'),
- array('first_name', 'maximum' => '32', 'too_long' => 'is too long.'),
- array('last_name', 'maximum' => '32', 'too_long' => 'is too long.'),
- );
- static $validates_format_of = array(
- //array('email', 'with' => '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/'),
- );
- static $validates_uniqueness_of = array(
- array('email', 'message' => 'is already in use.'),
- array('username', 'message' => 'is already in use.'),
- );
- function set_password($plaintext)
- {
- $this->hashed_password = $this->hash_password($plaintext);
- }
- private function hash_password($password)
- {
- $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
- $hash = hash('sha256', $salt . $password);
- return $salt . $hash;
- }
- private function validate_password($password)
- {
- $salt = substr($this->hashed_password, 0, 64);
- $hash = substr($this->hashed_password, 64, 64);
- $password_hash = hash('sha256', $salt . $password);
- return $password_hash == $hash;
- }
- public static function validate_login($username, $password)
- {
- $user = User::find_by_username($username);
- if($user && $user->validate_password($password))
- {
- User::login($user->id);
- return $user;
- }
- else
- return FALSE;
- }
- public static function login($user_id)
- {
- $CI =& get_instance();
- $CI->session->set_userdata('user_id', $user_id);
- }
- public static function logout()
- {
- $CI =& get_instance();
- $CI->session->sess_destroy();
- }
- function register($data)
- {
- $user = User::create(array(
- 'username' => $data['username'],
- 'hashed_password' => User::hash_password($data['password']),
- 'first_name' => $data['first_name'],
- 'last_name' => $data['last_name'],
- 'email' => $data['email'],
- ));
- if($user)
- return $user;
- else
- return FALSE;
- }
- }
Add Comment
Please, Sign In to add comment