<?php
/* Decomposition et retour tableau du contenu du tableau sys lang*/
function get_array_file($value){
	$r=explode('|||',$value);
	$s['type']=$r[0];
	$s['path']=$r[1];
	$s['txt']=$r[2];
	$s['ext']=@$r[3];
	return $s;
}
/* encodage de la key de fichier de lang*/
function key_spec_lg_file($code_lang_long,$code_lang_court, $file ,$mode='test'){
	if ($mode=='res'){
	$lalangue='##langue##';
		$tmp= eregi_replace("_(".$code_lang_long.")|(".$code_lang_long.")_|/(".$code_lang_long.")\.",$lalangue,$file);
		return md5(trim(eregi_replace("_(".$code_lang_court.")|(".$code_lang_court.")_|/(".$code_lang_court.")",$lalangue,$tmp)));
	}
	else {
		if(eregi("/.*(_(".$code_lang."|".$text_lang.")|(".$code_lang."|".$text_lang.")_|/(".$code_lang."|".$text_lang.")\.)",$r['value'])) return true;
		else return false;
	}
}
/* Ajout sql lang */
function add_lang_sql($code, $code_long, $txt){
	$query =" INSERT INTO ".TBL_LANG_INC.' ( txt, lang )'." VALUES  (  '".$txt."' ,' ".$code_long."' )";
	echo sql ('INSERT',ADD_PATH_ADMIN.TBL_LANG_INC,'lg',$query,$code);
}
/* Formualire de choix de lang */
function choose_lang($code_lang){
	global $lg;
	echo "<h2>".CHOOSE_LANGUE_BOX."</h2>";
	static $dg;
	$dg++;
	/* choix de la langue */
	print '<form id="change_lang'.$dg.'" name="change_lang'.$dg.'" method="get"><p>';
	print '<select name="choix_lang" OnChange="document.change_lang'.$dg.'.submit();" style="width:200px;">';
	reset($lg);
		print '<option value="'.$code_lang.'">'.$lg[$code_lang]['txt'].'</option>';
	while($r=each($lg))
		print '<option value="'.$r['key'].'">'.$r['value']['txt'].'</option>';
	print '</select>';
	print '</p></form>';
}
/* Control presence sructure folder des langue*/
function control_rep_lang($code,$code_lg) {
	$code=trim($code); $code_lg=trim($code_lg);
	if(!file_exists('include/language/'.$code_lg)) mkdir('include/language/'.$code_lg);
	if(!file_exists(ADD_PATH_ADMIN.'include/languages/'.$code)) mkdir(ADD_PATH_ADMIN.'include/languages/'.$code);
	if(!file_exists(ADD_PATH_ADMIN.'include/languages/'.$code_lg.'.php')) {
		$hd=fopen(ADD_PATH_ADMIN.'include/languages/'.$code_lg.'.php','w');
		fwrite($hd, '<?php include(\'include/languages/'.$code.'.php\')');
		fclose($hd);
	}
}

/* Boite d'appel auto des fichier de langue manquant via module missol */
function recup_file_box($code_lang,$text_lang){
	/* Traite les ext de langue */
	$query =" SELECT * FROM ".TBL_INDEX_LANG.' WHERE  id = \''.$code_lang.'\' ';
	$result=sql ('',TBL_INDEX_LANG,'_PX_ilang',$query,'');
	$query2 =" SELECT * FROM ".TBL_INDEX_LANG.' WHERE  id = \'fr\' ';
	$result2=sql ('',TBL_INDEX_LANG,'_PX_ilang',$query2,'');
	/* Recupération des fichier de langue manaquant par rapprt lang defaul fr*/
	print '<script type="text/javascript"> var array_file = new Array();var array_file_o = new Array();</script>';
	?>
	<br />
	<div id="ppb" style="border:1px solid black;background:white;width:500px;height:10px; margin: 0 auto;"><div id='ppb_2' style="border:1px solid black;background:green;height:inherit;width:1px"></div></div>
	<div style="width:80%; margin:0 auto; height:100px;overflow:auto;"><ul>
	<?php
	$i=0;
	while ( $r=each($result2[0])){
		if ($r['key'] !='id'){
			$tabl_value=get_array_file($r['value']);
			if (!isset($result[0][$r['key']])) {
				$psod=str_replace(array('/french','_french.','_fr.','/fr'),array('/'.$text_lang.'','_'.$text_lang.'.','_'.$code_lang.'.','/'.$code_lang.''),$tabl_value['path']);
				echo "<p>".FILE_SYSTHEM_NO_FOUND." ".$psod.$r['key'].'</p>';
				/* cg function module missol */
				if (defined('BO_MOD_MISSOL')){
					print '<script type="text/javascript"> array_file.push("'.$psod.'");array_file_o.push("'.$tabl_value['path'].'");</script>';
					print '<li id="txt_li_file_repl'.$i.'"></li>';
					$i++;
				}
				else echo "<p>".RECUP_FILE_ALERT_INSCRIPTION." ".$tabl_value['txt'].'</p>';
			}
		}
	}
	/* Retour si pas mise a jour */
	if ($i==0) echo NO_FILE_MANQUANT;
	?>
	</ul></div>
	<script type="text/javascript">
		var multiple = (  500 / array_file.length );
		document.getElementById('ppb_2').style.width="10px";
		for ( o=0 ;o<= array_file.length ;o++)  {
			upload_ajax('txt_li_file_repl'+o,'<?php echo ''.CHEM_MODULES.'missol/lib/recup_file_langue_vs_missolserveur.inc'?>', 'recup_file_langue_vs_missolserveur', " '"+ array_file[o] +"','"+ array_file_o[o] +"' ", 'true' );
			var fgfd=(multiple * o );
			setTimeout ("document.getElementById('ppb_2').style.width=\"" +fgfd +"px\";", (150*o));
		}
	</script><br />
	<?php
}
/* Recup des lement present, generer par langue et type */
function tep_array_file_sys($code_lang,$text_lang,$mode='false'){
	$file_conf_mod= array();$file_conf = array();
	/* Extensions*/
	if ($mode=='ext'){
		require (TBL_SYS_UNINSTALL_INC);
/* Inserer un appel type sql en remplacement de l'inclusion du fichier */
		 while ( $row =each($_PX_uninstall))
			if (file_exists(ADD_PATH_ADMIN.$row['value'].$row['key']."/language/")) {
				$nom=NewFct('cstr_liste',ADD_PATH_ADMIN.$row['value'].$row['key']."/language/",'',true) ;
					 while ( $r =each($nom))
						if(eregi("/.*(_(".$code_lang."|".$text_lang.")|(".$code_lang."|".$text_lang.")_|/(".$code_lang."|".$text_lang.")\.)",$r['value'])) array_push($file_conf_mod, $r['value']);
			}

		return $file_conf_mod;
	} /* system */
	elseif ($mode=='false'){
		array_push($file_conf, ADD_PATH_ADMIN."include/languages/".$text_lang.".php");
		array_push($file_conf, "include/language/".$text_lang.".php");
		if (file_exists("include/language/".$text_lang."/")) {
			$nom=NewFct('cstr_liste',"include/language/".$text_lang."/",'',true) ;
			$file_conf=array_merge($file_conf,$nom);
		}

		return $file_conf;

	}
}
/* Reindexation du fichier d'index des fichier de langues */
function control_index_file_langue($file_conf,$file_conf_mod,$code_lang,$text_lang){
	$file_tmp=array_merge($file_conf,$file_conf_mod);
	$key_index=$code_lang;
	include(TBL_INDEX_LANG);
/* Inserer un appel type sql en remplacement de l'inclusion du fichier */
	while ($r=each($file_tmp)){
		if(eregi("(/modules/)",$r['value'])) {
			$type='paquet';
			eregi("(modules/([a-z0-9_-]*)/).*/([a-z0-9_-]*)\.[a-z]*",$r['value'],$reg);
			$key=$reg[2].'_'.$reg[3];
			$ext=$reg[2];
		} elseif(eregi("(ext/)",$r['value'])) {
			$type='paquet';
			eregi("(ext/([a-z0-9_-]*)/).*/([a-z0-9_-]*)\.[a-z]*",$r['value'],$reg);
			$key=$reg[2].'_'.$reg[3];
			$ext=$reg[2];
		} else {
			$type='lossim';
			$key = (!eregi('\.\./',$r['value'])) ? 'admin_': 'public_' ;
			$key .=eregi_replace(".*/|\.[a-z]*",'',$r['value']);
		}

		$query =" INSERT INTO ".TBL_INDEX_LANG.' ( '.key_spec_lg_file($code_lang,$text_lang, $r['value'] ,'res').' )'." VALUES  (  '".$type." ||| ".$r['value']." ||| ".$key." ||| ".@$ext."' ); ";
		if (!isset($_PX_ilang[$key_index][key_spec_lg_file($code_lang,$text_lang, $r['value'] ,'res')])) sql ('INSERT',TBL_INDEX_LANG,'_PX_ilang',$query,$key_index);
	}
}

?>