Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class School_lib
- {
- /**
- *
- * @var object
- */
- private $CI;
- public function __construct()
- {
- $this->CI =& get_instance();
- $this->CI->load->library('session');
- $this->CI->load->helper('cookie');
- $this->CI->load->model('ibn_auth_mod');
- }
- /**
- * create a new user
- *
- * @param string $name
- * @param string $email
- * @param string $password
- */
- public function create_user($name, $email, $password)
- {
- $this->CI->ibn_auth_mod->create_user($name, $email, $password);
- }
- /**
- * check if email is avaiable
- *
- * @param string $email
- */
- public function check_email($email)
- {
- $this->CI->ibn_auth_mod->check_email($email);
- }
- /**
- * login the user
- *
- * @param string $email
- * @param string $password
- */
- public function login($email, $password)
- {
- $user = $this->CI->ibn_auth_mod->login($email, $password);
- if ($user)
- {
- $session = array(
- 'logged' => TRUE,
- 'id' => $user->id,
- 'name' => $user->name,
- 'email' => $user->email,
- 'last_login' => $user->last_login
- );
- $this->create_session($session);
- }
- }
- /**
- * reset user password and email the new one
- *
- * @param string $email
- */
- public function reset_password($email)
- {
- if ($this->check_email($email))
- {
- $random_password = $this->random_string();
- $this->CI->ibn_auth_mod->reset_password($random_password);
- }
- }
- /**
- * generate a random string
- *
- * @param integer $length
- *
- * @return string
- */
- public function random_string($length = 8)
- {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
- $string = '';
- for ($i = 0; $i < $length; $i++)
- {
- $string .= $characters[mt_rand(0, strlen($characters))];
- }
- return $string;
- }
- /**
- * create cookie from database for "remember me"
- *
- * @param integer $user_id
- */
- public function create_cookie($user_id)
- {
- $cookie = sha1(uniqid(rand(), TRUE));
- $this->CI->ibn_auth_mod->create_cookie($user_id, $cookie);
- }
- /**
- * check cookie from database for "remember me"
- *
- * @param string $cookie
- */
- public function check_cookie($cookie)
- {
- $user = $this->CI->ibn_auth_mod->check_cookie($cookie);
- if ($user) $this->autologin($user->id);
- }
- /**
- * create user session
- *
- * @param array $session
- */
- public function create_session($session)
- {
- $this->session->set_userdata($session);
- }
- /**
- * logout the user
- */
- public function logout()
- {
- $this->session->sess_destroy();
- if (get_cookie('remember_me'))
- {
- $this->delete_cookie($this->session->userdata('id'));
- }
- }
- /**
- * delete cookie from database and remember_me cookie
- *
- * @param integer $user_id
- */
- public function delete_cookie($user_id)
- {
- delete_cookie("remember_me");
- $this->CI->ibn_auth_mod->delete_cookie($user_id);
- }
- /**
- * auto login the user for "remember me"
- *
- * @param integer $id
- */
- public function auto_login($id)
- {
- $user = $this->CI->ibn_auth_mod->auto_login($id);
- if ($user)
- {
- $session = array(
- 'logged' => TRUE,
- 'id' => $user->id,
- 'name' => $user->name,
- 'email' => $user->email,
- 'last_login' => $user->last_login
- );
- $this->create_session($session);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement