<?php

	// http://192.168.1.70/create_one_site_target_execute_script.php
		
	//--------------------------------------------------------------------------------------------------------
	$code_erreur	= 0;

	$Flag_debug_1 = 1 ;
	$Flag_debug_1 = 0 ;
	
	$MySQL_connected = false;
	
	$p_NEW_NAME	= isset($_POST["NEW_SITE_NAME"])	? $_POST["NEW_SITE_NAME"]	: "SANS_NOM";
	$p_NEW_SITE	= isset($_POST["NEW_SITE_ID"])		? $_POST["NEW_SITE_ID"]		: "00000_000";
	$p_NEW_LON		= isset($_POST["NEW_SITE_LON"])	? $_POST["NEW_SITE_LON"]	: "0.000000";
	$p_NEW_LAT		= isset($_POST["NEW_SITE_LAT"])	? $_POST["NEW_SITE_LAT"]	: "0.000000";

	$message_1 	= "" ;

	//--------------------------------------------------------------------------------------------------------
	// TESTS
	//--------------------------------------------------------------------------------------------------------
	if ($Flag_debug_1 == 1) {
		echo("nouveau site : " . $p_NEW_SITE . "</br>") ;
	}

	if ( $code_erreur == 0 ) {
		if ( ( $p_NEW_SITE == "00000_000" ) or ( $p_NEW_SITE == "" ) ) {
			$message_1 = "<font color='Salmon'>saisir un identifiant pour le site à créer !!!</font></br>" ;
			$code_erreur = 101 ;
		}
	}

	if ( $code_erreur == 0 ) {
		if ( $p_NEW_NAME == "" ) {
			$message_1 = "<font color='Salmon'>saisir un nom pour le site à créer !!!</font></br>" ;
			$code_erreur = 102 ;
		}
	}

	//--------------------------------------------------------------------------------------------------------
	// TESTS
	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {

		if ( ! is_numeric($p_NEW_LON) ) {
			$code_erreur = 201 ;	
		}
		else {
			if ( abs(floatval($p_NEW_LON)) > 180.0 ) {
				$code_erreur = 202 ;	
			}
			else {
				$lon_A = str_replace("-", "", $p_NEW_LON) ;
				$lon_B = explode(".", $lon_A) ;

				if ( sizeof($lon_B) != 2 ) {
					$code_erreur = 203 ;
				}
				else {
					if ( strlen($lon_B[1]) != 6 ) {
						$code_erreur = 204 ;
					}
				}
				
			}
			
		}
		
	}

	if ( $code_erreur == 0 ) {

		if ( ! is_numeric($p_NEW_LAT) ) {
			$code_erreur = 301 ;	
		}
		else {
			if ( abs(floatval($p_NEW_LAT)) > 90.0 ) {
				$code_erreur = 302 ;	
			}
			else {
				$lat_A = str_replace("-", "", $p_NEW_LAT) ;
				$lat_B = explode(".", $lat_A) ;

				if ( sizeof($lat_B) != 2 ) {
					$code_erreur = 303 ;
				}
				else {
					if ( strlen($lat_B[1]) != 6 ) {
						$code_erreur = 304 ;
					}
				}
				
			}
			
		}
		
	}

	if ( $code_erreur != 0 ) {
		if ( $code_erreur >= 300 ) {
			$message_1 = "- latitude = " . $p_NEW_LAT . " : valeur et/ou format incorrect(s) !!!</br>" ;
		}
		else {
			$message_1 = "- longitude = " . $p_NEW_LON . " : valeur et/ou format incorrect(s) !!!</br>" ;
		}
	}

	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		
		include("../_identifiants/sql_identification_main.php");

		$link_AQCM05 = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
		
		if ( $link_AQCM05 ) {

			$MySQL_connected = true ;
			
		}
		else {
			
			$code_erreur = 401 ;
			$message_1 = "- connexion à la base de donnée MySQL : <font color='Salmon'>échec !!!</font></br>";
			
		}

	}

	//--------------------------------------------------------------------------------------------------------
	// test d'existence du site : 
	// 1 - recherche des sites répertoriés
	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		
		$NB_sites = 0 ;

		$query_sites_list = "SELECT SITES_NAMES, SITES_ID, SITES_STATUS, SITES_T_MAX, SITES_NB_MAX FROM AQCM05_LIST_SITES ORDER BY SITES_ID";

		$result_sites_list = mysqli_query($link_AQCM05, $query_sites_list); 
		
		if (!$result_sites_list) {
			$code_erreur = 11;
		} 
		else {
			
			if ( mysqli_num_rows($result_sites_list) != 0 ) {	
				
				$i=0;
				while ($row = mysqli_fetch_array($result_sites_list, MYSQLI_BOTH)) {

					$aqcm_SITES_ID[$i]		  	= $row["SITES_ID"];
					$aqcm_SITES_NAMES[$i]	  	= $row["SITES_NAMES"];
					$aqcm_SITES_STATUS[$i]		= $row["SITES_STATUS"];
					$aqcm_SITES_T_MAX[$i]		= $row["SITES_T_MAX"];
					$aqcm_SITES_NB_MAX[$i]		= $row["SITES_NB_MAX"];
					
					$i++ ;
				}
				
				$NB_sites = $i;
				
				if ($Flag_debug_1 == 1) {
					
					echo("NB_sites = " . $NB_sites . "</br>") ;
					
					for ($j = 0; $j < $NB_sites; $j++) {
						echo($aqcm_SITES_ID[$j] . "</br>") ;
					}
					
				}
				
			}
			
		}

	}


	//--------------------------------------------------------------------------------------------------------
	// test d'existence du site
	// 2 - le nouveau site est-il déjà répertorié ?
	//--------------------------------------------------------------------------------------------------------
	if ( ( $code_erreur == 0 ) and ( $NB_sites != 0 ) ) {
		
		$k = 0 ;
		
		for ($j = 0; $j < $NB_sites; $j++) {
			
			if ( $aqcm_SITES_ID[$j] == $p_NEW_SITE ) {
			
				$k++;
				
			}
		}
		
		if ( $k != 0 ) {
			$code_erreur = 501 ;
			$message_1 = "- <font color='Salmon'>le site «&nbsp;" . $p_NEW_NAME . "&nbsp;» existe déjà !!!</font></br>";
		}

	}

	//--------------------------------------------------------------------------------------------------------
	// test d'existence de la base associée au nouveau site
	//--------------------------------------------------------------------------------------------------------
	$new_data_base = "AQCM05_SITE_" . $p_NEW_SITE ;

	if ( $code_erreur == 0 ) {
		
		if ( mysqli_select_db($link_AQCM05, $new_data_base ) ) {
			
			$code_erreur = 601;
			$message_1 = "- <font color='Salmon'>la base existe déjà !!!</font></br>";

		}
		
	}
	
	//--------------------------------------------------------------------------------------------------------
	// création du site
	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		
		$query = "CREATE DATABASE IF NOT EXISTS `" . $new_data_base . "` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */;" ;
		$result = mysqli_query($link_AQCM05, $query);
		if ( ! $result )  { $code_erreur = 701; }
		
	}		

	if ( $code_erreur == 0 ) {
		
		if ( !mysqli_select_db($link_AQCM05, $new_data_base ) ) { $code_erreur = 702; }

	}
	
	if ( $code_erreur == 0 ) {
		
		$query = "CREATE TABLE `AQCM05_SITE_USERS` (
			`idx` 						int(11) 		NOT NULL AUTO_INCREMENT,
			`USER_ID` 				varchar(64) 	NOT NULL DEFAULT 'default@gmail.com',
			`USER_PW` 				varchar(80) 	NOT NULL DEFAULT '12345678',
			`USER_STATUS` 			varchar(3) 	NOT NULL DEFAULT 'ON',
			`USER_LEVEL` 				tinyint(4) 	NOT NULL DEFAULT 1,
			`USER_T_MAX` 				varchar(12) 	NOT NULL DEFAULT '1999999999',
			`USER_WEEK_MAIL` 		varchar(3) 	NOT NULL DEFAULT 'OFF',
			`USER_2FA_STATUS`		varchar(3) 	NOT NULL DEFAULT 'OFF',
			`USER_2FA_PHONE` 		varchar(12) 	NOT NULL DEFAULT '+33123456789',
			`USER_2FAS_STATUS`		varchar(3) 	NOT NULL DEFAULT 'OFF',
			`USER_2FAS_SECRET`		varchar(16) 	NOT NULL DEFAULT '1234567890ABCDEF',
			`USER_NEW_MAIL` 			varchar(64) 	NOT NULL DEFAULT 'default@gmail.com',
			`USER_NEW_MAIL_CODE` 	varchar(8) 	NOT NULL DEFAULT '123456',
			`USER_NEW_MAIL_T_MAX` 	varchar(12) 	NOT NULL DEFAULT '0999999999',
			`USER_RX_WARNING_EN` 	varchar(3) 	NOT NULL DEFAULT 'OFF',
			PRIMARY KEY (`idx`),
			UNIQUE KEY `idx_2` (`idx`),
			KEY `idx` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result)  { $code_erreur = 703; }

	}
	
	if ( $code_erreur == 0 ) {
		
		$query = "
			CREATE TABLE `AQCM05_SONDES_AUTOREG` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_AUTOREG_MAC` varchar(17) NOT NULL DEFAULT '00_00_00_00_00_00',
			  `aqcm_AUTOREG_HASH_PWD` varchar(80) NOT NULL DEFAULT '1234',
			  `aqcm_AUTOREG_NAME` varchar(20) NOT NULL DEFAULT 'NONAME',
			  `aqcm_AUTOREG_QNH` varchar(6) NOT NULL DEFAULT '0000',
			  `aqcm_AUTOREG_BAT` varchar(2) NOT NULL DEFAULT '1',
			  `aqcm_AUTOREG_NIVEAU` varchar(2) NOT NULL DEFAULT '0',
			  `aqcm_AUTOREG_HW` varchar(2) NOT NULL DEFAULT '02',
			  `aqcm_AUTOREG_SSID` varchar(32) NOT NULL DEFAULT 'Target_WiFi_SSID',
			  `aqcm_AUTOREG_PSK` varchar(64) NOT NULL DEFAULT 'Target_WiFi_PSK',
			  `aqcm_AUTOREG_URL` varchar(120) NOT NULL DEFAULT 'https://www.aqcm.fr/v2/M5_2/autoreg.php',
			  `aqcm_AUTOREG_URL_TEST` varchar(120) NOT NULL DEFAULT 'https://www.aqcm.fr/v2/M5_2/test_connexion.php',
			  `aqcm_AUTOREG_URL_SVR` varchar(120) NOT NULL DEFAULT 'https://www.aqcm.fr/v2/',
			PRIMARY KEY (`idx`),
			UNIQUE KEY `idx_2` (`idx`),
			KEY `idx` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
		
		" ;

		$result = mysqli_query($link_AQCM05, $query);
		if ( !$result ) { $code_erreur = 704; } 
		
		$query = "
			INSERT INTO `AQCM05_SONDES_AUTOREG` (`idx`, `aqcm_AUTOREG_MAC`, `aqcm_AUTOREG_HASH_PWD`, `aqcm_AUTOREG_NAME`, `aqcm_AUTOREG_QNH`, `aqcm_AUTOREG_BAT`, `aqcm_AUTOREG_NIVEAU`, `aqcm_AUTOREG_HW`, `aqcm_AUTOREG_SSID`, `aqcm_AUTOREG_PSK`, `aqcm_AUTOREG_URL`, `aqcm_AUTOREG_URL_TEST`, `aqcm_AUTOREG_URL_SVR`) VALUES
			(1,	'00_00_00_00_00_00',	'ne pas modifier cette ligne !!!',	'NONAME',	'0000',	'1',	'0',	'02',	'Target_WiFi_SSID',	'Target_WiFi_PSK',	'https://www.aqcm.fr/v2/M5_2/autoreg.php',	'https://www.aqcm.fr/v2/M5_2/test_connexion.php',	'https://www.aqcm.fr/v2/');
		";

		$result = mysqli_query($link_AQCM05, $query);
		if ( !$result ) { $code_erreur = 705; } 


	}
		
	if ( $code_erreur == 0 ) {
		
		$query = "
			CREATE TABLE `AQCM05_SONDES_FIRMWARE` (
			`idx` mediumint(9) NOT NULL AUTO_INCREMENT,
			`aqcm_MAC` varchar(17) NOT NULL DEFAULT '00_00_00_00_00_00',
			`aqcm_NAME` varchar(20) NOT NULL DEFAULT 'NONAME',
			`aqcm_IP_SONDE` varchar(15) NOT NULL DEFAULT '---.---.---.---',
			`aqcm_FW_FILENAME` varchar(64) NOT NULL DEFAULT 'AQCM05_firmware_OK.zip',
			`aqcm_FW_UPDATE_FF` varchar(4) NOT NULL DEFAULT 'Done',
			`aqcm_BAT` varchar(2) NOT NULL DEFAULT 'B1',
			`aqcm_NIVEAU` varchar(2) NOT NULL DEFAULT 'N0',
			`aqcm_FIRMWARE_PWD` varchar(80) NOT NULL DEFAULT '87654321',
			`aqcm_SITE_ID` varchar(16) NOT NULL DEFAULT '00000_000',
			`aqcm_TYPE` varchar(20) NOT NULL DEFAULT 'AQCM05',
			`aqcm_MAIL` varchar(64) NOT NULL DEFAULT 'unknown@gmail.com',
			`aqcm_HW` varchar(6) NOT NULL DEFAULT 'HW_03',
			`aqcm_T_MAX` varchar(12) NOT NULL DEFAULT '1999999999',
			PRIMARY KEY (`idx`),
			UNIQUE KEY `idx_2` (`idx`),
			KEY `idx` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query);
		if ( !$result ) { $code_erreur = 706; } 
		
	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_SONDES_MESURES` (
			  `idx` mediumint(9) NOT NULL AUTO_INCREMENT,
			  `aqcm_MAC` varchar(17) NOT NULL DEFAULT '00_00_00_00_00_00',
			  `aqcm_NAME` varchar(20) NOT NULL DEFAULT 'NONAME',
			  `aqcm_IP_SONDE` varchar(15) NOT NULL DEFAULT '---.---.---.---',
			  `aqcm_MESURES` varchar(16) NOT NULL DEFAULT 'YYYNNNNNNNN',
			  `aqcm_BAT` varchar(2) NOT NULL DEFAULT 'B1',
			  `aqcm_NIVEAU` varchar(2) NOT NULL DEFAULT 'N0',
			  `aqcm_QNH` varchar(6) NOT NULL DEFAULT '0000',
			  `aqcm_PWD` varchar(80) NOT NULL DEFAULT '12345678',
			  `aqcm_SERVER_PWD` varchar(80) NOT NULL DEFAULT '12345678',
			  `aqcm_SITE_ID` varchar(16) NOT NULL DEFAULT '00000_000',
			  `aqcm_TYPE` varchar(20) NOT NULL DEFAULT 'AQCM05',
			  `aqcm_MAIL` varchar(64) NOT NULL DEFAULT 'unknown@gmail.com',
			  `aqcm_HW` varchar(2) NOT NULL DEFAULT '00',
			  `aqcm_T_MAX` varchar(12) NOT NULL DEFAULT '1999999999',
			  `aqcm_SPL_TRIG_MODE` varchar(12) NOT NULL DEFAULT 'SPL_ONLY',
			  `aqcm_SPL_TRIG_MOY` varchar(6) NOT NULL DEFAULT '35.0',
			  `aqcm_SPL_TRIG_MAX` varchar(6) NOT NULL DEFAULT '55.0',
			  `aqcm_SCHEDULER` varchar(1008) NOT NULL DEFAULT '000000000000000000000000000000000000000000000000111111111111111111111111000000000111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111000000000111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111000000000111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111000000000111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111000000000111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
			  `aqcm_CO2_TRIG_LOW` varchar(6) NOT NULL DEFAULT '800',
			  `aqcm_CO2_TRIG_HIGH` varchar(6) NOT NULL DEFAULT '1500',
			  `aqcm_RX_WARNING_EN` varchar(3) NOT NULL DEFAULT 'OFF',
			  `aqcm_RX_MESURES` varchar(3) NOT NULL DEFAULT '000',
			  `aqcm_RX_STATUS` varchar(3) NOT NULL DEFAULT 'OFF',
			  `aqcm_RX_ALERT` varchar(3) NOT NULL DEFAULT 'OFF',
			  `aqcm_SHELLY_EN` varchar(3) NOT NULL DEFAULT 'OFF',
			  `aqcm_SHELLY_SERVER` varchar(64) NOT NULL DEFAULT 'https://shelly-xx-eu.shelly.cloud',
			  `aqcm_SHELLY_KEY` varchar(100) NOT NULL DEFAULT '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
			  `aqcm_SHELLY_ID` varchar(12) NOT NULL DEFAULT '1a2b3c4d5e6f',
			  `aqcm_SHELLY_POWER_ON` varchar(3) NOT NULL DEFAULT 'NON',
			  `aqcm_SHELLY_POWER_OFF` varchar(3) NOT NULL DEFAULT 'NON',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx_2` (`idx`),
			  KEY `idx` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;


		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 707; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_SONDES_WG` (
				  `idx` mediumint(9) NOT NULL AUTO_INCREMENT,
				  `aqcm_WG_NAME` varchar(20) NOT NULL DEFAULT 'NONAME',
				  `aqcm_WG_Peer_AllowedIPs` varchar(18) NOT NULL DEFAULT '---.---.---.---/--',
				  `aqcm_WG_MAC` varchar(17) NOT NULL DEFAULT '00_00_00_00_00_00',
				  `aqcm_WG_IP_LAN` varchar(15) NOT NULL DEFAULT '---.---.---.---',
				  `aqcm_WG_BAT` varchar(2) NOT NULL DEFAULT 'B1',
				  `aqcm_WG_NIVEAU` varchar(2) NOT NULL DEFAULT 'N0',
				  `aqcm_WG_HW` varchar(2) NOT NULL DEFAULT '00',
				  `aqcm_WG_MAIL` varchar(64) NOT NULL DEFAULT 'unknown@gmail.com',
				  `aqcm_WG_PW` varchar(80) NOT NULL DEFAULT '1234',
				  `aqcm_WG_Peer_PublicKey` varchar(50) NOT NULL DEFAULT '123456abcdef123456abcdef123456abcdef123456abcdef',
				  `aqcm_WG_T_MAX` varchar(12) NOT NULL DEFAULT '1999999999',
				  `aqcm_WG_STATUS` varchar(3) NOT NULL DEFAULT 'ON',
				  `aqcm_WG_SITE_ID` varchar(16) NOT NULL DEFAULT '00000_000',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx_2` (`idx`),
			  KEY `idx` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 708; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_CO2` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_CO2_TRIG_0` varchar(6) NOT NULL DEFAULT '700',
			  `aqcm_CO2_TRIG_1` varchar(6) NOT NULL DEFAULT '1000',
			  `aqcm_CO2_TRIG_2` varchar(6) NOT NULL DEFAULT '1300',
			  `aqcm_CO2_TRIG_3` varchar(6) NOT NULL DEFAULT '1700',
			  `aqcm_CO2_TRIG_4` varchar(6) NOT NULL DEFAULT '2000',
			  `aqcm_CO2_TRIG_5` varchar(6) NOT NULL DEFAULT '3000',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 709; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_CO2` (`idx`, `aqcm_CO2_TRIG_0`, `aqcm_CO2_TRIG_1`, `aqcm_CO2_TRIG_2`, `aqcm_CO2_TRIG_3`, `aqcm_CO2_TRIG_4`, `aqcm_CO2_TRIG_5`) VALUES
			(1,	'600',	'800',	'1000',	'1300',	'1700',	'2200');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 710; } 

	}

	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_COV` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_COV_TRIG_0` varchar(6) NOT NULL DEFAULT '50',
			  `aqcm_COV_TRIG_1` varchar(6) NOT NULL DEFAULT '100',
			  `aqcm_COV_TRIG_2` varchar(6) NOT NULL DEFAULT '150',
			  `aqcm_COV_TRIG_3` varchar(6) NOT NULL DEFAULT '200',
			  `aqcm_COV_TRIG_4` varchar(6) NOT NULL DEFAULT '300',
			  `aqcm_COV_TRIG_5` varchar(6) NOT NULL DEFAULT '400',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 711; } 

	}

	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_COV` (`idx`, `aqcm_COV_TRIG_0`, `aqcm_COV_TRIG_1`, `aqcm_COV_TRIG_2`, `aqcm_COV_TRIG_3`, `aqcm_COV_TRIG_4`, `aqcm_COV_TRIG_5`) VALUES
			(1,	'100',	'180',	'260',	'340',	'400',	'460');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 712; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_HCHO` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_HCHO_TRIG_0` varchar(6) NOT NULL DEFAULT '10',
			  `aqcm_HCHO_TRIG_1` varchar(6) NOT NULL DEFAULT '30',
			  `aqcm_HCHO_TRIG_2` varchar(6) NOT NULL DEFAULT '50',
			  `aqcm_HCHO_TRIG_3` varchar(6) NOT NULL DEFAULT '100',
			  `aqcm_HCHO_TRIG_4` varchar(6) NOT NULL DEFAULT '200',
			  `aqcm_HCHO_TRIG_5` varchar(6) NOT NULL DEFAULT '300',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 713; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_HCHO` (`idx`, `aqcm_HCHO_TRIG_0`, `aqcm_HCHO_TRIG_1`, `aqcm_HCHO_TRIG_2`, `aqcm_HCHO_TRIG_3`, `aqcm_HCHO_TRIG_4`, `aqcm_HCHO_TRIG_5`) VALUES
			(1,	'10',	'25',	'50',	'100',	'200',	'500');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 714; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_HR` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_HR_TRIG_0` varchar(6) NOT NULL DEFAULT '20.0',
			  `aqcm_HR_TRIG_1` varchar(6) NOT NULL DEFAULT '30.0',
			  `aqcm_HR_TRIG_2` varchar(6) NOT NULL DEFAULT '40.0',
			  `aqcm_HR_TRIG_3` varchar(6) NOT NULL DEFAULT '60.0',
			  `aqcm_HR_TRIG_4` varchar(6) NOT NULL DEFAULT '70.0',
			  `aqcm_HR_TRIG_5` varchar(6) NOT NULL DEFAULT '80.0',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 715; } 

	}

	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_HR` (`idx`, `aqcm_HR_TRIG_0`, `aqcm_HR_TRIG_1`, `aqcm_HR_TRIG_2`, `aqcm_HR_TRIG_3`, `aqcm_HR_TRIG_4`, `aqcm_HR_TRIG_5`) VALUES
			(1,	'20.0',	'30.0',	'40.0',	'60.0',	'70.0',	'80.0');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 716; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_LUX` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_LUX_TRIG_0` varchar(6) COLLATE utf8_unicode_ci NOT NULL DEFAULT '100',
			  `aqcm_LUX_TRIG_1` varchar(6) COLLATE utf8_unicode_ci NOT NULL DEFAULT '200',
			  `aqcm_LUX_TRIG_2` varchar(6) COLLATE utf8_unicode_ci NOT NULL DEFAULT '500',
			  `aqcm_LUX_TRIG_3` varchar(6) COLLATE utf8_unicode_ci NOT NULL DEFAULT '1000',
			  `aqcm_LUX_TRIG_4` varchar(6) COLLATE utf8_unicode_ci NOT NULL DEFAULT '2000',
			  `aqcm_LUX_TRIG_5` varchar(6) COLLATE utf8_unicode_ci NOT NULL DEFAULT '5000',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 717; } 

	}

	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_LUX` (`idx`, `aqcm_LUX_TRIG_0`, `aqcm_LUX_TRIG_1`, `aqcm_LUX_TRIG_2`, `aqcm_LUX_TRIG_3`, `aqcm_LUX_TRIG_4`, `aqcm_LUX_TRIG_5`) VALUES (1,	'50',	'100',	'200',	'500',	'1000',	'2000');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 718; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_NOX` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_NOX_TRIG_0` varchar(6) NOT NULL DEFAULT '50',
			  `aqcm_NOX_TRIG_1` varchar(6) NOT NULL DEFAULT '100',
			  `aqcm_NOX_TRIG_2` varchar(6) NOT NULL DEFAULT '150',
			  `aqcm_NOX_TRIG_3` varchar(6) NOT NULL DEFAULT '200',
			  `aqcm_NOX_TRIG_4` varchar(6) NOT NULL DEFAULT '300',
			  `aqcm_NOX_TRIG_5` varchar(6) NOT NULL DEFAULT '400',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 719; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_NOX` (`idx`, `aqcm_NOX_TRIG_0`, `aqcm_NOX_TRIG_1`, `aqcm_NOX_TRIG_2`, `aqcm_NOX_TRIG_3`, `aqcm_NOX_TRIG_4`, `aqcm_NOX_TRIG_5`) VALUES
			(1,	'10',	'20',	'30',	'40',	'50',	'60');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 720; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_PM25` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_PM25_TRIG_0` varchar(6) NOT NULL DEFAULT '10',
			  `aqcm_PM25_TRIG_1` varchar(6) NOT NULL DEFAULT '30',
			  `aqcm_PM25_TRIG_2` varchar(6) NOT NULL DEFAULT '50',
			  `aqcm_PM25_TRIG_3` varchar(6) NOT NULL DEFAULT '100',
			  `aqcm_PM25_TRIG_4` varchar(6) NOT NULL DEFAULT '200',
			  `aqcm_PM25_TRIG_5` varchar(6) NOT NULL DEFAULT '300',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 721; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_PM25` (`idx`, `aqcm_PM25_TRIG_0`, `aqcm_PM25_TRIG_1`, `aqcm_PM25_TRIG_2`, `aqcm_PM25_TRIG_3`, `aqcm_PM25_TRIG_4`, `aqcm_PM25_TRIG_5`) VALUES
			(1,	'10',	'25',	'50',	'100',	'200',	'500');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 722; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_PM10` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_PM10_TRIG_0` varchar(6) NOT NULL DEFAULT '10',
			  `aqcm_PM10_TRIG_1` varchar(6) NOT NULL DEFAULT '30',
			  `aqcm_PM10_TRIG_2` varchar(6) NOT NULL DEFAULT '50',
			  `aqcm_PM10_TRIG_3` varchar(6) NOT NULL DEFAULT '100',
			  `aqcm_PM10_TRIG_4` varchar(6) NOT NULL DEFAULT '200',
			  `aqcm_PM10_TRIG_5` varchar(6) NOT NULL DEFAULT '300',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 723; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_PM10` (`idx`, `aqcm_PM10_TRIG_0`, `aqcm_PM10_TRIG_1`, `aqcm_PM10_TRIG_2`, `aqcm_PM10_TRIG_3`, `aqcm_PM10_TRIG_4`, `aqcm_PM10_TRIG_5`) VALUES
			(1,	'10',	'25',	'50',	'100',	'200',	'500');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 724; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_SPL` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_SPL_TRIG_0` varchar(6) NOT NULL DEFAULT '35',
			  `aqcm_SPL_TRIG_1` varchar(6) NOT NULL DEFAULT '65',
			  `aqcm_SPL_TRIG_2` varchar(6) NOT NULL DEFAULT '75',
			  `aqcm_SPL_TRIG_3` varchar(6) NOT NULL DEFAULT '90',
			  `aqcm_SPL_TRIG_4` varchar(6) NOT NULL DEFAULT '105',
			  `aqcm_SPL_TRIG_5` varchar(6) NOT NULL DEFAULT '120',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 725; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_SPL` (`idx`, `aqcm_SPL_TRIG_0`, `aqcm_SPL_TRIG_1`, `aqcm_SPL_TRIG_2`, `aqcm_SPL_TRIG_3`, `aqcm_SPL_TRIG_4`, `aqcm_SPL_TRIG_5` ) VALUES
			( 1,	'35',	'65',	'75',	'90',	'105',	'120' );
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 726; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			CREATE TABLE `AQCM05_TRIG_T` (
			  `idx` int(11) NOT NULL AUTO_INCREMENT,
			  `aqcm_T_TRIG_0` varchar(6) NOT NULL DEFAULT '15.0',
			  `aqcm_T_TRIG_1` varchar(6) NOT NULL DEFAULT '17.0',
			  `aqcm_T_TRIG_2` varchar(6) NOT NULL DEFAULT '19.0',
			  `aqcm_T_TRIG_3` varchar(6) NOT NULL DEFAULT '23.0',
			  `aqcm_T_TRIG_4` varchar(6) NOT NULL DEFAULT '25.0',
			  `aqcm_T_TRIG_5` varchar(6) NOT NULL DEFAULT '27.0',
			  PRIMARY KEY (`idx`),
			  UNIQUE KEY `idx` (`idx`),
			  KEY `idx_2` (`idx`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 727; } 

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_TRIG_T` (`idx`, `aqcm_T_TRIG_0`, `aqcm_T_TRIG_1`, `aqcm_T_TRIG_2`, `aqcm_T_TRIG_3`, `aqcm_T_TRIG_4`, `aqcm_T_TRIG_5`) VALUES
			(1,	'15.0',	'17.0',	'19.0',	'22.0',	'24.0',	'26.0');
			" ;

		$result = mysqli_query($link_AQCM05, $query); 
		if (!$result) { $code_erreur = 728; } 

	}

	if ( $code_erreur == 0 ) {

		if ( ! mysqli_select_db($link_AQCM05, "AQCM05_main" ) ) { $code_erreur = 729; }

	}
	
	if ( $code_erreur == 0 ) {

		$query = "
			INSERT INTO `AQCM05_LIST_SITES` (SITES_ID, SITES_NAMES, SITES_STATUS, SITES_T_MAX, SITES_NB_MAX, SITES_LON, SITES_LAT) 
			VALUES ('" . $p_NEW_SITE . "', '" . $p_NEW_NAME . "', 'ON', '1999999999', '9', '" . $p_NEW_LON . "', '" . $p_NEW_LAT . "');
			" ;
			
		$result = mysqli_query($link_AQCM05, $query); 
		if ( ! $result ) { $code_erreur = 730; } 

	}
	
	if ( $code_erreur > 700 ) {
		$message_1 = "- création des tables MySQL : <font color='Salmon'>échec !!!</font></br>";
	}

	if ( $code_erreur == 0 ) {
		$message_2 	= "- création du site : «&nbsp;" . $p_NEW_NAME . "&nbsp;» : <font color='Lime'>succès...</font><br>" ;
	}
	else {
		$message_2 	= "- création du site : «&nbsp;" . $p_NEW_NAME . "&nbsp;» : <font color='Salmon'>échec !!!</font><br>" ;
	}


	//--------------------------------------------------------------------------------------------------------
	// 
	//--------------------------------------------------------------------------------------------------------
	if ( $MySQL_connected ) {
		mysqli_close($link_AQCM05);
	}

	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		log_string( "AQCM05", "NOTICE ---> " . "création du site [ " . $p_NEW_SITE . " ] par : " . $_SESSION['login'] . " : succès..." ) ;
	} else {
		log_string( "AQCM05", "NOTICE ---> " . "création du site [ " . $p_NEW_SITE . " ] par : " . $_SESSION['login'] . " : échec !!!" ) ;
	}

	//--------------------------------------------------------------------------------------------------------

?>
