require('./conf/config.php');
/****************************************************************************
*
* addSwitch($idSwitch, $ip, $mac, $type, $location, $level, $portIgnore)
* selectSwitch($idSwitch)
* delSwitch($idSwicth)
* upateSwitch($idSwitch, $ip, $mac, $type, $location, $level, $portIgnore)
* addSwitchService($idSwitch, $idService)
* selectSwitchService($idSwitch, $idService)
* delSwitchService($idSwitch, $idService)
*
*****************************************************************************/
//TODO: prévoir la syntaxe pour le champs portIgnore
//TODO: prévoir l'ajout de routeur
function addSwitch($idSwitch, $location, $level, $portIgnore) {
//connect DB
$link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
$db_selected = @mysql_select_db($GLOBALS['nameDB'], $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
//Add switch
$sql = "INSERT INTO SWITCH VALUE('$idSwitch', '$location', '$level', '$portIgnore');";
if (mysql_query($sql, $link)) {
$buffer = 1;
} else {
$buffer = "CREATE SWITCH $idSwicth: [FAILED] :" . mysql_error() ;
}
return $buffer;
}
function selectSwitch($idSwitch) {
//connect DB
$link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
$db_selected = @mysql_select_db($GLOBALS['nameDB'], $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
if (empty($idSwitch)) {
$sql = "SELECT * FROM SWITCH, DEVICE WHERE SWITCH.idSwitch = DEVICE.hostname";
}else{
$sql = "SELECT * FROM SWITCH, DEVICE WHERE SWITCH.idSwitch = DEVICE.hostname AND idSwitch='$idSwitch'; ";
}
if ($result = mysql_query($sql, $link)) {
//test if $return is_array
return $result ;
} else {
$buffer = "SELECT SWITCH $idSwitch: [FAILED] :" . mysql_error() ;
return $buffer ;
}
}
function delSwitch($idSwitch) {
$buffer = "";
//connect DB
$link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
$db_selected = @mysql_select_db($GLOBALS['nameDB'], $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
//Before del user of Table USER, del user in all others Table
//del switch from CMDSWITCH
$sql="DELETE FROM CMDSWITCH WHERE idSwitch='$idSwitch';";
if (mysql_query($sql, $link)) {
$buffer .= "DELETE SWITCH $idSwitch FROM CMDSWITCH: [OK]
";
} else {
$buffer = "DELETE SWITCH $idSwitch FROM CMDSWITCH: [FAILED] :" . mysql_error() ;
return $buffer;
}
//del switch from SERVICESWITCH
$sql = "DELETE FROM SERVICESWITCH WHERE idSwitch='$idSwitch'";
if (mysql_query($sql, $link)) {
$buffer .= "DELETE SWITCH $idSwitch FROM SERVICESWITCH: [OK]
";
} else {
$buffer = "DELETE SWITCH $idSwitch FROM SERVICESWITCH: [FAILED] :" . mysql_error() ;
return $buffer;
}
//del switch
$sql = "DELETE FROM SWITCH WHERE idswitch='$idSwitch';";
if (mysql_query($sql, $link)) {
$buffer .= "DELETE SWITCH $idSwitch: [OK]
";
} else {
$buffer = "DELETE SWITCH $idSwitch: [FAILED] :" . mysql_error() ;
return $buffer;
}
return $buffer;
}
function updateSwitch($idSwitch, $location, $level, $portIgnore ) {
//connect DB
$link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
$db_selected = @mysql_select_db($GLOBALS['nameDB'], $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
//update switch
$sql = "UPDATE SWITCH SET
location='$location',
level='$level',
portIgnore='$portIgnore'
WHERE idSwitch='$idSwitch' ;" ;
if (mysql_query($sql, $link)) {
$buffer = 1;
} else {
$buffer = "UPDATE SWITCH $idSwicth: [FAILED] :" . mysql_error() ;
}
return $buffer;
}
function addSwitchService($idSwitch, $idService) {
//connect DB
$link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
$db_selected = @mysql_select_db($GLOBALS['nameDB'], $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
//Add switch for a service
$sql = "INSERT INTO SERVICESWITCH VALUE('$idService', '$idSwitch');";
if (mysql_query($sql, $link)) {
$buffer = 1 ;
}else{
$buffer = "CREATE SERVICESWITCH $idService: [FAILED] :" . mysql_error() ;
}
return $buffer;
}
function selectSwitchService($idSwitch, $idService) {
//connect DB
$link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
$db_selected = @mysql_select_db($GLOBALS['nameDB'], $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
if ( empty($idSwitch) && empty($idService)) {
$sql = "SELECT * FROM SERVICESWITCH";
}else{
if ( !empty($idSwitch) && !empty($idService) ) {
$sql = "SELECT * FROM SERVICESWITCH
WHERE idSwitch='$idSwitch'
AND idService='$idService';";
}else{
if ( !empty($idSwitch) && empty($idService) ) {
$sql = "SELECT * FROM SERVICESWITCH
WHERE idSwitch='$idSwitch';";
}else{
$sql = "SELECT * FROM SERVICESWITCH
WHERE idService='$idService';";
}
}
}
if ($result = mysql_query($sql, $link)) {
return $result ;
}else{
$buffer = "SELECT SWITCH $idSwitch for SERVICE $idService : [FAILED] :" . mysql_error() ;
}
}
function delSwitchService($idSwitch, $idService) {
//connect DB
$link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']);
if (!$link) {
$buffer = "Connexion [FAILED]: " . mysql_error() ;
return $buffer;
}
$db_selected = @mysql_select_db($GLOBALS['nameDB'], $link);
if (!$db_selected) {
$buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error();
}
//del switch for a service
$sql = "DELETE FROM SERVICESWITCH WHERE idSwitch='$idSwitch'
AND idService='$idService' ;";
if (mysql_query($sql, $link)) {
$buffer = "DELETE SWITCH $idSwitch for SERVICE $idService : [OK] ";
}else{
$buffer = "DELETE SWITCH $idSwitch for SERVICE $idService : [FAILED] :" . mysql_error() ;
}
return $buffer;
}
?>