<?php
function corsair_verify_login() {

	echo '
    <style>

    input[type="text"],input[type="email"] {
    width: 100%;
	}
    input[type="submit"] {
        background: #fff;
        border: none;
        padding: 6px 30px;
        border-radius:2px;
        color:#143f5e;
        font-weight:500;
    }
	input[type="submit"]:hover,input[type="submit"]:focus {
	 	background:#fff;
		color:#143f5e;
		outline:none;
		border:none;
	 }
    </style>
    ';

    echo '
    <form action="" method="post">

    <div><input type="hidden" name="username" ></div>
     
    <div>
    <label style="color:#fff;" for="email">Email <strong>*</strong></label><br>
    <input type="email" name="email" class="form-control" aria-describedby="emailHelp" placeholder="Enter email" required /><br>
    </div>
    <div>
    <input type="submit" class="btn btn-primary" name="submit" value="Verify"/>
    <div>
    </form>
    ';
    wp_nonce_field( 'corsair_login_request', 'nonce', false );
    if(isset($_POST['submit']))
    {
		global $wpdb;
		$tokenHolderTbl = $wpdb->prefix.'token_holder';
        $email_account=$_POST['email'];
		$getTokenHolder = $wpdb->get_row("SELECT * FROM ".$tokenHolderTbl." WHERE email = '".$email_account."'");
		
		if( ! $getTokenHolder ){
			echo "<b>There are no CSR Distribution Records for ".$email_account.". Please check again.</b>";
		}else{			
			$nonce = ( isset( $_POST['nonce']) ) ? $nonce = sanitize_key( $_POST['nonce'] ) : false;
			session_start();
			$_SESSION["nonce"] = $nonce;
			$subject = __('CSR Distribution - change of email address',CIP_TXTDOMAIN);
			$randomnum = $_SESSION['random'];
			
			$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
			add_filter( 'wp_mail_from', function(){ return 'noreply@'.$_SERVER['HTTP_HOST']; } );
			add_filter( 'wp_mail_from_name', function(){ return sprintf( __( 'CSR Distribution - change of email address %s',CIP_TXTDOMAIN), get_option('blogname') ); } );
			add_filter('wp_mail_content_type', function(){ return "text/html"; } );
			
			$htmlurl = plugin_dir_url(__FILE__).'mail_template.html';
			$htmlContent = file_get_contents($htmlurl);
			
			$verify_url = site_url() . '/csr-record-checking/?nonce=' . $nonce . '&uname=' . $email_account . '&rand=' . $randomnum;
			
			$message = sprintf(__('CSRnow.com <br><br> This is an e-mail from %s. <br> Click the button below to migrate your CSR Distribution records from %s to %s <br> <p><a href="%s" target="_blank" REPLACE_STYLE>Please click here for verify email</a></p> <br>If the button above does not work, paste this link into your web browser: <br><br> %s <br><br> If you did not make this request, you can safely ignore this e-mail.', CIP_TXTDOMAIN),$blogname, $email_account, $_SESSION['refAccount'], esc_url($verify_url), esc_url($verify_url) );
			
			$htmlContent = str_replace('REPLACE_MESSAGE', $message, $htmlContent);
			$htmlContent = str_replace('REPLACE_SITEURL', site_url(), $htmlContent);
			$htmlContent = str_replace('REPLACE_COPYRIGHT', 'Copyright © CSRnow.com '.date('Y').'. All rights reserved.', $htmlContent);
			$htmlContent = str_replace('REPLACE_HEADER_LOGO', wp_get_attachment_url(1679), $htmlContent);
			$htmlContent = str_replace('REPLACE_STYLE', 'style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; color: #ffffff; text-decoration: none; padding: 6px 24px; display: inline-block; background: linear-gradient( 210deg, #3A31A1 0%, #D01342 100%);"', $htmlContent);
			
			$sent = wp_mail( $email_account, $subject, $htmlContent );
			if( $sent ){
				echo "<b>Email has been sent successfully. Please check your inbox</b>";
			}else{
				echo "<b>Email is not been sent. Please try agin.</b>";
			}
		}
		
    }
}
function corsair_verify_function() {
    corsair_verify_login();
}
?>