<?php
session_start ();
/**
 * ******************************************
 * INCLUDE
 * *******************************************
 */
include ('classes/Bootstrap.UI.class.php');
include ('classes/Bootstrap.Button.class.php');
include ('classes/Bootstrap.Select.class.php');
include ('classes/Bootstrap.Input.class.php');
include ('classes/Bootstrap.DatePicker.class.php');

include ('classes/Connection.class.php');
include ('classes/Data.class.php');
include ('classes/Datatable.class.php');
include ('classes/Log.class.php');
include ('classes/Message.class.php');
include ('classes/Owner.class.php');
include ('classes/Pet.class.php');
include ('classes/Security.class.php');
include ('classes/Species.class.php');
include ('classes/Template.class.php');
include ('classes/Translation.class.php');
include ('classes/User.class.php');
include ('classes/Visit.class.php');
include ('classes/Watchdog.class.php');
require ('classes/fpdf/fpdf.php');
require ('classes/PDF.class.php');

include ('libraries/util.lib.php');
include ('includes/libraries/template.lib.php');
include ('includes/libraries/general.lib.php');
include ('includes/libraries/owner.lib.php');
include ('includes/libraries/pet.lib.php');
include ('includes/libraries/species.lib.php');
include ('includes/libraries/visit.lib.php');

include ('config.inc.php');


// THEME SECTION: set default path for themes
$template 				= _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/layout.tpl";
$template_main_menu 	= _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/main-menu.tpl";

$layout	 			= new Template ( $template );
$layout_main_menu 	= new Template ( $template_main_menu );

// TEMPLATE: setting common templete sections
if( empty($_SESSION) == false ){
	// get a themed and translated menu then set applicaion name and menu block
	$layout_main_menu = build_themed_menu( 'main', $_SESSION['rid'], $layout_main_menu );
	$layout->set ( "user-menu-name", _("My Account") );
	$layout->set ( "my-profile-url", "user.php?op=f&uid=" . $_SESSION['uid']);
	$layout->set ( "my-profile-name", _("My Profile") );
	$layout->set ( "security-name", _("Security") );
	$layout->set ( "security-url", "user.php?op=pwd&uid=" . $_SESSION['uid']);
	$layout->set ( "logout-name", _("Logout") );
}
$layout->set ( "application-name", _APPLICATION_NAME);
$layout->set ( "application-url", _APPLICATION_URL);

$layout->set ( "main-menu-block", $layout_main_menu->output() );


// MESSAGE SECTION: handle message box writing out the ouut
$message = new Message();
$message_array= $message->get();
$html_messages = '';

if( empty( $message_array ) == false ){
	$bs = new Bootstrap();
	foreach( $message_array as $message_item ){
		$html_messages .= $bs->message( $message_item['message'], $message_item['type'], 3000 );
	}
	$message->reset();
	unset($bs);
}

$layout->set ( "message-box", $html_messages);
	

// @todo
// SECURITY SECTION: check ecurity and role access
if(User::isUserLogged()===FALSE){
	// header('HTTP/1.1 301 Moved Permanently');
	// header('Location: user_login.php');
}

// ERROR SECTION: system error managent
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
error_reporting ( E_ALL );


// LOCALE SECTION: locale managent 

// @todo: change locale for logged user or use default locale for guests
$locale = array_key_exists('user_array', $_SESSION) ? $_SESSION['user_array'] ['locale'] : _DEFAULT_LOCALE;

// set locale
$domain = "messages"; // name of .po/.mo file
setlocale( LC_ALL, $locale.".UTF-8" );
setlocale( LC_TIME, $locale.".UTF-8" );
putenv( "LANG=$locale" );
bindtextdomain( $domain, _APPLICATION_PATH . "/locale" );
textdomain( $domain );
$_LOCALE = localeconv();

// @todo:
// FUNCTION SECTION: system functions used somewhere and to move soon somewhere else... :)
/**
 * Return the URI of the referring page.
 */
function referer_uri() {
	if (isset ( $_SERVER ['HTTP_REFERER'] )) {
		return $_SERVER ['HTTP_REFERER'];
	}
}

/**
 * Encode special characters in a plain-text string for display as HTML.
 */
function check_plain($text) {
	return htmlspecialchars ( $text, ENT_QUOTES );
}

/**
 * Since request_uri() is only available on Apache, we generate an
 * equivalent using other environment variables.
 */
function request_uri() {
	if (isset ( $_SERVER ['REQUEST_URI'] )) {
		$uri = $_SERVER ['REQUEST_URI'];
	} else {
		if (isset ( $_SERVER ['argv'] )) {
			$uri = $_SERVER ['PHP_SELF'] . '?' . $_SERVER ['argv'] [0];
		} else {
			$uri = $_SERVER ['PHP_SELF'] . '?' . $_SERVER ['QUERY_STRING'];
		}
	}
	return $uri;
}

