<?php
error_reporting(1);
ini_set('memory_limit', '256M');
ini_set('error_reporting', E_ALL);
ini_set('max_execution_time', 1000000000000000);

date_default_timezone_set('America/Sao_Paulo');

header('Content-Security-Policy: upgrade-insecure-requests');
$date_expire_cookie = new DateTime();
$date_expire_cookie->setTimezone(new \DateTimeZone('GMT-3'));
$date_cookie = $date_expire_cookie->format(DateTimeInterface::COOKIE);
@session_start();

function getUrlSite()
{
	$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
	$actual_link = explode("//", $actual_link);
	$protocol = $actual_link[0];
	$actual_link = explode("/", $actual_link[1]);
	$url_site = $protocol . '//' . $actual_link[0] . dirname($_SERVER['PHP_SELF']);
	$url_site = explode('/modulos', $url_site);
	$url_site =  $url_site[0];
	$url_site = str_replace('\\', '', $url_site);
	return $url_site . '/';
}

function getRelativePath($file)
{
	$rel_path_string = str_replace("\\/", "/", substr($file, strlen($_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF']) . '/')));
	$rel_path_string = str_replace("\\", "/", $rel_path_string);
	$rel_path_contructor = explode('/', $rel_path_string);
	$rel_path = '';
	$i = 0;
	foreach ($rel_path_contructor as $key) {
		if (++$i != count($rel_path_contructor)) {
			$rel_path .= '../';
		}
	}
	return $rel_path;
}
function getAbsolutePath($file)
{
	$folder = getUrlSite() . str_replace("\\", "/", substr($file, strlen($_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF'])))) . '/';
	$folder = str_replace('//modulos', '/modulos', $folder);
	$folder = str_replace('//', '/', $folder);
	$folder = str_replace('https:/', 'https://', $folder);
	$folder = str_replace('http:/', 'http://', $folder);
	$last_char = substr($folder, -1);
	if ($last_char == '/') {
		$folder = substr($folder, 0, -1);
	}
	$folder = $folder . '/';
	return $folder;
}
define('ROOT', __DIR__);
define('DS', DIRECTORY_SEPARATOR);
define('__ABSOULTE_PATH__', getAbsolutePath(dirname(__FILE__)));
define('__IMAGES__', __ABSOULTE_PATH__ . 'images/');
define('__CSS__', __ABSOULTE_PATH__ . 'assets/css/');
define('__JAVASCRIPT__', __ABSOULTE_PATH__ . 'assets/js/');
define('__PHP__', __ABSOULTE_PATH__ . 'assets/php/');
define('__FILES__', __ABSOULTE_PATH__ . 'files/');
define('__AJAX_OPTIONS__', __ABSOULTE_PATH__ . 'ajax_options/');
define('__URLSITE__', getUrlSite());
define('__CDN__', __URLSITE__);
define('base_url', __URLSITE__);
$url_site = __URLSITE__;



function loadClasses($className)
{
	$className = strtolower($className);
	$paths = scandir(__DIR__ . '/classes/');
	$include = '';
	foreach ($paths as $class_dir) {
		if (file_exists(ROOT . DS . 'classes/' . $class_dir . DS . $className . '.php')) {
			$include = ROOT . DS . 'classes/' . $class_dir  . DS . $className . '.php';
		}
	}
	if ($include != '') {
		require_once($include);
	} else {
		throw new Exception("Classe $className não encontrada. Verifique o nome do arquivo e tente novamente");
		exit();
	}
}
spl_autoload_register("loadClasses");

function debug_code($data,  $exit = true)
{
	echo "<pre>";
	print_r($data);
	echo "</pre>";
	if ($exit) {
		exit(0);
	}
}

// URL Amigável
if (isset($_GET["url"])) {
	$url = explode("/", $_GET["url"]);
	$url_nav = '';
	$last_key = end($url);
	if (is_numeric($last_key)) {
		$url_id = $last_key;
	}
	foreach ($url as $key_url => $data_url) {
		if (strlen($last_key) == 32 || is_numeric($last_key)) {
			if ($data_url != $last_key) {
				$url_nav = $url_nav . "/" . $data_url;
			}
		} else {
			$url_nav = $url_nav . "/" . $data_url;
		}
	}
} else {
	$url = array();
	$url_nav = '';
}

//END PARAMS
require_once('vendor/autoload.php');


if (
	strpos($_SERVER['REQUEST_URI'], "login.php") === false &&
	strpos($_SERVER['REQUEST_URI'], "inscricao2") === false &&
	strpos($_SERVER['REQUEST_URI'], "inscricao") === false &&
	strpos($_SERVER['REQUEST_URI'], "login_participante") === false
) {
	if ((!isset($_SESSION["usuario_id"]) ||
		!isset($_SESSION["usuario_login"])
	)) {
		echo "<script language='javascript'>location.href='" . __URLSITE__ . "login.php';</script>";
	}
}
