<?php
if (session_id() == "") session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
?>
<?php include_once "ewcfg12.php" ?>
<?php include_once ((EW_USE_ADODB) ? "adodb5/adodb.inc.php" : "ewmysql12.php") ?>
<?php include_once "phpfn12.php" ?>
<?php include_once "ficha_usuarioinfo.php" ?>
<?php include_once "userfn12.php" ?>
<?php

//
// Page class
//

$default = NULL; // Initialize page object first

class cdefault {

	// Page ID
	var $PageID = 'default';

	// Project ID
	var $ProjectID = "{43FC4B6C-8131-46B1-916C-14DB52B1691D}";

	// Page object name
	var $PageObjName = 'default';

	// Page name
	function PageName() {
		return ew_CurrentPage();
	}

	// Page URL
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		return $PageUrl;
	}

	// Message
	function getMessage() {
		return @$_SESSION[EW_SESSION_MESSAGE];
	}

	function setMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_MESSAGE], $v);
	}

	function getFailureMessage() {
		return @$_SESSION[EW_SESSION_FAILURE_MESSAGE];
	}

	function setFailureMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_FAILURE_MESSAGE], $v);
	}

	function getSuccessMessage() {
		return @$_SESSION[EW_SESSION_SUCCESS_MESSAGE];
	}

	function setSuccessMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_SUCCESS_MESSAGE], $v);
	}

	function getWarningMessage() {
		return @$_SESSION[EW_SESSION_WARNING_MESSAGE];
	}

	function setWarningMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_WARNING_MESSAGE], $v);
	}

	// Methods to clear message
	function ClearMessage() {
		$_SESSION[EW_SESSION_MESSAGE] = "";
	}

	function ClearFailureMessage() {
		$_SESSION[EW_SESSION_FAILURE_MESSAGE] = "";
	}

	function ClearSuccessMessage() {
		$_SESSION[EW_SESSION_SUCCESS_MESSAGE] = "";
	}

	function ClearWarningMessage() {
		$_SESSION[EW_SESSION_WARNING_MESSAGE] = "";
	}

	function ClearMessages() {
		$_SESSION[EW_SESSION_MESSAGE] = "";
		$_SESSION[EW_SESSION_FAILURE_MESSAGE] = "";
		$_SESSION[EW_SESSION_SUCCESS_MESSAGE] = "";
		$_SESSION[EW_SESSION_WARNING_MESSAGE] = "";
	}

	// Show message
	function ShowMessage() {
		$hidden = FALSE;
		$html = "";

		// Message
		$sMessage = $this->getMessage();
		$this->Message_Showing($sMessage, "");
		if ($sMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sMessage;
			$html .= "<div class=\"alert alert-info ewInfo\">" . $sMessage . "</div>";
			$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
		}

		// Warning message
		$sWarningMessage = $this->getWarningMessage();
		$this->Message_Showing($sWarningMessage, "warning");
		if ($sWarningMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sWarningMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sWarningMessage;
			$html .= "<div class=\"alert alert-warning ewWarning\">" . $sWarningMessage . "</div>";
			$_SESSION[EW_SESSION_WARNING_MESSAGE] = ""; // Clear message in Session
		}

		// Success message
		$sSuccessMessage = $this->getSuccessMessage();
		$this->Message_Showing($sSuccessMessage, "success");
		if ($sSuccessMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sSuccessMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sSuccessMessage;
			$html .= "<div class=\"alert alert-success ewSuccess\">" . $sSuccessMessage . "</div>";
			$_SESSION[EW_SESSION_SUCCESS_MESSAGE] = ""; // Clear message in Session
		}

		// Failure message
		$sErrorMessage = $this->getFailureMessage();
		$this->Message_Showing($sErrorMessage, "failure");
		if ($sErrorMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sErrorMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sErrorMessage;
			$html .= "<div class=\"alert alert-danger ewError\">" . $sErrorMessage . "</div>";
			$_SESSION[EW_SESSION_FAILURE_MESSAGE] = ""; // Clear message in Session
		}
		echo "<div class=\"ewMessageDialog\"" . (($hidden) ? " style=\"display: none;\"" : "") . ">" . $html . "</div>";
	}
	var $Token = "";
	var $TokenTimeout = 0;
	var $CheckToken = EW_CHECK_TOKEN;
	var $CheckTokenFn = "ew_CheckToken";
	var $CreateTokenFn = "ew_CreateToken";

	// Valid Post
	function ValidPost() {
		if (!$this->CheckToken || !ew_IsHttpPost())
			return TRUE;
		if (!isset($_POST[EW_TOKEN_NAME]))
			return FALSE;
		$fn = $this->CheckTokenFn;
		if (is_callable($fn))
			return $fn($_POST[EW_TOKEN_NAME], $this->TokenTimeout);
		return FALSE;
	}

	// Create Token
	function CreateToken() {
		global $gsToken;
		if ($this->CheckToken) {
			$fn = $this->CreateTokenFn;
			if ($this->Token == "" && is_callable($fn)) // Create token
				$this->Token = $fn();
			$gsToken = $this->Token; // Save to global variable
		}
	}

	//
	// Page class constructor
	//
	function __construct() {
		global $conn, $Language;
		global $UserTable, $UserTableConn;
		$GLOBALS["Page"] = &$this;
		$this->TokenTimeout = ew_SessionTimeoutTime();

		// Language object
		if (!isset($Language)) $Language = new cLanguage();

		// Page ID
		if (!defined("EW_PAGE_ID"))
			define("EW_PAGE_ID", 'default', TRUE);

		// Start timer
		if (!isset($GLOBALS["gTimer"])) $GLOBALS["gTimer"] = new cTimer();

		// Open connection
		if (!isset($conn)) $conn = ew_Connect();

		// User table object (ficha_usuario)
		if (!isset($UserTable)) {
			$UserTable = new cficha_usuario();
			$UserTableConn = Conn($UserTable->DBID);
		}
	}

	// 
	//  Page_Init
	//
	function Page_Init() {
		global $gsExport, $gsCustomExport, $gsExportFile, $UserProfile, $Language, $Security, $objForm;

		// Security
		$Security = new cAdvancedSecurity();

		// Global Page Loading event (in userfn*.php)
		Page_Loading();

		// Page Load event
		$this->Page_Load();

		// Check token
		if (!$this->ValidPost()) {
			echo $Language->Phrase("InvalidPostRequest");
			$this->Page_Terminate();
			exit();
		}

		// Create Token
		$this->CreateToken();
	}

	//
	// Page_Terminate
	//
	function Page_Terminate($url = "") {
		global $gsExportFile, $gTmpImages;

		// Page Unload event
		$this->Page_Unload();

		// Global Page Unloaded event (in userfn*.php)
		Page_Unloaded();

		// Export
		$this->Page_Redirecting($url);

		 // Close connection
		ew_CloseConn();

		// Go to URL if specified
		if ($url <> "") {
			if (!EW_DEBUG_ENABLED && ob_get_length())
				ob_end_clean();
			header("Location: " . $url);
		}
		exit();
	}

	//
	// Page main
	//
	function Page_Main() {
		global $Security, $Language;
		if (!$Security->IsLoggedIn()) $Security->AutoLogin();
		$Security->LoadUserLevel(); // Load User Level
		if ($Security->AllowList(CurrentProjectID() . 'registro'))
		$this->Page_Terminate("registrolist.php"); // Exit and go to default page
		if ($Security->AllowList(CurrentProjectID() . 'adherencia'))
			$this->Page_Terminate("adherencialist.php");
		if ($Security->AllowList(CurrentProjectID() . 'administradora_salud'))
			$this->Page_Terminate("administradora_saludlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'area_ocurrencia'))
			$this->Page_Terminate("area_ocurrencialist.php");
		if ($Security->AllowList(CurrentProjectID() . 'barrio_vereda'))
			$this->Page_Terminate("barrio_veredalist.php");
		if ($Security->AllowList(CurrentProjectID() . 'clasificacion_inicial_caso'))
			$this->Page_Terminate("clasificacion_inicial_casolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'condicion_final'))
			$this->Page_Terminate("condicion_finallist.php");
		if ($Security->AllowList(CurrentProjectID() . 'departamento'))
			$this->Page_Terminate("departamentolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'enfermedades_asociadas'))
			$this->Page_Terminate("enfermedades_asociadaslist.php");
		if ($Security->AllowList(CurrentProjectID() . 'entidad_upgd'))
			$this->Page_Terminate("entidad_upgdlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'esquema_bebe'))
			$this->Page_Terminate("esquema_bebelist.php");
		if ($Security->AllowList(CurrentProjectID() . 'esquema_gestante'))
			$this->Page_Terminate("esquema_gestantelist.php");
		if ($Security->AllowList(CurrentProjectID() . 'estadio'))
			$this->Page_Terminate("estadiolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'estado_civil'))
			$this->Page_Terminate("estado_civillist.php");
		if ($Security->AllowList(CurrentProjectID() . 'estado_clinico_paciente'))
			$this->Page_Terminate("estado_clinico_pacientelist.php");
		if ($Security->AllowList(CurrentProjectID() . 'evento'))
			$this->Page_Terminate("eventolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'ficha_usuario'))
			$this->Page_Terminate("ficha_usuariolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'grupo_poblacional'))
			$this->Page_Terminate("grupo_poblacionallist.php");
		if ($Security->AllowList(CurrentProjectID() . 'ips'))
			$this->Page_Terminate("ipslist.php");
		if ($Security->AllowList(CurrentProjectID() . 'localidad'))
			$this->Page_Terminate("localidadlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'mecanismo_probable_transmision'))
			$this->Page_Terminate("mecanismo_probable_transmisionlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'medicamentos_antiretrovirales'))
			$this->Page_Terminate("medicamentos_antiretroviraleslist.php");
		if ($Security->AllowList(CurrentProjectID() . 'municipio'))
			$this->Page_Terminate("municipiolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'nivel_educativo'))
			$this->Page_Terminate("nivel_educativolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'ocupacion'))
			$this->Page_Terminate("ocupacionlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'pais'))
			$this->Page_Terminate("paislist.php");
		if ($Security->AllowList(CurrentProjectID() . 'pertenencia_etnica'))
			$this->Page_Terminate("pertenencia_etnicalist.php");
		if ($Security->AllowList(CurrentProjectID() . 'seguimiento_bebe'))
			$this->Page_Terminate("seguimiento_bebelist.php");
		if ($Security->AllowList(CurrentProjectID() . 'seguimiento_clasificacion_inicial'))
			$this->Page_Terminate("seguimiento_clasificacion_iniciallist.php");
		if ($Security->AllowList(CurrentProjectID() . 'seguimiento_individual3'))
			$this->Page_Terminate("seguimiento_individual3list.php");
		if ($Security->AllowList(CurrentProjectID() . 'seguimiento_materno'))
			$this->Page_Terminate("seguimiento_maternolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'sexo'))
			$this->Page_Terminate("sexolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'si_no'))
			$this->Page_Terminate("si_nolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'tipo_documento'))
			$this->Page_Terminate("tipo_documentolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'tipo_mecanismo_probable_transmision'))
			$this->Page_Terminate("tipo_mecanismo_probable_transmisionlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'tipo_prueba'))
			$this->Page_Terminate("tipo_pruebalist.php");
		if ($Security->AllowList(CurrentProjectID() . 'tipo_regimen_salud'))
			$this->Page_Terminate("tipo_regimen_saludlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'tipo_tratamiento'))
			$this->Page_Terminate("tipo_tratamientolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'tipo_tratamiento_bebe'))
			$this->Page_Terminate("tipo_tratamiento_bebelist.php");
		if ($Security->AllowList(CurrentProjectID() . 'unidad_medida_edad'))
			$this->Page_Terminate("unidad_medida_edadlist.php");
		if ($Security->AllowList(CurrentProjectID() . 'userlevelpermissions'))
			$this->Page_Terminate("userlevelpermissionslist.php");
		if ($Security->AllowList(CurrentProjectID() . 'userlevels'))
			$this->Page_Terminate("userlevelslist.php");
		if ($Security->AllowList(CurrentProjectID() . 'registroprueba'))
			$this->Page_Terminate("registropruebalist.php");
		if ($Security->AllowList(CurrentProjectID() . 'carga_viral'))
			$this->Page_Terminate("carga_virallist.php");
		if ($Security->AllowList(CurrentProjectID() . 'periodo'))
			$this->Page_Terminate("periodolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'periodo_epidemiologico'))
			$this->Page_Terminate("periodo_epidemiologicolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'semana_epidemiologica'))
			$this->Page_Terminate("semana_epidemiologicalist.php");
		if ($Security->AllowList(CurrentProjectID() . 'barrios_veredas'))
			$this->Page_Terminate("barrios_veredaslist.php");
		if ($Security->AllowList(CurrentProjectID() . 'identidad_genero'))
			$this->Page_Terminate("identidad_generolist.php");
		if ($Security->AllowList(CurrentProjectID() . 'poblacion_clave'))
			$this->Page_Terminate("poblacion_clavelist.php");
		if ($Security->IsLoggedIn()) {
			$this->setFailureMessage(ew_DeniedMsg() . "<br><br><a href=\"logout.php\">" . $Language->Phrase("BackToLogin") . "</a>");
		} else {
			$this->Page_Terminate("login.php"); // Exit and go to login page
		}
	}

	// Page Load event
	function Page_Load() {

		//echo "Page Load";
	}

	// Page Unload event
	function Page_Unload() {

		//echo "Page Unload";
	}

	// Page Redirecting event
	function Page_Redirecting(&$url) {

		// Example:
		//$url = "your URL";

	}

	// Message Showing event
	// $type = ''|'success'|'failure'
	function Message_Showing(&$msg, $type) {

		// Example:
		//if ($type == 'success') $msg = "your success message";

	}
}
?>
<?php ew_Header(FALSE) ?>
<?php

// Create page object
if (!isset($default)) $default = new cdefault();

// Page init
$default->Page_Init();

// Page main
$default->Page_Main();
?>
<?php include_once "header.php" ?>
<?php
$default->ShowMessage();
?>
<?php include_once "footer.php" ?>
<?php
$default->Page_Terminate();
?>
