<?php
error_reporting(E_ALL);
$host = "";//host - Meestal localhost
$user = "";//User van db
$pass = "";//Pass van db
$db = "";//Database van db
mysql_connect($host,$user,$pass);
mysql_select_db($db);
if(isset($_GET['restore'])){
	mysql_query("truncate table html_check");
	mysql_query("INSERT INTO `html_check` VALUES (1, '<body>', '</body>');");
	mysql_query("INSERT INTO `html_check` VALUES (1, '<body>', '</body>');");
	mysql_query("INSERT INTO `html_check` VALUES (2, '<table>', '</table>');");
	mysql_query("INSERT INTO `html_check` VALUES (3, '<tr>', '</tr>');");
	mysql_query("INSERT INTO `html_check` VALUES (4, '<td>', '</td>');");
	mysql_query("INSERT INTO `html_check` VALUES (5, '<head>', '</head>');");
	mysql_query("INSERT INTO `html_check` VALUES (6, '<html>', '</html>');");
	mysql_query("INSERT INTO `html_check` VALUES (7, '<br>', '');");
	mysql_query("INSERT INTO `html_check` VALUES (8, '<a>', '</a>');");

}
?>
<html>
<body text="black" bgcolor="white">
<?php
//Als de tag list word upgedate
if(isset($_POST['submit'])){
	mysql_query("truncate table html_check");
	for($i=0;$i<$_POST['fields']+1;$i++){
		$tag_begin=$_POST['tag_begin'.$i];
		$tag_end=$_POST['tag_end'.$i];
		$sql="insert into html_check set tag_begin='".$tag_begin."', tag_end='".$tag_end."'";
		if($tag_begin!=""){
			mysql_query($sql);
		}
	}
}
//Functie voor pagina inhoud te lezen
function read($page){
	$contents="";
	$handle = @fopen ($page, "r");
	if($handle){
		while (!feof ($handle)) {
		$buffer = fgets($handle, 4096);
		$contents=$contents.$buffer;
	}
	fclose ($handle);
	return $contents;
}
else{
	return "Cannot load";
}
}
//formuliet verwerken
if(empty($_GET['url'])){
	$url='http://www.che-lives-clan.com/index.php';
}
else{
	$url=$_GET['url'];
}
echo '<center><form action="check_html.php" method="GET">';
echo 'Url - <input type="text" name="url" value="'.$url.'" size="50"><br>';
echo '<input type="submit" name="submit" value="Submit">';
echo '</form></center>';
//Pagina openen en controleren
if(isset($_GET['submit'])){
	if(empty($_GET['url'])){
		echo '<center><hr width="30%">Gelieve een url op te geven</center>';
	}
	else{
		echo '<hr width="30%">';
		$page=$_GET['url'];
		echo '<table width="30%" border="1" align="center">';
		echo '<tr><td colspan="2" align="center"><b>Resultaten van pagina: </b><a href="'.$page.'" target="_new">'.$page.'</a></td></tr>';
		$inhoud=read($page);
		if($inhoud=='Cannot load'){
			$count=0;
			echo '<tr><td colspan="2" align="center">De pagina kon niet gevonden worden</td></tr>';

		}
		else{
			$inhoud = strtolower($inhoud); 
			$pagina = $inhoud;
			$pagina = str_replace ('<', '&#60;', $pagina);
			$pagina = str_replace ('>', '&#62;', $pagina);
			$pagina = str_replace ('/', '&#47;', $pagina);
			$sql="select * from  html_check";
			$res=mysql_query($sql);
			$count=mysql_num_rows($res);
		}
			for($i=0;$i<$count;$i++){
			$row=mysql_fetch_array($res);
			$tag_begin=$row['tag_begin'];
			$tag_end=$row['tag_end'];
			$tag_check_begin = str_replace ('>', '', $tag_begin);
			$tago=substr_count($inhoud, $tag_check_begin);
			if($tag_end!=""){
				$tagg=substr_count($inhoud, $tag_end);
				if($tago!=$tagg){
					$tago='<font color="red">'.$tago.'</font>';
					$tagg='<font color="red">'.$tagg.'</font>';
				}
				else{
					$tago='<font color="green">'.$tago.'</font>';
					$tagg='<font color="green">'.$tagg.'</font>';
				}
			}
			else{
				$tago='<font color="green">'.$tago.'</font>';
			}
			$tag_begin = str_replace ('<', '&#60;', $tag_begin);
			$tag_begin = str_replace ('>', '&#62;', $tag_begin);
			$tag_begin = str_replace ('/', '&#47;', $tag_begin);
			$tag_end = str_replace ('<', '&#60;', $tag_end);
			$tag_end = str_replace ('>', '&#62;', $tag_end);
			$tag_end = str_replace ('/', '&#47;', $tag_end);
			if($tag_end!=""){
				echo '<tr><td width="50%"><b>'.$tag_begin.' tags geopent:</b></td><td width="50%"><b>'.$tago.'</b></td></tr>';
				echo '<tr><td width="50%"><b>'.$tag_end.' tags gesloten:</b></td><td width="50%"><b>'.$tagg.'</b></td></tr>';
			}
			else{
			echo '<tr><td width="50%"><b>'.$tag_begin.' tags:</b></td><td width="50%"><b>'.$tago.'</b></td></tr>';
			}
			}
		echo '</table>';
	}
}
echo '<hr width="30%">';
$php_self=$_SERVER['REQUEST_URI'];
echo '<form action="'.$php_self.'" method="POST">';
echo '<table width="30%" border="1" align="center">';
echo '<tr><td colspan="2" align="center"><b>Welke tags controleren</b></td></tr>';
$sql="select * from  html_check";
$res=mysql_query($sql);
$count=mysql_num_rows($res);
for($i=0;$i<$count;$i++){
	$row=mysql_fetch_array($res);
	echo '<tr><td align="center" width="50%"><input type="text" name="tag_begin'.$i.'" value="'.$row['tag_begin'].'"></td>';
	echo '<td align="center" width="50%"><input type="text" name="tag_end'.$i.'" value="'.$row['tag_end'].'"></td></tr>';
}
echo '<tr><td align="center" colspan="2"><b>Toevoegen:</b></td></tr>';
echo '<tr><td align="center"><input type="text" name="tag_begin'.$i.'"></td><td align="center"><input type="text" name="tag_end'.$i.'"></td></tr>';
echo '<tr><td align="center" colspan="2"><b>Update:</b></td></tr>';
echo '<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Update"><input type="reset" name="reset" value="Reset"></td></tr>';
echo '<tr><td align="center" colspan="2"><a href="check_html.php?restore=1">Herstellen naar standaard tags</a></td></tr>';
echo '</table>';
echo '<input type="hidden" name="fields" value="'.$i.'"></form>';
?>
</body>
</html>