-
-
Save NaszvadiG/6471149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/* | |
* statuses: | |
* 0 - banned | |
* 1 - member | |
* 2 - moderator | |
* 3 - admin | |
* | |
* | |
* */ | |
class User { | |
public $error; | |
public $data = array(); | |
public function User() | |
{ | |
$this->CI =& get_instance(); | |
log_message('debug', 'User class initialized'); | |
} | |
public function create_table() | |
{ | |
$sql = "create table if not exists users( | |
id smallint unsigned not null auto_increment primary key, | |
username varchar(255) not null, | |
password varchar(255) not null, | |
email varchar(255) not null, | |
ip varchar(15) not null, | |
status tinyint not null default 1 | |
);"; | |
$this->CI->db->query($sql); | |
return 'Done'; | |
} | |
public function drop_table() | |
{ | |
$sql = 'drop table users;'; | |
$this->CI->db->query($sql); | |
return 'Done'; | |
} | |
/* | |
* @input array('username' => 'admin', 'password' => 'pass', 'email' => 'mail@gmail.com', 'ip' => '127.0.0.1', 'status' => 1) | |
* @inpput true or false for check email and ip default true | |
* */ | |
public function register( $data, $emailcheck=true, $ipcheck=true) | |
{ | |
if( empty($data['username']) || empty($data['email']) || empty($data['password']) ) { | |
return $this->error = 'Data is emptty'; | |
} | |
if($ipcheck) { | |
if( $this->CI->db->get_where('users', array('ip' => $_SERVER['REMOTE_ADDR']))->num_rows() > 0 ) { | |
$this->error = "IP {$_SERVER['REMOTE_ADDR']} already assigned to an account."; | |
return $this->error; | |
} | |
} | |
if($emailcheck) { | |
if( $this->CI->db->get_where('users', array('email' => $data['email']))->num_rows > 0 ) { | |
$this->error = "This {$data['email']} already taken."; | |
return $this->error; | |
} | |
} | |
if( $this->CI->db->get_where('users', array('username' => $data['username']))->num_rows() > 0 ) { | |
$this->error = "This {$data['username']} already taken."; | |
return $this->error; | |
} | |
$data['password'] = $this->crypt($data['password']); | |
$this->CI->db->insert('users', $data); | |
} | |
/* | |
* @input array('username' => 'username, 'password' => 'pass') | |
* @ return boolean true if all good | |
* @optional can return all data from row | |
* */ | |
public function login($data, $return_all_data=false) | |
{ | |
if( empty($data['password']) ) { | |
return $this->error = 'Data password is emptty'; | |
} | |
$data['password'] = $this->crypt( $data['password'] ); | |
$check = $this->CI->db->get_where('users', $data); | |
if( $check->num_rows() == 0 ) { | |
return false; | |
} | |
$Q = $check->row(); | |
$check->free_result(); | |
if($Q->status == 0) { | |
//if banned | |
return false; | |
} | |
$this->CI->session->set_userdata(array( | |
'username' => $Q->username, | |
'status' => $Q->status, | |
'logged_in' => 1 | |
)); | |
if($return_all_data) { | |
return $Q; | |
} else { | |
return true; | |
} | |
} | |
public function logged_in() | |
{ | |
if( !empty($this->CI->session->userdata['logged_in']) AND $this->CI->session->userdata['logged_in'] === 1 ){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function is_admin() | |
{ | |
if( !empty($this->CI->session->userdata['status']) AND $this->CI->session->userdata['status'] === 3 ){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function log_out() | |
{ | |
$this->CI->session->unset_userdata('logged_in'); | |
} | |
private function crypt($data) | |
{ | |
return md5(md5($data)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment