<?php
Class Master_m extends CI_Model{
	function __construc(){
		parent::__construct();
	}
	
	function HarusLogin(){
		if(empty($this->session->userdata('login'))){
			redirect(base_url());
		}else{
			
			if(!$this->ValidUser()){
				redirect(base_url('logout'));
			}
			
		}
	}
	
	function SudahLogin(){
		if(!empty($this->session->userdata('login'))){
			return true;
		}else{
			return false;
		}
	}
	
	function ValidUser(){
		$id_user=$this->session->userdata('id_user');
		$key=$this->session->userdata('key');
		$q=$this->db->get_where('user',array('id_user'=>$id_user,'key'=>$key))->num_rows();
		if($q==1){
			return true;
		}else{
			return false;
		}
	}
	
	function redirect_login(){
		if($this->SudahLogin()){
			redirect(base_url('home'));
		}
	}
	function FilterInput($input){
		return preg_replace("/[^A-Za-z0-9@_+#$ ]/", '',$input);
	}
	
	function InputPost($input){
		return $this->FilterInput(purify($this->input->post($input)));
	}
	
	function smart_save($nama_db,$custom=array()){
		foreach($_POST as $k=>$v){
			$data[purify($k)]=purify($v);
		}
		$fields = $this->db->list_fields($nama_db);
		
		$data_simpan=array();
		foreach ($fields as $field)
		{
			if(!empty($data[$field]))
				$data_simpan[$field]=$data[$field];
		}
		if(count($custom)>0){
			foreach($custom as $k=>$v){
			   $data_simpan[$k]=$v;
			}
		}
		$this->db->insert($nama_db, $data_simpan); 
	}
	
	function getHashedPassword($text){
		return password_hash($text, PASSWORD_DEFAULT);
	}
	
	function verifyHashedPassword($plainPassword,$hashedPassword){
        return password_verify($plainPassword, $hashedPassword) ? true : false;
	}
}