<?php if(defined('TENANT_URL') && !empty(TENANT_URL)) {
    $_REQUEST['tenant'] = TENANT_URL;
}


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;
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":
$stmt = $GLOBALS['con']->prepare("UPDATE `system` SET
`mail_host`='".$_REQUEST['mail_host']."',
`mail_username`='".$_REQUEST['mail_username']."',
`mail_password`='".$_REQUEST['mail_password']."',
`mail_from`='".$_REQUEST['mail_from']."',
`mail_from_name`='".$_REQUEST['mail_from_name']."',
`mail_test_address`='".$_REQUEST['mail_test_address']."',
`mail_test_name`='".$_REQUEST['mail_test_name']."'
WHERE `id`='1'

")or die(mysqli_error($GLOBALS['con']));
$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;
$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;
}
?>