<?php

require __DIR__ . '/app/init.php';

$routeExplode = explode('?', $_SERVER['REQUEST_URI']);
$route = array_values(array_filter(explode('/', $routeExplode[0])));
if (SUBFOLDER_NAME != '/'){
	array_shift($route);
}

if (!route(0)){
	$route[0] = 'index';
}

if (!file_exists(controller(route(0)))){
	$route[0] = '404';
}

function sayac(){
	global $db;
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} else {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	$sql = $db->prepare("SELECT * FROM counter where counter_ip=:counter_ip and counter_tarih=:counter_tarih");
	$sql->execute(array('counter_ip' => $ip,'counter_tarih' => date('Y-m-d')));
	$row=$sql->fetch(PDO::FETCH_ASSOC);
	$total=$sql->rowCount();
	if ($total==0) {
		$query = $db->prepare("INSERT INTO counter SET counter_ip=:counter_ip,counter_tarih=:counter_tarih");
		$insert = $query->execute(array(
			'counter_ip' => $ip,
			'counter_tarih' => date('Y-m-d')
		));
	}
}

sayac();

require controller(route(0));