<?php
class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
		error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
        $this->load->model(array('m_user'));
		$this->load->helper('file');
        if ($this->session->userdata('username')) {
            redirect('dashboard');
        }
    }
    function index() {
			$this->load->view('login5');
    }
	public function balikin($str){
		$search = array ("'xpsijix'");
		$replace = array ("'");
		$str = preg_replace($search,$replace,$str);
		return $str;
	}
    function proses() {
        $this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
        $this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('login5');
        } else {
			$menuju='';
            $usr = $this->input->post('username');
            $psw = $this->input->post('password');
            $u = $this->db->escape_str($usr);
            $p = $this->db->escape_str($psw);
					$cek = $this->m_user->cek($u, $p);
					if ($cek->num_rows() > 0) {
						//login berhasil, buat session
						foreach ($cek->result() as $qad) {
							$menuju="dashboard";
							$sess_data['id'] = $qad->id;
							$sess_data['nama'] = $this->balikin($qad->nama);
							$sess_data['username'] = $qad->user;
							$this->session->set_userdata($sess_data);
							$pesane=date("d M Y")." ".date("h:i:s")." Nama : ".$this->balikin($qad->nama)." username : ".$qad->user." Telah melakukan login";
								kirimtelegram($pesane);
						}
						
						redirect($menuju);
					} else {
						$this->session->set_flashdata('result_login', '<br>Username atau Password yang anda masukkan salah.'.$adauser);
						redirect('login');
					}

        }
    }

    function logout() {
        $this->session->sess_destroy();
        redirect('login');
    }
}
