function connectDB($hostDB, $userDB, $passwordDB) {
$link = mysql_connect("$hostDB", "$userDB", "$passwordDB")
or die("Impossible de se connecter : " . mysql_error());
//mysql_close($link);
}
/************************************************************************************
*
*
* Create Table USER
* Create Table SERVICE
* Create Table POWER
* Create Table SERVICEUSER
* Create Table CMDUSER
* Create Table CMDSERVICE
* Create Table SWITCH
* Create Table CMDSWITCH
* Create Table SERVICESWITCH
* CREATE Table DEVICE
*
*TODO: METTRE LES PRIMARY KEY
*************************************************************************************/
function createDatabase($hostDB, $nameDB, $userCreateDB, $passwdCreateDB, $userDB, $passwdDB, $adminApp, $passwdAdminApp)
{
//connect DB
$buffer = "";
$link = @mysql_connect($hostDB, $userCreateDB, $passwdCreateDB);
if (mysql_error()) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
//Create DB
$sql = "CREATE DATABASE IF NOT EXISTS $nameDB";
if (@mysql_query($sql, $link)) {
$buffer = 1 ;
} else {
$buffer = "Create database: [FAILED] :" . mysql_error() ;
return $buffer ;
}
//echo "step create user
";
//Création de l'utilisateur pour l'accès à la base.
$sql = "GRANT ALL PRIVILEGES ON $nameDB.* TO $userDB@localhost IDENTIFIED BY '$passwdDB' ";
if (@mysql_query($sql, $link)) {
$buffer = 1;
} else {
$buffer = "Create ACCESS DATABASE FROM $userDB [FAILED] :" . mysql_error() ;
return $buffer ;
}
mysql_close();
$link = @mysql_connect($hostDB, $userDB, $passwdDB);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}else{
$db_selected = @mysql_select_db($nameDB, $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
}
//Create Table USER
$sql = "CREATE TABLE IF NOT EXISTS USER (
idUser VARCHAR(50) NOT NULL,
passwdUser VARCHAR(100) NOT NULL,
PRIMARY KEY (idUser) );";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "Create Table USER: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
//Create Table SERVICE
$sql = "CREATE TABLE IF NOT EXISTS SERVICE (
idService VARCHAR(50) NOT NULL,
PRIMARY KEY (idService) );";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "Create Table SERVICE: [FAILED] : " . mysql_error();
}else{
$buffer = 1;
}
//Create Table SERVICEUSER
$sql = "CREATE TABLE IF NOT EXISTS SERVICEUSER (
idUser VARCHAR(50) NOT NULL,
idService VARCHAR(50) NOT NULL,
PRIMARY KEY (idUser, idService) );";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "CREATE TABLE SERVICEUSER: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
// Create Table POWER
/*$sql = "CREATE TABLE IF NOT EXISTS Power (
idPower VARCHAR(50) NOT NULL,
descPower VARCHAR(50) NOT NULL;)";*/
$sql = "CREATE TABLE IF NOT EXISTS POWER (
idPower VARCHAR(50) NOT NULL,
descPower VARCHAR(200),
PRIMARY KEY (idPower) );";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "Create Table POWER: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
//Create Table CMDUSER
$sql = "CREATE TABLE IF NOT EXISTS CMDUSER (
idPower VARCHAR(50) NOT NULL,
idUser VARCHAR(50) NOT NULL,
PRIMARY KEY (idPower, idUser));";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "Create Table CMDUSER: [FAILED] : " . mysql_error();
}else{
$buffer = 1;
}
//Create Table CMDSERVICE
$sql = "CREATE TABLE IF NOT EXISTS CMDSERVICE (
idPower VARCHAR(50) NOT NULL,
idService VARCHAR(50) NOT NULL,
PRIMARY KEY (idPower, idService))";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "Create Table CMDSERVICE: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
//Create Table SWITCH
$sql = "CREATE TABLE IF NOT EXISTS SWITCH (
idSwitch VARCHAR(50) NOT NULL,
location VARCHAR(200),
level INT(2),
portignore VARCHAR(50) ,
PRIMARY KEY (idSwitch)
);";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "Create Table SWITCH: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
//Create Table CMDSWITCH
$sql = "CREATE TABLE IF NOT EXISTS CMDSWITCH (
idPower VARCHAR(50) NOT NULL,
idSwitch VARCHAR(50) NOT NULL,
idUser VARCHAR(50) NOT NULL,
PRIMARY KEY (idPower, idSwitch, idUser)
);";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "Create Table CMDSWITCH: [FAILED] : " . mysql_error();
return $buffer;
}else{
$buffer = 1;
}
//Create Table SERVICESWITCH
$sql = "CREATE TABLE IF NOT EXISTS SERVICESWITCH (
idService VARCHAR(50) NOT NULL,
idSwitch VARCHAR(50) NOT NULL,
PRIMARY KEY (idService, idSwitch)
);";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "CREATE TABLE SERVICESWITCH: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
//CREATE Table RESEAU
$sql = "CREATE TABLE IF NOT EXISTS NETWORK (
idNetwork VARCHAR(50) NOT NULL,
nameNetwork VARCHAR(50) NOT NULL,
interface VARCHAR(8) NOT NULL,
mainRouter VARCHAR(50) NOT NULL,
PRIMARY KEY (idNetwork)
);";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "CREATE TABLE NETWORK: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
//CREATE Table DEVICE
$sql = "CREATE TABLE IF NOT EXISTS DEVICE (
mac VARCHAR(20) NOT NULL,
hostname VARCHAR(50) NOT NULL,
ip VARCHAR(50) NOT NULL,
type VARCHAR(20),
PRIMARY KEY (hostname)
);";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "CREATE TABLE DEVICE: [FAILED] : " . mysql_error();
return $buffer ;
}else{
$buffer = 1;
}
//init table POWER
$power = array("addUser",
"delUser",
"updateUser",
"addService",
"delService",
"updateService",
"addServiceUser",
"delServiceUser",
"addSwitch",
"delSwitch",
"updateSwitch",
"addServiceSwitch",
"delServiceSwitch",
"addRight",
"delRight",
"addNetwork",
"delNetwork",
"addDevice",
"delDevice",
"disablePort",
"enablePort",
"genFileKlaskConf",
"klaskupdatesw",
"klaskupdatedb",
"klaskexportdb",
"klaskgenmap",
"klasksearch" );
foreach( $power as $idPower) {
$sql = "INSERT INTO POWER VALUE('$idPower', '') ";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer = "INSERT POWER $idPower IN TABLE SERVICESWITCH: [FAILED] : " . mysql_error();
return $buffer;
}else{
$buffer = 1;
}
}
//init table user, insert adminApp account
$passwdAdminApp= md5($passwdAdminApp);
$sql= "INSERT INTO USER VALUE('$adminApp', '$passwdAdminApp');";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer .= "INSERT USER ADMIN $adminApp IN TABLE USER: [FAILED] : " . mysql_error();
return $buffer;
}else{
$buffer = 1;
}
//init table CMSUSER for adminApp account
foreach( $power as $idPower) {
$sql = "INSERT INTO CMDUSER VALUE('$idPower', '$adminApp') ";
@mysql_query($sql,$link);
if (mysql_error()){
$buffer .= "INSERT CMDUSER $idPower FOR USER $adminApp: [FAILED] : " . mysql_error();
return $buffer;
}else{
$buffer = 1;
}
}
return $buffer;
}
function createFileConfig($hostDB, $nameDB, $userDB, $passwdDB, $adminApp) {
//Create file config.php
$fileConfig="./conf/config.php";
$content = "\n";
$content .= "\$GLOBALS['hostDB']=\"$hostDB\";\n";
$content .= "\$GLOBALS['nameDB']=\"$nameDB\";\n";
$content .= "\$GLOBALS['userDB']=\"$userDB\";\n";
$content .= "\$GLOBALS['passwdDB']=\"$passwdDB\";\n";
$content .= "\$GLOBALS['auth']=\"classic\";\n";
$content .= "\$GLOBALS['adminApp']=\"$adminApp\";\n";
$content .= "?>";
if (!$handle=fopen($fileConfig, "w+")) {
$buffer = "Open file $fileConfig: [FAILED]";
return $buffer;
}
if (!fwrite($handle, $content)) {
$buffer = "Write in file $fileConfig: [FAILED]";
return $buffer;
}
return 1;
}
function displaySaisieDB()
{
$buffer = "