|
1 <?php |
|
2 class Aumodel extends Model |
|
3 { |
|
4 private $obj; |
|
5 |
|
6 function Aumodel() |
|
7 { |
|
8 parent::Model(); |
|
9 $this->obj =& get_instance(); |
|
10 $this->config->load('auconfig'); |
|
11 if ( $this->obj->config->item('check_if_tables_exist') ) |
|
12 $this->CheckTablesExist(); |
|
13 if ($this->obj->config->item('au_language') != '') |
|
14 $curLang = $this->obj->config->item('au_language'); |
|
15 else |
|
16 $curLang = $this->obj->config->item('language'); |
|
17 $this->obj->lang->load('au', $curLang); |
|
18 } |
|
19 /* |
|
20 * Function for process login/logout etc |
|
21 */ |
|
22 function login($name, $pass, $autologin=FALSE) |
|
23 { |
|
24 $this->db->where('name', $name); |
|
25 $this->db->where('password', $pass); |
|
26 $success = $this->db->count_all_results('auser'); |
|
27 if($success) |
|
28 { |
|
29 $this->db->where('name', $name); |
|
30 $this->db->where('password', $pass); |
|
31 $user_info = $this->db->get('auser')->result_array(); |
|
32 $user_data = $user_info[0]; |
|
33 if(!$user_data['activate_code']) |
|
34 { |
|
35 $data = array('last_visit' => date("Y-m-d H:i:s")); |
|
36 $this->db->update('auser', $data, array('id' => $user_data['id'])); |
|
37 return $user_data['id']; |
|
38 } |
|
39 } |
|
40 return FALSE; |
|
41 } |
|
42 function registers($name, $pass, $email, $confirm) |
|
43 { |
|
44 $activation_code = ($confirm)? $this->makeConfirmationCode() : NULL; |
|
45 $data = array( |
|
46 'name' => $name , |
|
47 'password' => $pass, |
|
48 'email' => $email, |
|
49 'activate_code' => $activation_code |
|
50 ); |
|
51 $this->db->insert('auser', $data); |
|
52 if(!$confirm) return $this->login($name, $pass, FALSE); |
|
53 else |
|
54 { |
|
55 $this->db->where('name', $name); |
|
56 $this->db->where('password', $pass); |
|
57 $user_info = $this->db->get('auser')->result_array(); |
|
58 $user_data = $user_info[0]; |
|
59 $this->_sendConfirmEmail($name, $email, $user_data['id'], $activation_code, NULL); |
|
60 return TRUE; |
|
61 } |
|
62 } |
|
63 function change($id, $data) |
|
64 { |
|
65 $this->db->update('auser', $data, array('id' => $id)); |
|
66 } |
|
67 function confirmEmail($id, $mess) |
|
68 { |
|
69 $this->db->where('id', $id); |
|
70 $this->db->where('activate_code', $mess); |
|
71 $success = $this->db->count_all_results('auser'); |
|
72 if($success) |
|
73 { |
|
74 $data = array('activate_code' => NULL); |
|
75 $this->db->update('auser', $data, array('id' => $id)); |
|
76 } |
|
77 return $success; |
|
78 } |
|
79 function forgotten($name, $email) |
|
80 { |
|
81 $this->db->where('name', $name); |
|
82 $this->db->where('email', $email); |
|
83 $is_exist = $this->db->count_all_results('auser'); |
|
84 if(!$is_exist) return FALSE; |
|
85 $activation_code = $this->makeConfirmationCode(); |
|
86 $new_pass = $this->makeConfirmationCode(); |
|
87 |
|
88 $this->db->where('name', $name); |
|
89 $this->db->where('email', $email); |
|
90 $user_info = $this->db->get('auser')->result_array(); |
|
91 $user_data = $user_info[0]; |
|
92 |
|
93 $data = array( |
|
94 'password' => md5($new_pass), |
|
95 'activate_code' => $activation_code |
|
96 ); |
|
97 $this->db->update('auser', $data, array('id' => $user_data['id'])); |
|
98 |
|
99 $this->_sendConfirmEmail($name, $email, $user_data['id'], $activation_code, $new_pass); |
|
100 return TRUE; |
|
101 } |
|
102 function userInfo($id, $field) |
|
103 { |
|
104 $this->db->where('id', $id); |
|
105 $this->db->select($field); |
|
106 $user_info = $this->db->get('auser')->result_array(); |
|
107 return (isset($user_info[0][$field])) ? $user_info[0][$field] : ''; |
|
108 } |
|
109 function userFullInfo($id) |
|
110 { |
|
111 $this->db->where('id', $id); |
|
112 $sql = $this->db->get('auser')->result_array(); |
|
113 return $sql[0]; |
|
114 |
|
115 } |
|
116 function getRefId($table, $name) |
|
117 { |
|
118 $this->db->where('name', $name); |
|
119 $rez = $this->db->get($table)->result_array(); |
|
120 return $rez[0]['id']; |
|
121 } |
|
122 function getRefName($table, $id) |
|
123 { |
|
124 $this->db->where('id', $id); |
|
125 $rez = $this->db->get($table)->result_array(); |
|
126 return $rez[0]['name']; |
|
127 } |
|
128 function setRefName($table, $id, $new_name) |
|
129 { |
|
130 $data = array('name' => $new_name); |
|
131 $this->db->update($table, $data, array('id' => $id)); |
|
132 } |
|
133 function getRefList($table) |
|
134 { |
|
135 $result = array(); |
|
136 $sql = $this->db->get($table)->result_array(); |
|
137 foreach($sql as $row) $result[$row['id']] = $row['name']; |
|
138 return $result; |
|
139 } |
|
140 function setRefIdToUser($table, $user_id, $new_value) |
|
141 { |
|
142 if($table == 'augroup') |
|
143 $data = array('group_id' => getRefId($table, $new_value)); |
|
144 else |
|
145 $data = array('role_id' => getRefId($table, $new_value)); |
|
146 $this->db->update('auser', $data, array('id' => $user_id)); |
|
147 } |
|
148 |
|
149 /* |
|
150 * |
|
151 */ |
|
152 function delete($table_name, $id) |
|
153 { |
|
154 $this->db->delete($table_name, array('id' => $id)); |
|
155 } |
|
156 function append($table_name) |
|
157 { |
|
158 $data = array('name' => 'new...'); |
|
159 $this->db->insert($table_name, $data); |
|
160 } |
|
161 function updateUsersRef($ref_name, $old_ref, $new_ref) |
|
162 { |
|
163 $this->db->update('auser', array($ref_name.'_id' => $new_ref), array($ref_name.'_id' => $old_ref)); |
|
164 } |
|
165 /* |
|
166 * service function |
|
167 */ |
|
168 function check_name($name) |
|
169 { |
|
170 $this->db->where('name', $name); |
|
171 return (bool) $this->db->count_all_results('auser'); |
|
172 } |
|
173 function makeConfirmationCode() |
|
174 { |
|
175 $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
176 $length = mt_rand(5, 10); |
|
177 $result = ''; |
|
178 for ($i = 0; $i < $length; $i++) |
|
179 $result .= $chars[(mt_rand(0, (strlen($chars)-1)))]; |
|
180 return $result; |
|
181 } |
|
182 function _sendConfirmEmail($name, $email, $id, $activation_code, $new_pass = NULL) |
|
183 { |
|
184 $this->load->library('email'); |
|
185 $this->config->load('email'); |
|
186 $this->email->clear(); |
|
187 $this->email->from($this->obj->config->item('admin_email')); |
|
188 $this->email->to($email); |
|
189 $this->email->subject($this->lang->line('email_confirm_sabject')); |
|
190 $activation_url = site_url('au/confirm/'.$id.'/'.$activation_code); |
|
191 if(!$new_pass) |
|
192 $mess_body = str_replace('%name%', $name, $this->lang->line('email_confirm_body')); |
|
193 else |
|
194 { |
|
195 $mess_body = str_replace('%name%', $name, $this->lang->line('email_confirm_pass_body')); |
|
196 $mess_body = str_replace('%new_pass%', $new_pass, $mess_body); |
|
197 } |
|
198 $mess_body = str_replace('%link%', $activation_url, $mess_body); |
|
199 $this->email->message($mess_body); |
|
200 $this->email->send(); |
|
201 } |
|
202 function CheckTablesExist() |
|
203 { |
|
204 $this->db->trans_start(); |
|
205 if(!$this->db->table_exists('augroup')) |
|
206 { |
|
207 $sql = "CREATE TABLE `augroup` ( `id` int(11) NOT NULL auto_increment, |
|
208 `name` varchar(128) default NULL, |
|
209 PRIMARY KEY (`id`) |
|
210 ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8"; |
|
211 $this->db->query($sql); |
|
212 |
|
213 $sql = "INSERT INTO `augroup` VALUES (1,'users')"; |
|
214 $this->db->query($sql); |
|
215 $sql = "INSERT INTO `augroup` VALUES (2,'admins')"; |
|
216 $this->db->query($sql); |
|
217 } |
|
218 |
|
219 if(!$this->db->table_exists('aurole')) |
|
220 { |
|
221 $sql = "CREATE TABLE `aurole` (`id` int(11) NOT NULL auto_increment, |
|
222 `name` varchar(50) default NULL, |
|
223 PRIMARY KEY (`id`) |
|
224 ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8"; |
|
225 $this->db->query($sql); |
|
226 $sql = "INSERT INTO `aurole` VALUES (1,'baned')"; |
|
227 $this->db->query($sql); |
|
228 $sql = "INSERT INTO `aurole` VALUES (2,'user')"; |
|
229 $this->db->query($sql); |
|
230 $sql = "INSERT INTO `aurole` VALUES (3,'moderator')"; |
|
231 $this->db->query($sql); |
|
232 $sql = "INSERT INTO `aurole` VALUES (4,'admin')"; |
|
233 $this->db->query($sql); |
|
234 } |
|
235 |
|
236 if(!$this->db->table_exists('auser')) |
|
237 { |
|
238 $sql = "CREATE TABLE `auser` (`id` int(11) NOT NULL auto_increment, |
|
239 `name` varchar(50) default NULL, |
|
240 `password` varchar(50) default NULL, |
|
241 `email` varchar(50) default NULL, |
|
242 `activate_code` varchar(50) default NULL, |
|
243 `created` timestamp NULL default CURRENT_TIMESTAMP, |
|
244 `last_visit` datetime default NULL, |
|
245 `role_id` int(11) unsigned default '1', |
|
246 `group_id` int(11) unsigned default '1', |
|
247 PRIMARY KEY (`id`) |
|
248 ) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8"; |
|
249 $this->db->query($sql); |
|
250 /* $sql = "INSERT INTO `auser` VALUES (1,'serg','76419c58730d9f35de7ac538c2fd6737','serg.kosij@list.ru',NULL,'2008-10-12 19:01:03','2008-10-17 23:20:13',4,2)"; |
|
251 $this->db->query($sql); |
|
252 $sql = "INSERT INTO `auser` VALUES (2,'Сергей','96e79218965eb72c92a549dd5a330112','serg.kosij@list.ru',NULL,'2008-10-17 19:24:39','2008-10-17 22:38:02',1,1)"; |
|
253 $this->db->query($sql); |
|
254 */ |
|
255 } |
|
256 $this->db->trans_complete(); |
|
257 } |
|
258 } |
|
259 ?> |