<?php if (defined('TENANT_URL') && !empty(TENANT_URL)) {
    $_REQUEST['tenant'] = TENANT_URL;
}
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (file_exists('../../../' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('../../../' . $_REQUEST['tenant'] . '/config.php');
}
if (file_exists('/config.php')) {
    require_once('/config.php');
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mailType = (int)($_REQUEST['mail_type'] ?? 1);
if (!in_array($mailType, [0, 1, 2], true)) {
    $mailType = 1;
}
switch ($_REQUEST['FCN']) {
    default:
        break;

    case "update":
        $stmt = $GLOBALS['con']->prepare("UPDATE `system` SET `" . $_REQUEST['col'] . "`='" . $_REQUEST['val'] . "'  WHERE `id`='1' ") or die(mysqli_error($GLOBALS['con']));
        $stmt->execute();
        $r = $stmt->get_result();
        $stmt->close();

        break;

    case "updateDB":
        $stmt = $GLOBALS['con']->prepare("UPDATE `system` SET
`db_tables`='" . $_REQUEST['db_tables'] . "',
`db_master`='" . $_REQUEST['db_master'] . "',
`db_username`='" . $_REQUEST['db_username'] . "',
`db_password`='" . $_REQUEST['db_password'] . "'
WHERE `id`='1'
") or die(mysqli_error($GLOBALS['con']));
        $stmt->execute();

        $content = '<?php session_start();
$master=mysqli_connect("localhost", "' . $_REQUEST['db_username'] . '", "' . $_REQUEST['db_password'] . '","' . $_REQUEST['db_master'] . '") or die(mysqli_error());
$con=mysqli_connect("localhost", "' . $_REQUEST['db_username'] . '", "' . $_REQUEST['db_password'] . '","' . $_REQUEST['db_tables'] . '") or die(mysqli_error());

$_SESSION[\'seclevel\']="admin";
$path="' . $_REQUEST['db_master'] . '";
 
?>
';

$dbfile = fopen("../connection.php", "w");
fwrite($dbfile, $content);
fclose($dbfile);
echo "success";
break;

case "updateSitePath":
$stmt = $GLOBALS['con']->prepare("UPDATE `system` SET
`site_path`='" . $_REQUEST['site_path'] . "'
WHERE `id`='1'
") or die(mysqli_error($GLOBALS['con']));
$stmt->execute();
echo "success";
break;

case "updateMailer":
$mailHost = (string)($_REQUEST['mail_host'] ?? '');
$mailUsername = (string)($_REQUEST['mail_username'] ?? '');
$mailPassword = (string)($_REQUEST['mail_password'] ?? '');
$mailFrom = (string)($_REQUEST['mail_from'] ?? '');
$mailFromName = (string)($_REQUEST['mail_from_name'] ?? '');
$mailTestAddress = (string)($_REQUEST['mail_test_address'] ?? '');
$mailTestName = (string)($_REQUEST['mail_test_name'] ?? '');

$stmt = $GLOBALS['con']->prepare("UPDATE `system` SET
`mail_type`=?,
`mail_host`=?,
`mail_username`=?,
`mail_password`=?,
`mail_from`=?,
`mail_from_name`=?,
`mail_test_address`=?,
`mail_test_name`=?
WHERE `id`='1'

") or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param(
'isssssss',
$mailType,
$mailHost,
$mailUsername,
$mailPassword,
$mailFrom,
$mailFromName,
$mailTestAddress,
$mailTestName
);
$stmt->execute();

echo "success";
break;
case "testMailer":

require '../../packages/mailer/Exception.php';
require '../../packages/mailer/PHPMailer.php';
require '../../packages/mailer/SMTP.php';

$mail = new PHPMailer(true);

try {
$mail->SMTPDebug = 0;
if ($mailType === 1) {
$mail->isSMTP();
}
$mail->Host = $_REQUEST['mail_host'];
$mail->SMTPAuth = true;
$mail->Username = $_REQUEST['mail_username'];
$mail->Password = $_REQUEST['mail_password'];
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom($_REQUEST['mail_from'], 'EnRep Test Mailer');

$mail->addAddress($_REQUEST['mail_test_address'], $_REQUEST['mail_test_name']);
$mail->addReplyTo($_REQUEST['mail_from'], 'EnRep Test Mailer');

//$mail->addAttachment('image.jpg');

$mail->isHTML(true);
$mail->Subject = 'EnRep test email';
$mail->Body = 'This is testing the email protocol';
$mail->AltBody = 'This is testing the email protocol';

$mail->send();
echo 'success';
$stmt = $GLOBALS['con']->prepare("UPDATE `system` SET `mail_check`='1' WHERE `id`='1'") or
die(mysqli_error($GLOBALS['con']));
$stmt->execute();
} catch (Exception $e) {
$stmt = $GLOBALS['con']->prepare("UPDATE `system` SET `mail_check`='0' WHERE `id`='1'") or
die(mysqli_error($GLOBALS['con']));
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

break;

case "saveSMode":
$stmt = $GLOBALS['con']->prepare("UPDATE `system` SET `smode`='" . $_REQUEST['mode'] . "' WHERE `id`='1'") or
die(mysqli_error($GLOBALS['con']));
$stmt->execute();
echo "success";
break;

case "saveNMode":
$stmt = $GLOBALS['con']->prepare("UPDATE `system` SET `nmode`='" . $_REQUEST['mode'] . "' WHERE `id`='1'") or
die(mysqli_error($GLOBALS['con']));
$stmt->execute();
echo "success";
break;
}