<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][4]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
				
		//MISES A JOUR
		?><style type="text/css">
		#mpa7 { background-color: #fff; }
		#mpa7 td { border: 3px #7D7777 solid; }
		#mpa6 td { border: 0; }
		</style><?php	
		echo '<h1>'.PARAC_MAJ1.'</h1>';
		
		$url = "https://infodoclog.iddocs.fr/log/version.txt";
		$file_content = @file($url);
		$vers_offbdd = "";
		$vers_offic = "";
		if ($file_content !== false) {
		   $vers_offurl = @htmlspecialchars(implode('', file($url)));
		   $vers_offurl = explode('/', $vers_offurl);
		}
		$vers_offbdd = $vers_offurl[1];
		$vers_offic = $vers_offurl[0];
		
		echo '<h2>'.PARAC_MAJ2.'</h2>';
		$versin = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'version_log'");
		$versin_info = mysqli_fetch_assoc($versin);
		echo '<p>'.PARAC_MAJ3.' '.$versin_info['valeur'].' '.PARAC_MAJ4.'</p>';

		if($vers_offic=="") {
			echo PARAC_MAJ5;
		}
		else if($vers_offic > $versin_info['valeur']) {
			echo '<p><b>'.PARAC_MAJ6.' ';
			$vers_aff = substr($vers_offic,2,2);
			echo ''.$vers_offic.'</b></p>';
			echo PARAC_MAJ7;
			// Recherche d'une mise à jour en montée de version
			$url = "https://infodoclog.iddocs.fr/log/versmaj.txt";
			$vers_offurl = @htmlspecialchars(implode('', file($url)));
			if (preg_match("/\b".$versin_info['valeur']."\b/i",$vers_offurl,$trouve)) {
				echo '<p class="maxwidth"><a href="http://infodoclog.iddocs.fr/log/idlogmaj'.$versin_info['valeur'].'to'.$vers_offic.'.zip"><i class="fas fa-angle-double-right"></i> '.PARAC_MAJ8.' '.$versin_info['valeur'].' '.PARAC_MAJ9.' '.$vers_offic.'</a>.</p>';
			}
			echo '<p class="maxwidth"><a href="http://infodoclog.iddocs.fr/log/idlog'.$vers_offic.'.zip"><i class="fas fa-angle-double-right"></i> '.PARAC_MAJ10.' '.$vers_offic.'</a>.</p>';
		}

		echo '<h2>'.PARAC_MAJ20.'</h2>';

		if(isset($_POST['valmajbdd'])) {
			include('./db/maj_bdd.php');
		}
		if($dossier = opendir('./db/bddmaj')) {
			$versinb = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'version_bdd'");
			$versinb_info = mysqli_fetch_assoc($versinb);
			$i=0;
			while(false !== ($fichier = readdir($dossier))) {
				if(substr($fichier,0,3) == 'bdd') {
					$verscheck = substr($fichier,3,1).'.'.substr($fichier,4,2);
					if($verscheck>$versinb_info['valeur']) {
						$newb = number_format(($versinb_info['valeur']+0.01),2);
						echo '<p class="signal maxwidth"><b>'.PARAC_MAJ11.' ('.$versinb_info['valeur'].') '.PARAC_MAJ12.'</b></p><p style="color:#900;">'.PARAC_MAJ13.' '.$verscheck.PARAC_MAJ14.'</p><p class="maxwidth">'.PARAC_MAJ15.'</p>';
						echo '<form method="POST" action="" name="majbadd"><p class="signal maxwidth"><input type="hidden" name="majbdd" value="'.substr($newb,0,1).substr($newb,2,2).'"><input class="bouton boutparam" type="submit" name="valmajbdd" id="majbdd" value="'.PARAC_MAJ16.' '.$versinb_info['valeur'].' '.PARAC_MAJ17.' '.number_format(($versinb_info['valeur']+0.01),2).'" /></p></form>';
						$i++;
						break;
					}
				}
			}
		}
		if($i==0) { echo '<p>'.PARAC_MAJ18.' '.$versinb_info['valeur'].'</p>'; }
		if($i==0&&$vers_offic > $versin_info['valeur']) {
			echo PARAC_MAJ19;
		}
		echo '<h2>'.PARAC_MAJ21.'</h2>';
		echo '<p>'.PARAC_MAJ22.' '.phpversion().'</p>';
		echo '<p>'.PARAC_MAJ23.' '.mysqli_get_server_info($bdd).'</p>';
		echo '<p>'.BDDN.' '.$base.'</p>';
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
		// Nom du serveur (domaine ou IP)
		$host = @$_SERVER['HTTP_HOST'];
		// URI (chemin après le domaine)
		$uri = @$_SERVER['REQUEST_URI'];
		// URL complète
		$url = $protocol . $host . $uri;
		$headers = @get_headers($url, 1);
		// Vérifier si le Content-Type est défini dans les en-têtes
		if (isset($headers) && is_array($headers) && array_key_exists('Content-Type', $headers)) {
		    //echo "Content-Type: " . $headers['Content-Type'] . "<br>";
		    // Extraire le charset si spécifié
		    if (preg_match('/charset=([a-zA-Z0-9-]+)/i', $headers['Content-Type'], $matches)) {
		        echo '<p>'.PARAC_MAJ39.' '.$matches[1].'</p>';
		    } else {
		        echo '<p>'.PARAC_MAJ40.'</p>';
		    }
		} else {
		    echo '<p>'.PARAC_MAJ41.'</p>';
		}
		if($versinb_info['valeur']>'0.54') {
			if(isset($_POST['charsetval'])) {
				mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_POST['charsetval']."' WHERE param_nom = 'charset'");
				echo '<script language="Javascript">
					<!--
					document.location.replace("param.php?page=majlog");
					// -->
					</script>';
			}
			echo '<p>'.PARAC_MAJ42.' ';
			$charset_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'charset'"));
			echo $charset_info['valeur'].' (<a style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'divcharset\');">'.MODIFY2.'</a>)</p>';
			echo '<form action="" method="POST" name="charsval"><div id="divcharset" style="display:none;"><div class="box"><select name="charsetval" id="charsetdef" value="">';
		    echo '<option value="utf8mb4"';
            if($charset_info['valeur'] == 'utf8mb4') {
            	echo ' selected ';
            }
            echo '>utf8mb4</option>';
            echo '<option value="utf8"';
            if($charset_info['valeur'] == 'utf8') {
            	echo ' selected ';
            }
            echo '>utf8</option>
            <option value="latin1"';
            if($charset_info['valeur'] == 'latin1') {
            	echo ' selected ';
            }
            echo '>latin1</option>';
	        echo '</select></div><input class="bouton boutparam" id="charsv" type="submit" value="'.MODIFY.'" name="charmenu" /></div></form>';	        
			if($charset_info['valeur']!="latin1") {
	        	if(isset($_POST['recodeval'])) {
	        		$_SESSION['save_auto'] = '2';
					include('./db/backup.php'); // CORRIGER POUR QUE CELA FONCTIONNE
					echo 'Modifications effectuées, si besoin, dans<br>';
	        		include('./db/proc/bdd_encode_utf8.php');
	        	}
	        	$querySavoirs = mysqli_query($bdd, "
				    SELECT s3_id, nom_savoir, notions 
				    FROM ".$prefix."_savoirs3 
				    WHERE nom_savoir REGEXP '[^\\x00-\\x7F]' OR 
				          notions REGEXP '[^\\x00-\\x7F]'");

				$queryEleves = mysqli_query($bdd, "
				    SELECT eleves_id, prenom 
				    FROM ".$prefix."_eleves 
				    WHERE prenom REGEXP '[^\\x00-\\x7F]'");

				$queryAgenda = mysqli_query($bdd, "
				    SELECT agenda_id, titre 
				    FROM ".$prefix."_agenda 
				    WHERE titre REGEXP '[^\\x00-\\x7F]'");
				if (mysqli_num_rows($querySavoirs) > 0 || mysqli_num_rows($queryEleves) > 0 || mysqli_num_rows($queryAgenda) > 0) {
					$testE = 0;
					foreach($queryEleves as $queryE) {
						$position = strpos($queryE['prenom'], 'Ã');
						if ($position !== false) {
							$testE = 1;
						}
					}
					foreach($querySavoirs as $queryE) {
						$position = strpos($queryE['nom_savoir'], 'Ã');
						if ($position !== false) {
							$testE = 1;
						}
					}
					foreach($queryAgenda as $queryE) {
						$position = strpos($queryE['titre'], 'Ã');
						if ($position !== false) {
							$testE = 1;
						}
					}
					if($testE==1) {
						echo PB_CHARSET1;
						echo '<form action="" method="POST" name="recode"><input class="bouton boutparam" type="submit" name="recodeval" value="'.PB_CHARSET2.'" />';
						$result_tables = mysqli_query($bdd,"SHOW TABLE STATUS LIKE '".$prefix."_%'");
						$_SESSION['nb_tables'] = mysqli_num_rows($result_tables);
						if($result_tables && $result_tables->num_rows) {
							while($obj_table = mysqli_fetch_assoc($result_tables)) {
								echo '<input type="hidden" id="'.$obj_table['Name'].'" name="'.$obj_table['Name'].'" checked />';
							}
						}
						echo '<input type="hidden" name="hidden_save" id="input_save" checked />';
						echo '</form>';
					}
				}
	        }
		}	

		//SAUVEGARDE
		echo '<h1>'.PARAC_MAJ24.'</h1>';
		if(isset($_POST['savetabs'])) { 
			$_SESSION['save_auto'] = 0;
			include('./db/backup.php'); 
		}
		echo '<p><form method="POST" name="savetabs" action="">'.PARAC_MAJ25.'</a></p>';
		echo '<p><a style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'divtables\');"><b>'.PARAC_MAJ26.'</b></a></p>';
		echo '<div  id="divtables" style="display:none;">';
		echo '<input id="checkAll" type="checkbox" checked />'.CHECKORNOT.'<br>';
		echo '<script type=\'text/javascript\'>
			$(\'#checkAll\').click(function() {
			var elevs = $("#tables").find(\':checkbox\'); 
			   if(this.checked){ 
				  elevs.prop(\'checked\', true); 
			   }else{ 
				  elevs.prop(\'checked\', false);
			   }          
			});
		</script>';
		echo '<ul id="tables">';
		$result_tables = mysqli_query($bdd,"SHOW TABLE STATUS LIKE '".$prefix."_%'");
		$_SESSION['nb_tables'] = mysqli_num_rows($result_tables);
		if($result_tables && $result_tables->num_rows) {
			while($obj_table = mysqli_fetch_assoc($result_tables)) {
				echo '<input type="checkbox" id="'.$obj_table['Name'].'" name="'.$obj_table['Name'].'" checked /> <label for="'.$obj_table['Name'].'">'.$obj_table['Name'].'</label><br>';
			}
		}
		echo '</ul></div>';
		echo '<input class="bouton boutparam" type="submit" name="savetabs" value="'.PARAC_MAJ27.'" /></form>';
		echo '<h2>'.PARAC_MAJ28.'</h2>';
		if(isset($_POST['restaure'])) { include('./db/restaure.php'); }
		if($dossier = opendir('./db/backup/'.$prefix)) {
			$tab_save = array();
			echo '<ul>';
			$i=1;
			while(false !== ($fichier = readdir($dossier))) {
				if($fichier != '.' && $fichier != '..') {
					if(substr($fichier,22,3)=="par") {
						$typepar = PARAC_MAJ29;
					}
					else {
						$typepar = PARAC_MAJ30;
					}
					$line = '<li><a href="./db/backup/'.$prefix.'/' . $fichier . '" download>' . $fichier . '</a> (<a style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'divrestaure'.$i.'\');">'.PARAC_MAJ31.'</a>)</li>
						<div id="divrestaure'.$i.'" style="display:none;"><p class="signal maxwidth">'.PARAC_MAJ32.' '.substr($fichier,12,2).'/'.substr($fichier,10,2).'/'.substr($fichier,6,4).', '.TO.' '.substr($fichier,15,2).'h'.substr($fichier,17,2).'. '.$typepar.' '.PARAC_MAJ33.'</p><form name="restaure" method="POST" action=""><input type="hidden" value="'.$fichier.'" name="fichier'.$i.'" />'.PARAC_MAJ34.' '.$fichier.DOTS.' <input class="bouton boutparam" type="submit" name="restaure" value="'.VALID.'" /></form><br></div>';
					array_push ($tab_save,$line);
					$i++;
				}
			}
			arsort($tab_save);
			$count = count($tab_save);
			if($count==0){
				echo PARAC_MAJ35;
			}
			else {
				rsort($tab_save);
				for($i=0;$i<$count;$i++) {
					echo $tab_save[$i];
				}
			}
			echo '</ul>';
			closedir($dossier);
		}
		echo "<script language='javascript'>
			window.onmousedown = function (e) {
		    var el = e.target;
		    if (el.tagName.toLowerCase() == 'option' && el.parentNode.hasAttribute('multiple')) {
		        e.preventDefault();

		        if (el.hasAttribute('selected')) el.removeAttribute('selected');
		        else el.setAttribute('selected', '');

		        var select = el.parentNode.cloneNode(true);
		        el.parentNode.parentNode.replaceChild(select, el.parentNode);
		    }
		}
		</script>";
		echo '<h1>'.PARAC_MAJ36.'</h1>';
		if (!extension_loaded('zip')) {
			echo PARAC_MAJ37;
		}
		else {
			$docus = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqdocs ORDER BY docname ASC");
			if(mysqli_num_rows($docus)==0) {
				echo PARAC_MAJ38;
			}
			else {
				echo '<form method="POST" action="edit.php" name="zip"><select name="files[]" class="inptxt" id="savedocs" multiple size="'.(mysqli_num_rows($docus)+1).'">';
				foreach($docus as $docu) {
					$filename = 'seqfichiers/'.$prefix.'/'.$docu['docurl'];
					if(file_exists($filename)) {
						$taille = @number_format(filesize('seqfichiers/'.$prefix.'/'.$docu['docurl'])/1048,2);
						echo '<option value="'.$docu['docurl'].'" title="'.$docu['docname'].'" selected>';
						if($docu['docname']!='') { echo $docu['docname']; }
						else { echo $docu['docurl']; }
						echo ' - '.$taille.' kB</option>';	
					}
				}
				echo '<select><br><input type="submit" value="'.DOWNLOAD.'" name="zipval" class="bouton boutparam"></form>';
			}
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>
