· 9 years ago · Jan 03, 2017, 06:16 AM
1<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2
3require_once('phpass-0.3/PasswordHash.php');
4
5define('PHPASS_HASH_STRENGTH', 8);
6define('PHPASS_HASH_PORTABLE', false);
7
8/**
9 * SimpleLoginSecure Class
10 *
11 * Makes authentication simple and secure.
12 *
13 * Simplelogin expects the following database setup. If you are not using
14 * this setup you may need to do some tweaking.
15 *
16 *
17 * CREATE TABLE `users` (
18 * `user_id` int(10) unsigned NOT NULL auto_increment,
19 * `user_email` varchar(255) NOT NULL default '',
20 * `user_pass` varchar(60) NOT NULL default '',
21 * `user_date` datetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'Creation date',
22 * `user_modified` datetime NOT NULL default '0000-00-00 00:00:00',
23 * `user_last_login` datetime NULL default NULL,
24 * PRIMARY KEY (`user_id`),
25 * UNIQUE KEY `user_email` (`user_email`),
26 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
27 *
28 * @package SimpleLoginSecure
29 * @version 2.1.1
30 * @author Stéphane Bourzeix, Pixelmio <stephane[at]bourzeix.com>
31 * @copyright Copyright (c) 2012-2013, Stéphane Bourzeix
32 * @license http://www.gnu.org/licenses/gpl-3.0.txt
33 * @link https://github.com/DaBourz/SimpleLoginSecure
34 */
35class SimpleLoginSecure
36{
37 var $CI;
38 var $user_table = 'users';
39
40 /**
41 * Create a user account
42 *
43 * @access public
44 * @param string
45 * @param string
46 * @param bool
47 * @return bool
48 */
49 public function create($user_email, $user_pass, $user_nama, $user_admin, $user_seksi)
50 {
51 $this->CI =& get_instance();
52
53 //Make sure account info was sent
54 if($user_email == '' OR $user_pass == '') {
55 return false;
56 }
57
58 //Check against user table
59 $this->CI->db->where('user_email', $user_email);
60 $query = $this->CI->db->get_where($this->user_table);
61
62 if ($query->num_rows() > 0) //user_email already exists
63 return false;
64
65 //Hash user_pass using phpass
66 $hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
67 $user_pass_hashed = $hasher->HashPassword($user_pass);
68
69 //Insert account into the database
70 $data = array(
71 'user_email' => $user_email,
72 'user_nama' => $user_nama,
73 'user_pass' => $user_pass_hashed,
74 'user_admin' => $user_admin,
75 'user_seksi' => $user_seksi,
76 'user_date' => date('c'),
77 'user_modified' => date('c'),
78 );
79
80 $this->CI->db->set($data);
81
82 if(!$this->CI->db->insert($this->user_table)) //There was a problem!
83 return false;
84 /*
85 if($auto_login)
86 $this->login($user_email, $user_pass);
87 */
88 return true;
89 }
90
91 /**
92 * Update a user account
93 *
94 * Only updates the email, just here for you can
95 * extend / use it in your own class.
96 *
97 * @access public
98 * @param integer
99 * @param string
100 * @param bool
101 * @return bool
102 */
103 public function update($user_id, $user_email, $user_nama, $user_admin, $user_seksi, $reset_pass='0')
104 {
105 $this->CI =& get_instance();
106
107 //Make sure account info was sent
108 if($user_id == null OR $user_email == '') {
109 return false;
110 }
111
112 //Check against user table
113 $this->CI->db->where('user_id', $user_id);
114 $query = $this->CI->db->get_where($this->user_table);
115
116 if ($query->num_rows() == 0){ // user don't exists
117 return false;
118 }
119
120 //Update account into the database
121 $data = array(
122 'user_email' => $user_email,
123 'user_nama' => $user_nama,
124 'user_admin' => $user_admin,
125 'user_seksi' => $user_seksi,
126 'user_modified' => date('c'),
127 );
128
129 if ($reset_pass!=='0') {
130 //Hash user_pass using phpass
131 $hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
132 $user_pass_hashed = $hasher->HashPassword($user_email);
133 $data['user_pass'] = $user_pass_hashed;
134 }
135
136 $this->CI->db->where('user_id', $user_id);
137
138 if(!$this->CI->db->update($this->user_table, $data)) //There was a problem!
139 return false;
140
141 return true;
142 }
143
144 /**
145 * Login and sets session variables
146 *
147 * @access public
148 * @param string
149 * @param string
150 * @return bool
151 */
152 public function login($user_email = '', $user_pass = '')
153 {
154 $this->CI =& get_instance();
155
156 if($user_email == '' OR $user_pass == '')
157 return false;
158
159
160 //Check if already logged in
161 if($this->CI->session->userdata('user_email') == $user_email)
162 return true;
163
164
165 //Check against user table
166 $this->CI->db->where('user_email', $user_email);
167 $query = $this->CI->db->get_where($this->user_table);
168
169
170 if ($query->num_rows() > 0)
171 {
172 $hasil = $query->row_array();
173
174 $hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
175
176 if(!$hasher->CheckPassword($user_pass, $hasil['user_pass']))
177 return false;
178
179 //Destroy old session
180 //$this->CI->session->sess_destroy();
181
182 //Create a fresh, brand new session
183 //$this->CI->session->sess_regenerate();
184 //session_start();
185
186 $this->CI->db->simple_query('UPDATE ' . $this->user_table . ' SET user_last_login = "' . date('c') . '" WHERE user_id = ' . $hasil['user_id']);
187
188 //ambil nama seksi
189 $this->CI->db->where('id', $hasil['user_seksi']);
190 $queryS = $this->CI->db->get_where('ref_seksi');
191 $hasilS = $queryS->row_array();
192
193 //Set session data
194 unset($user_data['user_pass']);
195 $user_data['nip9'] = $hasil['user_email']; // for compatibility with Simplelogin
196 $user_data['user_sisuka'] = $hasil['user_email']; // for compatibility with Simplelogin
197 $user_data['user_admin'] = $hasil['user_admin'];
198 $user_data['nama'] = $hasil['user_nama'];
199 $user_data['jabatan'] = "Admin Surat";
200 $user_data['logged_in'] = true;
201 $user_data['id_seksi'] = $hasil['user_seksi'];
202 $user_data['seksi'] = $hasilS['nama_sikka'];
203 $this->CI->session->set_userdata($user_data);
204
205 return true;
206 }
207 else
208 {
209 return false;
210 }
211
212 }
213
214 /**
215 * Logout user
216 *
217 * @access public
218 * @return void
219 */
220 public function logout() {
221 $this->CI =& get_instance();
222
223 $this->CI->session->sess_destroy();
224 }
225
226 /**
227 * Delete user
228 *
229 * @access public
230 * @param integer
231 * @return bool
232 */
233 public function delete($user_id)
234 {
235 $this->CI =& get_instance();
236
237 if(!is_numeric($user_id))
238 return false;
239
240 return $this->CI->db->delete($this->user_table, array('user_id' => $user_id));
241 }
242
243
244 /**
245 * Edit a user password
246 * @author Stéphane Bourzeix, Pixelmio <stephane[at]bourzeix.com>
247 * @author Diego Castro <castroc.diego[at]gmail.com>
248 *
249 * @access public
250 * @param string
251 * @param string
252 * @param string
253 * @return bool
254 */
255 public function edit_password($user_email, $old_pass, $new_pass)
256 {
257 $this->CI =& get_instance();
258 // Check if the password is the same as the old one
259 $this->CI->db->select('user_pass');
260 $query = $this->CI->db->get_where($this->user_table, array('user_email' => $user_email));
261 $user_data = $query->row_array();
262
263 $hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
264 if (!$hasher->CheckPassword($old_pass, $user_data['user_pass'])){ //old_pass is the same
265 return FALSE;
266 }
267
268 // Hash new_pass using phpass
269 $user_pass_hashed = $hasher->HashPassword($new_pass);
270 // Insert new password into the database
271 $data = array(
272 'user_pass' => $user_pass_hashed,
273 'user_modified' => date('c')
274 );
275
276 $this->CI->db->set($data);
277 $this->CI->db->where('user_email', $user_email);
278 if(!$this->CI->db->update($this->user_table, $data)){ // There was a problem!
279 return FALSE;
280 } else {
281 return TRUE;
282 }
283 }
284
285}
286?>