load->library('Authorization_Token'); } public function index() { if ($this->my_usessionpublic->logged_in) { // echo "
"; print_r($this->my_usessionpublic); exit;
            $data['title'] = 'Owner Area';
            $this->load->view('user/index-unla', $data);
        }
        else
        {
            redirect('index.php/user/login');

        }
    }
    
    function autoNumber($column,$tbl){
        $q = "SELECT max(".$column.")+1 as max FROM ".$tbl."" ;
        $query  = $this->db->query($q);
        $max = ''; 

        if ($query->num_rows() != 0)
        {
            $row = $query->row();
            $max=$row->max;
        }
        if ($max == null){
            $max=0;
        }
        return $max;
    }

    function nm_field($column,$tbl,$whereb, $wherea){
        $q = "SELECT ".$column." as nm FROM ".$tbl." where ".$whereb." = '".$wherea."' " ;
        $query  = $this->db->query($q);
        $nm= ''; 

        if ($query->num_rows() != 0)
        {
            $row = $query->row();
            $nm=$row->nm;
        }
        return $nm;
    }  


    public function login()
    {
        if ($this->my_usessionpublic->logged_in)
        {
            redirect('index.php/user/index');
        }
        else
        {
            $data['title'] = 'User Login';
            $data['pesan']="";
            $this->load->view('user/login', $data);
        }
    }

    public function ext_is_unique_username()
    {
        $cond = array('username' => $_POST['username']);
        $query = $this->db->get_where('tbl_admin', $cond);
        if ($query->num_rows() != 0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }
    
    public function ext_is_unique_email()
    {
        $cond = array('email' => $_POST['email']);
        $query = $this->db->get_where('tbl_admin', $cond);
        if ($query->num_rows() != 0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }

    public function ext_logout()
    {

      if($this->my_usessionpublic->userdata('level_member1unla') != '10'){
       $this->updatelog();
   }

   if (@$this->my_usessionpublic->userdata('pesan')){
    echo "{".$this->my_usessionpublic->userdata('pesan')."}";
}  else{
    echo "{success:true}";
}

$this->my_usessionpublic->unset_userdata("authorization");
$this->my_usessionpublic->unset_userdata("logged_in");
$this->my_usessionpublic->unset_userdata("idlog1unla");
$this->my_usessionpublic->unset_userdata("user_id1unla");
$this->my_usessionpublic->unset_userdata('status_aktifasi1unla');
$this->my_usessionpublic->unset_userdata('username1unla');
$this->my_usessionpublic->unset_userdata('level_member1unla');
$this->my_usessionpublic->unset_userdata('nm_klp1unla');
$this->session->sess_destroy();  


}

function anti_xss($source)
{
 $f=stripslashes(strip_tags(htmlspecialchars ($source,ENT_QUOTES)));
 return $f;
}

public function ext_login()
{
    $cond = array(
        'userid' => $this->anti_xss($this->input->post('logUsername',TRUE)),
            //'userid' => $this->input->post('logUsername',TRUE),
        'password' => $this->anti_xss(base64_encode($this->input->post('logPassword')), TRUE)
    );

    $query = $this->db->get_where('pengguna', $cond);
    if ($query->num_rows() != 0)
    {
        $row = $query->row();
        $this->my_usessionpublic->set_userdata('status_aktifasi1unla', $row->idstatus);
        if($this->my_usessionpublic->userdata('status_aktifasi1unla') == 1){
            $token_data['id'] = $row->userid;
            $token_data['nik'] = $row->userid; 
            $tokenData = $this->authorization_token->generateToken($token_data);

            $this->my_usessionpublic->set_userdata('authorization', $tokenData);
            $this->my_usessionpublic->set_userdata('logged_in', true);
            $this->my_usessionpublic->set_userdata('user_id1unla', $row->userid);
            $this->my_usessionpublic->set_userdata('username1unla', $row->nmlengkap);
            $this->my_usessionpublic->set_userdata('level_member1unla', $row->idklppengguna);
            $this->my_usessionpublic->set_userdata('nm_klp1unla', $this->nm_field('nmklppengguna','klppengguna','idklppengguna',$row->idklppengguna));
            if($this->my_usessionpublic->userdata('level_member1unla') != '10'){
                $this->savelog();
            }

            $data['pesan']="";
            $data['authorization']=$tokenData;
        }else{
            $data['pesan']="User Tidak Aktif!";
        }

    }
    else if (!$cond['userid'] && !$cond['password'])
    {
        $data['pesan']="Masukkan User ID dan Password!";
    }
    else
    {
        $data['pesan']="User ID atau Password Salah!";
    }

    echo json_encode($data);
}

// public function ext_login()
// {
//     $cond = array(
//         'userid' => $this->anti_xss($this->input->post('logUsername',TRUE)),
//             //'userid' => $this->input->post('logUsername',TRUE),
//         'password' => $this->anti_xss(base64_encode($this->input->post('logPassword')), TRUE)
//     );
//     echo "
"; print_r($cond); exit;
//     $query = $this->db->get_where('pengguna', $cond);
//     if ($query->num_rows() != 0)
//     {
//         $row = $query->row();
//         $this->my_usessionpublic->set_userdata('status_aktifasi1unla', $row->idstatus);
//         if($this->my_usessionpublic->userdata('status_aktifasi1unla') == 1){
//             $token_data['id'] = $row->userid;
//             $token_data['nik'] = $row->userid; 
//             $tokenData = $this->authorization_token->generateToken($token_data);

//             $this->my_usessionpublic->set_userdata('authorization', $tokenData);
//             $this->my_usessionpublic->set_userdata('logged_in', true);
//             $this->my_usessionpublic->set_userdata('user_id1unla', $row->userid);
//             $this->my_usessionpublic->set_userdata('username1unla', $row->nmlengkap);
//             $this->my_usessionpublic->set_userdata('level_member1unla', $row->idklppengguna);
//             $this->my_usessionpublic->set_userdata('nm_klp1unla', $this->nm_field('nmklppengguna','klppengguna','idklppengguna',$row->idklppengguna));
//             if($this->my_usessionpublic->userdata('level_member1unla') != '10'){
//                 $this->savelog();
//             }

//             redirect('index.php/user/index');
//         }else{
//             $data['pesan']="User Tidak Aktif!";
//             $this->load->view('user/login', $data);
//         }

//     }
//     else if (!$cond['userid'] && !$cond['password'])
//     {
//         $data['pesan']="Masukkan User ID dan Password!";
//         $this->load->view('user/login', $data);
//     }
//     else
//     {
//         $data['pesan']="User ID atau Password Salah!";
//         $this->load->view('user/login', $data);
//     }
// }

	function savelog(){      // ISTRA
       $ipaddress = $_SERVER['REMOTE_ADDR'];
       $idlog = $this->autoNumber('idlog','logpengguna');

       $data = array(
        'idlog'=> $idlog,
        'userid'=> $_POST['logUsername'],
        'ipaddress'=> $ipaddress,
    );

       $this->db->query("CALL SP_insertlog (?,?,?)", $data);

       $this->my_usessionpublic->set_userdata('idlog1unla', $idlog);

       if($this->db->trans_status()=== FALSE)
       {
        $this->db->trans_rollback();
        $ret["success"]=false;
        $ret["message"]="Simpan Data gagal";
    }
    else
    {
        $this->db->trans_commit();
        $ret["success"]=true;
        $ret["message"]="Simpan Data Berhasil";
    }
    return $ret;
}

	function updatelog(){      // ISTRA

     $data = array(
         'idlog'=>  $this->my_usessionpublic->userdata('idlog1unla'),
     );

     $this->db->query("CALL SP_updatelog (?)", $data);

     if($this->db->trans_status()=== FALSE)
     {
        $this->db->trans_rollback();
        $ret["success"]=false;
        $ret["message"]="Simpan Data gagal";
    }
    else
    {
        $this->db->trans_commit();
        $ret["success"]=true;
        $ret["message"]="Simpan Data Berhasil";
    }
    return $ret;
}

}