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