Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.81 KB | None | 0 0
  1. <?php if(!defined('BASEPATH')) exit('Not allowed!');
  2.  
  3.  
  4. class Model_login extends CI_Model{
  5.  
  6.     function login($datax){
  7.  
  8.         $pass = $this->encryptIt($datax['password']);
  9.         $user = $datax['email'];
  10.         //return $user;
  11.         $sql = "select a._id, a.first_name, a.middle_name, a.last_name, a.email, b.username from clients a join credential_table b on a.credential_table_hash = b.table_hash
  12.         where (b.username='". $user."' or a.email='".$user."' ) and b.pass='".$pass."' ";
  13.          
  14.         $result = $this->db->query($sql);
  15.         if ($result->num_rows() > 0) {
  16.             $data = $result->result_array();
  17.            
  18.             $session_token = $this->insert_session($data[0]['_id'],1,0);
  19.  
  20.             return array_merge($data, array('session_key'=>$session_token['session'], 'user_agent_token'=>$session_token['token']  ) );;
  21.         }
  22.         else {
  23.             return 0;
  24.         }
  25.     }
  26.  
  27.  
  28.     function virtual_login($passer,$user){
  29.  
  30.         $pass = $this->encryptIt($passer);
  31.        
  32.         //return $user;
  33.         $sql = "select a._id, a.first_name, a.middle_name, a.last_name, a.email, b.username from clients a join credential_table b on a.credential_table_hash = b.table_hash
  34.         where (b.username='". $user."' or a.email='".$user."' ) and b.pass='".$pass."' ";
  35.          
  36.         $result = $this->db->query($sql);
  37.         if ($result->num_rows() > 0) {
  38.             $data = $result->result_array();
  39.            
  40.             return 1;
  41.         }
  42.         else {
  43.             return 0;
  44.         }
  45.     }
  46.  
  47.  
  48.     function encryptIt( $q ) {
  49.         $cryptKey  = 'qJdB0rrGtaIn5eUB1taxeG03mefrCptasdh';
  50.         $qEncoded      = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
  51.         return( $qEncoded );
  52.     }
  53.  
  54.     function decryptIt( $q ) {
  55.         $cryptKey  = 'qJdB0rrGtaIn5eUB1taxeG03mefrCptasdh';
  56.         $qDecoded      = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
  57.         return( $qDecoded );
  58.     }
  59.  
  60.     function get_user_id_by_session_key($session_key,$is_client,$is_end){
  61.  
  62.         $sql = "select user_id from session_table where session_key ='$session_key'";
  63.         if($is_client>0){
  64.                 $sql.=" and is_client = 1 ";
  65.         }else{ $sql.=" and is_end = 1 ";
  66.         }
  67.  
  68.         $result = $this->db->query($sql);
  69.         $output;
  70.  
  71.         if ($result->num_rows() > 0) {
  72.             $data = $result->result_array();
  73.         }
  74.         else {
  75.             return null;
  76.         }
  77.  
  78.         $output = array();
  79.  
  80.         foreach ($data as $user_id=>$datas) {
  81.             return $emp = $datas['user_id'];
  82.         }
  83.  
  84.         return $emp;
  85.  
  86.  
  87.     }
  88.     function insert_session($id,$is_client,$is_end){
  89.        
  90.         $date = date("Y-m-d h:i:sa");
  91.         $session = $this->generate_session();
  92.         $token = $this->generate_simple_token();
  93.         $ip =   getenv('HTTP_CLIENT_IP')?:
  94.                 getenv('HTTP_X_FORWARDED_FOR')?:
  95.                 getenv('HTTP_X_FORWARDED')?:
  96.                 getenv('HTTP_FORWARDED_FOR')?:
  97.                 getenv('HTTP_FORWARDED')?:
  98.                 getenv('REMOTE_ADDR');
  99.         $qry = "insert into sessions_table (session_key,user_id,is_client,is_end,ip,user_agent_token,date) values('$session','$id','$is_client','$is_end','$ip','$token','$date')";
  100.         $run_query = $this->db->query($qry);
  101.         $isSuccess = $this->db->affected_rows();
  102.  
  103.         return array('session'=>$session,'token'=>$token);
  104.         //print 'success'.$isSuccess;
  105.     }
  106.  
  107.     function generate_session(){
  108.         //$_session =sha1(openssl_random_pseudo_bytes(50).uniqid().openssl_random_pseudo_bytes(999 )).hash(sha1,openssl_random_pseudo_bytes(50).uniqid().openssl_random_pseudo_bytes(uniqid(uniqid()) )) ;
  109.                 $_session =sha1(openssl_random_pseudo_bytes(-1).uniqid().openssl_random_pseudo_bytes(-333)).sha1(rand(0, 777)) ;
  110.             //echo $_session;
  111.         return $_session;
  112.     }
  113.  
  114.     function generate_simple_token(){
  115.         //$_session =sha1(openssl_random_pseudo_bytes(50).uniqid().openssl_random_pseudo_bytes(999 )).hash(sha1,openssl_random_pseudo_bytes(50).uniqid().openssl_random_pseudo_bytes(uniqid(uniqid()) )) ;
  116.                 $_session =md5(uniqid()) ;
  117.             //echo $_session;
  118.         return $_session;
  119.     }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement