Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if(!defined('BASEPATH')) exit('Not allowed!');
- class Model_login extends CI_Model{
- function login($datax){
- $pass = $this->encryptIt($datax['password']);
- $user = $datax['email'];
- //return $user;
- $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
- where (b.username='". $user."' or a.email='".$user."' ) and b.pass='".$pass."' ";
- $result = $this->db->query($sql);
- if ($result->num_rows() > 0) {
- $data = $result->result_array();
- $session_token = $this->insert_session($data[0]['_id'],1,0);
- return array_merge($data, array('session_key'=>$session_token['session'], 'user_agent_token'=>$session_token['token'] ) );;
- }
- else {
- return 0;
- }
- }
- function virtual_login($passer,$user){
- $pass = $this->encryptIt($passer);
- //return $user;
- $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
- where (b.username='". $user."' or a.email='".$user."' ) and b.pass='".$pass."' ";
- $result = $this->db->query($sql);
- if ($result->num_rows() > 0) {
- $data = $result->result_array();
- return 1;
- }
- else {
- return 0;
- }
- }
- function encryptIt( $q ) {
- $cryptKey = 'qJdB0rrGtaIn5eUB1taxeG03mefrCptasdh';
- $qEncoded = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
- return( $qEncoded );
- }
- function decryptIt( $q ) {
- $cryptKey = 'qJdB0rrGtaIn5eUB1taxeG03mefrCptasdh';
- $qDecoded = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
- return( $qDecoded );
- }
- function get_user_id_by_session_key($session_key,$is_client,$is_end){
- $sql = "select user_id from session_table where session_key ='$session_key'";
- if($is_client>0){
- $sql.=" and is_client = 1 ";
- }else{ $sql.=" and is_end = 1 ";
- }
- $result = $this->db->query($sql);
- $output;
- if ($result->num_rows() > 0) {
- $data = $result->result_array();
- }
- else {
- return null;
- }
- $output = array();
- foreach ($data as $user_id=>$datas) {
- return $emp = $datas['user_id'];
- }
- return $emp;
- }
- function insert_session($id,$is_client,$is_end){
- $date = date("Y-m-d h:i:sa");
- $session = $this->generate_session();
- $token = $this->generate_simple_token();
- $ip = getenv('HTTP_CLIENT_IP')?:
- getenv('HTTP_X_FORWARDED_FOR')?:
- getenv('HTTP_X_FORWARDED')?:
- getenv('HTTP_FORWARDED_FOR')?:
- getenv('HTTP_FORWARDED')?:
- getenv('REMOTE_ADDR');
- $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')";
- $run_query = $this->db->query($qry);
- $isSuccess = $this->db->affected_rows();
- return array('session'=>$session,'token'=>$token);
- //print 'success'.$isSuccess;
- }
- function generate_session(){
- //$_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()) )) ;
- $_session =sha1(openssl_random_pseudo_bytes(-1).uniqid().openssl_random_pseudo_bytes(-333)).sha1(rand(0, 777)) ;
- //echo $_session;
- return $_session;
- }
- function generate_simple_token(){
- //$_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()) )) ;
- $_session =md5(uniqid()) ;
- //echo $_session;
- return $_session;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement