<?php
// kate: replace-trailing-space-save true; space-indent on; indent-width 2; mixedindent off; indent-mode cstyle; word-wrap-marker-color #41C2A4

/**
* @author oscim - LossimTeam
* @arg $mixed_returned_vars = NewFct(string fonction_name [, mixed vars]);
* @note Chargement de functions "a la volee"
*/
function NewFct($fct){
  $vars = '';
  if(func_num_args()>1) {
    for($i=1;$i<(func_num_args()-1);$i++){
      $t[$i] = func_get_arg($i);
      $vars.="\$t[$i],";
    }
    $t[$i] = func_get_arg($i);
    $vars.="\$t[$i]";
  }

  if (!function_exists($fct)){ if(!@include_once ("include/inc/$fct.inc")) include_once (ADD_PATH_ADMIN."lib_partage/$fct.inc"); }

  eval("\$ret = $fct($vars);");
  unset ($vars);
  unset ($t);

  return $ret;
}


/**
* @author oscim
* @arg
* @note prefix, pour  mode d'accessibilite au fichier, en chemin relatif ou absolu
*/
function _prefix_chemin(){
  if (@include(CHEMIN_DU_SITE."include/sys_config.php")) define('ADD_PATH_ADMIN', CHEMIN_DU_SITE);
  else define('ADD_PATH_ADMIN', "../");
  return ADD_PATH_ADMIN;
}


/**
* @author oscim
* @arg le non de la constante
* @note renvoi true si constante defini et egale à true, sinon false
*/
function test_const($constante){
  $e=@constant(strtoupper($constante));
  if( (is_string($e) && strtoupper($e)=='TRUE')||( is_bool($e) && $e==TRUE)  )  return true;
  elseif( (is_string($e) && (strtoupper($e)=='FALSE'))  || ( is_bool($e) && $e==FALSE)  ) return false;
  return false;
}

/**
 Test boolean ou equivalent ('true', 'True', 'TRUE', true)
 @param
  @a string/boolean

 @return boolean
*/
function test_bool($var){
  if(empty($var)) return false;
  if( (is_string($var) && strtoupper($var)=='TRUE')||(is_numeric($var) && $var==0)||( is_bool($var) && $var==TRUE)  )  return true;
  elseif( (is_string($var) && (strtoupper($var)=='FALSE'))  || (is_numeric($var) && $var==1)||( is_bool($var) && $var==FALSE)  ) return false;
  return false;
}

/**
* @author oscim
* @note return etat module public Uniquement (rep/include/modules/) true ou false
Prend en charge le choix entre class ou module, dans le cas d'une class active, renvoi le module actif si different de false
*/
function test_addon_activ($name,$type='module'){
  $NAME=strtoupper($name);

  if ($type=='module') {
    if (defined('PX_CONFIG_MODULE_'.$NAME)AND @constant('PX_CONFIG_MODULE_'.$NAME)=='true') return $name;
    if (defined('BO_MOD_'.$NAME)AND @constant('BO_MOD_'.$NAME)=='true') return $name;
    else return false;
  } elseif ($type=='class') {
    if (defined('PX_CONFIG_CLASS_'.$NAME)AND @constant('PX_CONFIG_CLASS_'.$NAME) !='false') return @constant('PX_CONFIG_CLASS_'.$NAME);
    if (defined('BO_CLASS_'.$NAME)AND @constant('BO_CLASS_'.$NAME) !='false') return @constant('BO_CLASS_'.$NAME);
    else return false;
  }
}

/**
$module name extension
$lib nom de la librairie (suffixe)
$type (module/class)
$type_file (lib,tbl)
*/
function include_addon_activ($module,$lib,$type='module',$type_file='lib'){
  if ($type=='module'){
    $NAME=($type_file=='lib') ? strtoupper('_'.$module.'_'.$lib) : strtoupper($module);
  }elseif ($type=='class') {
    $base = (defined('PX_CONFIG_CLASS_'.strtoupper($module))) ?  'PX_CONFIG_CLASS_'.strtoupper($module) : 'BO_CLASS_'.strtoupper($module) ;
    $NAME=($type_file=='lib') ? strtoupper('_'.constant($base).'_'.$lib) : strtoupper('tbl_'.constant($base).'_'.$lib);
  }
  include_file(constant($NAME), true);
// 	page::include_file (constant($NAME),'');
}


/**
 chargement de module admin
*/
function bo_mod_charge($bo_name_mod){
  if (file_exists(_BO_REP_MOD.$bo_name_mod.'/chargeur.inc')) @include(_BO_REP_MOD.$bo_name_mod.'/chargeur.inc');
}


/**
*/
function getFileExtension($file_name){
  if (preg_match('/\.([A-Za-z0-9]{2,4})$/i',$file_name, $match)) return $match[1];
  return 'default';
}


/**
  traite le fichier de lang, est return un tableau composé des lang configuré  tableau[fr] = français
*/
function appel_lang(){
  require(ADD_PATH_ADMIN.TBL_LANG_INC);
  $multi = (count($lg)>1) ? true: false ;
  if ( ($multi==true)AND (!defined('LANG_MULTI')) ) define('LANG_MULTI',true);
  if (!defined('LANG_DEFAUT')) define('LANG_DEFAUT',$lg[LANG]."/");
  return $lg;
}


function charg_lang($post_lang,$url_lang){
  if ( (!empty($post_lang)) AND(!empty($url_lang)) AND (eregi(".*/([a-z]{2,3})(/..*)",$url_lang,$reg)) ) {
          $e=appel_lang();
          if (isset($e[$post_lang])) return $post_lang;
  }
  elseif ( (empty($post_lang)) AND(!empty($url_lang)) AND (eregi(".*/([a-z]{2,3})(/..*)",$url_lang,$reg)) ) {
          $e=appel_lang();
          if (isset($e[$reg[1]])) return $reg[1];
  }
  elseif ( (!empty($post_lang)) AND (!eregi("^([a-z]{2,3})/(..*)",$url_lang)) ) return $post_lang;
}


/**
  tep link, constructeur de liens pour bo
  Prend en charge control  acl
*/
function tep_links($id, $href,$alt,$txt, $type=false,$target='', $tabindex='',$html='',$droit_user=1){
  $s="<a ";
  if (tep_not_null($id)) $s.=" id='$id'  name='$id' ";
  $s.=" href='$href' ";
  if (tep_not_null($html)) $s.=$html;
  if (tep_not_null($tabindex)) $s.=" tabindex='$tabindex' ";
  if (tep_not_null($target)) $s.=" target='$target' ";
  if (tep_not_null($alt)) $s.=" title='$alt' ";
  $s .=">".$txt."</a>\n";
  if ((_USER_PERSO__GRP<=$droit_user)){
    if(!$type) return ($s);
    else print $s;
  }
}


function tep_bts_form($FB,$SP,$SE,$SR,$SB,$SC,$HB,$type=false){
  $s='';
  if(!empty($FB)) $s.="<a id='btform_fb' class='btform' href=\"javascript:bbstyle(-1)\">".FERMER_LES_BALISES."</a>\n";
  if(!empty($SP)) $s.="<input id='btform_sp' class='btform' type=\"submit\" value=".SUBMIT_PREVU." name=\"preview\" />\n";
  if(!empty($SE)) $s.="<input id='btform_se' class='btform' type=\"submit\" value=".SUBMIT_ENVOYER." name=\"confirm\" />\n";
  if(!empty($SR)) $s.="<input  id='btform_sr' class='btform' type=\"reset\" value=".SUBMIT_RESET." /></p>\n";
  if(!empty($SB)) $s.="<input  id='btform_sb' class='btform' type=\"reset\" value=".SUBMIT_BACK." name=\"preview_go\" />\n";
  if(!empty($SC)) $s.="<input  id='btform_sc' class='btform' type=\"submit\" value=".SUBMIT_COMIT." name=\"confirm\" />\n";
  if(!empty($HB)) $s.="<a id='btform_hb' class='btform' href=\"javascript:history.back()\">".HISTORY_BACK."</a>\n";
  if(!$type) print ($s);
  else return $s;
}

/**
*/
function tep_form ($dest_form,$session_id,$arg_form,$name_form,$meth_form="POST",$meth=true,$js_form='' ) {
// 	if (tep_not_null($arg_form)) $arg_form="&amp;".$arg_form;
	$e="<form action=\"$dest_form?".$arg_form."\" method=\"$meth_form\" id=\"$name_form\" name=\"$name_form\" $js_form>\n";
	if ($meth) {print($e); return true; }
	else return $e;
}

/**
*/
function form_rapid ($dest_form,$arg_form,$name_form,$meth_form,$meth,$js_form,$txt_bt,$array_name_value ){
	$s=tep_form ($dest_form,'',$arg_form,$name_form,$meth_form,$meth,$js_form );
	while ($f=each($array_name_value))
		$s .="<input type='hidden' name='".$f['key']."' value='".$f['value']."'/>";
	$s .=php_f_button('submit',$txt_bt,'','');
	$s .="</form>\n";
	if ($meth) {print($s); return true; }
	else return $s;
}

/**
*/
function entete_xml ($_c_message,$d,$auteur,$titre_page='',$desc_page,$tag_page) {
## Entete articles
$message = <<<EOF
<?xml version="1.0" encoding="utf8" standalone='yes'?>
<!-- Fichier XML, conception oscim.fr -->
<news date="$d" auteur="$auteur" >
<htmlcontenu name="description" content="$desc_page"></htmlcontenu>
<htmlcontenu name="tag" content="$tag_page"></htmlcontenu>
<!-- DEBUT REEL PAGE -->
$_c_message
</news>
EOF;
return $message;
}


/**
 control droit user sur la page
*/
function control_niv_user($page_admin){
  static $files_admin;
  include_once("config/sys_file_admin.php");

  if (!isset($files_admin[$page_admin])) return false;
  $r=explode(',',$files_admin[$page_admin]['control']);
  while ($e=each($r))
          if (_USER_PERSO__GRP==$e['value']) return true;
  return false;
}


/**
 Constrution du li du menu principal
*/
function _app_top_tep_ss_menu_li($titre,$img='',$link='',$niveau=5,$html='',$alt=''){
  $l= "<li><a href='%3\$s' title='%6\$s' %5\$s ><span>%4\$s</span><img src='template/%1\$s/img/%2\$s.png' /></a></li>\n";
  $s= (_USER_PERSO__GRP<=$niveau) ? sprintf($l,TEMPLATE_ADMIN,$img,$link,str_replace(' ','&nbsp;',nett_acen($titre)),$html,$alt) : '';
  if (tep_not_null($link)) return $s;
}


/**
 Convertion des tableaux de config en constante
*/
function var_config_cst($tabl_var_config,$prefix=''){
  if ( (!isset($tabl_var_config))AND (!is_array($tabl_var_config)) ) return false;
  while ($r = each($tabl_var_config))
    if (is_array($r['value'])) {
     $pr=$prefix.'_'.$r['key'].'';
     var_config_cst($r['value'],$pr);
    }
    else {
      if ( (empty($prefix))&& !defined(strtoupper($r['key'])) )  define(strtoupper($r['key']), $r['value']);
      else if(!defined(strtoupper($prefix."_".$r['key'])) )define(strtoupper($prefix."_".$r['key']), $r['value']);
    }
  return true;
}

/**
*/
function post_hidden($post,$s=false){
  $t='';
  while ($r=each($post))
    if ($r['key'] !='boutton'){
      if ($s) print ("<input type='hidden' name='".$r['key']."' value='".$r['value']."' />\n");
      else $t.= "<input type='hidden' name='".$r['key']."' value='".$r['value']."' />";
    }
  if (!$s) return $t;
 }


/**
  Send email (text/html) using MIME This is the central mail function. The SMTP Server should be configured  correct in php.ini
*/
function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {
    if (SEND_EMAILS != 'true') return false;
    require_once (ADD_PATH_ADMIN."lib_partage/class.email.php");
    require_once (ADD_PATH_ADMIN."lib_partage/class.mime.php");
    require_once (ADD_PATH_ADMIN."lib_partage/lib.email.php");
    $message = new email(array('X-Mailer: lossim Mailer'));
    $text = strip_tags($email_text);
    if (EMAIL_USE_HTML !=='false' )  $message->add_html($email_text, $text);
    else  $message->add_text($text);
    $message->build_message();
    $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject);
    if($message ==false) return false;
    else return true;
}

/**
 Not null = true
*/
function tep_not_null($value) {
  if (is_array($value)) {
    if (sizeof($value) > 0) return true;
    else return false;
  } else {
    if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0))return true;
    else return false;
  }
}


/**
 creat repertoire
*/
function tep_mkdir($dir){
  if(!tep_not_null($dir))return false;
  if (!file_exists($dir)){
    if(!mkdir($dir)) return false;
    elseif(!calibre_droit_file($dir)) return false;
    return true;
  } else {
    if(!calibre_droit_file($dir)) return false;
    return true;
  }
}


/**
 creat file
*/
function tep_file($file){
  if (!file_exists($file)){
    if(!$g=@fopen($file,'w+')) return false;
    elseif(!calibre_droit_file($file)) return false;
    fclose($g);
    return true;
  } else {
    if(!calibre_droit_file($file)) return false;
    return true;
  }
}


/**
 test calibre droit file et rep
*/
function calibre_droit_file($file){
  if (!file_exists($file))  return false;
  elseif(!is_readable($file) || OS_SERVEUR == "windows") return false;
  if(is_dir($file))@chmod($file, 0777);
  else @chmod($file, 0664);
  return true;
}

/**
 $vers_page liens complet avec argument
*/
function tep_page_confirm($vers_page,$arg,$txt,$_POST, $html=''){
  $t="<h2>%1\$s</h2>\n";
  $f="<p>%1\$s</p>\n";
  printf($t,TITRE_CONFIRM);
  printf($f,$txt);
  tep_form ($vers_page,"",$arg,"post","POST",true," ".$html );
  print post_hidden($_POST,false);
  tep_bts_form('','','SE','','','','HB',false);
  print "</form>\n";
}


/**
*/
function etat_files($file){
  if (file_exists($file)&& (is_writable($file))OR (OS_SERVEUR == "windows")) return true;
  else return false;
}
 // Etat dir
function etat_rep_rub($dir){
	if (file_exists($dir)&& is_dir($dir)) return true;
	else return false;
}
function etat_rep_rub_multi($ref){
 $lg=appel_lang(); $s=array();
 while ($r=each($lg)){
	  if (etat_rep_rub(ADD_PATH_ADMIN.REP_DATA.$r['key']."/".$ref)) array_push($s,true);
	  else array_push($s,false);
	  }
if (in_array(false,$s)) return false;
else return true;
}
/* recuperation key du tableau php */
function recup_name_tabl_php($file){
	require(ADD_PATH_ADMIN.TBL_CONFIG_GEN);
	while($r=each($_Config_gen['key_tableau']))
		if (eregi("(/".$r['key'].")",str_replace('.','_',$file))) return $r['value'];
	print (' nom de tableau inconnu dans TBL_CONFIG_GEN '."\n");
}
/// ATTENTION REQUTE SUR TABLE UNIQUE
function sql ($action,$tables,$prefix,$query,$id=''){
	@require_once(ADD_PATH_ADMIN."lib_partage/class.sql_php.php");
	if (!eregi("(\.\./)",$tables)&&( file_exists($tables) || file_exists('admin/'.$tables)) ){/* Ratrape deplacement class*/
		 $tables='admin/'.str_replace('../','',$tables);
	}elseif (eregi('\.\./',$tables))$tables=str_replace('../','',$tables);
	$sql= new sql_php($tables);
	if(!$sql) die(ERROR);
	if ($prefix =='')/* Insertion gestion prefix auto pour element de congig_general*/
		 $prefix=recup_name_tabl_php($tables);

	$sql->prefix($prefix);
	if(tep_not_null($id))$sql->id($id);
	ereg(".*(UPDATE|SELECT|DELETE|INSERT).*",$query,$reg);
	$action_select=(tep_not_null($reg[1]))? $reg[1] : $action ;
	switch ($action_select){
		case 'UPDATE':
			$g=$sql->update($query);
			if(!$sql) die(ERROR);
		break;
		case 'SELECT':
			$g=$sql->select($query);
			if(!$sql) die(ERROR);
		break;
		case 'DELETE':
			$g=$sql->deletes($query);
			if(!$sql) die(ERROR);
		break;
		case 'INSERT':
			$g=$sql->insert($query);
			if(!$sql) die(ERROR);
		break;
	}
	/* Var debug retour des class */
	if (DEBUG_SQL=='true')print_r(@$sql->retour_debugg);
	return $g;
}

/**
 Correspondance droit utilisateur humain <> config
*/
function corresp_droits($id=null){
$corresp_droits=array ( "1" => USERS_ADMIN, "2" => USERS_REDAC, "3" => USERS_USER);
if($id !=null) return $corresp_droits[$id];
else {
  if (_USER_PERSO__GRP==1)return array("1" => USERS_ADMIN, "2" => USERS_REDAC, "3" => USERS_USER);
  elseif(_USER_PERSO__GRP==2)return array( "2" => USERS_REDAC, "3" => USERS_USER);
  else return array( "3" => USERS_USER);
  }
}

/**
*/
function tep_setcookie($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = 0) {
  setcookie($name, $value, $expire, $path, (tep_not_null($domain) ? $domain : ''), $secure);
}

/**
*/
function tep_get_ip_address() {
  if (isset($_SERVER)) {
   if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
   elseif (isset($_SERVER['HTTP_CLIENT_IP']))$ip = $_SERVER['HTTP_CLIENT_IP'];
   else $ip = $_SERVER['REMOTE_ADDR'];
  } else {
    if (getenv('HTTP_X_FORWARDED_FOR'))$ip = getenv('HTTP_X_FORWARDED_FOR');
    elseif (getenv('HTTP_CLIENT_IP')) $ip = getenv('HTTP_CLIENT_IP');
    else $ip = getenv('REMOTE_ADDR');
  }
  return $ip;
}

/**
  Remplacement des caratéres accentués, ponctuatiion et tous caratéres non imprimables
*/
function clean_for_name_files ($content){
  $content = strtolower($content);
  $content = stripslashes($content);
  $array_1 = array("œ","µ","§","°","¿","¬","æ","©","€","ø","þ","¶","±","«","»","ß","×","@","$","£","%"," ","!","?",";",":",",",".","&","\"","'"," - ","^","\'","/","__","(",")","ç","é","è","ê","ë","à","â","ù","û","ô","î","ï","Ç","É","È","Ê","Ë","À","Â","Ù","Û","Ô","Î","Ï");
  $array_2 = array("-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","_","","","","","_","-","-","","","-","+","'","-","_","-","-","c","e","e","e","e","a","a","u","u","o","i","i","C","E","E","E","E","A","A","U","U","O","I","I");
  $content = str_replace($array_1, $array_2, $content);
  $content = str_replace(")", "-", $content);
  return $content;
}

/**
  destinée production nxtxt menbu
*/
function txt_on_off($vars,$txt_on,$txt_off){
  $VAR=strtoupper($vars);
  $e=@constant($VAR);
  if (!$e) print "<p class='erreur'>".ALERTE_VAR_CONF_ABS.$VAR."</p>";
  if($e =='true') return $txt_off;
  else return $txt_on;
}

/**
 creer un liens vers wiki, ouverture en popup
*/
function create_link_popup_wiki($url,$texte,$sortie=false){
  $s='<a href="'.$url.'" title="'.WIKI_HELP.'" target="_blanck" '."onclick=\"window.open('".$url."','awindow','location=0,status=0,toolbar=0,menubar=0,scrollbars=1,width=950'); return false;\" ".'>'.$texte.'</a>';
  if ($sortie !=false) print $s;
  else return $s;
}

/**
 Affichage type title plsu detaillé au survol
*/
function Toolip($txt,$type_s=false){
	$t = "onmouseover=\"showTooltip('".addslashes($txt)."');\" onmouseout=\"hideTooltip()\" onclick=\"hideTooltip()\"";
	if (!$type_s) return $t;
	else print $t;
}
## produit ligne complete avec lien pour deplie option.
function option_deplie($id,$txt,$sortie=false,$help=false,$html_js='',$toolip=''){
  $cpt=0;$cpt++;
  $tool= (empty($tool))? Toolip($toolip) : '' ;
  $x="<a href=\"#no_block\"  onclick=\"{ aff_cache_plus_minus('".$id."','".$id."_".$cpt."','".TEMPLATE_ADMIN."'); ".$html_js."}; return false;\" ".$tool."><img id='".$id."_".$cpt."' src='template/".TEMPLATE_ADMIN."/img/plus.png' alt='img' />".$txt."</a>";
  if (!$sortie) print $x;
  else return $x;
}

// ajax averc confirmation js préalable
function ajax_droit_confirm($txt_confirm,$lib_dest, $funct_dest,$var_dest,$dest_ret,$txt_ret){
$format_ajax_inline="upload_ajax('%1\$s','%2\$s','%3\$s','%4\$s');";
$s = 	"{ if (confirm(' ".addslashes($txt_confirm)." ')){ "
	." ajax_load('retour_ajax_txt'); montre('retour_ajax'); montre('backgd');  "
	.sprintf($format_ajax_inline,"retour_ajax_txt",$lib_dest,$funct_dest, $var_dest);
	if (!empty($dest_ret))$s.=" add_link_nav2('retour_ajax_nav','".$dest_ret."','".$txt_ret."','' );";
	$s.=" return false; "
	." } "
	." else  return false;"
	." } ";
return $s;

}
// ajax averc confirmation js préalable +++ Souple
function ajax_confirm($txt_confirm,$lib_dest, $funct_dest,$var_dest,$dest_ret,$txt_ret){
	$s ="{ if (confirm(' ".addslashes($txt_confirm)." ')){ "
	." montre('onload');"
	.sprintf("upload_ajax('%1\$s','%2\$s','%3\$s','%4\$s');","form_ajax_txt",$lib_dest,$funct_dest, urlencode($var_dest));
	if (!empty($dest_ret))$s.=" add_link_nav2('form_ajax_nav','".$dest_ret."','".addslashes($txt_ret)."','' );";
	$s.=" cache('onload'); montre('backgd'); montre('form_ajax');"
	." return false; "
	." } "
	." else  return false;"
	." } ";
	return $s;
}
// ajax pour gestion forms
function ajax_forms($lib_dest, $funct_dest,$var_dest,$dest_ret,$lib_ret,$funct_ret,$var_ret,$txt_ret){
$format_ajax_inline="upload_ajax('%1\$s','%2\$s','%3\$s','%4\$s')";
$s = 	"{  montre('onload');"
	.sprintf($format_ajax_inline,"form_ajax_txt",$lib_dest,$funct_dest, urlencode($var_dest))
	."; "
	." cache('onload'); montre('backgd'); montre('form_ajax');"
	." return false; "
	." } ";
return $s;
}
function ajax_action ($lib_dest, $funct_dest,$var_dest,$dest_ret='',$txt_ret=RETOUR,$time=''){
$re=(!empty($dest_ret))? " add_link_nav2('form_ajax_nav','".$dest_ret."','".$txt_ret."','' );": '';
return	"{  montre('onload');"
	.sprintf("upload_ajax('%1\$s','%2\$s','%3\$s','%4\$s');",'form_ajax_txt',$lib_dest,$funct_dest,urlencode($var_dest))
	."cache_t('onload','".$time."'); montre('backgd'); montre('form_ajax'); "
	."}";
}


/**
* @author oscim - LossimTeam
* @arg le liens de destinataion de la redirection
* @note
*/
function redirect($destination='index.php'){
  session_write_close();
  if(!@header("Location:".URL_ADMIN.str_replace('&amp;','&',$destination)) )  print "<meta HTTP-EQUIV=\"refresh\" content=\"0; URL=".URL_ADMIN.str_replace('&amp;','&',$destination)."\"> ";
}

/**
* @author oscim - LossimTeam
* @arg
* @note string en entree, sortie tableau key/value. separation des element par & et sepraration key value par =
*/
function extract_option($option){
  if(!empty($option)) {
    if (!is_array($option)&&!ereg("&$",$option)) $option.='&';
    $opt=explode ('&',$option);
    while ($r=each($opt))
      if (!empty($r['value'])) {
      $key=trim(substr($r['value'],0, (strpos($r['value'],'='))));
      $val=trim(substr($r['value'],(strpos($r['value'],'=')+1), strlen($r['value'])));
      $options[$key] = $val;
      }
      return $options;
  }
  return false;
}


/**
 Focntion associé control des dependances, requis en fucntion independande pour module externe
La table est un tableau 2 D
	Key1 >> extensin requeteur
	Key2 >> exten lies dep or conf
	value >> detail key2
retrun true si dep non presente on key 1
*/
function ctr_int_tbl_dep($module,$table,$la_dep){
  $cores=array('sup_strict'=>'>','sup'=>'>=','inf_strict'=>'<','inf'=>'=<', 'egal'=>'==',''=>'>=');
  if (!isset($table[$module][$la_dep['name_dep']]))  return true;
  else{
// while ($r=each($table[$la_dep['name_dep']])){
    $r1=extract_option($table[$module][$la_dep['name_dep']]);
    if ($r1['vers_dep'] ==''|| $r1['vers_dep'] ==0)return true;
    elseif (($ret=eval("\$ret= \$r1['vers_dep'].\$r1['mode_dep'].\$la_dep['vers_dep'] ;")) ==true)return true;
// }
  }
  return false;
}

/* Constrol recursif de dependance + tableau de conflit Appel par function de cette lib et par config_modules
Nom du module + tableau de dep+conf+cons
Le $mod_dep bloque l'appel au dependance heitée des element.*/
function control_extension_dep($tableau_dep, $nom_module,$t_dep,$t_conflit,$mod_dep=false) {
if (is_array($tableau_dep)){
	reset($tableau_dep);
	if (isset($tableau_dep[$nom_module])) {
	while ($r0=each($tableau_dep[$nom_module])){
		$r=extract_option($r0['value']);
		/*verif dependance + conflit*/
			if ($r['type']=='dep') {
				##verif presence deja incluonce ni dans dep ni conflit
				if ( ctr_int_tbl_dep($r['name_pkg'],$t_dep,$r['name_dep']) && ctr_int_tbl_dep($r['name_pkg'],$t_conflit,$r['name_dep']) ) {
					$t_dep[$r['name_pkg']][$r['name_dep']]='name_pkg='.$r['name_pkg'].'&pkg_id='.$r['pkg_id'].'&name_dep='.$r['name_dep'].'&vers_dep='.$r['vers_dep'].'&mode_dep='.$r['mode_dep'];
					/* Control des dependance heritée   uniquement si mode_dep on */
					if (isset($tableau_dep[$r['name_dep']]) && $mod_dep==true) {
						$res=control_extension_dep ($tableau_dep,$r['name_dep'],$t_dep,$t_conflit,$mod_dep);
						$t_dep=$res['t_dep'];
						$t_conflit=$res['t_conflit'];
					}
				}
			}
			elseif ( ($r['type']=='conflit')  OR ($r['type']=='conf') ) {
				if ( ctr_int_tbl_dep($r['name_pkg'],$t_conflit,$r['name_dep']) ) $t_conflit[$r['name_pkg']][$r['name_dep']]='name_pkg='.$r['name_pkg'].'&pkg_id='.$r['pkg_id'].'&name_dep='.$r['name_dep'].'&vers_dep='.$r['vers_dep'].'&mode_dep='.$r['mode_dep'];
			}

		/*ajoute le mode requereur a liste paquet (apes dependnaces)*/
// 		if ($mod_dep==true) $t_dep[$nom_module][]=$nom_module;

	}
	}
}
		return array('t_dep'=>$t_dep, 't_conflit'=> $t_conflit);
}

function unhtmlentities($chaineHtml,$utf8=false) {
	 $tmp = get_html_translation_table(HTML_ENTITIES);
	 $tmp = array_flip ($tmp);
	 $chaineTmp = ($utf8==true) ?utf8_encode(strtr ($chaineHtml, $tmp)) : strtr ($chaineHtml, $tmp);
	 return $chaineTmp;
 }


/* Geston et retoru erreur */
function verbose($value,$type='erreur'){
  if (defined('DEVELOP')) {
    switch($type){
      case 'erreur':
      case 'error':
        print '<p class="error">'.$value.'</p>';
      break;
      case 'notice':
      default:
        print '<p class="notice">'.$value.'</p>';
    }
  }
}

/* Appel des fichiers est evite doublon */
function include_file ($value,$ref=false,$force=false) {
  static $deja_charge;
  if (!tep_not_null($value)) return false;
  if ($ref){
          $refe['url']= URL_ADMIN;
          $refe['path']= '';
  } else  {
          $refe['url']= URL_DU_SITE;
          $refe['path']= ADD_PATH_ADMIN;
  }

  $long=strlen($value);
  $key=md5($value);
  if (!isset($deja_charge[$key]) || $force) {
          if (substr($value,($long-3),$long)=='css') print "<link rel=\"stylesheet\" href=\"".$refe['url'].$value."\"  type=\"text/css\" media=\"all\" />\n";
          elseif (substr($value,($long-2),$long)=='js') print '<script language="JavaScript" type="text/javascript" src="'.$refe['url'].$value.'"></script>'."\n";
          elseif(substr($value,($long-3),$long)=='php'||substr($value,($long-3),$long)=='inc') include_once ($refe['path'].$value);
  }
  $deja_charge[$key]='';
}


/**
  gestion des module actif et constrution gestion des class de modules
  @since 3.3
*/
function module_actif_dyn($_PX_config_module){
  $cl=$class=$mod=array();
  while ($r=each($_PX_config_module)){
    if (isset($r['value']['class']))$class[$r['value']['class']][$r['key']]= "".$r['value']['etat']."";
    else $mod[$r['key']]=$r['value']['etat'];
  }
  if (is_array($class))
    while ($r2=each($class)){
      $r3=each($r2['value']);
      if (!isset($cl[$r2['key']])){
        $mod[$r3['key']]=$r3['value'];
        $cl[$r2['key']]=$r3['key'];
      }
    }
  var_config_cst($cl,'PX_config_class');
  return  $mod;
}

/**
  @return boolean si nom key enregsitre ds session
  @since 3.7
*/
function is_register_session($key){
   if (empty($key) || !isset($_SESSION[$key])) return false;
   else return true;
}

/**
  Enregsitre element ds session
  @since 3.7
*/
function add_in_session($key,$value){
  $_SESSION[$key]= $value;
}

/**
  Destruct element session
  @since 3.7
*/
function del_in_session($key){
  if (isset($_SESSION[$key])) unset($_SESSION[$key]);
  else return false;
}


/**
  retour element in session
  @since 3.7
*/
function value_in_session($key){
    if (isset($_SESSION[$key])) return $_SESSION[$key];
    else return false;
}

/**
  Block form complet pour gestion affichage
  @since 3.7
   @param
    @a int page alias env
    @a string get a passer au form
    @a string params supp html
    @a string valeur selected
*/
function tep_row_by_page($page,$get,$params,$selected=''){
  global $cstr;
  for($i=1 ; $i<10 ; $i++){ $v=($i*5); $arryData[$v]=' '.$v.' '; }
  $s=tep_form ($cstr->page_content.'.php','',get_all_param(array('row_by_page')),"thefom","GET",false);
  $s.='<p>'.input_select('row_by_page',$arryData,$selected,' onchange="document.thefom.submit();"');
  $s.=php_f_hidden('env',@$_GET['env']).'</p>';
  $s.='</form>';
  return $s;
}

/**
  Row by page
  @a array key des element qui ne seront pas reportés
  @since 3.7
*/
function get_all_param($array=''){
  $s='';
  while($r=each($_GET))
    if (!in_array($r['key'],(array)$array)) $s.=$r['key'].'='.$r['value'].'&amp;';

  return $s;
}


/**
 penser a modifier les element des page modifier_un_fichier et creer_une_page et menu_ajax_complet  pour les liens de visiter cet articles, si l extention des fichier et modifier
gestion des liens et construction des url. si option $get >> ajout de parametre get (tec=1&tec=2)
*/
function tep_url_link($link='',$ext=true,$url=true,$get='',$ancre=''){
  $ext='.'.EXT;
  if (!empty($get)) {
    $get= (substr($get, 0,1) =='&')? substr($get,1) :$get ;
  } else $get='';

  if (!@eregi("(^http:|^#|mailto|.(zip|tar|gz|pdf|ps|png|jpg|gif)$)",$link)){
    if($url==true){
      if (test_bool('LANG_MULTI')  && !eregi("(^[a-z]{2}/)",$link)) $link=LANG_POST.'/'.$link;
      $link = (URL_FORMAT=='simple') ? URL_DU_SITE."?".$link.$get : URL_DU_SITE.$link.$get;
    }
    if($ext==true) $link = $link.$ext.$ancre;
  }elseif (eregi("(^#)",$link)) {
    $resl=NewFct('extract_nom','');
    $lin  = (URL_FORMAT=='simple') ? URL_DU_SITE."?".$resl['url'].$get : URL_DU_SITE.$resl['url'].$get;
    if($ext==true) $lin = $lin.$ext.$ancre;
    $link = $lin.$link;
  }
  return $link;
}
?>