forked from howframework/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_user.php
executable file
·113 lines (95 loc) · 3.14 KB
/
m_user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_user extends CI_Model {
var $table = 'users';
var $max_idle_time = 300; // allowed idle time in secs, 300 secs = 5 minute
function __construct()
{
// Call the Model constructor
parent::__construct();
}
// Save a new user.
function save( $user_data ) {
$this->db->insert($this->table , $user_data);
return $this->db->insert_id();
}
// Update an existing user
function update( $user_data ) {
if (isset($user_data['id'])) {
$this->db->where('id', $user_data['id'] );
$this->db->update( $this->table , $user_data);
return $this->db->affected_rows();
}
return false;
}
// get user by username
function get_by_username( $username ) {
$query = $this->db->get_where($this->table, array('username' => $username), 1);
if( $query->num_rows() > 0 ) return $query->row_array();
return false;
}
// set login session
function allow_pass( $user_data ) {
$this->session->set_userdata( array( 'last_activity' => time(), 'logged_in' => 'yes', 'user' => $user_data ) );
}
// Check if user is logged in and update session
function is_logged_in() {
$last_activity = $this->session->userdata('last_activity');
$logged_in = $this->session->userdata('logged_in');
$user = $this->session->userdata('user');
if ( ($logged_in == 'yes')
&& ((time() - $last_activity) < $this->max_idle_time )) {
$this->allow_pass( $user );
return true;
} else {
$this->remove_pass();
return false;
}
}
// remove pass
function remove_pass() {
$array_items = array('last_activity' => '', 'logged_in' => '', 'user' => '');
$this->session->unset_userdata($array_items);
}
// get user by id
function get_by_id( $id ) {
$query = $this->db->get_where($this->table, array('id' => $id), 1);
if( $query->num_rows() > 0 ) return $query->row_array();
return false;
}
// Check if email address already exists
function email_exists( $email ) {
$query = $this->db->get_where($this->table, array('email' => $email), 1);
if( $query->num_rows() > 0 ) return true;
return false;
}
// Check if username already exists
function username_exists( $username ) {
$query = $this->db->get_where($this->table, array('username' => $username), 1);
if( $query->num_rows() > 0 ) return true;
return false;
}
// Generate hashed password
function hash_password( $password ) {
$salt = $this->generate_salt();
return $salt.'.'.md5( $salt.$password);
}
// Check if password is valid
function check_password( $password, $hashed_password ) {
list($salt, $hash) = explode('.', $hashed_password);
$hashed2 = $salt.'.'.md5( $salt.$password);
return ($hashed_password == $hashed2);
}
// create salt for password hashing
private function generate_salt( $length = 10 ) {
$characterList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$i = 0;
$salt = "";
while ($i < $length) {
$salt .= $characterList{mt_rand(0, (strlen($characterList) - 1))};
$i++;
}
return $salt;
}
}
/* End of file m_user.php */
/* Location: ./application/models/m_user.php */