PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/users.php

https://bitbucket.org/simpfc/shop
PHP | 294 lines | 276 code | 12 blank | 6 comment | 102 complexity | eafb8201bf6c2d7077e590ee06c86438 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Users {
  3. public function __construct() {
  4. $this->CI =& get_instance();
  5. $this->CI->load->library('session');
  6. $this->CI->load->helper('translite');
  7. $this->CI->load->database();
  8. }
  9. /* Функция получения информации о юзере. */
  10. public function getUser($u='current') {
  11. if($u)
  12. {
  13. if($u=='current') {
  14. $login=$this->CI->session->userdata('login');
  15. if($login<>'') {
  16. $this->CI->db->select('users.*, users_profile.*, country.id as country_id, country.country_name_ru');
  17. $this->CI->db->join('users_profile','users_profile.userID = users.id','right');
  18. $this->CI->db->join('country','country.id = users_profile.country','left');
  19. $profile=$this->CI->db->get_where('users',array('users.login'=>$login));
  20. if($profile->num_rows() > 0) {
  21. $result=$profile->row_array(0);
  22. } else {
  23. $result=FALSE;
  24. }
  25. } else $result=FALSE;
  26. }
  27. if ((!is_numeric($u)) AND ($u<>'current')) {
  28. $login=$u;
  29. $this->CI->db->select('users.*, users_profile.*, country.id as country_id, country.country_name_ru');
  30. $this->CI->db->join('users_profile','users_profile.userID = users.id','right');
  31. $this->CI->db->join('country','country.id = users_profile.country','left');
  32. $profile=$this->CI->db->get_where('users',array('users.login'=>$login));
  33. if($profile->num_rows() > 0) {
  34. $result=$profile->row_array(0);
  35. } else {
  36. $result=FALSE;
  37. }
  38. }
  39. if(is_numeric($u)) {
  40. $this->CI->db->select('users.*, users_profile.*, country.id as country_id, country.country_name_ru');
  41. $this->CI->db->join('users_profile','users_profile.userID = users.id','right');
  42. $this->CI->db->join('country','country.id = users_profile.country','left');
  43. $profile=$this->CI->db->get_where('users',array('users.id'=> $u));
  44. if($profile->num_rows() > 0) {
  45. $result=$profile->row_array(0);
  46. } else {
  47. $result=FALSE;
  48. }
  49. }
  50. return $result;
  51. } else return FALSE;
  52. }
  53. public function id_by_alias($alias=FALSE) {
  54. if($alias)
  55. {
  56. $user = $this->CI->db->get_where('users',array('alias' => $alias));
  57. if(($user->num_rows() > 0) and ($user->row(0)->id > 0)) return intval($user->row(0)->id); else return FALSE;
  58. } else return FALSE;
  59. }
  60. /* Получение роли пользователя */
  61. public function getRole($u='current') {
  62. $user = $this->getUser($u);
  63. if($user) {
  64. return $user['role'];
  65. } else return FALSE;
  66. }
  67. public function status($u='current') {
  68. $user=$this->getUser($u);
  69. if($user)
  70. {
  71. if($user['blocked']==='0') return TRUE; else return FALSE;
  72. } else return FALSE;
  73. }
  74. public function check($u,$p) {
  75. $profile=$this->getUser($u);
  76. if(($profile['password']===base64_decode($p)) AND ($profile['blocked']==0)) return TRUE; else return FALSE;
  77. }
  78. public function get($field,$u='current')
  79. {
  80. $user = $this->getUser($u);
  81. if($user)
  82. {
  83. return $user[$field];
  84. } else return FALSE;
  85. }
  86. public function set($field,$value,$u='current')
  87. {
  88. $user = $this->getUser($u);
  89. if($user)
  90. {
  91. //send notify by action
  92. if($field == 'blocked' and $value == 1) $this->CI->notify->account_bloked($user['userID']);
  93. if($field == 'blocked' and !$value and $user['last_enter']!="0") $this->CI->notify->account_unbloked($user['userID']);
  94. // update user info
  95. if($this->CI->db->update('users',array($field => $value), array('id' => $user['id']))) return TRUE; else return FALSE;
  96. } else return FALSE;
  97. }
  98. /* Создание */
  99. public function create($login,$password,$name,$email,$role='user',$blocked="1",$profile=array()) {
  100. $check = $this->CI->db->where(array('email' => $login))->or_where(array('login' => $login))->get('users')->num_rows();
  101. if($check == 0)
  102. {
  103. $userdata=array(
  104. 'login'=>$login,
  105. 'password'=>md5($password),
  106. 'name' => $name,
  107. 'email'=>$email,
  108. 'role'=>$role,
  109. 'blocked'=>$blocked,
  110. 'code' => md5(time().md5($login))
  111. );
  112. $this->CI->db->insert('users',$userdata);
  113. $str = $this->CI->db->insert_id();
  114. if($str>0)
  115. {
  116. $this->CI->db->update('users',array('alias' => 'id'.$str), array('id' => $str));
  117. $this->CI->db->update('users',array('reg_ip' => $this->CI->input->server('REMOTE_ADDR')), array('id' => $str));
  118. if($this->update_profile($str,$profile))
  119. {
  120. $this->CI->notify->activation_link($str);
  121. return TRUE;
  122. } else return FALSE;
  123. } else return FALSE;
  124. } else return FALSE;
  125. }
  126. public function update($id,$login,$password='',$name,$email,$role='user',$phone=NULL,$blocked=NULL) {
  127. $myrole = $this->getRole();
  128. if ($myrole<>'sudo') return FALSE;
  129. else {
  130. if($password!='') {
  131. $userdata=array(
  132. 'login'=>$login,
  133. 'password'=>md5($password),
  134. 'name' => $name,
  135. 'email'=>$email,
  136. 'role'=>$role
  137. );
  138. } else {
  139. $userdata=array(
  140. 'login'=>$login,
  141. 'name' => $name,
  142. 'email'=>$email,
  143. 'role'=>$role
  144. );
  145. }
  146. if($phone!=NULL) $userdata['phone']=$phone;
  147. if($blocked!=NULL) $userdata['blocked']=$blocked;
  148. if($userdata['blocked'] == 1) $this->CI->notify->account_bloked($id);
  149. if($this->CI->db->update('users',$userdata,'id ='.$id)) return TRUE; else return FALSE;
  150. }
  151. }
  152. public function update_profile($uid,$params = array())
  153. {
  154. if(count($params)>0)
  155. {
  156. foreach ($params as $key => $value) {
  157. $data[$key]=$value;
  158. }
  159. }
  160. if(!empty($data)) {
  161. if( $this->CI->db->get_where('users_profile',array('userID' => $uid))->num_rows() > 0 )
  162. {
  163. if($this->CI->db->update('users_profile',$data,array('userID' => $uid))) return TRUE; else return FALSE;
  164. }
  165. else
  166. {
  167. $data['userID']=$uid;
  168. if($this->CI->db->insert('users_profile',$data)) return TRUE; else return FALSE;
  169. }
  170. }
  171. else return FALSE;
  172. }
  173. public function setProfile($uid,$field,$value)
  174. {
  175. if($this->CI->db->update('users_profile',array($field => $value),array('userID' => $uid))) return TRUE; else return FALSE;
  176. }
  177. public function getProfile($field,$uid='current')
  178. {
  179. $user = $this->getUser($uid);
  180. if($user)
  181. {
  182. return $user['status'];
  183. } else return FALSE;
  184. }
  185. public function delete_user($user) {
  186. $u=$this->getUser($user);
  187. if($u)
  188. {
  189. $this->CI->db->delete('users',array('id' => $u['id']));
  190. $this->CI->db->delete('users_profile',array('userID' => $u['id']));
  191. if($this->getUser($user)) return FALSE; else return TRUE;
  192. }
  193. }
  194. public function log($what) {
  195. $u=$this->getUser();
  196. if($u=='no such user') $u['id']=0;
  197. if($this->CI->db->insert('log',array('users_id'=>$u['id'],'when'=>mktime(),'what'=>$what))) return TRUE; else return FALSE;
  198. }
  199. public function readlogs($wfrom='0',$wto='',$user='any') {
  200. $this->CI->db->order_by('id','DESC');
  201. if(is_numeric($wfrom)) {
  202. if($wto=='') $wto=mktime();
  203. if($user=='any') {
  204. $this->CI->db->where('when >='.$wform.' AND when <='.$wto);
  205. $logs=$this->CI->db->get('log');
  206. if($logs->num_rows()>0) {
  207. return $logs->result_array();
  208. } else return FALSE;
  209. } else {
  210. $u=$this->getUser($user);
  211. $this->CI->db->where('users_id ='.$u['ud'].' AND when >='.$wform.' AND when <='.$wto);
  212. $logs=$this->CI->db->get('log');
  213. if($logs->num_rows()>0) {
  214. return $logs->result_array();
  215. } else return FALSE;
  216. }
  217. } else {
  218. if($user=='any') {
  219. $this->CI->db->where('when >='.$wform.' AND when <='.$wto);
  220. $logs=$this->CI->db->get('log');
  221. if($logs->num_rows()>0) {
  222. return $logs->result_array();
  223. } else return FALSE;
  224. } else {
  225. $u=$this->getUser($user);
  226. $this->CI->db->where('users_id ='.$u['id']);
  227. $logs=$this->CI->db->get('log');
  228. if($logs->num_rows()>0) {
  229. return $logs->result_array();
  230. } else return FALSE;
  231. }
  232. }
  233. }
  234. public function clearuserslog($user='current') {
  235. $u=$this->getUser($user);
  236. $this->CI->db->delete('log',array('users_id'=>$u['id']));
  237. return TRUE;
  238. }
  239. public function cur_id($u='current') {
  240. $u=$this->getUser($u);
  241. if($u) return $u['id']; else return FALSE;
  242. }
  243. public function is_online($uid)
  244. {
  245. $user = $this->getUser($uid);
  246. if($user)
  247. {
  248. if($user['last_enter'] >= (time() - 900)) return TRUE; else return FALSE;
  249. }
  250. }
  251. public function count_is_online($time = 900)
  252. {
  253. $count_online = $this->CI->db->select('*')
  254. ->where('last_enter >=', (time() - $time))
  255. ->join('users_profile','users_profile.userID = users.id','left')
  256. ->get('users');
  257. //->num_rows();
  258. if($count_online->num_rows()>0) return $count_online->result_array(); else return FALSE;
  259. }
  260. public function subscribe($subscribeTo)
  261. {
  262. $user = $this->cur_id();
  263. if($user)
  264. {
  265. if(!$this->is_subscribed($subscribeTo,$user))
  266. {
  267. if($this->CI->db->insert('users_subscribe',array('usersID' => $user, 'subscribedTo' => $subscribeTo))) return 'added'; else return FALSE;
  268. } else {
  269. while($this->is_subscribed($subscribeTo,$user))
  270. {
  271. $this->CI->db->delete('users_subscribe',array('usersID' => $user, 'subscribedTo' => $subscribeTo));
  272. }
  273. return 'removed';
  274. }
  275. } else return FALSE;
  276. }
  277. public function is_subscribed($subscribedTo,$subscribedWho)
  278. {
  279. $check = $this->CI->db->get_where('users_subscribe',array('usersID' => $subscribedWho, 'subscribedTo' => $subscribedTo));
  280. if($check->num_rows()>0) return TRUE; else return FALSE;
  281. }
  282. }