<?php
namespace App\Controllers\User;
use App\Views;
use App\Models\CommonModel;
use App\Helpers\site_helper;
use App\Libraries\PHPMailer;  
use App\Libraries\My_PHPMailer;
use PHPMailer\PHPMailer\Exception;
// use PHPMailer\PHPMailer\PHPMailer;
use App\Libraries\MyEncrypt;
use App\Libraries\mathcaptcha;

class Register extends BaseController {

	/**
	 * 
	 * Start 
	 * Create Date: 1 Augest, 2016
	 * This controller to manage register page.
	 * Created karthiga.
	 * @see http://zhyip.com/user/register/
	 * Last Updated: 23 Augest, 2016
	 * End
	 * 
	*/

	public function __construct() 
	{

		$session = \Config\Services::session();
        $this->session = session();
		$this->common_model = model('CommonModel');
		helper('site_helper');
		$status = site_info('site_status');
		if($status!='on')
		{
			redirect('offline');
		}  
	}
	

	public function index() 
	{
      $uri = service('uri');
      $segments = $uri->getSegments();
		

		/* if check IP is blocked */
		/*$blocked_list = Checkip();
		if($blocked_list) 
		{
			redirect('');
		}*/
		$this->session = \Config\Services::session();
		$this->db = db_connect();

		if($this->session->get('reff_name'))
		{
			$this->data['reff_name'] = $this->session->get('reff_name');
			$this->data['sponsor_details'] = $this->db->query("SELECT * FROM uusers where username='".$this->session->get('reff_name')."'")->getRow();
		}
		else
		{
			$this->data['sponsor_details']="";
		}
		
		if(!empty($segments[1]))
         $this->data['referral'] = $segments[1];
		else
		$this->data['referral'] = '';
		$this->data['country'] = $this->db->query("select * from country")->getResult();
		echo view('user/register1',$this->data); 

	}

    public function reg()
    {
		$session = \Config\Services::session();
		helper('site_helper');

		if($this->request->getPost()) 
		{
			//$email=$this->request->getPost('email');
			
	
		$password=md5($this->request->getPost('password'),'');
		$trans_pin=md5($this->request->getPost('trans_pin'),'');
		//$password=$this->request->getPost('password');

		//$password=password_hash($password, PASSWORD_BCRYPT);

		$this->db  = \Config\Database::connect();
            
            $invitation_code = $this->invitation_code_update();

			$get_referral=$this->db->table('uusers')->where('invitation_code',$this->request->getPost('invitor_code'))->get()->getRow();
			$get_mode= $get_referral->new_entry;


            
			$data= array(
				"password" => $password,
				"trans_password" => $trans_pin,
           		"country" => $this->request->getPost('country'),
				"username" => $this->request->getPost('username'),
				"email" => $this->request->getPost('email'),
				"status" =>'1',
				"createdipaddr"=>$_SERVER['REMOTE_ADDR'],
				"createdate" =>date('Y-m-d'),
				"referral_id" => $get_referral->uusersid,
				"invitation_code"=>$invitation_code,
				'new_entry' => $get_mode
			);

			$this->db = db_connect();
			$query_status = $this->db->table('uusers')->insert($data);
			$lastuserid = $this->db->insertID();
            $user_details = $this->db->table('uusers')->where('uusersid',$lastuserid)->get()->getRow();
            $member_count_update = $this->spil_chk($user_details->referral_id,"uusers_matrix",$user_details->uusersid);

            $qury = $this->db->query("SELECT * FROM suggested_strategy WHERE id='1' ")->getResult();
            $qry=$qury[0];
            $margin_column_limit =$qry->margin_column_limit;
            $first_buy_amount=$qry->first_buy_amount;
            $open_position_doubled=$qry->open_position_doubled;
            $whole_profit_ratio=$qry->whole_profit_ratio;
            $whole_profit_callback=$qry->whole_profit_callback;
            $buyin_callback=$qry->buyin_callback;
            $sub_position_callback  = $qry->sub_position_callback;
            $sub_position  = $qry->sub_position;
            
            $tade1=$qry->margincall_drop ;
            $tade2=$qry->buyin_ratio_times;

            $trade_data=array(
                "uusersid"              =>  $lastuserid,
                "first_buy_amount"      =>  $first_buy_amount,
                "open_position_doubled" =>  $open_position_doubled,
                "margin_column_limit"   =>  $margin_column_limit,
                "whole_profit_ratio"    =>  $whole_profit_ratio,
                "whole_profit_callback" =>  $whole_profit_callback,
                "buyin_callback"        =>  $buyin_callback,
                "margincall_drop"       =>  $tade1,
                "buyin_ratio_times"     =>  $tade2,
                "sub_position_callback" =>  $sub_position_callback,
                "sub_position" =>  $sub_position,
                "sync_strategy_status"  =>  0,
                "strategy_mode"         =>  1,
                "strategy_type"         => 'Single strategy',
				"risk_type"             => 1
            );

            $this->db = db_connect();
            
            $query_status1 = $this->db->table('user_strategy')->insert($trade_data);

			$security_data = array(
				"uusersid"	=>	$lastuserid,
				"changeip"	=>	'disabled',
				"change_browser"=>	'enabled',
				
			);
			$query_status = $this->db->table('uusers_security')->insert($security_data);

			$referral=$get_referral->uusersid;
			$query_status='1';
			if($query_status)
			{

				$query=$this->db->query("SELECT * FROM uusers WHERE referral_id='".$referral."'");
				$user_count=$query->getNumRows();

			}

			$useremail=$this->session->get('email');

			$user1=$this->db->query("SELECT * FROM uusers WHERE email='".$useremail."'");
			$row=$user1->getRow();

			$uusers_id=$row->uusersid;

			$referral_id=$this->session->get('referral');
		    $matrix_data= array(
		    	"uusersid"=>$uusers_id,
		    	"referral_id"=>$referral_id,
		    	"spillover_id"=>$referral_id,
		    	"user_count"=>'0',
		    	"cr_date" => date('Y-m-d H:i:s')

		    );	

		    $query_status = $this->db->table('uusers_matrix')->insert($matrix_data);

		    if($query_status)
		    {
		    	$user_count=$user_count;

		    	$data=array(
		    		"user_count"=>$user_count
		    	);

		    	$update_status = $this->db->table('uusers_matrix')->where('uusersid',$referral_id)->update($data);
			    if($query_status)
		    	{	
		    		$subject = 'Account Activated Successfully';
		    		$mailid = '2';
		    		$link =  base_url().'/login';
					$invite_url =  base_url()."/register/".$invitation_code;
		    		$mail_subject_data = $this->common_model->getMailTemplate($mailid);
		    		$message  = str_replace('[EMAIL]', $this->session->get('email'), $mail_subject_data->message);
		    		$message  = str_replace('[FULLNAME]', $this->request->getPost('username'), $message);
					$message  = str_replace('[Referral]', $invite_url, $message);
		    		$message  = str_replace('[URL]', $link, $message);
		    		$message  = str_replace('[PASSWORD]', $this->request->getPost('password'), $message);
		    		$message  = str_replace('#sitename', site_info_lang('site_name',$this->session->get('langid')), $message);
		    		$mail_status = $this->sendmail($useremail,$message,$subject,$mail_subject_data->from_mail);
		    	}
				$this->session = session();	


		    	$session->setFlashData('success_message', 'Sign up Completed');
		    	return redirect()->to('user/login');	

		    } 

		    else 
		    {
		    	$session->setFlashData('error_message', 'Failed to Signup');
				return redirect()->to('user/login');

		    }
		}
		
  
	}



    public function invitation_code_update(){
            $this->db  = \Config\Database::connect();
            $code = $this->invitation_code();
            $res = $this->db->query("SELECT * from uusers where invitation_code = '".$code."'")->getNumRows();
            if($res > 0)
                $this->invitation_code_update();
            else
                return $code;

    }
	public function reg1()
	{

		$session = \Config\Services::session();
		helper('site_helper');


		if($this->request->getPost()) 
		{
			//$email=$this->request->getPost('email');
			$password=crypt(urlencode($this->request->getPost('password')),NULL);

			
			$fullname = $this->request->getPost('first_name')." ".$this->request->getPost('last_name');
			//$cpassword=$this->request->getPost('cpassword');

            $this->db  = \Config\Database::connect();
            $res = $this->db->query("SELECT * from uusers where invitation_code = '".$this->session->get('invitation_code')."'")->getNumRows();
            if($res == 0){
                $invitation_code = $this->session->get('invitation_code');
            }else{
                $invitation_code = $this->invitation_code_update();
            }
			$data= array(
				"password" => $password,
                "fullname" => $fullname,
				"country" => $this->request->getPost('country'),
				"email" => $this->session->get('email'),
				"status" =>'1',
				"createdipaddr"=>$_SERVER['REMOTE_ADDR'],
				"createdate" =>date('Y-m-d'),
				"referral_id" => $this->session->get('referral'),
				"invitation_code"=>$invitation_code
			);
			
			print_r($data);
			exit;
			
			$this->db = db_connect();
			$query_status = $this->db->table('uusers')->insert($data);
			$lastuserid = $this->db->insertID();
            $user_details = $this->db->table('uusers')->where('uusersid',$lastuserid)->get()->getRow();
            $member_count_update = $this->spil_chk($user_details->referral_id,"uusers_matrix",$user_details->uusersid);



            $qury = $this->db->query("SELECT * FROM suggested_strategy WHERE id='1' ")->getResult();
            $qry=$qury[0];
            $margin_column_limit =$qry->margin_column_limit;
            $first_buy_amount=$qry->first_buy_amount;
            $open_position_doubled=$qry->open_position_doubled;
            $whole_profit_ratio=$qry->whole_profit_ratio;
            $whole_profit_callback=$qry->whole_profit_callback;
            $buyin_callback=$qry->buyin_callback;
            $sub_position_callback  = $qry->sub_position_callback;
            $sub_position  = $qry->sub_position;
            
            $tade1=$qry->margincall_drop ;
            $tade2=$qry->buyin_ratio_times;

            $trade_data=array(
                "uusersid"              =>  $lastuserid,
                "first_buy_amount"      =>  $first_buy_amount,
                "open_position_doubled" =>  $open_position_doubled,
                "margin_column_limit"   =>  $margin_column_limit,
                "whole_profit_ratio"    =>  $whole_profit_ratio,
                "whole_profit_callback" =>  $whole_profit_callback,
                "buyin_callback"        =>  $buyin_callback,
                "margincall_drop"       =>  $tade1,
                "buyin_ratio_times"     =>  $tade2,
                "sub_position_callback" =>  $sub_position_callback,
                "sub_position" =>  $sub_position,
                "sync_strategy_status"  =>  0,
                "strategy_mode"         =>  1,
                "strategy_type"         => 'One-shot strategy'
            );

            $this->db = db_connect();
            
            $query_status1 = $this->db->table('user_strategy')->insert($trade_data);

			$security_data = array(
				"uusersid"	=>	$lastuserid,
				"changeip"	=>	'disabled',
				"change_browser"	=>	 'enabled',
			);
			$query_status = $this->db->table('uusers_security')->insert($security_data);

			$referral=$this->session->get('referral');
			if($query_status)
			{

				$query=$this->db->query("SELECT * FROM uusers WHERE referral_id='".$referral."'");
				$user_count=$query->getNumRows();

			}

			$useremail=$this->session->get('email');

			$user1=$this->db->query("SELECT * FROM uusers WHERE email='".$useremail."'");
			$row=$user1->getRow();

			$uusers_id=$row->uusersid;

			$referral_id=$this->session->get('referral');
		    /*$spillover_id=$this->db->query("SELECT * FROM uusers WHERE uusersid='".$spill_id."'");
		    $row=$spillover_id->getRow();
		    $spill_over_id=$row->referral_id;*/
		    $matrix_data= array(
		    	"uusersid"=>$uusers_id,
		    	"referral_id"=>$referral_id,
		    	"spillover_id"=>$referral_id,
		    	"user_count"=>'0',
		    	"cr_date" => date('Y-m-d H:i:s')

		    );	

		    $query_status = $this->db->table('uusers_matrix')->insert($matrix_data);

		    if($query_status)
		    {
		    	$user_count=$user_count;

		    	$data=array(
		    		"user_count"=>$user_count
		    	);

		    	$update_status = $this->db->table('uusers_matrix')->where('uusersid',$referral_id)->update($data);



		    	if($query_status)
		    	{	
		    		$subject = 'Account Activated Successfully';
		    		$mailid = '2';
		    		$link =  base_url().'/login';
		    		$mail_subject_data = $this->common_model->getMailTemplate($mailid);
		    		$message  = str_replace('[EMAIL]', $this->session->get('email'), $mail_subject_data->message);
		    		$message  = str_replace('[FULLNAME]', $fullname, $message);
		    		$message  = str_replace('[URL]', $link, $message);
		    		$message  = str_replace('[PASSWORD]', $this->request->getPost('password'), $message);
		    		$message  = str_replace('#sitename', site_info_lang('site_name',$this->session->get('langid')), $message);
		    		$mail_status = $this->sendmail($useremail,$message,$subject,$mail_subject_data->from_mail);
		    	}

	        //$session->unset('otp');
			//$this->session->destroy();

		    	$this->session = session();	


		    	$session->setFlashData('success_message', 'Sign up Completed');
		    	return redirect()->to('user/login');	

		    } 

		    else 
		    {
		    	$session->setFlashData('error_message', 'Failed to Signup');

		    	return redirect()->to('user/login');

		    }
		}

	}

    public function spil_chk($spilover,$table,$memberid)
    {
        $this->db = db_connect();
        $find_next=$this->db->query("select * from $table where uusersid='".$spilover."'")->getRow();
        if($find_next)
        {
            $spilloverid=$find_next->uusersid;
            $spilover=$find_next->spillover_id;  

            if($spilloverid > 0)
            {
                $members_cnt=$find_next->members_cnt;
                
                if($members_cnt=='' || $members_cnt=='0')
                {
                    $member_ads=$memberid;
                }
                else
                {
                    $member_ads=$members_cnt.",".$memberid; 
                }
                $this->db->query("update $table set members_cnt='".$member_ads."' where uusersid='".$spilloverid."'");
                $this->spil_chk($spilover,$table,$memberid);
            }
        }
    }

	public function invitation_code()
	{

		$cde='0123456789abcdeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

		$invite=substr(str_shuffle(str_repeat($cde, 7)), 0, 7);

		return $invite;

	}
	public function regmail()
	{
		$this->session = session();		    

		$email=$this->request->getPost('email');
		$referral=$this->request->getPost('referral');

		$this->db = db_connect();

		$referral1=$this->db->query("SELECT * FROM uusers WHERE invitation_code='".$referral."'");
		$row=$referral1->getRow();

		$referal_id=$row->uusersid;
			//$id=$res->uusersid;
		$invite=$this->invitation_code();

		$repeat_invite=$this->db->query("SELECT * FROM uusers WHERE invitation_code='".$invite."'");

		$numrows=$repeat_invite->getNumRows();

		if($numrows>0)
		{
			$invite=$this->invitation_code();
		}


		$this->session->set("invitation_code" , $invite);

		     //$session->set($newdata);
		redirect('user/register/reg',$this->data);

	}

    public function verifyregcode()
    {
	    /*$email = $this->request->getPost('email');
	    $referral=$this->request->getPost('invitor_code'); 
	    $this->db = db_connect();
		if ($referral) 
		{
		    $referral1=$this->db->query("SELECT * FROM uusers WHERE invitation_code='".$referral."'");
		    $row=$referral1->getRow();

		    $referal_id=$row->uusersid;

    	}
    	else
    	{

    		$default_refrral_setting_get = $this->db->query("SELECT * FROM `site_controls` WHERE `settings_name` LIKE 'referral' AND site_key = 'ref_user' ")->getRow();

		    $default_refrral = $default_refrral_setting_get->site_value;	

		    $default_referral_details = $this->db->query("SELECT * FROM `uusers` WHERE `uusersid` = '".$default_refrral."' ")->getRow();

		    $referal_id = $default_referral_details->uusersid;

		    $referral = $default_referral_details->invitation_code;
    	}

    	if($referal_id=='')
    	{
    		$json['message'] = "exist1";	
    	}
    	else
    	{

	    	$condition = "email ='" . $email . "'";
		   	$uemail = $this->common_model->GetRow('uusers', $condition);
		   	if($uemail)
		   	{
		   		$json['message'] = "exist";
		   	}
		   	else
		   	{
				$otp = rand(100000, 999999); //generates random otp
				//echo $otp;
		    	$message = "Your one time email verification code is " . $otp;
		    	$sub = "Email verification";
				$this->session = \Config\Services::session();
				$this->session->set('referral',$referal_id);
		    	$this->session->set('email',$email);
		    	$this->session->set('otp',$otp);
				$this->db = db_connect();
		    	$site_data = $this->common_model->GetSettingall('email');
				if($site_data) 
				{
					foreach ($site_data as $row) 
					{
		    			$admin_email[$row->site_key] = $row->site_value;
		    		}

		    	}	 
		    	if($admin_email['admin_email'])
		    	{
		    		$from_mail=$admin_email['admin_email'];
		    	}
		    	else
				{
		    		$from_mail = "support@trading.com";
		    	}

		    	if($email)
		    	{
				    $sub = 'Verify E-Mail Address';
				    $mailid = '1';
				    $mail_subject_data = $this->common_model->getMailTemplate($mailid);
				    $message  = str_replace('[OTP]', $otp, $mail_subject_data->message);
				    $message  = str_replace('#sitename', site_info_lang('site_name',$this->session->get('langid')), $message);
				    $mail=$this->sendmail($email,$message,$sub,$mail_subject_data->from_mail);
				    $mail=1;
		    		if($mail)
		    		{
						    $data=array(
						    "referral" => $referral,
						    "email" => $email,
						    "otp" => $otp
						    );
							$this->db = db_connect();
							$query_status = $this->db->table('register')->insert($data);
		    				$json['message'] = "OTP send to your Email.";
		    		}
					
				}
				else
		    	{        	
		    		$json['message'] = "Cannot Send OTP.";
			    }
	 		}
	 	}*/

		 $this->db = db_connect();
		$email = $this->request->getPost('email');
		$condition = "email ='" . $email . "'";
		$uemail = $this->common_model->GetRow('register', $condition);

		$otp = rand(100000, 999999);

		if($uemail)
		{
			$data=array(
				"otp" => $otp
			);
			$query_status = $this->db->table('register')->where('email',$email)->set($data)->update();

		}
		else
		{
			$data=array(
				"email" => $email,
				"otp" => $otp
			);
				$query_status = $this->db->table('register')->insert($data);
				

		}

		$message = "Your one time email verification code is " . $otp;
		$sub = "Email verification";
		$this->session = \Config\Services::session();
		$this->session->set('email',$email);
		$this->session->set('otp',$otp);
		$this->db = db_connect();
		$site_data = $this->common_model->GetSettingall('email');
		if($site_data) 
		{
			foreach ($site_data as $row) 
			{
				$admin_email[$row->site_key] = $row->site_value;
			}

		}	 
		if($admin_email['admin_email'])
		{
			$from_mail=$admin_email['admin_email'];
		}
		else
		{
			$from_mail = "support@trading.com";
		}

		$sub = 'Verify E-Mail Address';
		$mailid = '1';
		$mail_subject_data = $this->common_model->getMailTemplate($mailid);
		$message  = str_replace('[OTP]', $otp, $mail_subject_data->message);
		$message  = str_replace('#sitename', site_info_lang('site_name',$this->session->get('langid')), $message);
		$mail=$this->sendmail($email,$message,$sub,$mail_subject_data->from_mail);
		$json['message'] = "OTP send to your Email.";



		// echo $otp;
    	echo json_encode($json['message']);
    	exit;
	}


    public function verifyotp()
    {
    	$this->session = session();
    	$code=$this->request->getPost('code1');
    	$otp=$this->session->get('otp');
		
		

    	if($code==$otp)
    	{
    		$json['valid'] = true;

    	}
    	else
    	{
    		$json['valid'] = false;
    	}

    	echo json_encode($json);

    }

    public function validreferral() {

    	$getPost_refname = $this->request->getPost('invitor_code');
    	if($getPost_refname){
    		$condition = "invitation_code ='" . $getPost_refname . "'";
    		$refname = $this->common_model->GetRow('uusers', $condition);
    		if($refname){
    			$json['valid'] = true;
    		}
    		else {
    			$json['valid'] = false;
    		}
    	}
    	else{
    		$json['valid'] = false;
    	}
    	echo json_encode($json);
    }

    public function changecountry(){

    	$iso2 = $this->request->getPost('id');

    	$condition = "iso_2 = '".$iso2."'";
    	$countries = $this->common_model->GetRow('country',$condition);

    	if($countries!='')
    	{
    		echo $countries->CountryCode;
    	}
    	else
    	{
    		echo 0;
    	}

    }

    public function validuser()
    {
    	$getPost_usrname = $this->request->getPost('username');
    	if($getPost_usrname){
    		$condition = "username ='" . $getPost_usrname . "'";
    		$uname = $this->common_model->GetRow('uusers', $condition);
    		if($uname){
    			$json['valid'] = false;
    		}
    		else {
    			$json['valid'] = true;
    		}
    	}
    	else{
    		$json['valid'] = true;
    	}
    	echo json_encode($json);

    }

    public function validemail(){
    	$getPost_email = $this->request->getPost('email');
    	if($getPost_email){
    		$condition = "email ='" . $getPost_email . "'";
    		$uemail = $this->common_model->GetRow('uusers', $condition);
    		if($uemail){
    			$json['valid'] = false;
    		}
    		else {
    			$json['valid'] = true;
    		}
    	}
    	else{
    		$json['valid'] = true;
    	}
    	echo json_encode($json);
    }


    public function captcha_check($str)
    {
    	$this->load->library('recaptcha');
    	$response = $this->recaptcha->verifyResponse($str);
    	if (isset($response['success']) and $response['success'] === true) {
    		return true;  
    	}
    	else
    	{	
    		$this->form_validation->set_message('captcha_check', ucwords($this->lang->line('cap_err')));
    		return false;
    	}

    }


	// public function referral() {

	// 	$referral = $this->uri->segment(2);

	// 	if($referral)
	// 	{
	// 		$condition = "username ='" . $referral . "'";
	// 		$sponser_user = $this->common_model->GetRow('uusers', $condition);
	// 		if($sponser_user){
	// 			$this->session->set_userdata('reff_name',$sponser_user->username);
	// 			redirect('user');
	// 		}
	// 		else {

	// 			redirect();
	// 		}
	// 	} else {
	// 		$this->session->unset_userdata('reff_name');
	// 	}

	// }

    public function referral() {

    	$uri = service('uri');
    	$this->uri = service('uri');
    	$this->uri = current_url(true);

    	$this->session = \Config\Services::session();
    	$referral =  $this->request->uri->getSegment('2');

    	if($referral)
    	{
    		$condition = "username ='" . $referral . "'";
    		$sponser_user = $this->common_model->GetRow('uusers', $condition);
    		if($sponser_user){
    			$this->session->set('reff_name',$sponser_user->username);

    			return redirect()->to('register');
    		}
    		else {

    			return redirect()->to('/');
    		}
    	} else {
    		$this->session->unsetGet('reff_name');
    	}

    }

    public function refersh(){

    	$this->mathcaptcha->init();
    	$math_captcha_question = $this->mathcaptcha->get_question();
    	$this->load->helper('captcha');
    	$img_name = time().'.jpg';
    	$img_path = 'captcha/';
    	$im = imagecreate('150', '50');
    	$text = array(245,171,53);
    	$bg_color   = imagecolorallocate ($im, 34, 49, 63);
    	$text_color = imagecolorallocate ($im, 255, 255, 255);	
    	ImageFilledRectangle($im, 0, 0, 150, 50, $bg_color);   
    	$cap = imagestring($im, 12, 45, 18, $math_captcha_question, $text_color);
    	$cp = ImageJPEG($im, $img_path.$img_name);
    	$this->data['ur'] = $img_path.$img_name;

    	echo base_url().$this->data['ur'];
    }


    public function sendmail($useremail,$message, $subject,$from_mail) {
        $this->session = session();

    	$condition = "settings_name = 'email'";
    	$email_setting_data = $this->common_model->GetSettingall_lang($condition);

    	foreach ($email_setting_data as $row) {

    		$mail_data[$row->site_key] = $row->site_value;
    	}

    	$message  = str_replace('#adminemail', $mail_data['admin_email'], $message);

    	if($mail_data['email_throw']=='php')
    	{
    		$mail = new PHPMailer();
    		$mail->isMail();

    	} else {

    		$mail = new PHPMailer();
    		$mail->IsSMTP(); 
    		$mail->SMTPAuth   = TRUE; 
    		$mail->SMTPSecure = $mail_data['smtp_type'];  
    		$mail->Host       = $mail_data['smtp_hostname'];      
    		$mail->Port       = $mail_data['smtp_portno'];                   
    		$mail->Username   = $mail_data['smtp_username'];  
    		$mail->Password   = $mail_data['smtp_password'];  
			$mail->SMTPOptions = array(
				'ssl' => array(
					'verify_peer' => false,
					'verify_peer_name' => false,
					'allow_self_signed' => true
				)
			); 
    	}

    	$mail->SetFrom($from_mail, site_info_lang('site_name',$this->session->get('langid')));  
    	$mail->AddReplyTo($from_mail,site_info_lang('site_name',$this->session->get('langid')));

    	$mail->Subject = $subject;
    	$mail->msgHTML($message);
    	$mail->AddAddress($useremail, $useremail);

    	if(!$mail->Send()) {
    		return false;
    	} else {
    		return true;
    	}
    }

    function check_math_captcha() {
    	$str = $this->request->getPost('math_captcha');

    	if( $str == $this->session->userdata('mathcaptcha_answer'))
    	{
    		$json['valid'] = true;
    	}
    	else
    	{
    		$json['valid'] = false;
    	}
    	echo json_encode($json);
    }

    public function getfullname(){

    	$getPost_sponsor = $this->request->getPost('sponsor');
    	if($getPost_sponsor){
    		$condition = "username ='" . $getPost_sponsor . "'";
    		$uemail = $this->common_model->GetRow('uusers', $condition);
    		if($uemail){
    			$json['fullname'] = $uemail->fullname;
    		}
    		else {
    			$json['valid'] = false;
    		}
    	}
    	else{
    		$json['valid'] = true;
    	}
    	echo json_encode($json);
    }

    function test() {
    	$this->load->view('user/demo');

    }


}