<?php
function controle_droits($user){
if (_USER_PERSO__GRP<=$user)  // droit insuffisant
	return false;
else return true;
}
/// REPORT NIVEAU USER
// Un Utilisateur ne peut modifier que les niveau user inférieur
function modif_user_droits($user){
	if (!controle_droits($user['grp'])) return $s=corresp_droits($user['grp']);
	else {
	$s = "<select name=".$user['login'].">"
		."<option value='".$user['grp']."'>".corresp_droits($user['grp'])."</option>";
	$e=corresp_droits();
		while ($r = each($e))
			$s .="<option value='".$r['key']."'>".$r['value']."</option>";
	$s .="</select>";
	return $s;
	}
}
/// ADD USER FILE
function add_user_file ($_POST){
if ( (is_writable("config/users/")) || (OS_SERVEUR == "windows") ) {
$new_user_file = <<<EOF
<?php
/***************************************************************************
 *   Copyright (C) 2008 by oscim   Project lossim                          *
 *   a@oscim.fr                                                            *
 *       [created : 14.03.2008, 19:02:36] @oscim                           *
 ***************************************************************************/
 ## FICHIER DE CONFIGURATION UTILISATEUR
 ## PS une mauvaise manip peut generer des erreurs

EOF;
$new_user_file.="\n\n\$_perso_['nom']='".$_POST['nom']."';#\n";
$new_user_file.="\$_perso_['prenom']='".$_POST['prenom']."';#\n";
$new_user_file.="\$_perso_['login']='".$_POST['login']."';#\n";
$new_user_file.="\$_perso_['email']='".$_POST['email']."';#\n";
$new_user_file.="\$_perso_['site']='".$_POST['site']."';#\n";
$new_user_file.="## groupe de l'utilisateur: 1> admin 2> redac 3>users \n ";
$new_user_file.="\$_perso_['grp']='".$_POST['grp']."';#\n";
$new_user_file.="\$_perso_['url_1']='".$_POST['url_1']."';#\n";
$new_user_file.="\$_perso_['url_2']='".$_POST['url_2']."';#\n";
$new_user_file.="\$_perso_['pref_language']='".$_POST['pref_language']."';# // language\n";
$new_user_file.="\$_perso_['pref_orga_gestion_modules']='".$_POST['pref_orga_gestion_modules']."';#  // 0 :liste uniquement un modules dans la liste_navi, ou 1 : tous\n";
$new_user_file.="\$_perso_['pref_mod_edit']='".$_POST['pref_mod_edit']."';# // language\n";
$new_user_file.="\$_perso_['accept_click_onload'] = 'false';#  // autoriser les click avant chargement complet\n";
$new_user_file.="\$_perso_['up_auto_edit_file'] = 'true';#  // sauvegarde auto\n";
// $new_user_file.="\$_perso_['up_auto_edit_file_time'] = '5000';#  //tps entre sauvegarde\n";
$new_user_file.="\$_perso_['duree_session'] = '5000';#  //duree session identification\n";
$new_user_file.="\$_perso_['nbr_box_xmedia'] = '20';#  //nbr img dans xmedia\n";
$new_user_file.="\$_perso_['theme'] = 'color';#  //theme user \n";
## fin de fichier
$new_user_file.=" \n ?>";
$e= md5(trim($_POST['login']) . trim($_POST['pass1']));
$handle = @fopen("./config/users/".$e.".inc", "w+");
$r=@fwrite($handle, $new_user_file);
if($r)$r=@fclose($handle);
return $r;
}else return false;
}


/// ADD USER BASE USERS
function add_user_base ($login,$pass,$type_s=false){
	$query =" INSERT INTO ".TBL_SYS_CONFIG_PERSO_INC.' ( '.$login.' )'." VALUES  ( '".md5(trim($login) . trim($pass))." ) ";
	$g=sql ('',TBL_SYS_CONFIG_PERSO_INC,'_user_',$query,'');
	if (!$type_s)return $g;
	else print $g;
}

/// MOD USER BASE USERS
function up_user_file ($menu_key,$_POST){
	$TBL="config/users/".$_POST['user_key'].".inc";
	$query=' UPDATE '.$TBL
	 .' SET nom = \''.$_POST['nom']."'"
	 .' , prenom = \''.$_POST['prenom']."'"
	 .' , login = \''.$_POST['login']."'"
	 .' , email = \''.$_POST['email']."'"
	 .' , site = \''.$_POST['site']."'"
	 .' , grp = \''.$_POST['grp']."'"
	 .' , url_1 = \''.@$_POST['url_1']."'"
	 .' , url_2 = \''.@$_POST['url_2']."'"
	 .' , pref_language = \''.$_POST['pref_language']."'"
	 .' , pref_orga_gestion_modules = \''.@$_POST['pref_orga_gestion_modules']."'"
	 .' , pref_mod_edit = \''.@$_POST['pref_mod_edit']."'"
	 .' , accept_click_onload = \''.@$_POST['accept_click_onload']."'"
// 	 .' , up_auto_edit_file = \''.@$_POST['up_auto_edit_file']."'"
// 	 .' , up_auto_edit_file_time = \''.@$_POST['up_auto_edit_file_time']."'"
	 .' , duree_session = \''.@$_POST['duree_session']."'"
	 .' , nbr_box_xmedia = \''.@$_POST['nbr_box_xmedia']."'"
	 .' , theme = \''.@$_POST['theme']."'";
// 	 .' WHERE  _perso_  =   \''.$_POST['login']."'";
	return sql ('',$TBL,'_perso_',$query,'');
}

/// SUPP USER BASE USERS
function supp_user_base ($login,$key,$type_s=false){
	$query=" DELETE FROM ".TBL_SYS_CONFIG_PERSO_INC." WHERE id = '".$login."' ";
	$s= sql ('',TBL_SYS_CONFIG_PERSO_INC,'_user_',$query,'');
	if (!$type_s)return $s;
	else print $s;
}
/// FORM SUPP USER
function supp_user_file($login,$key,$type_s=false){
	$file= 'config/users/'.$key.'.inc';
	$s='';
	if (file_exists($file)) $s=@unlink($file);
	if(!$s) $s = "function supp_user_file unlink erreur";
	$s= '<p class="ok">'.CONFIRM_SUPP_USER.'</p>';
	if (!$type_s)return $s;
	else print $s;
}

/// FORM TEST LOGIN
function aff_user_form_login($type_s=false){
	$t="<h2>%1\$s</h2>";
	$f="<p>%1\$s</p>";
	$s=sprintf($t,ADD_TITRE_FORM);
	$s.=tep_form ("gestion_users.php",'',"conf=new&amp;login=ok","post","POST",false," onsubmit='return checkForm(this)'" );
	$s.=sprintf($f,php_f_textField("login",50,50,ADD_LOGIN,'',"onblur=\"if (this.value == '') {this.value = '".ADD_LOGIN."';}\" onfocus=\"if (this.value == '".ADD_LOGIN."') {this.value = '';}\"").FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($f,FORM_CHAMPS_OBLIGATOIRE_TXT);
	$s.=tep_bts_form('','','SE','SR','','','',true);
	$s.="</form>\n";
	$s .='<a href="gestion_users.php" alt="'.RETOUR.'" >'.RETOUR.'</a>';
	if (!$type_s)return $s;
	else print $s;
}


/// FORMULAIRE ADD USER
function aff_user_form($login,$type_s=false){
	$t="<h2>%1\$s</h2>";
	$op="<option value='%1\$s' %2\$s>%3\$s</option>";
	$p="<p>%1\$s</p>";
	$f_ok="<p class='ok'>%1\$s</p>";
	$arryData=array("Default"=>'french',LG_FRENCH=>"french",LG_ENGLISH=>"english");
	$arryData2=array("openwysing" => "openwysing","bbcode" => "bbcode");
	if (test_const('PX_CONFIG_MODULE_TINYMCE')) $arryData2['tinymce']='tinymce';
	if (test_const('PX_CONFIG_MODULE_FCKEDITOR'))  $arryData2['fckeditor']='fckeditor';

	$js_value= "onblur=\"if (this.value == '') {this.value = '%1\$s';}\" onfocus=\"if (this.value == '%1\$s') {this.value = '';}\"";
	$s=sprintf($t,ADD_TITRE_FORM);
	$s.=tep_form ("gestion_users.php",'',"conf=add","post","POST",false," onsubmit='return checkForm(this)'" );
	$s.=sprintf($p,php_f_textField("login",50,50,trim($login),'',' disabled'));
	$s.=sprintf($p,php_f_hidden("login",$login));
	$s.=sprintf($p,php_f_combobox("grp",array_flip(corresp_droits()),'','','','','').CHOIX_NIV_USER.FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_combobox("pref_language",$arryData,'','','','','').FORM_MOD_USER_LG.FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("nom",50,50,ADD_NAME,'',Toolip(ADD_NAME_D).sprintf($js_value,ADD_NAME)).FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("prenom",50,50,ADD_POSTNAME,'',Toolip(ADD_POSTNAME_D).sprintf($js_value,ADD_POSTNAME))." ");
	$s.=sprintf($p, php_f_textField("email",50,50,ADD_MAIL,'',Toolip(ADD_MAIL_D).sprintf($js_value,ADD_MAIL)).FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p, php_f_textField("site",50,50,ADD_SITE,'',Toolip(ADD_SITE_D).sprintf($js_value,ADD_SITE)));
	$s.=sprintf($p,php_f_textField("pass1",50,50,ADD_PASS1,'',Toolip(ADD_PASS1_D).sprintf($js_value,ADD_PASS1)).FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("pass2",50,50,ADD_PASS2,'',Toolip(ADD_PASS2_D).sprintf($js_value,ADD_PASS2)).FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("pref_orga_gestion_modules",50,50,'1','',Toolip(ADD_PREF_ORG_MODULES_D).sprintf($js_value,ADD_PREF_ORG_MODULES)).ADD_PREF_ORG_MODULES." ");
	$s.=sprintf($p,php_f_combobox("pref_mod_edit",$arryData2,'','','','',Toolip(PREF_MOD_EDIT_D).sprintf($js_value,PREF_MOD_EDIT)).PREF_MOD_EDIT.FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("accept_click_onload",50,50,'false','',Toolip(ADD_ACCEPT_CLICK_ONLOAD_D)).ADD_ACCEPT_CLICK_ONLOAD." ");
// 	$s.=sprintf($p,php_f_textField("up_auto_edit_file",50,50,'false','',Toolip(ADD_ACCEPT_CLICK_ONLOAD_D)).ADD_ACCEPT_CLICK_ONLOAD." ");
// 	$s.=sprintf($p,php_f_textField("up_auto_edit_file_time",50,50,'false','',Toolip(ADD_ACCEPT_CLICK_ONLOAD_D)).ADD_ACCEPT_CLICK_ONLOAD." ");
	$s.=sprintf($p,php_f_textField("duree_session",50,50,'9600','',Toolip(ADD_DUREE_SESSION_D)).ADD_DUREE_SESSION." ");
	$s.=sprintf($p,php_f_textField("nbr_box_xmedia",50,50,'20','',Toolip(ADD_NBR_BOX_XMEDIA_D)).ADD_NBR_BOX_XMEDIA." ");
	$s.=sprintf($p,php_f_textField("theme",50,50,'20','',Toolip(ADD_THEME_D)).ADD_THEME." ");
	$s.=sprintf($p,ADD_INFO_URL_PERS);
	$s.=sprintf($p,php_f_textField("url_1",50,100,ADD_URL1,'',Toolip(ADD_URL1_D).sprintf($js_value,ADD_URL1))." ");
	$s.=sprintf($p,php_f_textField("url_2",50,100,ADD_URL2,'',Toolip(ADD_URL2_D).sprintf($js_value,ADD_URL2))." ");
	$s.=sprintf($p,FORM_CHAMPS_OBLIGATOIRE_TXT);
	$s.=tep_bts_form('','','SE','SR','','','',true);
	$s.="</form>\n";
	if (!$type_s) return $s;
	else print $s;
}
/// FORMULAIRE MOD USER
function mod_user_form($user_key,$type_s=false){
	include("config/users/".$user_key.".inc");
	$t="<h2>%1\$s</h2>";
	$op="<option value='%1\$s' %2\$s>%3\$s</option>";
	$p="<p>%1\$s</p>";
	$f_ok="<p class='ok'>%1\$s</p>";
	$arryData=array("Default"=>$_perso_['pref_language'],LG_FRENCH=>"french",LG_ENGLISH=>"english");
	if (_USER_PERSO__GRP==1)$arryData1=array(USERS_ADMIN => "1",USERS_REDAC => "2",USERS_USER => "3");
	elseif(_USER_PERSO__GRP==2)$arryData1=array(USERS_REDAC => "2",USERS_USER => "3");
	else $arryData1=array(USERS_USER => "3");
	$arryData2=array("openwysing" => "openwysing","bbcode" => "bbcode");
	if (test_const('PX_CONFIG_MODULE_TINYMCE')) $arryData2['tinymce']='tinymce';
	if (test_const('PX_CONFIG_MODULE_FCKEDITOR'))  $arryData2['fckeditor']='fckeditor';
	$s=sprintf($t,MOD_TITRE_FORM);
	$s.=tep_form ("gestion_users.php",'',"conf=up","post","POST",false," onsubmit='return checkForm(this)'" );
	$s.=sprintf($p,php_f_textField("login",50,50,$_perso_['login'],'',' disabled ').FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_hidden("login",$_perso_['login']));
	$s.=sprintf($p,php_f_hidden("user_key",$user_key));
	$s.=sprintf($p,php_f_combobox("grp",$arryData1,$_perso_['grp'],'','','','').CHOIX_NIV_USER.FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_combobox("pref_language",$arryData,$_perso_['pref_language'],'','','','').FORM_MOD_USER_LG.FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("nom",50,50,$_perso_['nom'],'',Toolip(ADD_NAME_D)).FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("prenom",50,50,$_perso_['prenom'],'',Toolip(ADD_POSTNAME_D))." ");
	$s.=sprintf($p,php_f_textField("email",50,50,$_perso_['email'],'',Toolip(ADD_MAIL_D)).FORM_CHAMPS_OBLIGATOIRE);
	$s.=sprintf($p,php_f_textField("site",50,50,$_perso_['site'],'',Toolip(ADD_SITE_D)));
	$s.=sprintf($p,php_f_textField("pref_orga_gestion_modules",50,50,$_perso_['pref_orga_gestion_modules'],'',Toolip(ADD_PREF_ORG_MODULES_D))." ");
	$s.=sprintf($p,php_f_combobox("pref_mod_edit",$arryData2,$_perso_['pref_mod_edit'],'','','',Toolip(PREF_MOD_EDIT_D)).PREF_MOD_EDIT);
	$s.=sprintf($p,php_f_textField("accept_click_onload",50,50,$_perso_['accept_click_onload'],'',Toolip(ADD_ACCEPT_CLICK_ONLOAD_D)).ADD_ACCEPT_CLICK_ONLOAD." ");
	$s.=sprintf($p,php_f_textField("duree_session",50,50,$_perso_['duree_session'],'',Toolip(ADD_DUREE_SESSION_D)).ADD_DUREE_SESSION." ");
	$s.=sprintf($p,php_f_textField("nbr_box_xmedia",50,50,$_perso_['nbr_box_xmedia'],'',Toolip(ADD_NBR_BOX_XMEDIA_D)).ADD_NBR_BOX_XMEDIA." ");
	$s.=sprintf($p,php_f_textField("theme",50,50,$_perso_['theme'],'',Toolip(ADD_THEME_D)).ADD_THEME." ");
	$s.=sprintf($p,ADD_INFO_URL_PERS);
	$s.=sprintf($p,php_f_textField("url_1",50,100,$_perso_['url_1'],'',Toolip(ADD_URL1_D))." ");
	$s.=sprintf($p,php_f_textField("url_2",50,100,$_perso_['url_2'],'',Toolip(ADD_URL2_D))." ");
	$s.=sprintf($p,FORM_CHAMPS_OBLIGATOIRE_TXT);
	$s.=tep_bts_form('','','SE','SR','','','',true);
	$s.="</form>\n";
	if (!$type_s) return $s;
	else print $s;
}


/// LISTE USERS
function aff_list_action_users($_user_){
	$t="<h2>%1\$s</h2><br /><br />";
	$f="<p>%1\$s</p>";

	$s=sprintf($t,TITRE_DE_PAGE);
	$s.="<table>\n<tbody>\n";
	$s.="<tr><th>".TABLE_SSTITRE_NOM."</th><th>".TABLE_SSTITRE_PRENOM."</th><th>".TABLE_SSTITRE_LOGIN."</th><th>".TABLE_SSTITRE_DROITS."</th><th>".TABLE_SSTITRE_MAIL."</th><th>".TABLE_SSTITRE_SITE."</th><th>".TABLE_SSTITRE_ACTION."</th></tr>\n";
	if (2<=count($_user_)){
		while ($row = each($_user_) ) {
		$form_lg_tabl="<tr><td>%1\$s</td><td>%2\$s</td><td>%3\$s</td><td>%4\$s</td><td>%5\$s</td><td>%6\$s</td><td>";
		$e=@include("config/users/".$row['value'].".inc");

			if ($_perso_['grp']>=_USER_PERSO__GRP)$form_lg_tabl.=tep_form ("gestion_users.php",'',"conf=edit","post","POST",false," onsubmit=\" "
// 								.ajax_forms('include/lib/lib.gestion_users.php','mod_user_form'," '".$row['value']."' , true " ,"gestion_users.php","include/lib/lib.menu.php","txt_base","'txt_base'",RETOUR)
								."\" " )
								."<input type='hidden' name='user_key' value='%7\$s'/>"
								.php_f_button('submit',BT_EDITER,'','')
								."</form>\n";
			if (1>=_USER_PERSO__GRP)$form_lg_tabl.=tep_form ("gestion_users.php",'',"conf=supp","post","POST",false," onsubmit='return checkForm(this)'" )
									."<input type='hidden' name='user_key' value='%7\$s'/>"
									."<input type='hidden' name='login' value='%3\$s'/>"
									.php_f_button('submit',BT_SUPP,'','')
									."</form>\n";
			$form_lg_tabl.="</td></tr>";
		$s.=sprintf($form_lg_tabl,$_perso_['nom'],$_perso_['prenom'],$row['key'],corresp_droits($_perso_['grp']),$_perso_['email'],$_perso_['site'],$row['value']);
		}
	}else{
		$form_lg_tabl="<tr><td>%1\$s</td><td>%2\$s</td><td>%3\$s</td><td>%4\$s</td><td>%5\$s</td><td>%6\$s</td><td>";
		$row = each($_user_);
		$form_lg_tabl.="</td></tr>";
		@include("config/users/".$row['value'].".inc");
		$s.=sprintf($form_lg_tabl,$_perso_['nom'],$_perso_['prenom'],$row['key'],corresp_droits($_perso_['grp']),$_perso_['email'],$_perso_['site'],$row['value']);
	}
	$s.="</tbody>\n</table>\n";
	return $s;
}
?>