source: pro-violet-viettel/sourcecode/application/modules/frontend/models/user_model.php @ 454

Last change on this file since 454 was 454, checked in by namnd, 11 years ago
File size: 3.9 KB
Line 
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3class User_model extends CI_Model
4{
5       
6        const TOKENPW = 'violet';
7       
8        function __construct(){
9        parent::__construct();
10                $this->vservices->setApiUrl($this->config->item('api_url'));
11                $this->vservices->setConnection($this->curl);
12    }
13       
14        function checkLogin ($src, $token, $username, $password)
15        {
16                $data = $this->vservices->actionExecute('login', array('src' => $src, 'token' => $token, 'username' => $username, 'password' => $password), 'user');           
17                $arr_users = explode("&", $data);
18                $str_status = "";
19                $str_fullname = "";
20                $str_usid = "";
21                $usid = "";
22                $fullname = "";
23                for ($i=0; $i<count($arr_users); $i++)
24                {
25                        if (strpos($arr_users[$i], 'status=') !== false)
26                        {
27                                $str_status = $arr_users[$i];
28                        }
29                       
30                        if (strpos($arr_users[$i], 'fullname=') !== false)
31                        {
32                                $str_fullname = $arr_users[$i];
33                        }
34                       
35                        if (strpos($arr_users[$i], 'id=') !== false)
36                        {
37                                $str_usid = $arr_users[$i];
38                        }
39                }
40                $arr_status = explode("=", $str_status);
41                if ($str_fullname != "")
42                {
43                        $arr_fullname = explode("=", $str_fullname);
44                        $fullname = $arr_fullname[1];
45                }
46                if ($str_usid !== "")
47                {
48                        $arr_usid = explode("=", $str_usid);
49                        $usid = $arr_usid[1];
50                }
51                $status = (int)$arr_status[1];
52                if ($status == 0 || $status = 4){
53                        $us_id = (int)$usid;
54                        $query = "SELECT * FROM tbluser WHERE us_id = ?";
55                        $result = $this->db->query($query, array($us_id));
56                        if($result->num_rows() == 1){
57                                return $data;
58                        }else
59                        {
60                                return null;
61                        }
62                }else
63                {
64                        return $data;
65                }
66        }
67
68        function register ($sentNumber, $receiverID, $serviceID, $commandcode, $info, $receiveTime)
69        {
70                //1. check collaborator info if $receiverID existed
71                //2. insert new user to database
72                //3. create random password for new user
73                //4. return sms contain password
74               
75                $username = '';
76                $collaborator = '';
77                if (is_null($receiverID) || $receiverID == '') {
78                        $username = $sentNumber;
79                }
80                else {
81                        $username = $receiverID;
82                        $collaborator = $sentNumber;
83                }
84                $user = array();
85                $smslog = array();
86                $password = $this->create_random_password();
87                $src = 'SBG';
88                $token = md5($username.self::TOKENPW);
89                $phone = $username;
90                $data = $this->vservices->actionExecute('update', array('src'=>$src, 'username'=>$username, 'password'=>$password, 'fullname'=>" ", 'phone'=>$username, 'token'=>$token), 'user');
91                $status = substr($data, 7, 1);
92                $sms = "";
93                switch($status)
94                {
95                        case "0":
96                                $us_id = (int)substr($data, 12);
97                                $user['created_time'] = $receiveTime;
98                                $user['updated_time'] = $receiveTime;
99                                $user['us_id'] = $us_id;
100                                $user['cellphone'] = $username;
101                                $user['collaborator'] = $collaborator;
102                                $this->db->insert('tbluser', $user);
103                                $smslog['sender'] = $sentNumber;
104                                $smslog['us_id'] = $us_id;
105                                $smslog['service_id'] = $serviceID;
106                                $smslog['commandcode'] = $commandcode;
107                                $smslog['info'] = $info;
108                                $smslog['created_time'] = $receiveTime;
109                                $this->db->insert('tblsmslog', $smslog);
110                                $sms = "Chuc mung Qui vi da dang ky thanh cong tai khoan Soan Bai giang Online.
111                                        Hay dang nhap vao http://soanbaigiang.smas.vn/ voi ten truy nhap [".$username."], mat khau [".$password."] de su dung.";
112                                return $sms;
113                        case "2":
114                                $sms = "Tai khoan cua Qui vi da duoc dang ky !";                       
115                                return $sms;
116                        case "4":
117                                $sms = "So dien thoai cá»§a Qui vi da duoc dang ky !";
118                                return $sms;
119                        default:
120                                break;
121                }
122        }
123       
124        function create_random_password()
125        {
126                $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
127                $pass = array();
128                $alphaLength = strlen($alphabet) - 1;
129                for ($i = 0; $i < 6; $i++) {
130                        $n = rand(0, $alphaLength);
131                        $pass[] = $alphabet[$n];
132                }
133                return implode($pass);
134        }
135       
136        function get_user_by_id($us_id)
137        {
138                $this->db->select('*')->from('tbluser')->where('us_id', $us_id);
139                $query = $this->db->get();
140                if($query->num_rows() > 0)
141                {
142                        $row = $query->row_array();
143                        return $row;
144                }
145        }
146}
Note: See TracBrowser for help on using the repository browser.