<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
if(isset($_SESSION['user_droits'])) {
	if($_REQUEST['param']=='message') {
		if($_REQUEST['openmsg_new']=="") {$_REQUEST['openmsg_new']="-";}
			$openmsg_new = mysqli_real_escape_string($bdd,trim($_REQUEST['openmsg_new']));
			if($openmsg_new=='') { $openmsg_new = '-'; }
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$openmsg_new."' WHERE param_nom = 'edt_openmsg'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typh']."' WHERE param_nom = 'edt_formhor'");
	}
	else if($_REQUEST['param']=='extraction') {
		$edtmsgopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_msgopt'"));
		$edtmsgopt_infos = explode(";",$edtmsgopt['valeur']);
		$edttitleopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_titopt'"));
		$edttitleopt_infos = explode(";",$edttitleopt['valeur']);
		
		if($_REQUEST['edtbotmsgsize']!="") { $edtbotmsgsize = $_REQUEST['edtbotmsgsize']; }
		else { $edtbotmsgsize = $edtmsgopt_infos[1]; }
		if($_REQUEST['edtbotmsgcolor']!="") { $edtbotmsgcolor = $_REQUEST['edtbotmsgcolor']; }
		else { $edtbotmsgcolor = $edtmsgopt_infos[2]; }
		
		if($_REQUEST['edttitlesize']!="") { $edttitlesize = $_REQUEST['edttitlesize']; }
		else { $edttitlesize = $edttitleopt_infos[1]; }
		if($_REQUEST['edttitlecolor']!="") { $edttitlecolor = $_REQUEST['edttitlecolor']; }
		else { $edttitlecolor = $edttitleopt_infos[2]; }
		
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['msg'].";".$edtbotmsgsize.";".$edtbotmsgcolor.";".$_REQUEST['agenda']."' WHERE param_nom = 'edt_msgopt'");
		
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['tit'].";".$edttitlesize.";".$edttitlecolor.";".$_REQUEST['sup']."' WHERE param_nom = 'edt_titopt'"); 
		// Gestion d'une modification du contenu :
		$msg_new = mysqli_real_escape_string($bdd,trim($_REQUEST['edt_msg']));
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$msg_new."' WHERE param_nom = 'edt_msg'");
		$tit_new = mysqli_real_escape_string($bdd,trim($_REQUEST['edt_tit']));
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$tit_new."' WHERE param_nom = 'edt_tit'");		
	}
	else if($_REQUEST['param']=='codeactiv') {
		$codezs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtcode ORDER BY code ASC");
		foreach($codezs as $codez) {
			mysqli_query($bdd,"UPDATE ".$prefix."_edtcode SET activ_code = '".$_REQUEST['t'.$codez['code_id'].'']."' WHERE code_id = '".$codez['code_id']."'");
		}
	}
	else if($_REQUEST['param']=='defaut') {
		$codezs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtcode ORDER BY code ASC");
		foreach($codezs as $codez) {
			mysqli_query($bdd,"UPDATE ".$prefix."_edtcode SET activ_code = '".$_REQUEST['t'.$codez['code_id'].'']."' WHERE code_id = '".$codez['code_id']."'");
		}
	}
	else if($_REQUEST['param']=='colorhbdef') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='#EEEEEE#D0CCC2#7D7777#7D7777' WHERE param_nom = 'edt_crencolors'");
	}
	else if($_REQUEST['param']=='colorhbval') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='".$_REQUEST['cren1'].$_REQUEST['cren2'].$_REQUEST['cren3'].$_REQUEST['cren4']."' WHERE param_nom = 'edt_crencolors'");
	}
	else if($_REQUEST['param']=='colortype') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typc']."' WHERE param_nom = 'edt_coloration'");
		if($_REQUEST['typc']=='cod') {
			$_REQUEST['tcoltext'] = '0';
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['tcoltext']."' WHERE param_nom = 'edt_color_text'");
	}
	else if($_REQUEST['param']=='colornivdef') {
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#F5A9A9' WHERE niveau = '6'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#F5D0A9' WHERE niveau = '5'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#E1F5A9' WHERE niveau = '4'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#BCF5A9' WHERE niveau = '3'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#A9F5BC' WHERE niveau = '2'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#A9F5F2' WHERE niveau = '1'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#A9A9F5' WHERE niveau = 't'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#E2A9F3' WHERE niveau = 'a'");
		mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='#E6E6E6' WHERE niveau = 'r'");
	}
	else if($_REQUEST['param']=='colornivval') {
		$nivcolor = mysqli_query($bdd,"SELECT id, niveau, bgcolor FROM ".$prefix."_edtnivcolor ORDER BY id");
		foreach($nivcolor as $champsniv) {
			mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor='".$_REQUEST['couleurniv'.$champsniv['niveau'].'']."' WHERE niveau = '".$champsniv['niveau']."'");
		}
	}
	else if($_REQUEST['param']=='colorcoddef') {
		$allcodes = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtcode ORDER BY code_id ASC");
		$color_array = array('#FF9900','#99FF00','#CCFFFF','#FF66FF','#CC6600','#FFFF33','#99CC66', '#1E7FCB','#9900FF','#FF0033','#CCCC00','#66CC66','#EFD242','#FFF48D','#CC99CC','#E6E697','#FF6666','#F4C430','#33CC33','#3399FF');
		$i=0;
		foreach($allcodes as $codecolor) {
			mysqli_query($bdd,"UPDATE ".$prefix."_edtcode SET bgcolor_code = '".$color_array[$i]."' WHERE code_id = '".$codecolor['code_id']."'");
			$i++;
		}
	}
	else if($_REQUEST['param']=='colorcodval') {
		$codcolor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtcode WHERE activ_code = 1 ORDER BY code_id ASC");
		foreach($codcolor as $champscod) {
			mysqli_query($bdd,"UPDATE ".$prefix."_edtcode SET bgcolor_code='".$_REQUEST['couleurcod'.$champscod['code_id'].'']."' WHERE code_id = '".$champscod['code_id']."'");
		}
	}
	else if($_REQUEST['param']=='verrouillage') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['verrou']."' WHERE param_nom = 'edt_brikverr'");
	}
	else if($_REQUEST['param']=='multdup') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['dupli']."' WHERE param_nom = 'edt_duplicate'");
	}
	else if($_REQUEST['param']=='extract') {
		$imgmode = $_REQUEST['typd'];
		$check_imgmode_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_imgmode'"));
		if($check_imgmode_info['valeur']=="spe"&&$imgmode=="gen") {
			// On recupere le nom des fichiers png et on les supprime
			$dossier = opendir('../../img/edt/'.$prefix.'/'); 
			$fichiers = array();
			while(false !== ($fichier = readdir($dossier))) {
				if($fichier != '.' && $fichier != '..'&& substr($fichier,0,5) != 'nbins') {
					array_push ($fichiers,$fichier);
				}
			}
			$count = count($fichiers);
			for($i=0;$i<$count;$i++) {
				unlink('../../img/edt/'.$prefix.'/'.$fichiers[$i]);
			}
			closedir($dossier);
		}
		if($check_imgmode_info['valeur']=="gen"&&$imgmode=="spe") {
			$ouverture = opendir('../../img/edt/'.$prefix.'/');
			$lecture = readdir($ouverture);
			$filename = '../../img/edt/'.$prefix.'/idlog_edt.png';
			if (file_exists($filename)) {
				unlink ('../../img/edt/'.$prefix.'/idlog_edt.png');
			}
			closedir($ouverture);
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$imgmode."' WHERE param_nom = 'edt_imgmode'");
		if($imgmode=="spe") {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'edt_imgnb'");
		}
	}
	else if($_REQUEST['param']=='delvig') {
		$ouverture = opendir('../../img/edt/'.$prefix.'/');
		$lecture = readdir($ouverture);
		$filename = '../../img/edt/'.$prefix.'/'.$_REQUEST['file'];
		if (file_exists($filename)) {
			unlink ('../../img/edt/'.$prefix.'/'.$_REQUEST['file']);
		}
		closedir($ouverture);
	}
}
mysqli_close($bdd);
?>