<?php
include 'pagename.php';
include $session;
Session::checkSession_app();
include '../config.php';

function test_input($data) { //filter value function
    $db = new database();
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = mysqli_real_escape_string($db->link, $data);
    return $data;
}
$error = true;
if (isset($_GET["verify_token"]) && isset($_GET["email_update"])) {
	$token = test_input($_GET['verify_token']);
	$update_email = test_input($_GET['email_update']);
    $evp_options_sql = "SELECT option_name, option_value FROM evp_options";
	$evp_options_read = $db->select($evp_options_sql);
	if ($evp_options_read) {
		$evp_options_check = mysqli_num_rows($evp_options_read);
		if ($evp_options_check > 0) {
			while ($row = $evp_options_read->fetch_assoc()) {
				$option_name[$row['option_name']] = $row['option_value'];
			}
			$evp_options_read->free();
			$get_email = $option_name['email_change_to'];
			$get_token = $option_name['verify_email_token'];
		}
	}
	if(($get_email == $update_email) && ($get_token == $token)) {
			$update_user_sql = "UPDATE evp_options
			SET option_value = CASE option_name
				WHEN 'email' THEN '$update_email'
				WHEN 'email_change_to' THEN NULL
				WHEN 'verify_email_token' THEN NULL
			END
			WHERE option_name IN ('email', 'email_change_to', 'verify_email_token')";
			$update_user_read = $db->update($update_user_sql);
			if($update_user_read){
				$action_cat = 'token';
				$error = false;
				$message = "Email Successfully Updated";
				$submessage = "Please login with new email";
			}
	}
}
if($error){
  Session:: destroy();
  Session::init();
  header("Location: $a_url/".$login_page);
}else{
  Session:: destroy();
  Session::init();
  Session::set("action_cat", $action_cat);
  Session::set("action_message", $message);
  Session::set("action_submessage", $submessage);
  header("Location: $a_url/".$login_page);
}
?>