<?php
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

include('vendor/autoload.php');
include_once('_logic/functions.php');

if (isset($_GET["action"]) && $_GET["action"] == "phpinfo")
{
	phpinfo();
	die();
}

if (isset($_GET["action"]) && $_GET["action"] == "mailtest")
{
	$mymail = $_POST["mymail"];
	include_once('_logic/mail.php');
	$arraySignatures = array();

	$htmlPdf = file_get_contents("mail_en.htm");
    $pdfAttach = -1;

    //PDF-Konvertierung
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetTitle("Test");
    $pdf->SetSubject("Subject");

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set font
    $pdf->SetFont('dejavusans', '', 10);

    $pdf->AddPage();
    $pdf->writeHTML($htmlPdf, true, false, true, false, '');

    $pdfFileName = __DIR__ . "/filetemp/" . time() . ".pdf";
    //file_put_contents("filetemp/html_debug.html", $htmlPdf);
    $pdf->Output($pdfFileName, 'F');

    sendMail($mymail, $mymail, "Testmail Formmail-Script", "Dies ist eine einfache Testnachricht.", true, false);

    echo "Falls keine Fehlermeldung erscheint wurde die Nachricht gesendet. Bitte den Posteingang überpr&uuml;fen";
    die();
}

if (isset($_GET["action"]) && $_GET["action"] == "pdftest")
{
    $htmlPdf = file_get_contents("mail_en.htm");

    //PDF-Konvertierung
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetTitle("Test");
    $pdf->SetSubject("Subject");

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set font
    $pdf->SetFont('dejavusans', '', 10);

    $pdf->AddPage();
    $pdf->writeHTML($htmlPdf, true, false, true, false, '');

    $pdfFileNameUrl = "filetemp/" . time() . ".pdf";
    $pdfFileName = __DIR__ . "/" .  $pdfFileNameUrl;
    $pdf->Output($pdfFileName, 'F');

    if (file_exists($pdfFileName))
    {
        echo "PDF-Datei wurde erzeugt: " . $pdfFileNameUrl . "</a>";
    } else {
        echo "Fehler: konnte keine PDF-Datei erzeugen.";
    }

    die();
}

include_once('_logic/ipcheck.php');
include("config.php");
include("_logic/dbaccess.php");
?>

<!DOCTYPE html>

<html>

<head>
    <meta charset="UTF-8" />
    <title>Formmailer Diagnose</title>   
    <meta name="description" content="Beschreibung" />   
    <link href="https://www.w3schools.com/w3css/4/w3.css" type="text/css" rel="stylesheet" />

</head>

<body>

<div class="w3-container"> 
 <header class="w3-container w3-indigo">
  <h1>Formmail-Script Diagnose</h1>
</header> 

<header class="w3-container w3-khaki">
  <h2>Script-Einstellungen</h2>
</header> 

<table class="w3-table w3-striped">
	<tr>
		<td>iplock</td>
		<td><?php echo $iplock; ?></td>
	</tr>
	
	<tr>
		<td>iplocktime</td>
		<td><?php echo $iplocktime; ?></td>
	</tr>	

	<tr>
		<td>dbname</td>
		<td><?php echo $dbname; ?></td>
	</tr>	

	<tr>
		<td>dbhost</td>
		<td><?php echo $dbhost; ?></td>
	</tr>	

	<tr>
		<td>dbuser</td>
		<td><?php echo $dbuser; ?></td>
	</tr>	

	<tr>
		<td>Config Log</td>
		<td><?php echo $conf_log; ?></td>
	</tr>	
	
	<tr>
		<td>CaptchaErrorPage</td>
		<td><?php echo $CaptchaErrorPage; ?></td>
	</tr>	
	
	<tr>
		<td>IPErrorPage</td>
		<td><?php echo $IPErrorPage; ?></td>
	</tr>		

	<tr>
		<td>AntiSpam</td>
		<td><?php echo $conf_antispam; ?></td>
	</tr>	

	<tr>
		<td>AntiSpam email:</td>
		<td><?php echo $conf_antispam_copy_mail; ?></td>
	</tr>	

	<tr>
		<td>AntiSpam email:</td>
		<td><?php echo $conf_antispam_mail_address; ?></td>
	</tr>		
	

	<tr>
		<td>IPErrorPage</td>
		<td><?php echo $IPErrorPage; ?></td>
	</tr>				
	
	<tr>
		<td>max_attach_size</td>
		<td><?php echo $max_attach_size; ?></td>
	</tr>		
	
	<tr>
		<td>FileErrorPage</td>
		<td><?php echo $FileErrorPage; ?></td>
	</tr>		
	
	<tr>
		<td>BlockFilePage</td>
		<td><?php echo $BlockFilePage; ?></td>
	</tr>	
	
	<tr>
		<td>BlockFileList</td>
		<td><?php var_dump($BlockFileList); ?></td>
	</tr>	
		
	<tr>
		<td>c_mail_send_type</td>
		<td><?php echo $c_mail_send_type; ?></td>
	</tr>

    <tr>
        <td>c_standard_mail</td>
        <td><?php
            if ($c_standard_mail == "") {
                    echo '<font color="#FF0000">Warn: nicht gesetzt / not set</font>';
            } else {
                echo $c_standard_mail;
            }

        ?></td>
    </tr>
	
	<tr>
		<td>c_smtp_host</td>
		<td><?php echo $c_smtp_host; ?></td>
	</tr>	
	
	<tr>
		<td>c_smtp_username</td>
		<td><?php echo $c_smtp_username; ?></td>
	</tr>	

	<tr>
		<td>c_smtp_port</td>
		<td><?php echo $c_smtp_port; ?></td>
	</tr>		
	
	<tr>
		<td>tablename</td>
		<td><?php echo $tablename; ?></td>
	</tr>		
	
	<tr>
		<td>tablename_captcha</td>
		<td><?php echo $tablename_captcha; ?></td>
	</tr>					
	
	<tr>
		<td>scriptParam</td>
		<td><?php echo $scriptParam; ?></td>
	</tr>		
	
	<tr>
		<td>c_captcha_ttf</td>
		<td><?php echo $c_captcha_breite; ?></td>
	</tr>		
	
	<tr>
		<td>tablename_captcha</td>
		<td><?php echo $c_captcha_hoehe; ?></td>
	</tr>		
	
	<tr>
		<td>c_captcha_type</td>
		<td><?php echo $c_captcha_type; ?></td>
	</tr>				
	
	<tr>
		<td>c_fontSize</td>
		<td><?php echo $c_fontSize; ?></td>
	</tr>			
	
	<tr>
		<td>c_font</td>
		<td><?php echo $c_font; ?></td>
	</tr>			
	
	<tr>
		<td>c_captchaLength</td>
		<td><?php echo $c_captchaLength; ?></td>
	</tr>			

	<tr>
		<td>c_backgroundcolor</td>
		<td><?php var_dump($c_backgroundcolor); ?></td>
	</tr>			
	
	<tr>
		<td>c_fontcolor1</td>
		<td><?php var_dump($c_fontcolor1); ?></td>
	</tr>				
	
	<tr>
		<td>c_fontcolor2</td>
		<td><?php var_dump($c_fontcolor2); ?></td>
	</tr>				
	
	<tr>
		<td>c_fontcolor3</td>
		<td><?php var_dump($c_fontcolor3); ?></td>
	</tr>				
			
	<tr>
		<td>c_noisepointscolor</td>
		<td><?php var_dump($c_noisepointscolor); ?></td>
	</tr>				
						
	<tr>
		<td>c_noiselinecolor</td>
		<td><?php var_dump($c_noiselinecolor); ?></td>
	</tr>				
							
	<tr>
		<td>c_noiselettercolor</td>
		<td><?php var_dump($c_noiselettercolor); ?></td>
	</tr>				
	
	<tr>
		<td>c_isNoisePoints</td>
		<td><?php var_dump($c_isNoisePoints); ?></td>
	</tr>			
	
	<tr>
		<td>c_isNoiseLinesOnTop</td>
		<td><?php var_dump($c_isNoiseLinesOnTop); ?></td>
	</tr>			
	
	<tr>
		<td>c_numNoiseLinesOnTop</td>
		<td><?php echo $c_numNoiseLinesOnTop; ?></td>
	</tr>			


	<tr>
		<td>c_isNoiseLinesBelow</td>
		<td><?php var_dump($c_isNoiseLinesBelow); ?></td>
	</tr>			
	
	<tr>
		<td>c_maxLineThickness</td>
		<td><?php echo $c_maxLineThickness; ?></td>
	</tr>			
	
	<tr>
		<td>c_isNoiseLetters</td>
		<td><?php var_dump($c_isNoiseLetters); ?></td>
	</tr>			
	
	<tr>
		<td>c_isImgSmooth</td>
		<td><?php var_dump($c_isImgSmooth); ?></td>
	</tr>			
	
	<tr>
		<td>c_smoothStrength</td>
		<td><?php echo $c_smoothStrength; ?></td>
	</tr>			
	
	<tr>
		<td>c_maxRotationDegree</td>
		<td><?php echo $c_maxRotationDegree; ?></td>
	</tr>			
	
	<tr>
		<td>c_placeholder_replacement</td>
		<td><?php echo $c_placeholder_replacement; ?></td>
	</tr>																
</table>

<hr />


<header class="w3-container w3-khaki">
  <h2>BOM Überprüfung Dateien</h2>
</header> 

<?php
echo bomCheck();
?>

<hr />

<header class="w3-container w3-khaki">
    <h2>Test Verzeichnisberechtigungen</h2>
</header>

    <?php
    echo dirCheck();
    ?>

    <hr/>

    <header class="w3-container w3-khaki">
  <h2>Test Datenbankverbindung</h2>
</header> 

<?php
$dblog = "<strong>Verbindung zur Datenbank:</strong><br>";
$connect = mysqli_connect($dbhost, $dbuser, $dbpass) or $dblog .= "<p style=\"color:red;\">Keine Verbindung zur Datenbank: '$dbhost'</p>";

$connect = mysqli_connect($dbhost, $dbuser, $dbpass);
if (!$connect) {
    $dblog .= "<p style=\"color:red;\">Verbindungsfehler: (" . mysqli_connect_errno() . ") " . mysqli_connect_error() . "</p>";
}

if ($connect) 
{
	$dblog .= "Datenbankverbidung erfolgreich!<br>";
	$dbselect = mysqli_select_db($connect, $dbname) or $dblog .= "<p style=\"color:red;\">Kann Datenbank nicht öffnen: '$dbname'</p>";
	
	if ($dbselect)
	{
		
		$test_query = "SHOW TABLES";
		$result = mysqli_query($connect, $test_query);
		$tables = "";

		$tblCnt = 0;
		while($tbl = mysqli_fetch_array($result)) {
		  $tblCnt++;
		  $tables .= $tbl[0]."<br />\n";
		}
		if (!$tblCnt) {
		  $dblog .= "Keine Tabellen in der Datenbank<br />\n";
		} else {
		  $dblog .= "Gefundene Tabellen: $tblCnt<br />\n";
		  $dblog .= $tables; 
		}		
	}	
}

?>

<table class="w3-table w3-striped">
	<tr>
	<td>Datenbankverbindungtest</td>
	<td><?php echo $dblog; ?></td>
	</tr>
</table>	

<hr>

<header class="w3-container w3-khaki">
  <h2>Captcha-Test</h2>
</header> 

<table class="w3-table w3-striped">
	<tr>
		<td>Captcha-Bild-Test:</td>
		<td><a href="formmail_v4.php?action=captcha&amp;id=740098"><img src="formmail_v4.php?action=captcha&amp;id=740098" border="1" alt="captcha" name="captchaimg"></a></td>
	</tr>
</table>

<hr>

<header class="w3-container w3-khaki">
  <h2>Variablenausgabe</h2>
</header> 

<table class="w3-table w3-striped">
<?php	
  foreach ($_POST as $key => $value) {
      echo "<tr>";
      echo "<td>";
      echo $key;
      echo "</td>";
      echo "<td>";
      echo $value;
      echo "</td>";
      echo "</tr>";
  }
?>    	
</table>

<hr>

<header class="w3-container w3-khaki">
  <h2>Mailtest</h2>
</header> 

<div class="container">
	<form class="w3-container" action="diag.php?action=mailtest" method="post">
		<input class="w3-input"type="text" id="mymail" name="mymail" value="mail@mail.com">
		<input type="submit" value="Mail senden">
	</form>
</div>

<hr>

    <header class="w3-container w3-khaki">
        <h2>PDF Test</h2>
    </header>

    <p>Erzeugen Sie ein Test-PDF:</p>

    <div class="container">
        <form class="w3-container" action="diag.php?action=pdftest" method="post">
            <input type="submit" value="PDF erzeugen">
        </form>
    </div>

    <hr/>

    <header class="w3-container w3-khaki">
  <h2>Weitere Diagnosen</h2>
</header> 

<div class="container">
	<ul>
		<li><a href="diag.php?action=phpinfo">PHPInfo</a></li>
	</ul>
</div>



</body>
</html> 


<?php

function dirCheck()
{
    $log = "";

    $rights = substr(sprintf('%o', fileperms('filetemp')), -3);
    if ($rights != "0777")
    {
        $log .= "<p style=\"color:red;\">Schreibzugriff (777) fehlt für Verzeichnis \"filetemp\", aktuelle Berechtigung: " . $rights . "</p>";
    } else {
        $log .= "<p>OK - filetemp: " . $rights . "</p>";
    }

    $rights = substr(sprintf('%o', fileperms('archiv')), -3);
    if ($rights != "0777")
    {
        $log .= "<p style=\"color:#f57e00;\">Schreibzugriff (777) fehlt für Verzeichnis \"archiv\" (nur für Archivfunktion nötig), aktuelle Berechtigung: " . $rights . "</p>";
    } else {
        $log .= "<p>OK - archiv: " . $rights . "</p>";
    }

    return $log;
}

function bomCheck()
{
	$bomLog = "";
	
	// SETTINGS
	////////////////////////////////////////////////////////////////////////////////
	$check_extensions = array('php');
	// MAIN
	////////////////////////////////////////////////////////////////////////////////
	define('STR_BOM', "\xEF\xBB\xBF");
	$file = null;
	$directory = getcwd();
	$rit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST);

	try
	{
	  foreach ($rit as $file)
	  {
	    if ($file->isFile())
	    {
	      $path_parts = pathinfo($file->getRealPath());
	      if (isset($path_parts['extension']) && in_array($path_parts['extension'],$check_extensions))
	      {
	        $object = new SplFileObject($file->getRealPath());
	        if (false !== strpos($object->getCurrentLine(), STR_BOM))
	        {
	          $bomLog .= "<p style=\"color:red;\">Fehler Datei mit BOM: " . $file->getRealPath() . '</p>';
	        } else
	        {
	        	$bomLog .= "<p>OK: " . $file->getRealPath() .'</p>';
	      	}	       
	      }
	    }
	  }
	}
	catch (Exception $e)
	{
	  $bomLog .= 'Exception caught: '. $e->getMessage();
	}
	
	return $bomLog;
}
 
?>