1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
---|
2 | |
---|
3 | class User_model extends CI_Model |
---|
4 | { |
---|
5 | |
---|
6 | const TOKENPW = 'violet'; |
---|
7 | |
---|
8 | public function __construct(){ |
---|
9 | parent::__construct(); |
---|
10 | $this->vservices->setApiUrl($this->config->item('api_url')); |
---|
11 | $this->vservices->setConnection($this->curl); |
---|
12 | } |
---|
13 | |
---|
14 | public function checkLogin ($src, $token, $username, $password) |
---|
15 | { |
---|
16 | $data = $this->vservices->actionExecute('login', array('src' => $src, 'token' => $token, 'username' => $username, 'password' => $password), 'user'); |
---|
17 | |
---|
18 | $arr_users = explode("&", $data); |
---|
19 | $str_status = ''; |
---|
20 | $str_fullname = ''; |
---|
21 | $str_usid = ''; |
---|
22 | $usid = ''; |
---|
23 | $fullname = ''; |
---|
24 | |
---|
25 | for ($i=0; $i < count($arr_users); $i++) |
---|
26 | { |
---|
27 | if (strpos($arr_users[$i], 'status=') !== false) |
---|
28 | { |
---|
29 | $str_status = $arr_users[$i]; |
---|
30 | } |
---|
31 | |
---|
32 | if (strpos($arr_users[$i], 'fullname=') !== false) |
---|
33 | { |
---|
34 | $str_fullname = $arr_users[$i]; |
---|
35 | } |
---|
36 | |
---|
37 | if (strpos($arr_users[$i], 'id=') !== false) |
---|
38 | { |
---|
39 | $str_usid = $arr_users[$i]; |
---|
40 | } |
---|
41 | } |
---|
42 | $arr_status = explode("=", $str_status); |
---|
43 | if ($str_fullname != '') |
---|
44 | { |
---|
45 | $arr_fullname = explode("=", $str_fullname); |
---|
46 | $fullname = $arr_fullname[1]; |
---|
47 | } |
---|
48 | if ($str_usid !== '') |
---|
49 | { |
---|
50 | $arr_usid = explode("=", $str_usid); |
---|
51 | $usid = $arr_usid[1]; |
---|
52 | } |
---|
53 | $status = (int)$arr_status[1]; |
---|
54 | |
---|
55 | if ($status == 0 || $status == 4){ |
---|
56 | $us_id = (int)$usid; |
---|
57 | $query = "SELECT * FROM tbluser WHERE us_id = ?"; |
---|
58 | $result = $this->db->query($query, array($us_id)); |
---|
59 | if($result->num_rows() == 1){ |
---|
60 | return $data; |
---|
61 | }else |
---|
62 | { |
---|
63 | return null; |
---|
64 | } |
---|
65 | }else |
---|
66 | { |
---|
67 | return $data; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | public function register ($username, $collaborator, &$status='') |
---|
72 | { |
---|
73 | $user = array(); |
---|
74 | $password = $this->create_random_password(); |
---|
75 | $src = 'SBG'; |
---|
76 | $sms = ''; |
---|
77 | $id = 0; |
---|
78 | $errMsg = ''; |
---|
79 | $token = md5($username.self::TOKENPW); |
---|
80 | $phone = $username; |
---|
81 | |
---|
82 | if (!is_null($collaborator) || $collaborator != ''){ |
---|
83 | if(!$this->isExistcollaborator($collaborator)){ |
---|
84 | $sms = "Khong tim thay cong tac vien nao co so dien thoai [".$collaborator."] tren soanbaigiang.smas.vn!"; |
---|
85 | $status = 0; |
---|
86 | return $status.'|'.$sms; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | $data = $this->vservices->actionExecute('update', array('src'=>$src, 'username'=>$username, 'password'=>$password, 'fullname'=>null, 'phone'=>$username, 'token'=>$token), 'user'); |
---|
91 | parse_str($data); |
---|
92 | switch((int)$status) |
---|
93 | { |
---|
94 | case 0: |
---|
95 | $us_id = $id; |
---|
96 | $user['created_time'] = date('Y-m-d H:i:s'); |
---|
97 | $user['us_id'] = $us_id; |
---|
98 | $user['cellphone'] = $username; |
---|
99 | if (!is_null($collaborator) || $collaborator != ''){ |
---|
100 | $user['collaborator'] = $collaborator; |
---|
101 | } |
---|
102 | $this->db->query('UNLOCK TABLES'); |
---|
103 | $this->db->insert('tbluser', $user); |
---|
104 | $sms = "Chuc mung Qui vi da dang ky thanh cong tai khoan Soan Bai giang Online. Hay dang nhap vao soanbaigiang.smas.vn voi ten truy nhap ".$username.", mat khau ".$password." de su dung."; |
---|
105 | $status = 1; |
---|
106 | break; |
---|
107 | case 2: |
---|
108 | case 4: |
---|
109 | $sms = "So dien thoai cua Qui vi da duoc dang ky!"; |
---|
110 | $status = 0; |
---|
111 | break; |
---|
112 | default: |
---|
113 | $status = 0; |
---|
114 | $sms = $errMsg; |
---|
115 | break; |
---|
116 | } |
---|
117 | |
---|
118 | return $status.'|'.$sms; |
---|
119 | } |
---|
120 | |
---|
121 | public function create_random_password() |
---|
122 | { |
---|
123 | //$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789"; |
---|
124 | $alphabet = "0123456789"; |
---|
125 | $pass = array(); |
---|
126 | $alphaLength = strlen($alphabet) - 1; |
---|
127 | for ($i = 0; $i < 6; $i++) { |
---|
128 | $n = rand(0, $alphaLength); |
---|
129 | $pass[] = $alphabet[$n]; |
---|
130 | } |
---|
131 | return implode($pass); |
---|
132 | } |
---|
133 | |
---|
134 | public function get_user_by_id($us_id) |
---|
135 | { |
---|
136 | $this->db->query('UNLOCK TABLES'); |
---|
137 | $this->db->select('*')->from('tbluser')->where('us_id', $us_id); |
---|
138 | $query = $this->db->get(); |
---|
139 | if($query->num_rows() > 0) |
---|
140 | { |
---|
141 | $row = $query->row_array(); |
---|
142 | return $row; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | public function get_user_info($src, $token, $us_id) |
---|
147 | { |
---|
148 | $data = $this->vservices->actionExecute('getinfo', array('src' => $src, 'us_id' => $us_id, 'token'=>$token), 'user'); |
---|
149 | return $data; |
---|
150 | } |
---|
151 | |
---|
152 | public function update_user($src, $token, $user, $us_id) |
---|
153 | { |
---|
154 | $data = $this->vservices->actionExecute('update', array('src'=>$src, 'token'=>$token, 'us_id'=>$us_id, |
---|
155 | 'password'=>$user['password'], 'fullname'=>$user['fullname'], |
---|
156 | 'school'=>$user['school'], 'province'=>$user['province'], 'oldpass'=>$user['oldpass']), |
---|
157 | 'user'); |
---|
158 | return $data; |
---|
159 | |
---|
160 | } |
---|
161 | |
---|
162 | public function changeProfile($sentNumber, $username, $password, &$status='') |
---|
163 | { |
---|
164 | $user = array(); |
---|
165 | $src = 'SBG'; |
---|
166 | $errMsg = ''; |
---|
167 | $sms = ''; |
---|
168 | $checkSentNumber = $this->get_user_by_cellphone($sentNumber); |
---|
169 | if($checkSentNumber) |
---|
170 | { |
---|
171 | $sms = "So Dien thoai ".$sentNumber." da dang ky tren he thong SBG Online soanbaigiang.smas.vn nen Qui vi khong the doi sang so dien thoai nay duoc."; |
---|
172 | $status = 0; |
---|
173 | return $status.'|'.$sms; |
---|
174 | } |
---|
175 | $result = $this->get_user_by_cellphone($username); |
---|
176 | if($result) |
---|
177 | { |
---|
178 | $us_id = $result['us_id']; |
---|
179 | $token = md5($us_id.self::TOKENPW); |
---|
180 | $data = $this->vservices->actionExecute('update', array('src'=>$src, 'token'=>$token, 'us_id'=>$us_id, 'username'=>$sentNumber, "password"=>$password, 'oldpass'=>$password, 'phone'=>$sentNumber), 'user'); |
---|
181 | parse_str($data); |
---|
182 | switch((int)$status) |
---|
183 | { |
---|
184 | case 0: |
---|
185 | $us_id = $id; |
---|
186 | $user['updated_time'] = date('Y-m-d H:i:s'); |
---|
187 | $user['us_id'] = $us_id; |
---|
188 | $user['cellphone'] = $sentNumber; |
---|
189 | $this->updateUser($us_id, $user); |
---|
190 | $sms = "Qui vi da doi so dien thoai tren soanbaigiang.smas.vn sang so dien thoai [".$sentNumber."]"; |
---|
191 | $status = 1; |
---|
192 | break; |
---|
193 | case 5: |
---|
194 | $sms = "So dien thoai cá»§a Qui vi chua duoc dang ky!"; |
---|
195 | $status = 0; |
---|
196 | break; |
---|
197 | case 6: |
---|
198 | $sms = "Mat khau cua qui vi khong dung !"; |
---|
199 | $status = 0; |
---|
200 | break; |
---|
201 | default: |
---|
202 | $status = 0; |
---|
203 | $sms = $errMsg; |
---|
204 | break; |
---|
205 | } |
---|
206 | }else |
---|
207 | { |
---|
208 | $sms = "So dien thoai cá»§a Qui vi chua duoc dang ky!"; |
---|
209 | $status = 0; |
---|
210 | } |
---|
211 | return $status.'|'.$sms; |
---|
212 | } |
---|
213 | |
---|
214 | public function changePassword($username, $password, &$status = '') |
---|
215 | { |
---|
216 | $user = array(); |
---|
217 | $src = 'SBG'; |
---|
218 | $errMsg = ''; |
---|
219 | $sms = ''; |
---|
220 | $isForgotPassword = false; |
---|
221 | $pass = $password; |
---|
222 | if (is_null($password) || $password == '') |
---|
223 | { |
---|
224 | $pass = $this->create_random_password(); |
---|
225 | $isForgotPassword = true; |
---|
226 | } |
---|
227 | $result = $this->get_user_by_cellphone($username); |
---|
228 | if($result) |
---|
229 | { |
---|
230 | $us_id = $result['us_id']; |
---|
231 | $token = md5($us_id.self::TOKENPW); |
---|
232 | $data = $this->vservices->actionExecute('update', array('src'=>$src, 'token'=>$token, 'us_id'=>$us_id, "password"=>$pass), 'user'); |
---|
233 | parse_str($data); |
---|
234 | switch((int)$status) |
---|
235 | { |
---|
236 | case 0: |
---|
237 | $us_id = $id; |
---|
238 | $user['updated_time'] = date('Y-m-d H:i:s'); |
---|
239 | $this->updateUser($us_id, $user); |
---|
240 | if ($isForgotPassword) |
---|
241 | { |
---|
242 | $sms = "Mat khau moi cua quy vi tren soanbaigiang.smas.vn la [".$pass."]"; |
---|
243 | }else{ |
---|
244 | $sms = "Qui vi da doi thanh cong mat khau tren soanbaigiang.smas.vn."; |
---|
245 | } |
---|
246 | $status = 1; |
---|
247 | break; |
---|
248 | case 5: |
---|
249 | $sms = "So dien thoai cá»§a Qui vi chua duoc dang ky!"; |
---|
250 | $status = 0; |
---|
251 | break; |
---|
252 | default: |
---|
253 | $status = 0; |
---|
254 | $sms = $errMsg; |
---|
255 | break; |
---|
256 | } |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | $sms = "So dien thoai cá»§a Qui vi da duoc dang ky!"; |
---|
261 | $status = 0; |
---|
262 | } |
---|
263 | return $status.'|'.$sms; |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | public function checkLicense ($userId) |
---|
268 | { |
---|
269 | $isNotExpired = false; |
---|
270 | $userInfo = array(); |
---|
271 | $userInfo = $this->get_user_by_id($userId); |
---|
272 | |
---|
273 | $expireDate = $userInfo['expire_date']; |
---|
274 | |
---|
275 | if ((trim($expireDate) == '')) return $isNotExpired; |
---|
276 | |
---|
277 | $today = mktime(); |
---|
278 | $expireTime = strtotime($expireDate.' 23:59:59'); |
---|
279 | $isNotExpired = ($today < $expireTime); |
---|
280 | return $isNotExpired; |
---|
281 | } |
---|
282 | |
---|
283 | public function registerVip($username, $p_code) |
---|
284 | { |
---|
285 | $user = array(); |
---|
286 | $sms = ''; |
---|
287 | |
---|
288 | $result = $this->get_user_by_cellphone($username); |
---|
289 | $package = $this->get_package((int)$p_code); |
---|
290 | if($result){ |
---|
291 | |
---|
292 | if ($result['acc_balanced'] >= $package['p_price']) |
---|
293 | { |
---|
294 | $us_id = $result['us_id']; |
---|
295 | $user['acc_balanced'] = $result['acc_balanced'] - $package['p_price']; |
---|
296 | if ($this->checkLicense($us_id)){ |
---|
297 | $user['expire_date'] = date('Y-m-d', strtotime($result['expire_date'].' + '.$package['p_period'].' month')); |
---|
298 | }else |
---|
299 | { |
---|
300 | $user['expire_date'] = date('Y-m-d', strtotime('now + '.$package['p_period'].' month')); |
---|
301 | } |
---|
302 | |
---|
303 | $user['p_id'] = $package['p_id']; |
---|
304 | $user['updated_time'] = date('Y-m-d H:i:s'); |
---|
305 | |
---|
306 | $this->updateUser($us_id, $user); |
---|
307 | $sms = "Chuc mung Qui vi da dang ky su dung Soan Bai giang truc tuyen tren soanbaigiang.smas.vn. Qui vi co the su dung dich vu den 24h00 ngay ".date('d/m/Y', strtotime($user['expire_date'])); |
---|
308 | $status = 1; |
---|
309 | }else |
---|
310 | { |
---|
311 | $sms = "Tai khoan cua qui vi khong du de dang ky su dung dich vu Soan Bai giang truc tuyen tren soanbaigiang.smas.vn. Hay nap tien va dang ky lai. Cam on!"; |
---|
312 | $status = 0; |
---|
313 | } |
---|
314 | |
---|
315 | }else{ |
---|
316 | $sms = "Tai khoan [".$username."] chua dang ki tai khoan tren he thong SBG Online soanbaigiang.smas.vn."; |
---|
317 | $status = 0; |
---|
318 | } |
---|
319 | return $status.'|'.$sms; |
---|
320 | } |
---|
321 | |
---|
322 | public function rechargeAccount($username, $amount) |
---|
323 | { |
---|
324 | $user = array(); |
---|
325 | $sms = ''; |
---|
326 | $result = $this->get_user_by_cellphone($username); |
---|
327 | if($result) |
---|
328 | { |
---|
329 | $us_id = $result['us_id']; |
---|
330 | $user['acc_balanced'] = $result['acc_balanced'] + $amount; |
---|
331 | $user['updated_time'] = date('Y-m-d H:i:s'); |
---|
332 | $this->updateUser($us_id, $user); |
---|
333 | $sms = "Qui vi da nap thanh cong ".$amount."VND vao tai khoan ".$username." tren soanbaigiang.smas.vn"; |
---|
334 | $status = 1; |
---|
335 | }else |
---|
336 | { |
---|
337 | $sms = "Tai khoan ".$username." chua dang ki tai khoan tren he thong SBG Online soanbaigiang.smas.vn."; |
---|
338 | $status = 0; |
---|
339 | } |
---|
340 | return $status.'|'.$sms; |
---|
341 | } |
---|
342 | |
---|
343 | public function get_user_by_cellphone($cellphone) |
---|
344 | { |
---|
345 | $this->db->query('UNLOCK TABLES'); |
---|
346 | $sql = 'SELECT * FROM tbluser where cellphone = ? LIMIT 0,1'; |
---|
347 | $query = $this->db->query($sql, array($cellphone)); |
---|
348 | $row = $query->row_array(); |
---|
349 | return $row; |
---|
350 | } |
---|
351 | |
---|
352 | public function isExistcollaborator($collaborator) |
---|
353 | { |
---|
354 | $this->db->query('UNLOCK TABLES'); |
---|
355 | $this->db->select('*')->from('tblcollaborator')->where('login_name', $collaborator)->limit(1); |
---|
356 | $result = $this->db->get()->row_array(); |
---|
357 | if ($result) return true; else return false; |
---|
358 | } |
---|
359 | |
---|
360 | public function updateUser($us_id, $data) |
---|
361 | { |
---|
362 | $this->db->query('UNLOCK TABLES'); |
---|
363 | $this->db->where('us_id', $us_id); |
---|
364 | $this->db->update('tbluser', $data); |
---|
365 | } |
---|
366 | |
---|
367 | public function get_package($p_code) |
---|
368 | { |
---|
369 | $this->db->query('UNLOCK TABLES'); |
---|
370 | $sql = 'SELECT * FROM tblservicepackage where p_code = ? LIMIT 0,1'; |
---|
371 | $query = $this->db->query($sql, array($p_code)); |
---|
372 | $row = $query->row_array(); |
---|
373 | return $row; |
---|
374 | } |
---|
375 | |
---|
376 | public function insertSmslog($sentNumber, $serviceID, $commandcode, $info, $receiveTime) |
---|
377 | { |
---|
378 | if (preg_match('/^(84)([0-9]{9,10})$/', $sentNumber, $aryMatch) == 1) { |
---|
379 | $sentNumber = '0'. $aryMatch[2]; |
---|
380 | } |
---|
381 | |
---|
382 | try { |
---|
383 | $user = $this->get_user_by_cellphone($sentNumber); |
---|
384 | } |
---|
385 | catch(Exception $e) { |
---|
386 | |
---|
387 | } |
---|
388 | |
---|
389 | $smslog = array(); |
---|
390 | $smslog['sender'] = $sentNumber; |
---|
391 | $smslog['us_id'] = isset($user['us_id']) ? $user['us_id']:NULL; |
---|
392 | $smslog['service_id'] = $serviceID; |
---|
393 | $smslog['commandcode'] = $commandcode; |
---|
394 | $smslog['info'] = $info; |
---|
395 | $smslog['created_time'] = $receiveTime; |
---|
396 | $this->db->query('UNLOCK TABLES'); |
---|
397 | $sql = 'INSERT INTO tblsmslog(sender, us_id, service_id, commandcode, info, created_time) VALUES(?,?,?,?,?,?)'; |
---|
398 | $query = $this->db->query($sql, $smslog); |
---|
399 | //$this->db->insert('tblsmslog', $smslog); |
---|
400 | } |
---|
401 | |
---|
402 | public function insertPaidlog($sentNumber, $receiveID, $paidType, $amount, $paidTime) |
---|
403 | { |
---|
404 | if (preg_match('/^(84)([0-9]{9,10})$/', $sentNumber, $aryMatch) == 1) { |
---|
405 | $sentNumber = '0'. $aryMatch[2]; |
---|
406 | } |
---|
407 | |
---|
408 | try { |
---|
409 | $user = $this->get_user_by_cellphone($sentNumber); |
---|
410 | } |
---|
411 | catch(Exception $e) { |
---|
412 | |
---|
413 | } |
---|
414 | $paidlog = array(); |
---|
415 | $paidlog['us_id'] = isset($user['us_id']) ? $user['us_id']:NULL; |
---|
416 | $paidlog['paidType'] = $paidType; |
---|
417 | $paidlog['amount'] = $amount; |
---|
418 | $paidlog['paidTime'] = $paidTime; |
---|
419 | $this->db->query('UNLOCK TABLES'); |
---|
420 | $sql = 'INSERT INTO tblpaidlog(us_id, paidType, amount, paidTime) VALUES (?,?,?,?)'; |
---|
421 | $query = $this->db->query($sql, $paidog); |
---|
422 | } |
---|
423 | } |
---|