<?php
if (!defined('ABSPATH')) {
	fwduvp_send_mail_maybe_bootstrap_wordpress();
}

if (!defined('ABSPATH')) {
	exit;
}

if (!function_exists('wp_unslash') || !function_exists('wp_mail')) {
	fwduvp_send_mail_maybe_bootstrap_wordpress();
}

// Verify nonce before processing request input.
$fwduvp_raw_nonce = filter_input(INPUT_GET, 'fwduvp_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE);

if ($fwduvp_raw_nonce === null || $fwduvp_raw_nonce === false || !function_exists('wp_verify_nonce')) {
	http_response_code(403);
	exit('Invalid nonce.');
}

$fwduvp_nonce = sanitize_text_field(wp_unslash((string) $fwduvp_raw_nonce));
if (!wp_verify_nonce($fwduvp_nonce, 'fwduvp_send_mail_to_friend')) {
	http_response_code(403);
	exit('Invalid nonce.');
}

// Read and sanitize input values from request.
$fwduvp_raw_to = filter_input(INPUT_GET, 'friendMail', FILTER_SANITIZE_EMAIL, FILTER_NULL_ON_FAILURE);
$fwduvp_raw_from = filter_input(INPUT_GET, 'yourMail', FILTER_SANITIZE_EMAIL, FILTER_NULL_ON_FAILURE);
$fwduvp_raw_link = filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE);

$fwduvp_raw_to = $fwduvp_raw_to === null || $fwduvp_raw_to === false ? '' : (string) $fwduvp_raw_to;
$fwduvp_raw_from = $fwduvp_raw_from === null || $fwduvp_raw_from === false ? '' : (string) $fwduvp_raw_from;
$fwduvp_raw_link = $fwduvp_raw_link === null || $fwduvp_raw_link === false ? '' : (string) $fwduvp_raw_link;

$fwduvp_to = sanitize_email(wp_unslash($fwduvp_raw_to));
$fwduvp_from = sanitize_email(wp_unslash($fwduvp_raw_from));
$fwduvp_link = esc_url_raw(wp_unslash($fwduvp_raw_link));

if (!is_email($fwduvp_to) || !is_email($fwduvp_from) || $fwduvp_link === '') {
	http_response_code(400);
	exit('Invalid parameters.');
}

$fwduvp_subject = 'Shared video from FWD Video Player';
$fwduvp_message = 'Your friend ' . $fwduvp_from . ' shared a video with you: ' . $fwduvp_link;

$fwduvp_host = wp_parse_url(home_url('/'), PHP_URL_HOST);
$fwduvp_host = is_string($fwduvp_host) ? strtolower($fwduvp_host) : '';
$fwduvp_host = preg_replace('/^www\./', '', $fwduvp_host);
$fwduvp_sender = $fwduvp_host !== '' ? 'no-reply@' . $fwduvp_host : '';

if (!is_email($fwduvp_sender)) {
	$fwduvp_sender = sanitize_email((string) get_option('admin_email', ''));
}

$fwduvp_headers = array(
	'From: FWD Video Player <' . $fwduvp_sender . '>',
	'Reply-To: ' . $fwduvp_from,
	'Content-Type: text/plain; charset=UTF-8',
);

if (!wp_mail($fwduvp_to, $fwduvp_subject, $fwduvp_message, $fwduvp_headers)) {
	echo 'error';
} else {
	echo 'sent';
}

function fwduvp_send_mail_maybe_bootstrap_wordpress()
{
	$fwduvp_search_dir = __DIR__;

	for ($fwduvp_depth = 0; $fwduvp_depth < 8; $fwduvp_depth++) {
		$fwduvp_candidate = $fwduvp_search_dir . DIRECTORY_SEPARATOR . 'wp-load.php';
		if (is_readable($fwduvp_candidate)) {
			require_once $fwduvp_candidate;
			return;
		}

		$fwduvp_parent_dir = dirname($fwduvp_search_dir);
		if ($fwduvp_parent_dir === $fwduvp_search_dir) {
			break;
		}

		$fwduvp_search_dir = $fwduvp_parent_dir;
	}
}