[40] | 1 | <? |
---|
| 2 | require('./conf/config.php'); |
---|
| 3 | |
---|
| 4 | /*************************************************************************** |
---|
| 5 | * |
---|
| 6 | * addPower($idPower, $descPower) |
---|
| 7 | * selectPower($idPower) |
---|
| 8 | * delPower($idPower) |
---|
| 9 | * updatePower($idPowern $descPower) |
---|
| 10 | * ctrlPower($idLogin, $idService, $idPower, $idSwitch) |
---|
| 11 | * |
---|
| 12 | *****************************************************************************/ |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | function addPower($idPower, $descPower) { |
---|
| 17 | //connect DB |
---|
| 18 | $link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']); |
---|
| 19 | if (!$link) { |
---|
| 20 | $buffer = "Connexion [FAILED]: " . mysql_error() ; |
---|
| 21 | return $buffer; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | $db_selected = @mysql_select_db($GLOBALS['nameDB'], $link); |
---|
| 25 | if (!$db_selected) { |
---|
| 26 | $buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error(); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | //Add Power |
---|
| 31 | $sql = "INSERT INTO POWER VALUE('$idPower', '$descPower');"; |
---|
| 32 | |
---|
| 33 | if (mysql_query($sql, $link)) { |
---|
| 34 | $buffer = 1; |
---|
| 35 | } else { |
---|
| 36 | $buffer = "CREATE POWER $idPower, $descPower: [FAILED] :" . mysql_error() ; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | return $buffer; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | function selectPower($idPower) { |
---|
| 45 | //connect DB |
---|
| 46 | $link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']); |
---|
| 47 | if (!$link) { |
---|
| 48 | $buffer = "Connexion [FAILED]: " . mysql_error() ; |
---|
| 49 | return $buffer; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | $db_selected = @mysql_select_db($GLOBALS['nameDB'], $link); |
---|
| 53 | if (!$db_selected) { |
---|
| 54 | $buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | if (empty($idPower)) { |
---|
| 59 | $sql = "SELECT * FROM POWER"; |
---|
| 60 | }else{ |
---|
| 61 | $sql = "SELECT * FROM POWER WHERE idPower='$idPower'; "; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | if ($result = mysql_query($sql, $link)) { |
---|
| 66 | //test if $return is_array |
---|
| 67 | return $result ; |
---|
| 68 | |
---|
| 69 | } else { |
---|
| 70 | $buffer = "SELECT POWER $idPower: [FAILED] :" . mysql_error() ; |
---|
| 71 | return $buffer ; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | function delPower($idPower) { |
---|
| 83 | //connect DB |
---|
| 84 | $link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']); |
---|
| 85 | if (!$link) { |
---|
| 86 | $buffer = "Connexion [FAILED]: " . mysql_error() ; |
---|
| 87 | return $buffer; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | $db_selected = @mysql_select_db($GLOBALS['nameDB'], $link); |
---|
| 91 | if (!$db_selected) { |
---|
| 92 | $buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | //del Power |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | $sql = "DELETE FROM Power WHERE idPower='$idPower';"; |
---|
| 100 | |
---|
| 101 | if (mysql_query($sql, $link)) { |
---|
| 102 | $buffer = "DELETE POWER $idPower: [OK]"; |
---|
| 103 | } else { |
---|
| 104 | $buffer = "DELETE POWER $idPower: [FAILED] :" . mysql_error() ; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | return $buffer; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | function updatePower($idPower, $descPower) { |
---|
| 112 | //connect DB |
---|
| 113 | $link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']); |
---|
| 114 | if (!$link) { |
---|
| 115 | $buffer = "Connexion [FAILED]: " . mysql_error() ; |
---|
| 116 | return $buffer; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | $db_selected = @mysql_select_db($GLOBALS['nameDB'], $link); |
---|
| 121 | if (!$db_selected) { |
---|
| 122 | $buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error(); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | //update Power |
---|
| 126 | $sql = "UPDATE POWER SET |
---|
| 127 | idPower='$idPower', |
---|
| 128 | descPower='$descPower', |
---|
| 129 | WHERE idPower='$idPower' ;" ; |
---|
| 130 | |
---|
| 131 | if (mysql_query($sql, $link)) { |
---|
| 132 | $buffer = "UPDATE POWER $idPower, $descPower: [OK]"; |
---|
| 133 | } else { |
---|
| 134 | $buffer = "UPDATE POWER $idPower, $descPower: [FAILED] :" . mysql_error() ; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | return $buffer; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | function ctrlPower($idLogin, $idPower, $idSwitch) { |
---|
| 145 | |
---|
| 146 | //connect DB |
---|
| 147 | $link = mysql_connect($GLOBALS['hostDB'], $GLOBALS['userDB'], $GLOBALS['passwdDB']); |
---|
| 148 | if (!$link) { |
---|
| 149 | $buffer = "Connexion [FAILED]: " . mysql_error() ; |
---|
| 150 | return $buffer; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | $db_selected = @mysql_select_db($GLOBALS['nameDB'], $link); |
---|
| 154 | if (!$db_selected) { |
---|
| 155 | $buffer .= "Impossible de sélectionner la base de données :$nameDB" . mysql_error(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | $varCtrl=0; |
---|
| 160 | |
---|
| 161 | //Powers for switch are just update, delete, disablePort, enablePort. /!\ No addSwitch is not possible to check Powers for a switch to create /!\ |
---|
| 162 | if (!empty($idSwitch)) { |
---|
| 163 | //Verify if service is authorized to execute Power for a switch |
---|
| 164 | |
---|
| 165 | //recupere le ou les services de l'utilisateur loggé |
---|
| 166 | $resultat = selectUserService($idLogin, ""); |
---|
| 167 | if ( !is_string($resultat) ) { |
---|
| 168 | while ( $row = mysql_fetch_assoc($resultat) ) { |
---|
| 169 | $idService = $row['idService']; |
---|
| 170 | //check if service is authorized to execute command |
---|
| 171 | $sql = "SELECT COUNT(CMDSERVICE.idService) FROM CMDSERVICE, SERVICESWITCH |
---|
| 172 | WHERE CMDSERVICE.idService='$idService' |
---|
| 173 | AND idPower='$idPower' |
---|
| 174 | AND SERVICESWITCH.idService = CMDSERVICE.idService |
---|
| 175 | AND SERVICESWITCH.idSwitch='$idSwitch';" ; |
---|
| 176 | |
---|
| 177 | if ($result = mysql_query($sql, $link)) { |
---|
| 178 | $buffer = "VEFICATION SERVICE'S POWER: $idService, $idPower FOR SWITCH $idSwitch : [OK]"; |
---|
| 179 | while ($rowCount = mysql_fetch_row($result) ) { |
---|
| 180 | if ( $rowCount[0] == 1 ) { |
---|
| 181 | $varCtrl=1; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | }else { |
---|
| 185 | $buffer = "VEFICATION SERVICE'S POWER: $idService, $idPower FOR SWITCH $idSwitch : [FAILED] :" .mysql_error() ; |
---|
| 186 | return $buffer; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | }else{ |
---|
| 190 | |
---|
| 191 | return $resultat; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | //verify if user is authorized to execute Power for a switch |
---|
| 197 | |
---|
| 198 | $sql = "SELECT COUNT(idPower) FROM CMDSWITCH |
---|
| 199 | WHERE idUser='$idLogin' |
---|
| 200 | AND idPower='$idPower' |
---|
| 201 | AND idSwitch='$idswitch';" ; |
---|
| 202 | |
---|
| 203 | if ($result = mysql_query($sql, $link)) { |
---|
| 204 | $buffer = "VEFICATION USER'S POWER : $idLogin, $idPower FOR SWITCH $idSwitch : [OK]"; |
---|
| 205 | while ($row = mysql_fetch_row($result) ) { |
---|
| 206 | if ( $row[0] == 1 ) { |
---|
| 207 | $varCtrl=1; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | }else { |
---|
| 211 | $buffer = "VEFICATION USER'S POWER: $idService, $idPower FOR SWITCH $idSwitch : [FAILED] :" . mysql_error() ; |
---|
| 212 | return $buffer; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | }else{ |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | //recupere le ou les services de l'utilisateur loggé |
---|
| 219 | $resultat = selectUserService($idLogin,""); |
---|
| 220 | if ( !is_string($resultat) ) { |
---|
| 221 | while ( $row = mysql_fetch_assoc($resultat) ) { |
---|
| 222 | $idService = $row['idService']; |
---|
| 223 | //check if service is authorized to execute command |
---|
| 224 | $sql = "SELECT COUNT(idService) FROM CMDSERVICE |
---|
| 225 | WHERE CMDSERVICE.idService='$idService' |
---|
| 226 | AND idPower='$idPower' ;"; |
---|
| 227 | |
---|
| 228 | if ($result = mysql_query($sql, $link)) { |
---|
| 229 | $buffer = "VEFICATION SERVICE'S POWER: $idService, $idPower : [OK]"; |
---|
| 230 | while ($row = mysql_fetch_row($result) ) { |
---|
| 231 | if ( $row[0] == 1 ) { |
---|
| 232 | $varCtrl=1; |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | }else { |
---|
| 236 | $buffer = "VEFICATION SERVICE'S POWER: $idService, $idPower : [FAILED] :" .mysql_error() ; |
---|
| 237 | return $buffer; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | }else{ |
---|
| 242 | |
---|
| 243 | return $resultat; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | //check if user is authorized to execute command |
---|
| 250 | $sql = "SELECT COUNT(idPower) FROM CMDUSER |
---|
| 251 | WHERE idUser='$idLogin' |
---|
| 252 | AND idPower='$idPower' " ; |
---|
| 253 | |
---|
| 254 | if ($result = mysql_query($sql, $link)) { |
---|
| 255 | $buffer = "VEFICATION USER'S POWER : $idLogin, $idPower : [OK]"; |
---|
| 256 | while ($row = mysql_fetch_row($result) ) { |
---|
| 257 | if ( $row[0] == 1 ) { |
---|
| 258 | $varCtrl=1; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | }else { |
---|
| 262 | $buffer = "VEFICATION USER'S POWER: $idService, $idPower : [FAILED] :" . mysql_error() ; |
---|
| 263 | return $buffer; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | if ( $varCtrl == 1 ) { |
---|
| 271 | return 1 ; |
---|
| 272 | }else{ |
---|
| 273 | return "ACCESS DENIED FOR THIS COMMAND $idPower FOR ". $idLogin ; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | ?> |
---|