<?php 


namespace App\Controllers\User;
use App\Views;
use App\Models\CommonModel;
use App\Helpers\site_helper;
use App\Libraries\MyEncrypt;
use App\Libraries\My_PHPMailer;
use App\Libraries\PHPMailer; 
use App\Libraries\mathcaptcha;
use App\Libraries\Google_authendicator;

class Twofactor extends BaseController {

/**
* 
* Start 
* Create Date: 24 Jun, 2016
* This controller to manage referral setting page.
* Created karthiga.
* @see http://ihyip.com/admin/referralsettings/
* Last Updated: 28 Jun, 2016
* End
* 
*/
public function __construct(){
		$this->session = session();
		$CommonModel = model('CommonModel');
	}
   public function index() {
   	$this->db = db_connect();
		$myencrypt = new MyEncrypt(true);
		$this->common_model = model('CommonModel');
		$this->finance_model = model('finance_model');
	    $this->userdash_model = model('userdash_model');
		$session = \Config\Services::session();
		$validation = \Config\Services::validation();
		$uri = service('uri');
		if(!($session->get('user_login') || $session->get('admin_login')))
		{			
			return redirect()->to('login');
		}
		$this->indexpage();
    }
	

	public function indexpage() {

        $CommonModel = model('CommonModel');

		$condition = "payment_key in('logo','status')";
		$payment_data_list = $CommonModel->GetPaymentsName($condition);
		$site_data = $CommonModel->GetSettingall('infobox');
		if($site_data) {
			
			foreach ($site_data as $row) {
				$this->data[$row->site_key] = $row->site_value;
			}

		} else {
			$site_data_default = $CommonModel->defaultlangSettingall('infobox');
			foreach ($site_data_default as $row) {
				$this->data[$row->site_key] = $row->site_value;
			}
		}

		$site_lang_data = $CommonModel->GetSiteSettings('site');
		
		if($site_lang_data){	
			$site_data = array_merge($site_data, $site_lang_data);
		}

		foreach ($site_data as $row) {
			$this->data[$row->site_key] = $row->site_value;
		}


		$this->db = db_connect();
		$this->session = \Config\Services::session();

     
}

	public function enable() {


	$this->db = db_connect();
	$this->session = \Config\Services::session();

	$query=$this->db->query("select * from uusers where uusersid='".$this->session->get('uusersid')."'")->getRow();

	if ($query->twofactor_secret_key == '') {

	$authenticator = new Google_authendicator();
	$secret=$authenticator->createSecret();

	$profile_image = array(
	"twofactor_secret_key"	=>	$secret
	);

	$update_status = $this->db->table('uusers')->where('uusersid',$this->session->get('uusersid'))->update($profile_image);

	}

	$profile_image = array(
	"twofactor_status"	=>	1
	);

	$update_status = $this->db->table('uusers')->where('uusersid',$this->session->get('uusersid'))->update($profile_image);

	if($update_status)
	{
	$this->session->setFlashData('success_message', 'Profile update SuccessFully..');
	return redirect()->to('/user/settings');	
	}
	else
	{
	$this->session->setFlashData('error_message', 'Profile update Faild!!');
	return redirect()->to('/user/settings');
	}

	}


	public function disable(){

	$this->db = db_connect();
	$this->session = \Config\Services::session();
	
	$profile_image = array(
	"twofactor_status"=>0
	);

	$update_status = $this->db->table('uusers')->where('uusersid',$this->session->get('uusersid'))->update($profile_image);

	if($update_status)
	{
	$this->session->setFlashData('success_message', 'Profile update SuccessFully..');
	return redirect()->to('/user/settings');	
	}
	else
	{
	$this->session->setFlashData('error_message', 'Profile update Faild!!');
	return redirect()->to('/user/settings');
	}


	}


}

