<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$host = 'localhost';
$db = 'ctf';
$user = 'ctf';
$pass = '38e6cbb3ecdc6b7c472091e78e0a40fa';
$flaga = 'KYN2016_LaoreetAliquet';

print_r($_SERVER);
print_r(file_get_contents('php://input'));

$headers = apache_request_headers();
@$site = $headers['KYN_2016'];

if ($site){
	$mysqli = new mysqli($host, $user, $pass, $db);
	$query = "SELECT * FROM news WHERE id='{$site}' LIMIT 0, 1";
	$res = $mysqli->query($query);
	$result = [];
	foreach($res->fetch_assoc() as $key=>$value){
		$result[$key] = $value;
		
		if(isset($result['id'], $result['title'], $result['text'])){
			echo json_encode($result);
			exit;
		}
	}
}
else
{
		?>
<html>
  <head>
	<meta charset="utf-8" />
	<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script></script>
	<title>Strona główna</title>
	<script>
		function loadPage(id){
			console.log(id)
			$.ajax({
				url: '?',
				headers: {'KYN_2016': id},
				success: function(result){
					result = JSON.parse(result);
					document.title = result.title
					$("#content").html(result.text);
					}
				});
		}
	</script>
  </head>
  <body>
    <h1>Taka sobie typowa strona</h1>
    <button onclick='loadPage(1)'>
		link1
    </button>
    <button onclick='loadPage(2)'>
		link2
    </button>
    <button onclick='loadPage(3)'>
		link3
    </button>
    <div id="content">
	  
    </div>
  </body>
</html>
		<?php
}
/**
create user ctf@localhost identified by password '38e6cbb3ecdc6b7c472091e78e0a40fa';
set password for ctf@localhost = password('38e6cbb3ecdc6b7c472091e78e0a40fa');
create database ctf;
grant all privileges on ctf.* to ctf@localhost;
create table news(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, title varchar(255), text TEXT);
insert into news(title, text) values('Kotki', 'Tu bedzie o kotkach');
insert into news(title, text) values('Pieski', 'Tu bedzie o pieskach');
insert into news(title, text) values('Krowki', 'Tu bedzie o krowkach');
insert into news(title, text) values('KYN2016_UzywamFantazyji', 'Bardzo sprytnie, ale musisz szukac innej tabeli. Pamietaj ze nie wolno Ci uzywac narzedzi automatycznych...');
create table hidd3n(flag varchar(255));
insert into hidd3n Values ('KYN2016_LaoreetAliquet');
* /


