<?php

	require("§lbl_inc.php");
	require("§prefs.php");
	require("§lib_db.php");

$server="localhost";
$login="root";
$password="";
$mydatabase="oxg";

	$LANG_PARAM=isset($_REQUEST["LANG"])?$_REQUEST["LANG"] : ""; 
	if($LANG_PARAM!="")
	{
		$_SESSION['lang']=$sLang=$LANG_PARAM;
	}
	else if(isset($_SESSION['lang'])==true)
	{
		$sLang=$_SESSION['lang'];
	}
$_SESSION['dbh'] = mysqli_connect($server, $login, $password, $mydatabase);
$_SESSION['dbh'] = mysqli_query($_SESSION['dbh'], "SELECT DATABASE()");


// Create connection
$_SESSION['dbh'] = new mysqli($server, $login, $password, $mydatabase);
{
	if($mydatabase=="")
	{
		$mydatabase="test";
	}
	$link = mysqli_connect($server, $login, $password, $mydatabase);
}

$_SESSION['dbh']->query("SET SESSION character_set_results = 'UTF8'") or mysqli_die()();

function display_lng_text($sIdText)
{
	global $sLang;
	global $def_text_fra;
	global $def_text_nld;
	global $def_text_cro;
	global $def_text_pol;
	global $def_text_rus;
	global $def_text_deu;
	global $def_text_spa;
	global $def_text_prt;
	global $def_text_eng;
	global $def_text_tur;
	
	if($sIdText=="GEDTAG_?")
	{
		$sText="";
	}
	else if($sLang=="fra")
	{
		echo "{$def_text_fra[$sIdText]}"; 
	}
	else if($sLang=="nld")
	{
		echo "{$def_text_nld[$sIdText]}"; 
	}
	else if($sLang=="cro")
	{
		echo "{$def_text_cro[$sIdText]}"; 
	}
	else if($sLang=="pol")
	{
		echo "{$def_text_pol[$sIdText]}"; 
	}
	else if($sLang=="rus")
	{
		echo "{$def_text_rus[$sIdText]}"; 
	}
	else if($sLang=="deu")
	{
		echo "{$def_text_deu[$sIdText]}"; 
	}
	else if($sLang=="spa")
	{
		echo "{$def_text_spa[$sIdText]}"; 
	}
	else if($sLang=="prt")
	{
		echo "{$def_text_prt[$sIdText]}"; 
	}
	else if($sLang=="tur")
	{
		echo "{$def_text_tur[$sIdText]}"; 
	}
	else
	{
		echo "{$def_text_eng[$sIdText]}"; 
	}
	return;
}

function decode_lng_text($sIdText, $vardata=null)
{
	global $sLang;
	global $def_text_fra;
	global $def_text_nld;
	global $def_text_cro;
	global $def_text_pol;
	global $def_text_rus;
	global $def_text_deu;
	global $def_text_spa;
	global $def_text_prt;
	global $def_text_eng;
	global $def_text_tur;

	if($sIdText=="GEDTAG_?")
	{
		$sText="";
	}
	else if($sLang=="fra")
	{
		$sText=$def_text_fra[$sIdText]; 
	}
	else if($sLang=="nld")
	{
		$sText=$def_text_nld[$sIdText]; 
	}
	else if($sLang=="cro")
	{
		$sText=$def_text_cro[$sIdText]; 
	}
	else if($sLang=="spa")
	{
		$sText=$def_text_spa[$sIdText]; 
	}
	else if($sLang=="deu")
	{
		$sText=$def_text_deu[$sIdText]; 
	}
	else if($sLang=="rus")
	{
		$sText=$def_text_rus[$sIdText]; 
	}
	else if($sLang=="pol")
	{
		$sText=$def_text_pol[$sIdText]; 
	}
	else if($sLang=="prt")
	{
		$sText=$def_text_prt[$sIdText]; 
	}
	else if($sLang=="tur")
	{
		$sText=$def_text_tur[$sIdText]; 
	}
	else
	{
		$sText="{$def_text_eng[$sIdText]}"; 
	}
	if($vardata==null)
	{
		return $sText;
	}
	else
	{
		return StrParse($sText, $vardata);
	}
}

function StrParse($str, $vardata)
{
	global $def_text_fra;

	if($vardata==null)
	{
		return $str;
	}
	# Takes a string, or piece of data, that contains PHP Variables
	
	# For example, unparsed variables like:  Test using time: $mytime
	# This example shows $mytime, and not the actual variable value.
	# The end result shows the actual variable value of $mytime.
	
	# This is useful for building a content management system,
	# and directing your variables into your content data,
	# where content is stored in a file or database, unparsed.
	# Of course this could slow down page loads, but it's a good way
	# to parse data from current variables into your loaded new data
	# making it compatible.
	
	# Then the variables are replaced with the actual variable..
	$getvarkeys=array_keys($vardata);
	$ret=$str;
	for ($x=0; $x < count($getvarkeys); $x++)
	{
		$myvar=$getvarkeys[$x];
	#	echo "Variable: " . $myvar . " [" . $vardata[$myvar] . "]<br>";
		$ret=str_replace('¤' . $myvar, $vardata[$myvar], $ret);
	}
	return $ret;
}

function affiche_pied_page($MainLink)
{
global $dbh;
/*		global $FAMILY;

	if($MainLink==true)
	{
		echo "<p>&nbsp;</p><p><center><a href='index.php?FAMILY=$FAMILY'>";
		display_lng_text("LIB_INC_MAIN");
		echo "</a></center>";
	}*/
	mysqli_close($_SESSION['dbh']);
	echo "<p>&nbsp;</p><center>";
	display_lng_text("LIB_INC_OXG");
	echo "</center></td></tr></table></body></html>";
}

function affiche_pied_page_admin($MainLink)
{
	if($MainLink==true)
	{
		echo "<p>&nbsp;</p><P><A HREF='§index_admin.php'>";
		display_lng_text("LIB_INC_LNK_ADMIN");
		echo "</A>";
	}
	echo "<P>";
	display_lng_text("LIB_INC_OXG");
}

function affiche_lien_individu($id_family, $id_individual, $bAffichePatronyme=true, $bAfficheDate=false, $bAfficheSurnom=false)
{
	global	$bSosa, $bSessionOk;

	if($id_family!=-1)
	{
		$sReqFam="and id_family=$id_family";
	}
	else
	{
		$sReqFam="";
	}
	
	$sInd="";
	
	$sReq  = "select first_name, libelle ";
	if($bSosa==true)
	{
		$sReq  .= ", sosa ";
	}
	$sReq .= ", date_naissance, date_deces, contemporary ";
	$sReq .= " from oxg_individuals, oxg_patronyms ";
	$sReq .= "where id_individual='$id_individual' ";
	$sReq .= "and oxg_individuals.id_patronym=oxg_patronyms.id_patronym ";
	$sReq .= $sReqFam;

	$result = mysqli_query($_SESSION['dbh'], $sReq ) or mysqli_die();
	if($row = mysqli_fetch_array($result));
	{
		$sInd.="<A HREF='§detail_individu.php?INDIVIDU=$id_individual&FAMILY=$id_family'>";
		if($row['first_name']=="")
		{
			$sInd.="?";
		}
		else
		{
			$sInd.=$row['first_name'];
		}
		if($bAffichePatronyme==true)
		{
			$sInd.=" $row[libelle]";
		}
		if($bAfficheSurnom==true)
		{
			$sInd.=display_nickname($id_individual, $sReqFam, "GIVN", "NICK", $row['sex']);
		}
		$sInd.="</A>";
		if($bSosa==true && $row['sosa']!='-1')
		{
			$sInd=$sInd." <strong><font color=red>$row[sosa]</font></strong>";
		}
		
		if($bAfficheDate==true && ($row['date_naissance']!="" || $row['date_deces']!=""))
		{
			$sInd.=" (";
			if($row['date_naissance']!="")
			{
				$sInd.=decode_date_eng($row['date_naissance']);
			}
			else
			{
				$sInd.="?";
			}
			if($row['date_deces']!="")
			{
				$sInd.=" - ";
				$sInd.=decode_date_eng($row["date_deces"]);
			}
			$sInd.=")";
		}
	}
	if ($row['first_name']=="" AND $row['libelle']=="")
	{
		return "?";
	}
	else
	{	
		if (($bSessionOk==false || $bSessionOk=="") and $row['contemporary']==0)
		{
			return decode_lng_text('PRIVATE_MSG');
		}
		else
		{
			return $sInd;
		}
	}
}

function affiche_lien_arbre($id_family, $id_individual)
{
	if($id_family!=-1)
	{
		$sReqFam="and id_family=$id_family";
	}
	else
	{
		$sReqFam="";
	}

	$sReq  = "select id_father, id_mother from oxg_individuals where id_individual=$id_individual $sReqFam";
	$result = mysqli_query($_SESSION['dbh'], $sReq ) or mysqli_die();
	$sLink="";
	if($row = mysqli_fetch_array($result))
	{
		if($row['id_father']!=-1 || $row['id_mother']!=-1)
		{
			$sLink.="<A HREF='§arbre_individu_v.php?INDIVIDU=$id_individual&FAMILY=$id_family'><IMG SRC='tree.png' ALT=\"";
			$sLink.=decode_lng_text("DETAIL_IND_ARBRE");
			$sLink.="\"></A> ";

			$sLink.="<A HREF='§arbre_individu.php?INDIVIDU=$id_individual&FAMILY=$id_family'><IMG SRC='tree_h.png' ALT=\"";
			$sLink.=decode_lng_text("DETAIL_IND_ARBRE");
			$sLink.="\"></A>";
		}
	}
	return $sLink;
}

function mysqli_die($error = "")
{
	global $strError, $strMySQLSaid, $strBack, $sql_query;

	echo "<HTML><b> $strError </b><p>";
	if(isset($sql_query) && !empty($sql_query))
	{
		display_lng_text("ERR_MYSQL");
		echo " : <b>$sql_query</b><p>";
	}
	if(!empty($error))
	{
		echo $strMySQLSaid.$error;
		echo "<p>";
	}
	echo $strMySQLSaid.mysqli_error($_SESSION['dbh']);
	echo "<p>";
//    echo "<p><a href=\"javascript:history.go(-1)\">$strBack</a></HTML>";
//    exit;
}

// Convert english date to current language
function decode_date_eng($sValDate)
{
	$sValDate=str_replace("before", decode_lng_text("BEFORE"), $sValDate);
	$sValDate=str_replace("after", decode_lng_text("AFTER"), $sValDate);
	$sValDate=str_replace("about", decode_lng_text("ABOUT"), $sValDate);
	$sValDate=str_replace("between", decode_lng_text("BETWEEN"), $sValDate);
	$sValDate=str_replace("and", decode_lng_text("AND"), $sValDate);
	$sValDate=str_replace("Calculated", decode_lng_text("CAL"), $sValDate);
	$sValDate=str_replace("January", decode_lng_text("MONTH1"), $sValDate);
	$sValDate=str_replace("February", decode_lng_text("MONTH2"), $sValDate);
	$sValDate=str_replace("March", decode_lng_text("MONTH3"), $sValDate);
	$sValDate=str_replace("April", decode_lng_text("MONTH4"), $sValDate);
	$sValDate=str_replace("May", decode_lng_text("MONTH5"), $sValDate);
	$sValDate=str_replace("June", decode_lng_text("MONTH6"), $sValDate);
	$sValDate=str_replace("July", decode_lng_text("MONTH7"), $sValDate);
	$sValDate=str_replace("August", decode_lng_text("MONTH8"), $sValDate);
	$sValDate=str_replace("September", decode_lng_text("MONTH9"), $sValDate);
	$sValDate=str_replace("October", decode_lng_text("MONTH10"), $sValDate);
	$sValDate=str_replace("November", decode_lng_text("MONTH11"), $sValDate);
	$sValDate=str_replace("December", decode_lng_text("MONTH12"), $sValDate);
	return $sValDate;
}

// Date formating from string
function decode_date($sValDate)
{
	$pos = strpos($sValDate, ":"); 
	$sDate = substr($sValDate, 0, $pos);

	if(trim($sDate)=="")
	{
		$result = substr($sValDate, $pos+1);
		$result=str_replace("BEF", decode_lng_text("BEFORE"), $result);
		$result=str_replace("AFT", decode_lng_text("AFTER"), $result);
		$result=str_replace("ABT", decode_lng_text("ABOUT"), $result);
		$result=str_replace("BET", decode_lng_text("BETWEEN"), $result);
		$result=str_replace("AND", decode_lng_text("AND"), $result);
		$result=str_replace("CAL", decode_lng_text("CAL"), $result);
		$result=str_replace("JAN", decode_lng_text("MONTH1"), $result);
		$result=str_replace("FEB", decode_lng_text("MONTH2"), $result);
		$result=str_replace("MAR", decode_lng_text("MONTH3"), $result);
		$result=str_replace("APR", decode_lng_text("MONTH4"), $result);
		$result=str_replace("MAY", decode_lng_text("MONTH5"), $result);
		$result=str_replace("JUN", decode_lng_text("MONTH6"), $result);
		$result=str_replace("JUL", decode_lng_text("MONTH7"), $result);
		$result=str_replace("AUG", decode_lng_text("MONTH8"), $result);
		$result=str_replace("SEP", decode_lng_text("MONTH9"), $result);
		$result=str_replace("OCT", decode_lng_text("MONTH10"), $result);
		$result=str_replace("NOV", decode_lng_text("MONTH11"), $result);
		$result=str_replace("DEC", decode_lng_text("MONTH12"), $result);
		if (trim($result)=="")
		{
			$result="?";
		}
	}
	else
	{	
		$day = "";
		$month = "";
		$year = "";
		$tab = explode("/", $sDate);
		if (count($tab)>0) { $year = $tab[0]; }
		if (count($tab)>1) { $month = $tab[1]; }
		if (count($tab)==3) { $day = $tab[2]; }
	
		if($month!="?" and $month!="")
		{
			$month=decode_lng_text("MONTH$month");
		}
		else
		{
			$month="";
		}
		if($day=="?")
		{
			$day="";
		}
		$result=decode_lng_text('DATE_FORMAT');
		$result = str_replace("Y", $year, $result);
		$result = str_replace("D", $day, $result);
		$result = str_replace("M", $month, $result);
		if ($result=="  ")
		{
			$result="?";
		}
	}
	return $result;
}

// Date formating from event
function decode_date_evnt($eventDate)
{
	$date1 = $eventDate['date1'];
	$date1 = str_replace("-0", "-", $date1);
	$date1 = str_replace("-", "/", $date1);
	$date2 = $eventDate['date2'];
	$date = $date1.":".$date2;
	
	return decode_date($date);
}

function donne_ind_evnt($id_family, $id_evnt)
{
	global $sql_query;

	if($id_family!=-1)
	{
		$sReqFam="and id_family=$id_family";
	}
	else
	{
		$sReqFam="";
	}
	$sRes="";
	$sql_query  = "select id_father, type_father from oxg_events where id_event=$id_evnt $sReqFam";
	$result_ind = mysqli_query($_SESSION['dbh'], $sql_query ) or mysqli_die();
	$row_ind = mysqli_fetch_array($result_ind);
	if($row_ind['type_father']==0)
	{
		$sRes=$row_ind['id_father'];
	}
	else
	{
		$sRes=donne_ind_evnt($id_family, $row_ind['id_father']);
	}
	return $sRes;
}

function display_country_flags($path, $file)
{
	global $sLang;


	if($sLang!="fra")
	{
		echo "<a href='$file&LANG=fra'><img src='$path./img/fl_fr_small.png' border='0'></a>"; 
		echo " &nbsp; ";
	}
	if($sLang!="eng")
	{
		echo "<a href='$file&LANG=eng'><img src='$path./img/fl_en_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="nld")
	{
		echo "<a href='$file&LANG=nld'><img src='$path./img/fl_nld_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="cro")
	{
		echo "<a href='$file&LANG=cro'><img src='$path./img/fl_cro_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="pol")
	{
		echo "<a href='$file&LANG=pol'><img src='$path./img/fl_pol_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="rus")
	{
		echo "<a href='$file&LANG=rus'><img src='$path./img/fl_rus_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="deu")
	{
		echo "<a href='$file&LANG=deu'><img src='$path./img/fl_deu_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="spa")
	{
		echo "<a href='$file&LANG=spa'><img src='$path./img/fl_sp_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="prt")
	{
		echo "<a href='$file&LANG=prt'><img src='$path./img/fl_prt_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
	if($sLang!="tur")
	{
		echo "<a href='$file&LANG=tur'><img src='$path./img/fl_tur_small.png' border='0'></a>"; 
		echo " &nbsp; "; 
	}
}

function display_country_flags2($path, $file)
{
	global $sLang;
	//$sLang=isset($_REQUEST["LANG"])?$_REQUEST["LANG"] : "fra"; 

	//echo "<form name='form1'>";
	//echo "<div>";
	//echo "Choix de la langue : ";
	echo "<select name='menu' onChange='window.location.href=this.value'>";
	echo "<option value='#'>Select</option>";

	if($sLang!="fra")
	{
	echo "<option value='$file&LANG=fra'>Français</option>";
	}
	else
	{
	$flag="$path./img/images/fl_fr_small.png";
	}
	if($sLang!="eng")
	{
	echo "<option value='$file&LANG=eng'>English</option>";
	}
	else
	{
	$flag="$path./img/images/fl_en_small.png";
	}
	if($sLang!="nld")
	{
	echo "<option value='$file&LANG=nld'>Nederlands</option>";
	}
	else
	{
	$flag="$path./img/images/fl_nld_small.png";
	}
	if($sLang!="cro")
	{
	echo "<option value='$file&LANG=cro'>Croatia</option>"; 
	}
	else
	{
	$flag="$path./img/images/fl_cro_small.png";
	}
	if($sLang!="pol")
	{
	echo "<option value='$file&LANG=pol'>Polska</option>";
	}
	else
	{
	$flag="$path./img/images/fl_pol_small.png";
	}
	if($sLang!="rus")
	{
	echo "<option value='$file&LANG=rus'>Russia</option>";
	}
	else
	{
	$flag="$path./img/images/fl_rus_small.png";
	}
	if($sLang!="deu")
	{
	echo "<option value='$file&LANG=deu'>Deutch</option>";
	}
	else
	{
	$flag="$path./img/images/fl_deu_small.png";
	}
	if($sLang!="spa")
	{
	echo "<option value='$file&LANG=spa'>Español</option>";
	}
	else
	{
	$flag="$path./img/images/fl_sp_small.png";
	}
	if($sLang!="prt")
	{
	echo "<option value='$file&LANG=prt'>Portugues</option>";
	}
		else
	{
	$flag="$path./img/images/fl_prt_small.png";
	}
	if($sLang!="tur")
	{
	echo "<option value='$file&LANG=tur'>Turc</option>";
	}
	else
	{
	$flag="$path./img/images/fl_tur_small.png";
	}

	echo "</select>";
	echo "  <IMG SRC=$flag WIDTH=22 HEIGHT=15> ";
	//echo "</div>";
	//echo "</form>";
}
// Time format is UNIX timestamp or PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 3)
{
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1))
    {
      $time1 = strtotime($time1);
    }
    if (!is_int($time2))
    {
      $time2 = strtotime($time2);
    }
 
    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2)
    {
      $ttime = $time1;
      $time1 = $time2;
      $time2 = $ttime;
    }
 
    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();
 
    // Loop thru all intervals
    foreach ($intervals as $interval)
    {
      // Create temp time from time1 and interval
      $ttime = strtotime('+1 ' . $interval, $time1);
      // Set initial values
      $add = 1;
      $looped = 0;
      // Loop until temp time is smaller than time2
      while ($time2 >= $ttime)
      {
        // Create new temp time from time1 and interval
        $add++;
        $ttime = strtotime("+" . $add . " " . $interval, $time1);
        $looped++;
      }
      $time1 = strtotime("+" . $looped . " " . $interval, $time1);
      $diffs[$interval] = $looped;
    }
    
    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value)
    {
      // Break if we have needed precission
      if ($count >= $precision)
      {
 		break;
      }
      // Add value and interval 
      // if value is bigger than 0
      if ($value > 0)
      {
		 if($interval=="year")
		 {
		   $interval = strtolower(decode_lng_text('STATS_OLD_2'));
		 }
		 if($interval=="month")
		 {
		   $interval = strtolower(decode_lng_text('STATS_MONTH'));
		 }
		 if($interval=="day")
		 {
		   $interval = strtolower(decode_lng_text('STATS_DAY'));
		 }
		 // Add s if value is not 1
		 if ($value != 1 && $interval!="mois" && $interval!="ans")
		 {
		   $interval .= "s";
		 }
		 // Add value and interval to times array
		 $times[] = $value . " " . $interval;
		 $count++;
		}
	}
 
	// Return string with times
	return implode(", ", $times);
}

function get_event($id_individual, $sReqFam, $sParentEvntCode, $sEvntCode)
{
	global $sql_query;

	$sql_query  = "select id_type_event from oxg_events_type where code='$sParentEvntCode'";
	$result_ind = mysqli_query($_SESSION['dbh'], $sql_query ) or mysqli_die();
	$row_ind = mysqli_fetch_array($result_ind);
	$sId_type_event=$row_ind['id_type_event'];
	if($sId_type_event=="")
	{
		return "";
	}
	$sql_query  = "select id_event from oxg_events where type_father=0 and type=$sId_type_event and id_father=$id_individual $sReqFam";
	$result_ind = mysqli_query($_SESSION['dbh'], $sql_query ) or mysqli_die();
	$row_ind = mysqli_fetch_array($result_ind);
	$sId_event=$row_ind['id_event'];
	if($sId_event=="")
	{
		return "";
	}
	$sql_query  = "select id_type_event from oxg_events_type where code='$sEvntCode'";
	$result_ind = mysqli_query($_SESSION['dbh'], $sql_query ) or mysqli_die();
	$row_ind = mysqli_fetch_array($result_ind);
	$sId_type_event=$row_ind['id_type_event'];
	if($sId_type_event=="")
	{
		return "";
	}
	$sql_query  = "select libelle from oxg_events where type_father=1 and type=$sId_type_event and id_father=$sId_event $sReqFam";
	$result_ind = mysqli_query($_SESSION['dbh'], $sql_query ) or mysqli_die();
	$row_ind = mysqli_fetch_array($result_ind);
	$sLibelle=$row_ind['libelle'];
	
	return $sLibelle;
}

function get_id_event_type($sEvntCode)
{
	global $sql_query;
	
	$sql_query  = "select id_type_event from oxg_events_type where code='$sEvntCode'";
	$result_ind = mysqli_query($_SESSION['dbh'], $sql_query ) or mysqli_die();
	$row_ind = mysqli_fetch_array($result_ind);
	return $row_ind['id_type_event'];
}

function display_nickname($id_individual, $sReqFam, $sParentEvntCode, $sEvntCode, $sSex)
{
	$sResult="";
	$sNickname=get_event($id_individual, $sReqFam, "GIVN", "NICK");
	if($sNickname!="")
	{
		if($sSex==1)	// Female
		{
			$sResult=" dite ".$sNickname;
		}
		else
		{
			$sResult=" dit ".$sNickname;
		}
	}
	return $sResult;
}

function drop_index_if_exists($ndxName, $tblName)
{
	global $sql_query;
	
	$sql_query="SHOW INDEXES from $tblName";
	$result=mysql_query($sql_query) or mysql_die();
	while($row = mysql_fetch_row($result))
	{
//		echo $tblName.".".$ndxName." - ".$row[2]." - ".$row[3]." - ".$row[4]." - ".$row[5]."<br>";
		if($row[2]==$ndxName)
		{
			$sql_query="ALTER TABLE $tblName DROP INDEX $ndxName";
			mysql_query($sql_query) or mysql_die();
			break;
		}
	}
}
?>