<?php
if(!defined('BASEPATH')) {
   die('Direct access to the script is not allowed');
}

$title .= "Confirm Email";

if( $_SESSION["excel_userlogin"] != 1  || $user["client_type"] == 1  ){
  Header("Location:".site_url('logout'));
}
if( $settings["email_confirmation"] == 1 && $user["email_type"] == 1  ){
  //Header("Location:".site_url(''));
}



    if( route(1) == "resend" ){
    $email = route(2);
    $row      =   $conn->prepare("SELECT * FROM clients WHERE client_id=:id");
    $row      ->  execute(array("id"=> iser["client_id"] ));
        $row    = $row->fetch(PDO::FETCH_ASSOC);
  
 
        $code = $user['apikey'];
        
        $max = $user["resend_max"] + 1;
               $update = $conn->prepare("UPDATE clients SET resend_max=:max WHERE client_id=:id");
        $update->execute(array("max"=> $max,"id"=> $user["client_id"] )); 

$message = "Please confirm email address for your account. Click the link below to confirm your email: ". site_url()."confirm_email/activate/$code";
$site_name = site_url();
$to = $user['email'];
$from =  $settings["smtp_user"]; 
$subject = 'Email Confirmation';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = "From: $site_name <$from>";
$headers .= 'Cc: '.$from . "\r\n"; 
$headers .= 'Bcc: '.$from . "\r\n"; 
$send = mail($to, $subject, $message, implode("\r\n", $headers));



            if( $send):


      $success    = 1;
        $successText= "A confirmation email has been sent";
echo '<script>setTimeout(function(){window.location="'.site_url().'"},2000)</script>';
   
        
        else:
          $error    = 1;
          $errorText= "Failed";
          $conn->rollBack();
        endif;
} 
    if( route(1) == "activate" ){
		
	
    $code = route(2);
    
        $update = $conn->prepare("UPDATE clients SET email_type=:type WHERE apikey=:key ");
        $update->execute(array("type"=> 1,"key"=> $code, "verified"=> "Yes" ));


    Header("Location:".site_url(''));
    exit();
  
} 
    

    
   if( $_POST ){
    foreach ($_POST as $key => $value) {
      $_SESSION["data"][$key]  = $value;
    }


$pass  = $_POST["password"];
$newemail  = $_POST["newemail"];
 if( empty($newemail) ){
        $error    = 1;
        $errorText= "Please enter a valid email";
} elseif( empty($pass) ){
        $error    = 1;
        $errorText= "Please enter your password";
  } elseif (userdata_check("email", $newemail)) {
    $error      = 1;
    $errorText  = "Email is already registered";
    }elseif( !userlogin_check($user["username"],$pass) ){

    $error      = 1;
    $errorText  = "Incorrect password";



} else {
$update = $conn->prepare("UPDATE clients SET email=:email, change_email=:mail WHERE client_id=:id");
        $update->execute(array("id"=> $user["client_id"], "mail"=> 1,"email"=> $newemail ));

$row      =   $conn->prepare("SELECT * FROM clients WHERE email=:email ");
    $row      ->  execute(array("email"=> route(2) ));
        $row    = $row->fetch(PDO::FETCH_ASSOC);
  $code = $user['apikey'];
$msg = $msg = "Please confirm email address for your account. Click the link below to confirm your email: ". site_url()."confirm_email/activate/".$code;
       
        $send = mail($newemail,"Email Confirmation",$msg);

      
$insert2= $conn->prepare("INSERT INTO client_report SET client_id=:c_id, action=:action, report_ip=:ip, report_date=:date ");
$mail =$user["email"];
            $insert2= $insert2->execute(array("c_id"=>$user["client_id"],"action"=>"User changed mail from $mail to ".$newemail.".","ip"=>GetIP(),"date"=>date("Y-m-d H:i:s") ));
        
     
        if( $update && $send):



      $success    = 1;
        $successText= "A confirmation email has been sent";
        echo '<script>setTimeout(function(){window.location="'.site_url().'"},2000)</script>';

        else:
          $error    = 1;
          $errorText= "Failed";
          $conn->rollBack(confirm_email);
        endif;
 } 
      }
 
 