<?php
function system_debug_log($msg)
{
	$logfile = '../tmp/logs/system_debug.log';
	$date = date('Y-m-d H:i:s');
	file_put_contents($logfile, "[$date] $msg\n", FILE_APPEND);
}

require_once __DIR__ . '/class.runtime.signal.php';
$signal = RuntimeSignal::instance();
if (!$signal->assert()) {
	system_debug_log('Runtime signal denied worker start: ' . $signal->message());
	exit();
}

function evp_app_url($server)
{
	$protocolSource = isset($server['SERVER_PROTOCOL']) ? strtolower($server['SERVER_PROTOCOL']) : 'http/1.1';
	$delimiter = strpos($protocolSource, '/');
	$baseProtocol = $delimiter !== false ? substr($protocolSource, 0, $delimiter) : $protocolSource;
	$ssl = (!empty($server['HTTPS']) && $server['HTTPS'] === 'on');
	$protocol = $baseProtocol . ($ssl ? 's' : '');
	$port = isset($server['SERVER_PORT']) ? $server['SERVER_PORT'] : '80';
	$portSuffix = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port;
	$host = isset($server['HTTP_HOST']) ? $server['HTTP_HOST'] : (isset($server['SERVER_NAME']) ? $server['SERVER_NAME'] : 'localhost');
	$uri = isset($server['REQUEST_URI']) ? $server['REQUEST_URI'] : '';
	return $protocol . '://' . $host . $portSuffix . $uri;
}

if (((isset($_REQUEST['task_exit'])) || (isset($_REQUEST['filename']) && isset($_REQUEST['uid']) && isset($_REQUEST['total_workers']))) && !isset($_REQUEST['inbox_check'])) {
	$host = dirname(dirname(evp_app_url($_SERVER)));
	$host = htmlspecialchars($host, ENT_QUOTES, 'UTF-8');

	for ($worker_id = 0; $worker_id < $_REQUEST['total_workers']; $worker_id++) {
		$post_fields = $_REQUEST;
		$post_fields['worker_id'] = $worker_id;
		$post_fields['total_workers'] = $_REQUEST['total_workers'];

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, "$host/functions/scripts/v_email.php");
		curl_setopt($curl, CURLOPT_POST, TRUE);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
		curl_setopt($curl, CURLOPT_TIMEOUT, 1);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
		curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
		curl_setopt($curl, CURLOPT_VERBOSE, 0);
		curl_exec($curl);


		system_debug_log("Worker $worker_id launched.");
	}
} elseif (isset($_REQUEST['filename']) && isset($_REQUEST['uid']) && isset($_REQUEST['inbox_check'])) {

	$host = dirname(dirname(evp_app_url($_SERVER)));
	$host = htmlspecialchars($host, ENT_QUOTES, 'UTF-8');

	system_debug_log("Verifying mailbox for filename/uid/inbox_check.");

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, "$host/functions/class.verify_mailbox.php");
	curl_setopt($curl, CURLOPT_POST, TRUE);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $_REQUEST);
	curl_setopt($curl, CURLOPT_TIMEOUT, 1);
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
	curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
	curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
	curl_setopt($curl, CURLOPT_VERBOSE, 0);
	curl_exec($curl);


	system_debug_log("Mailbox verification completed.");
}