<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8" />
<title>tghost.co.uk Mail filters</title>

<script type="text/javascript">
  function showhide(id) {
    var e = document.getElementById(id);
      if(e.style.display == 'block')
        e.style.display = 'none';
      else
        e.style.display = 'block';
  }
</script>

</head>

<body>

<?php

define("MFPATH", "/var/vmail/mailfilters/");

session_start();

if (isset($_GET['logout'])) {
  session_unset();
}

if (isset($_POST['username']) && isset($_POST['password'])) {

  $_SESSION['username'] = $_POST['username'];
  $_SESSION['password'] = $_POST['password'];

  $errrpt = error_reporting();
  error_reporting(0);

  if (!imap_open("{mail.tghost.co.uk:993/imap/ssl/validate-cert/readonly}", $_SESSION['username'], $_SESSION['password'], OP_HALFOPEN, 1)) {
    echo '<h2>Invalid password!</h2>' . "\n";
    echo '</body></html>';
    exit();
  }

  $mailfilter = file_get_contents(MFPATH . $_SESSION['username']);

  if ($mailfilter === FALSE) {
    echo "<pre>Error reading mail filter, please report this to your administrator.\n</pre>";
  }

  error_reporting($errrpt);

  echo '<h2>Editing maildrop mailfilter for ' . $_SESSION['username'] . ' &#40;<a href="' . basename(__FILE__) . '?logout">logout</a>&#41;</h2>' . "\n";

  echo '<a href="#" onclick="showhide(\'example\');">Show example</a>' . "\n";
  echo '<div id="example" style="display: none;"><strong>To automatically place spam email into a folder called Junk:</strong><br>' . "\n";
  echo '<i>&#40;Be sure to create the folder in your IMAP inbox first.&#41;</i><pre>';
  echo 'if (/^X-Spam-Flag:.*YES/)' . "\n";
  echo '{' . "\n";
  echo '  exception {' . "\n";
  echo '    to $DEFAULT/.Junk/' . "\n";
  echo '  }' . "\n";
  echo '}' . "\n";
  echo '</pre></div>' . "\n";

  echo '<form action="' . basename(__FILE__) . '" method="post">' . "\n";
  echo '<textarea name="mailfilter" id="mailfilter" rows="20" cols="80">' . $mailfilter . '</textarea><br>' . "\n";
  echo '<input type="submit" value="Apply">' . "\n";
  echo '</form>' . "\n";
  echo "\n";
  echo '<script type="text/javascript">' . "\n";
  echo '  document.getElementById("mailfilter").focus();' . "\n";
  echo '</script>' . "\n";

} else if (isset($_POST['mailfilter'])) {

  $mailfilter = preg_replace('~\R~u', "\n", $_POST['mailfilter']);
  if (!file_put_contents(MFPATH . $_SESSION['username'], $mailfilter)) {
    echo '<h2>Sorry, something went wrong...</h2>' . "\n";
  } else {
    echo '<h2>Changes applied!</h2>' . "\n";
  }

  session_unset();

} else {

  echo '<h2>tghost.co.uk Mail filters login</h2>' . "\n";

  if (!isset($_SERVER['HTTPS'])) {
    echo '<h3 style="color: red; font-weight: bold;">WARNING: You are viewing this page using plain HTTP.  &nbsp;This is NOT secure. &nbsp;Please consider using <a href="https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '">HTTPS</a> instead.</h3>' . "\n";
  }

  echo '<form action="' . basename(__FILE__) . '" method="post">' . "\n";
  echo '<label for="username">Login &#40;email&#41;:</label> <input type="text" id="username" name="username"><br>' . "\n";
  echo '<label for="password">Password:</label> <input type="password" id="password" name="password"><br>' . "\n";
  echo '<input type="submit" value="Login">' . "\n";
  echo '</form>' . "\n";
  echo "\n";
  echo '<script type="text/javascript">' . "\n";
  echo '  document.getElementById("username").focus();' . "\n";
  echo '</script>' . "\n";

}

echo '<p><a href="/">Back to tghost.co.uk Mail</a> | <a href="' . basename(__FILE__) . 's">Source</a><br></p>'
?>

</body>

</html>