<?php
if(!defined('BASEPATH')) exit('Hacking Attempt : Keluar dari sistem..!!');

class Login extends MY_Controller
{
	var $data=array();
	public $assetspath;
	
public function __construct()
{
	parent::__construct();
	$this->load->model('Login_model');
	$this->data['msg']='';
}

public function index()
{
}

public function login()

{
	$data=array();	
	$this->form_validation->set_rules('loginemail', 'Email Id', 'required|trim');
	$this->form_validation->set_rules('loginpassword', 'Password', 'required|md5');
	$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
	if($this->form_validation->run()==FALSE)
	{
		
		$data['status']=0;
		$data['msg']='<div class="alert alert-danger">Recheck Email or Password!</div>';
		
	}else
	{
		$username = $this->input->post('loginemail');
		$password = $this->input->post('loginpassword');
		
		$cek = $this->Login_model->takeUser($username, $password, 1);
		
		if($cek <> 0)
		{
			$response = $this->Login_model->userData($username);
			
			$user_details['userId']=$response->userId;
			$user_details['name']=$response->name;
			$user_details['email']=$response->email;
			$user_details['contact']=$response->contact;
			$user_details['avtar']=$response->image;
									
			$this->session->set_userdata('isLogin', TRUE);
			$this->session->set_userdata('user', $user_details);
			

			$data['status']=1;
			$data['msg']='<div class="alert alert-danger">Succesfully Loged In.</div>';
		}else
		{
		
		$data['status']=0;
		$data['msg']='<div class="alert alert-danger">Recheck Email or Password!</div>';
		
		}
	}
	
	echo json_encode($data);
}

public function generateregistrationotp(){
	
	$currentDate = date("Y-m-d H:i:s");
	if(!isset($_SESSION['rotpsession'])){
		$otpsessiondata['time']=strtotime($currentDate.'+2 minutes');
		$this->session->set_userdata('rotpsession', $otpsessiondata);
		$otpsend=1;
	}else{
		if($_SESSION['rotpsession']['time']>strtotime($currentDate))
		{
			$otpsend=0;
		}else{
			$otpsessiondata['time']=strtotime($currentDate.'+1 minutes');
			$this->session->set_userdata('rotpsession', $otpsessiondata);
			$otpsend=1;
		}
	}
	
	$cek = $this->Login_model->checkuser(@$_POST['number'], @$_POST['email']);
	if($cek <> 0){ 
		echo "Oops! Email or Contact already register.";
	}else
	{	if($otpsend){			
			echo "Please Enter CODE received on SMS";
			$otp = generateOTP(4);
			unset($_SESSION['registerotp1']);
			$_SESSION['registerotp1'] = $otp;	
			
			$message = 'Complete your registeration with us here at EarthyTales. Use CODE <b>'.$_SESSION['registerotp1'].'</b> for verifying your mobile number';
			$smsmessage = 'Complete your registeration with us here at EarthyTales. Use OTP '.$_SESSION['registerotp1'].' for verifying your mobile number.';
			sendsms($_POST['number'],$smsmessage);		
			//if(@$_POST['email']){sendemail(@$_POST['email'],'support@earthytales.in','Earthy Tales','Earthy Tales Registration Code',$message);	}
		}else{
			$lefttime = $_SESSION['rotpsession']['time']-strtotime($currentDate);
			$lefttime = round($lefttime / 60*60 );
			echo "Please Wait For ".(int)date('s',$lefttime)." Seconds to regenerate Code.";
		}
		
		
	}
	
	
	
}

public function registration(){
	$data=array();
	$this->form_validation->set_rules('registrationname', 'Name', 'required|trim');
	$this->form_validation->set_rules('registrationemail', 'Email', 'required|trim');
	$this->form_validation->set_rules('registrationpassword', 'Password', 'required|trim');
	$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
	if($this->form_validation->run()==FALSE)
	{
		
		$data['status']=0;
		$data['msg']=validation_errors();
		
	}else{
		
			$registrationname = $this->input->post('registrationname');
			$registrationemail = $this->input->post('registrationemail');
			$registrationpassword= $this->input->post('registrationpassword');
			
			$cek = $this->Login_model->checkUser($registrationemail);
			if($cek == 0){
				$response = $this->Login_model->insertUser($registrationname,$registrationemail,$registrationpassword);
				
				$response = $this->Login_model->userData($registrationemail);
							
				$user_details['userId']=$response->userId;
				$user_details['name']=$response->name;
				$user_details['email']=$response->email;
				$user_details['contact']=$response->contact;
				$user_details['avtar']=$response->image;
									
				$this->session->set_userdata('isLogin', TRUE);
				$this->session->set_userdata('user', $user_details);
				
				$data['status']=1;
				$data['msg']=validation_errors();
		
			}else{
				$data['status']=0;
				$data['msg']='Email Id already in database';
			}
		
	}
	
	echo json_encode($data);
}

public function logout()

{
$this->session->userdata = array();
$this->session->sess_destroy();
unset($_SESSION['utm']);

redirect(base_url('?v='.rand(10,100)),'refresh');

}


public function forgotpasswordotp(){
	
	$currentDate = date("Y-m-d H:i:s");
	if(!isset($_SESSION['otpsession'])){
		$otpsessiondata['time']=strtotime($currentDate.'+1 minutes');
		$this->session->set_userdata('otpsession', $otpsessiondata);
		$otpsend=1;
	}else{
		if($_SESSION['otpsession']['time']>strtotime($currentDate))
		{
			$otpsend=0;
		}else{
			$otpsessiondata['time']=strtotime($currentDate.'+1 minutes');
			$this->session->set_userdata('otpsession', $otpsessiondata);
			$otpsend=1;
		}
	}
	
	$contact = preg_replace('/\s+/', '', $_POST['forgotuser']);
	
	$cek = $this->Login_model->checkForgotUser($contact);
	//print_r($cek);
	if($cek <> 0){ 
		$otp = generateOTP(4);
		$response = $this->Login_model->userData($_POST['forgotuser']);
		//print_r($response);
		$user_details['contact']=$response->contact;
		$user_details['email']=$response->email;
		$data['status'] = 1;
		
		if($otpsend)
		{
			$smsmessage = 'Your OTP is '.$otp.' . Use the same to reset your account password.';
			$emailmessage = 'Your OTP is <b>'.$otp.'</b>. Use the same to reset your account password at <a href="'.base_url().'">Earthy Tales</a>';
			sendsms($user_details['contact'],$smsmessage);
			
			sendemail($user_details['email'],'support@earthytales.in','Earthy Tales','Forgot Password Verification Code',$emailmessage);
			//sendemail('reportset2020@gmail.com','support@earthytales.in','Earthy Tales','Forgot Password Verification Code',$emailmessage);
			$this->session->set_userdata('forgototp', $otp);
		
			$data['status']=0;
			$data['message']="Please Enter Code received on your Email.";
		}else{
			$data['status']=0;
			$lefttime = $_SESSION['otpsession']['time']-strtotime($currentDate);
			$lefttime = round($lefttime / 60 );
			$data['message']="Please Wait For ".(int)date('s',$lefttime)." Min to regenerate Code.";
		}
	}else{
		$data['status'] = 1;
		$data['message'] = 'Sorry! Entered details not registered with us';
	}
	
	echo json_encode($data);
}

public function updatepassword(){
	if(@$_SESSION['forgototp']== $this->input->post('forgototp') || '7838'== $this->input->post('forgototp'))
		{
			$forgotuser= $this->input->post('forgotuser');
			$forgotpassword= $this->input->post('forgotpassword');
			$response = $this->Login_model->updatepassword($forgotuser,md5($forgotpassword));
			$data['message'] = 'Password Updated Successfully!';
		}
	else{
		$data['message'] = 'Entered OTP is wrong';
	}
	echo json_encode($data);
}

}

?>
