/application/libraries/users.php
PHP | 294 lines | 276 code | 12 blank | 6 comment | 102 complexity | eafb8201bf6c2d7077e590ee06c86438 MD5 | raw file
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Users {
- public function __construct() {
- $this->CI =& get_instance();
- $this->CI->load->library('session');
- $this->CI->load->helper('translite');
- $this->CI->load->database();
- }
- /* Функция получения информации о юзере. */
- public function getUser($u='current') {
- if($u)
- {
- if($u=='current') {
- $login=$this->CI->session->userdata('login');
- if($login<>'') {
- $this->CI->db->select('users.*, users_profile.*, country.id as country_id, country.country_name_ru');
- $this->CI->db->join('users_profile','users_profile.userID = users.id','right');
- $this->CI->db->join('country','country.id = users_profile.country','left');
- $profile=$this->CI->db->get_where('users',array('users.login'=>$login));
- if($profile->num_rows() > 0) {
- $result=$profile->row_array(0);
- } else {
- $result=FALSE;
- }
- } else $result=FALSE;
- }
- if ((!is_numeric($u)) AND ($u<>'current')) {
- $login=$u;
- $this->CI->db->select('users.*, users_profile.*, country.id as country_id, country.country_name_ru');
- $this->CI->db->join('users_profile','users_profile.userID = users.id','right');
- $this->CI->db->join('country','country.id = users_profile.country','left');
- $profile=$this->CI->db->get_where('users',array('users.login'=>$login));
- if($profile->num_rows() > 0) {
- $result=$profile->row_array(0);
- } else {
- $result=FALSE;
- }
- }
- if(is_numeric($u)) {
- $this->CI->db->select('users.*, users_profile.*, country.id as country_id, country.country_name_ru');
- $this->CI->db->join('users_profile','users_profile.userID = users.id','right');
- $this->CI->db->join('country','country.id = users_profile.country','left');
- $profile=$this->CI->db->get_where('users',array('users.id'=> $u));
- if($profile->num_rows() > 0) {
- $result=$profile->row_array(0);
- } else {
- $result=FALSE;
- }
- }
-
- return $result;
- } else return FALSE;
- }
- public function id_by_alias($alias=FALSE) {
- if($alias)
- {
- $user = $this->CI->db->get_where('users',array('alias' => $alias));
- if(($user->num_rows() > 0) and ($user->row(0)->id > 0)) return intval($user->row(0)->id); else return FALSE;
- } else return FALSE;
- }
- /* Получение роли пользователя */
- public function getRole($u='current') {
- $user = $this->getUser($u);
- if($user) {
- return $user['role'];
- } else return FALSE;
- }
- public function status($u='current') {
- $user=$this->getUser($u);
- if($user)
- {
- if($user['blocked']==='0') return TRUE; else return FALSE;
- } else return FALSE;
- }
- public function check($u,$p) {
- $profile=$this->getUser($u);
- if(($profile['password']===base64_decode($p)) AND ($profile['blocked']==0)) return TRUE; else return FALSE;
- }
- public function get($field,$u='current')
- {
- $user = $this->getUser($u);
- if($user)
- {
- return $user[$field];
- } else return FALSE;
- }
- public function set($field,$value,$u='current')
- {
- $user = $this->getUser($u);
- if($user)
- {
- //send notify by action
- if($field == 'blocked' and $value == 1) $this->CI->notify->account_bloked($user['userID']);
- if($field == 'blocked' and !$value and $user['last_enter']!="0") $this->CI->notify->account_unbloked($user['userID']);
- // update user info
- if($this->CI->db->update('users',array($field => $value), array('id' => $user['id']))) return TRUE; else return FALSE;
- } else return FALSE;
- }
- /* Создание */
- public function create($login,$password,$name,$email,$role='user',$blocked="1",$profile=array()) {
- $check = $this->CI->db->where(array('email' => $login))->or_where(array('login' => $login))->get('users')->num_rows();
- if($check == 0)
- {
- $userdata=array(
- 'login'=>$login,
- 'password'=>md5($password),
- 'name' => $name,
- 'email'=>$email,
- 'role'=>$role,
- 'blocked'=>$blocked,
- 'code' => md5(time().md5($login))
- );
-
- $this->CI->db->insert('users',$userdata);
- $str = $this->CI->db->insert_id();
- if($str>0)
- {
- $this->CI->db->update('users',array('alias' => 'id'.$str), array('id' => $str));
- $this->CI->db->update('users',array('reg_ip' => $this->CI->input->server('REMOTE_ADDR')), array('id' => $str));
-
- if($this->update_profile($str,$profile))
- {
- $this->CI->notify->activation_link($str);
- return TRUE;
- } else return FALSE;
- } else return FALSE;
- } else return FALSE;
- }
- public function update($id,$login,$password='',$name,$email,$role='user',$phone=NULL,$blocked=NULL) {
- $myrole = $this->getRole();
- if ($myrole<>'sudo') return FALSE;
- else {
- if($password!='') {
- $userdata=array(
- 'login'=>$login,
- 'password'=>md5($password),
- 'name' => $name,
- 'email'=>$email,
- 'role'=>$role
- );
- } else {
- $userdata=array(
- 'login'=>$login,
- 'name' => $name,
- 'email'=>$email,
- 'role'=>$role
- );
- }
- if($phone!=NULL) $userdata['phone']=$phone;
- if($blocked!=NULL) $userdata['blocked']=$blocked;
- if($userdata['blocked'] == 1) $this->CI->notify->account_bloked($id);
- if($this->CI->db->update('users',$userdata,'id ='.$id)) return TRUE; else return FALSE;
- }
- }
- public function update_profile($uid,$params = array())
- {
- if(count($params)>0)
- {
- foreach ($params as $key => $value) {
- $data[$key]=$value;
- }
- }
- if(!empty($data)) {
- if( $this->CI->db->get_where('users_profile',array('userID' => $uid))->num_rows() > 0 )
- {
- if($this->CI->db->update('users_profile',$data,array('userID' => $uid))) return TRUE; else return FALSE;
- }
- else
- {
- $data['userID']=$uid;
- if($this->CI->db->insert('users_profile',$data)) return TRUE; else return FALSE;
- }
- }
- else return FALSE;
- }
- public function setProfile($uid,$field,$value)
- {
- if($this->CI->db->update('users_profile',array($field => $value),array('userID' => $uid))) return TRUE; else return FALSE;
- }
- public function getProfile($field,$uid='current')
- {
- $user = $this->getUser($uid);
- if($user)
- {
- return $user['status'];
- } else return FALSE;
- }
- public function delete_user($user) {
- $u=$this->getUser($user);
- if($u)
- {
- $this->CI->db->delete('users',array('id' => $u['id']));
- $this->CI->db->delete('users_profile',array('userID' => $u['id']));
- if($this->getUser($user)) return FALSE; else return TRUE;
- }
- }
- public function log($what) {
- $u=$this->getUser();
- if($u=='no such user') $u['id']=0;
- if($this->CI->db->insert('log',array('users_id'=>$u['id'],'when'=>mktime(),'what'=>$what))) return TRUE; else return FALSE;
- }
- public function readlogs($wfrom='0',$wto='',$user='any') {
- $this->CI->db->order_by('id','DESC');
- if(is_numeric($wfrom)) {
- if($wto=='') $wto=mktime();
- if($user=='any') {
- $this->CI->db->where('when >='.$wform.' AND when <='.$wto);
- $logs=$this->CI->db->get('log');
- if($logs->num_rows()>0) {
- return $logs->result_array();
- } else return FALSE;
- } else {
- $u=$this->getUser($user);
- $this->CI->db->where('users_id ='.$u['ud'].' AND when >='.$wform.' AND when <='.$wto);
- $logs=$this->CI->db->get('log');
- if($logs->num_rows()>0) {
- return $logs->result_array();
- } else return FALSE;
- }
- } else {
- if($user=='any') {
- $this->CI->db->where('when >='.$wform.' AND when <='.$wto);
- $logs=$this->CI->db->get('log');
- if($logs->num_rows()>0) {
- return $logs->result_array();
- } else return FALSE;
- } else {
- $u=$this->getUser($user);
- $this->CI->db->where('users_id ='.$u['id']);
- $logs=$this->CI->db->get('log');
- if($logs->num_rows()>0) {
- return $logs->result_array();
- } else return FALSE;
- }
- }
- }
- public function clearuserslog($user='current') {
- $u=$this->getUser($user);
- $this->CI->db->delete('log',array('users_id'=>$u['id']));
- return TRUE;
- }
- public function cur_id($u='current') {
- $u=$this->getUser($u);
- if($u) return $u['id']; else return FALSE;
- }
- public function is_online($uid)
- {
- $user = $this->getUser($uid);
- if($user)
- {
- if($user['last_enter'] >= (time() - 900)) return TRUE; else return FALSE;
- }
- }
- public function count_is_online($time = 900)
- {
- $count_online = $this->CI->db->select('*')
- ->where('last_enter >=', (time() - $time))
- ->join('users_profile','users_profile.userID = users.id','left')
- ->get('users');
- //->num_rows();
- if($count_online->num_rows()>0) return $count_online->result_array(); else return FALSE;
- }
- public function subscribe($subscribeTo)
- {
- $user = $this->cur_id();
- if($user)
- {
- if(!$this->is_subscribed($subscribeTo,$user))
- {
- if($this->CI->db->insert('users_subscribe',array('usersID' => $user, 'subscribedTo' => $subscribeTo))) return 'added'; else return FALSE;
- } else {
- while($this->is_subscribed($subscribeTo,$user))
- {
- $this->CI->db->delete('users_subscribe',array('usersID' => $user, 'subscribedTo' => $subscribeTo));
- }
- return 'removed';
- }
- } else return FALSE;
- }
- public function is_subscribed($subscribedTo,$subscribedWho)
- {
- $check = $this->CI->db->get_where('users_subscribe',array('usersID' => $subscribedWho, 'subscribedTo' => $subscribedTo));
- if($check->num_rows()>0) return TRUE; else return FALSE;
- }
- }