<?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']=='ordreaff') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typc']."' WHERE param_nom = 'freq_ordreaff'");
	}
	else if($_REQUEST['param']=='activactiv') {
		$codezs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv WHERE code_activ <> 'ZERO' ORDER BY nom_activ ASC");
		foreach($codezs as $codez) {
			if($_REQUEST['t'.$codez['code_activ']]==0) {
				mysqli_query($bdd,"UPDATE ".$prefix."_freqactiv SET activ_activ = '".$_REQUEST['t'.$codez['code_activ'].'']."', default_activ = 0 WHERE activ_id = '".$codez['activ_id']."'");
			}
			else {
				mysqli_query($bdd,"UPDATE ".$prefix."_freqactiv SET activ_activ = '".$_REQUEST['t'.$codez['code_activ'].'']."' WHERE activ_id = '".$codez['activ_id']."'");
			}
		}
	}
	else if($_REQUEST['param']=='consultactiv') {
		$codezs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv WHERE code_activ <> 'ZERO' ORDER BY nom_activ ASC");
		foreach($codezs as $codez) {
			mysqli_query($bdd,"UPDATE ".$prefix."_freqactiv SET consult_activ = '".$_REQUEST['tc'.$codez['code_activ'].'']."' WHERE activ_id = '".$codez['activ_id']."'");
		}
	}
	else if($_REQUEST['param']=='visibleactiv') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['visible']."' WHERE param_nom = 'freq_consultactiv'");	
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['obliga']."' WHERE param_nom = 'freq_activ_obli'");			
	}
	else if($_REQUEST['param']=='anticipe') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'freq_creneausuiv'");	
	}
	else if($_REQUEST['param']=='multicren') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'freq_multiinscrit'");
	}
	else if($_REQUEST['param']=='crenprec') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typc']."' WHERE param_nom = 'freq_crprechoix'");
	}
	else if($_REQUEST['param']=='passages') {
		if($_REQUEST['ensemble']==1) {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['ensemble']."' WHERE param_nom = 'freq_grappes'");
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['passages']."' WHERE param_nom = 'freq_passages'");
		}
		else {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '0' WHERE param_nom = 'freq_grappes'");
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '0' WHERE param_nom = 'freq_passages'");
		}
	}
	else if($_REQUEST['param']=='seuili') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'freq_seuilnbi'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typd']."' WHERE param_nom = 'freq_seuilreaci'");		
	}
	else if($_REQUEST['param']=='seuilr') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'freq_seuilnbr'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typd']."' WHERE param_nom = 'freq_seuilreacr'");		
	}
	else if($_REQUEST['param']=='visibleseuil') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['visible']."' WHERE param_nom = 'freq_vision_espaces_ins'");		
	}
	else if($_REQUEST['param']=='suspduree') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'freq_resref_date'");
	}
	else if($_REQUEST['param']=='suspannul') {
		mysqli_query($bdd,"DELETE FROM ".$prefix."_freqresref WHERE eleves_id = '".$_REQUEST['ele_id']."'");
	}
	else if($_REQUEST['param']=='crenchoix') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['crendef']."' WHERE param_nom = 'freq_crendef'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['crch']."' WHERE param_nom = 'freq_crenchoix'");
		if($_REQUEST['crch']==0) {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '1' WHERE param_nom = 'freq_reseleve_creneau'");
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['onlyresas']."' WHERE param_nom = 'freq_onlyresas'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['resavisu']."' WHERE param_nom = 'freq_resvisuedt'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['blocage']."' WHERE param_nom = 'freq_resblockevent'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['precision']."' WHERE param_nom = 'freq_resactiv'");
	}
	else if($_REQUEST['param']=='ibullesub') {
		$cide = mysqli_real_escape_string($bdd,$_REQUEST['cide']);
		$cmdp = mysqli_real_escape_string($bdd,$_REQUEST['cmdp']);
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['ibopt'].";".$cide.";".$cmdp."' WHERE param_nom = 'freq_connexion_infobulle'");		
	}
	else if($_REQUEST['param']=='reservsub') {
		if($_REQUEST['resopt']==1&&(strlen($_REQUEST['mdp'])<12||strlen($_REQUEST['mdp'])>15)) {
			alert('Le mot de passe donné ne remplit pas les conditions.');
			$_REQUEST['resopt']=0;
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['resopt']."' WHERE param_nom = 'freq_reseleve'");
		if($_REQUEST['resopt']==1) {
			$mdp = mysqli_real_escape_string($bdd,$_REQUEST['mdp']);
			do {
			    $sel = bin2hex(random_bytes(16));
			} while ($sel === '0');
			$mdp_avec_sel = $mdp.$sel;
			$hashed_password = hash('sha256', $mdp_avec_sel);
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur= '".$hashed_password."' WHERE param_nom = 'freq_reseleve_mdp'");
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur= '".$sel."' WHERE param_nom = 'freq_reseleve_mdpsel'");
		}
	}
	else if($_REQUEST['param']=='reservopt') {
		function strToNoAccent($var) {
			$var = str_replace(
				array(
					'à', 'â', 'ä', 'á', 'ã', 'å',
					'î', 'ï', 'ì', 'í', 
					'ô', 'ö', 'ò', 'ó', 'õ', 'ø', 
					'ù', 'û', 'ü', 'ú', 
					'é', 'è', 'ê', 'ë', 
					'ç', 'ÿ', 'ñ',
					'À', 'Â', 'Ä', 'Á', 'Ã', 'Å',
					'Î', 'Ï', 'Ì', 'Í', 
					'Ô', 'Ö', 'Ò', 'Ó', 'Õ', 'Ø', 
					'Ù', 'Û', 'Ü', 'Ú', 
					'É', 'È', 'Ê', 'Ë', 
					'Ç', 'Ÿ', 'Ñ', '-', ' ','\''
				),
				array(
					'a', 'a', 'a', 'a', 'a', 'a', 
					'i', 'i', 'i', 'i', 
					'o', 'o', 'o', 'o', 'o', 'o', 
					'u', 'u', 'u', 'u', 
					'e', 'e', 'e', 'e', 
					'c', 'y', 'n', 
					'a', 'a', 'a', 'a', 'a', 'a', 
					'i', 'i', 'i', 'i', 
					'o', 'o', 'o', 'o', 'o', 'o', 
					'u', 'u', 'u', 'u', 
					'e', 'e', 'e', 'e', 
					'c', 'y', 'n', '','',''
				),$var);
			return $var;
		}
		function rand_nb($length = 8) {
		    $characters = '123456789';
		    $charactersLength = strlen($characters);
		    $randomString = '';
		    for ($i = 0; $i < $length; $i++) {
		        $randomString .= $characters[rand(0, $charactersLength - 1)];
		    }
		    return $randomString;
		}
		$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'"));
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['reselec']."' WHERE param_nom = 'freq_reseleve_creneau'");
		if($_REQUEST['reselec']==0) {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '1' WHERE param_nom = 'freq_crenchoix'");
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['reselea']."' WHERE param_nom = 'freq_reseleve_activ'");
		$auth_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_resmodauth'"));
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['resmod']."' WHERE param_nom = 'freq_resmodauth'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['resmdp']."' WHERE param_nom = 'freq_reseleve_cgtmdp'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['seqsvisu']."' WHERE param_nom = 'freq_res_seqsvisu'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['resopt']."' WHERE param_nom = 'freq_resprintedt'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['raison']."' WHERE param_nom = 'freqres_raison_opt'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['nodiv']."' WHERE param_nom = 'freq_reseleve_nodiv'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['allday']."' WHERE param_nom = 'freqres_inscriptallday'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['maxo']."' WHERE param_nom = 'freqres_max_origin'");
		if($espopt['valeur']==1) {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['reselee']."' WHERE param_nom = 'freq_reseleve_esp'");
		}
		if($_REQUEST['done']!=""&&$_REQUEST['dtwo']!="") {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['done'].$_REQUEST['dtwo']."' WHERE param_nom = 'freq_reseleve_dureemax'");
		}
		else if($_REQUEST['val']!="") {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['val']."' WHERE param_nom = 'freq_reseleve_dureemax'");
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['max']."' WHERE param_nom = 'freqres_max'");
		if($auth_info['valeur']!=$_REQUEST['resmod']&&($_REQUEST['resmod']=="prenomnom"||$_REQUEST['resmod']=="pnom")) {
			$eleves = mysqli_query($bdd,"SELECT eleves_id, nom, prenom FROM ".$prefix."_eleves");
			foreach($eleves as $eleve) {
				if($_REQUEST['resmod']=="prenomnom") {
					$ident = substr(strToNoAccent($eleve['prenom']),0,20).'.'.substr(strToNoAccent($eleve['nom']),0,20);
				}
				else if($_REQUEST['resmod']=="pnom") {
					$ident = substr(strToNoAccent($eleve['prenom']),0,1).substr(strToNoAccent($eleve['nom']),0,20);
				}
				$checke = mysqli_query($bdd,"SELECT id_constr FROM ".$prefix."_eleves WHERE id_constr = '".mysqli_real_escape_string($bdd,strtolower($ident))."' AND eleves_id <> '".$eleve['eleves_id']."'");
				$checku = mysqli_query($bdd,"SELECT login FROM ".$prefix."_user WHERE login = '".mysqli_real_escape_string($bdd,strtolower($ident))."'");
				if(mysqli_num_rows($checke)>0||mysqli_num_rows($checku)>0||$ident=='reserver') {
					$ident = $ident.rand_nb(2);
				}
				mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET id_constr = '".strtolower($ident)."' WHERE eleves_id = '".$eleve['eleves_id']."'");
			}
		}
	}
	else if($_REQUEST['param']=='sigb') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['type']."' WHERE param_nom = 'freq_sigb'");
	}
	else if($_REQUEST['param']=='ordrreta') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['type']."' WHERE param_nom = 'freq_retards_ordre'");
	}
	else if($_REQUEST['param']=='sigbresa') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['type']."' WHERE param_nom = 'freq_sigb_resa'");
	}
	else if($_REQUEST['param']=='ordrresa') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['type']."' WHERE param_nom = 'freq_sigb_resaord'");
	}
	else if($_REQUEST['param']=='notifsv') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['valn']."' WHERE param_nom = 'freq_notif_nb'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['notifopt']."' WHERE param_nom = 'freq_notif_opt'");
	}
	else if($_REQUEST['param']=='notifsa') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['notifbirth']."' WHERE param_nom = 'freq_birthday_img'");
	}
	else if($_REQUEST['param']=='alii') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['cienchoix']."' WHERE param_nom = 'freq_cienchoix'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['validmes']."' WHERE param_nom = 'freq_validmes'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['codeonly']."' WHERE param_nom = 'freq_codeonly'");
		if($_REQUEST['codeonly']=='1') {
			$_REQUEST['classe']='0';
			$_REQUEST['seance_appel']='0';
			$_REQUEST['ordgc']='-perdiv';
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['classe'].$_REQUEST['ordgc']."' WHERE param_nom = 'freq_classe'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['seance_appel']."' WHERE param_nom = 'freq_seance_appel'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['chgtactiv']."' WHERE param_nom = 'freq_chgtactiv'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['jouhor']."' WHERE param_nom = 'freq_jhors'");
		if(isset($_REQUEST['appelchoix'])=='1') { $app =2; } else { $app =1; }
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$app.":1' WHERE param_nom = 'freq_appel'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['buttonchoix']."' WHERE param_nom = 'freq_inscritbutton'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['pronote']."' WHERE param_nom = 'freq_inscritpronote'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['apinbins']."' WHERE param_nom = 'freq_api_nb'");
		$filePath = BASE_PATH . '/img/edt/'.$prefix.'/nbinscrits.txt';
		if($_REQUEST['apinbins']=='1') {
			if (!file_exists($filePath)) {
			    $initial = 0;
			    file_put_contents($filePath,$initial);
			    chmod($filePath, 0644);			    
			}
		}
		else if($_REQUEST['apinbins']=='0') {
			if (file_exists($filePath)) {
			    unlink($filePath);
			}
		}
	}
}
mysqli_close($bdd);
?>