[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-dhcp/sources/www/ -> dhcpd.inc.php (source)

   1  <?php
   2  
   3  /**
   4  
   5   * Fonctions du serveur DHCP
   6   * @Version $Id: dhcpd.inc.php 8573 2015-03-15 16:38:50Z plouf $
   7  
   8   * @Projet LCS / SambaEdu
   9  
  10   * @auteurs GrosQuicK   eric.mercier@crdp.ac-versailles.fr
  11   * @auteurs Plouf
  12  
  13   * @note Ce fichier de fonction doit etre appele par un include
  14  
  15   */
  16  /**
  17  
  18   * @Repertoire: dhcp
  19   * file: dhcp.inc.php
  20  
  21   */
  22  
  23  /**
  24  
  25   * Affiche la conf du serveur DHCP
  26  
  27   * @Parametres $error : Message d'erreur
  28  
  29   * @Return Affichage HTML
  30  
  31   */
  32  function dhcp_config_form($error) {
  33      require_once  ("ihm.inc.php");
  34      // Recuperation des donnees dans la base SQL
  35      $query = "SELECT * from params where name REGEXP '^dhcp*' ";
  36      $result = mysql_query($query);
  37      $ret = "<table>\n";
  38      // Menu select pour les vlan
  39      $nbr_vlan = dhcp_vlan_test();
  40      if ($nbr_vlan > 0) {
  41          $i = 1;
  42          $ret .= "<form name=\"configuration\" method=\"post\" action=\"config.php\">\n";
  43          $ret .= "<tr><td>";
  44          $ret .= gettext("Vlan");
  45          $ret .= "</td><td>";
  46          $ret .= ": <select name=\"vlan\" onchange=submit()>";
  47          $ret .= "<option value=\"0\">D&#233;faut</option>";
  48          while ($i <= $nbr_vlan) {
  49              $ret .= "<option ";
  50              if ($_POST['vlan'] == $i) {
  51                  $ret .= "selected";
  52              }
  53              $ret .= " value=\"$i\">vlan $i</option>";
  54              $i++;
  55          }
  56          $ret .= "</td><td></td></tr>\n";
  57          $ret .= "</form>\n";
  58      }
  59  
  60      // formulaire
  61      $ret .= "<form name=\"configuration\" method=\"post\" action=\"config.php\">\n";
  62      while ($resultat = mysql_fetch_assoc($result)) {
  63          $dhcp[$resultat['name']]["value"] = $resultat['value'];
  64          $dhcp[$resultat['name']]["descr"] = $resultat['descr'];
  65      }
  66      // dhcp_iface : interface d'ecoute du dhcp
  67      $ret .= "<tr><td>" . gettext($dhcp["dhcp_iface"]["descr"]) . "</td><td>\n";
  68      $ret .= ": <input type=\"text\" SIZE=5 name=\"dhcp_iface\" value=\"" . $dhcp["dhcp_iface"]["value"] . "\" maxlength=\"5\"";
  69      if ($_POST['vlan'] > 0) {
  70          $ret .= " disabled ";
  71      }
  72      $ret .= ">";
  73      if (isset($error['dhcp_iface'])) {
  74          $ret .= "<b>" . $error['dhcp_iface'] . "</b>";
  75      }
  76      $ret .= "</td></tr>\n";
  77  
  78      // dhcp_domain_name : Nom du domaine
  79      $ret .= "<tr><td>" . gettext($dhcp["dhcp_domain_name"]["descr"]) . "</td><td>\n";
  80      $ret .= ": <input type=\"text\" SIZE=60 name=\"dhcp_domain_name\" value=\"" . $dhcp["dhcp_domain_name"]["value"] . "\" maxlength=\"55\"";
  81      if ($_POST['vlan'] > 0) {
  82          $ret .= " disabled ";
  83      }
  84      $ret .= ">";
  85      if (isset($error['dhcp_domain_name'])) {
  86              $ret .= "<b>" . $error['dhcp_domain_name'] . "</b>";
  87      }
  88      $ret .= "</td></tr>\n";
  89  
  90      // dhcp_in_boot : dhcp start oon boot ? 0 ou 1
  91      $ret .= "<tr><td>" . gettext($dhcp["dhcp_on_boot"]["descr"]) . "</td><td>\n";
  92      if ($dhcp["dhcp_on_boot"]["value"] == 1) {
  93          $CHECKED = "CHECKED";
  94      } else {
  95          $CHECKED = "";
  96      }
  97      $ret .= ": <input type=\"checkbox\" name=\"dhcp_on_boot\" $CHECKED ";
  98      if ($_POST['vlan'] > 0) {
  99          $ret .= " disabled ";
 100      }
 101      $ret .= ">";
 102      if (isset($error['dhcp_iface'])) {
 103              $ret .= "<b>" . $error['dhcp_on_boot'] . "</b>";
 104              }
 105      $ret .= "</td></tr>\n";
 106  
 107      // dhcp_max_lease : bail maximal
 108      $ret .= "<tr><td>" . gettext($dhcp["dhcp_max_lease"]["descr"]) . "</td><td>\n";
 109      $ret .= ": <input type=\"text\" SIZE=10 name=\"dhcp_max_lease\" value=\"" . $dhcp["dhcp_max_lease"]["value"] . "\" maxlength=\"10\"";
 110      if ($_POST['vlan'] > 0) {
 111          $ret .= " disabled ";
 112      }
 113      $ret .= ">";
 114      if (isset($error['dhcp_max_lease'])) {
 115              $ret .= "<b>" . $error['dhcp_max_lease'] . "</b>";
 116              }
 117      $ret .= "</td></tr>\n";
 118  
 119  
 120      // dhcp_default_lease : bail par defaut
 121      $ret .= "<tr><td>" . gettext($dhcp["dhcp_default_lease"]["descr"]) . "</td><td>\n";
 122      $ret .= ": <input type=\"text\" SIZE=10 name=\"dhcp_default_lease\" value=\"" . $dhcp["dhcp_default_lease"]["value"] . "\" maxlength=\"10\"";
 123      if ($_POST['vlan'] > 0) {
 124          $ret .= " disabled ";
 125      }
 126      $ret .= ">";
 127      if (isset($error['dhcp_default_lease'])) {
 128              $ret .= "<b>" . $error['dhcp_default_lease'] . "</b>";
 129      }
 130      $ret .= "</td></tr>\n";
 131  
 132      // dhcp_ntp : Serveur NTP
 133      $ret .= "<tr><td>" . gettext($dhcp["dhcp_ntp"]["descr"]) . "</td><td>\n";
 134      $ret .= ": <input type=\"text\" SIZE=20 name=\"dhcp_ntp\" value=\"" . $dhcp["dhcp_ntp"]["value"] . "\"  maxlength=\"20\"";
 135      if ($_POST['vlan'] > 0) {
 136          $ret .= " disabled ";
 137      }
 138      $ret .= ">";
 139      if (isset($error['dhcp_ntp'])) {
 140              $ret .= "<b>" . $error['dhcp_ntp'] . "</b>";
 141      }
 142  
 143      // dhcp_wins : Serveur WINS
 144      $ret .= "<tr><td>" . gettext($dhcp["dhcp_wins"]["descr"]) . "</td><td>\n";
 145      $ret .= ": <input type=\"text\" SIZE=20 name=\"dhcp_wins\" value=\"" . $dhcp["dhcp_wins"]["value"] . "\"maxlength=\"30\"";
 146      if ($_POST['vlan'] > 0) {
 147          $ret .= " disabled ";
 148      }
 149      $ret .= ">";
 150      if (isset($error['dhcp_wins'])) {
 151           $ret .= "<b>" . $error['dhcp_wins'] . "</b>";
 152      }
 153      $ret .= "</td></tr>\n";
 154  
 155      // dhcp_dns_server_prim : Serveur DNS primaire
 156      $ret .= "<tr><td>" . gettext($dhcp["dhcp_dns_server_prim"]["descr"]) . "</td><td>\n";
 157      $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_dns_server_prim\" value=\"" . $dhcp["dhcp_dns_server_prim"]["value"] . "\"maxlength=\"15\"";
 158      if ($_POST['vlan'] > 0) {
 159          $ret .= " disabled ";
 160      }
 161      $ret .= ">";
 162      if (isset($error['dhcp_dns_server_prim'])) {
 163              $ret .= "<b>" . $error['dhcp_dns_server_prim'] . "</b>";
 164      }
 165      $ret .= "</td></tr>\n";
 166  
 167      // dhcp_dns_server_sec : Serveur DNS secondaire
 168      $ret .= "<tr><td>" . gettext($dhcp["dhcp_dns_server_sec"]["descr"]) . "</td><td>\n";
 169      $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_dns_server_sec\" value=\"" . $dhcp["dhcp_dns_server_sec"]["value"] . "\" maxlength=\"15\"";
 170      if ($_POST['vlan'] > 0) {
 171          $ret .= " disabled ";
 172      }
 173      $ret .= ">";
 174      if (isset($error['dhcp_dns_server_sec'])) {
 175              $ret .= "<b>" . $error['dhcp_dns_server_sec'] . "</b>";
 176      }
 177      $ret .= "</td></tr>\n";
 178  
 179      // partie reserve si on a des vlan
 180  
 181      if ($_POST['vlan'] > 0) {
 182          // Adresse du reseau
 183          $ret .= "<tr><td>" . gettext("Adresse de r&#233;seau ");
 184          $ret .= gettext(" du vlan ") . $_POST['vlan'];
 185          $ret .= "</td><td>\n";
 186          $dhcp_reseau_vlan = "dhcp_reseau_" . $_POST['vlan'];
 187          $ret .= ": <input type=\"text\" SIZE=15 name=\"$dhcp_reseau_vlan\" value=\"" . $dhcp["$dhcp_reseau_vlan"]["value"] . "\" maxlength=\"15\">";
 188  
 189  
 190          // Masque du reseau
 191          $ret .= "<tr><td>" . gettext("Masque de r&#233;seau ");
 192          $ret .= gettext(" du vlan ") . $_POST['vlan'];
 193          $ret .= "</td><td>\n";
 194          $dhcp_masque_vlan = "dhcp_masque_" . $_POST['vlan'];
 195          $ret .= ": <input type=\"text\" SIZE=15 name=\"$dhcp_masque_vlan\" value=\"" . $dhcp["$dhcp_masque_vlan"]["value"] . "\" maxlength=\"15\">";
 196      }
 197      if (isset($error['dhcp_gateway'])) {
 198              $ret .= "<b>" . $error['dhcp_gateway'] . "</b>";
 199      }
 200      $ret .= "</td></tr>\n";
 201  
 202  
 203      // dhcp_gateway : PASSERELLE
 204  
 205      if ($_POST['vlan'] > 0) {
 206          $ret .= "<tr><td>" . gettext($dhcp["dhcp_gateway"]["descr"]);
 207          $ret .= gettext(" du vlan ") . $_POST['vlan'];
 208          $ret .= "</td><td>\n";
 209          $dhcp_gateway_vlan = "dhcp_gateway_" . $_POST['vlan'];
 210          $ret .= ": <input type=\"text\" SIZE=15 name=\"$dhcp_gateway_vlan\" value=\"" . $dhcp["$dhcp_gateway_vlan"]["value"] . "\" maxlength=\"15\">";
 211          if (isset($error['dhcp_gateway'])) {
 212                  $ret .= "<b>" . $error['dhcp_gateway'] . "</b>";
 213          }
 214          $ret .= "</td></tr>\n";
 215      } else {
 216          if ($nbr_vlan == "0") {
 217              $ret .= "<tr><td>" . gettext($dhcp["dhcp_gateway"]["descr"]);
 218              $ret .= "</td><td>\n";
 219              $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_gateway\" value=\"" . $dhcp["dhcp_gateway"]["value"] . "\" maxlength=\"15\">";
 220              if (isset($error['dhcp_gateway'])) {
 221                      $ret .= "<b>" . $error['dhcp_gateway'] . "</b>";
 222              }
 223              $ret .= "</td></tr>\n";
 224          }
 225      }
 226  
 227      // dhcp_ip_min : Debut de la plage de reservations
 228      if ($_POST['vlan'] > 0) {
 229          $ret .= "<tr><td>" . gettext($dhcp["dhcp_ip_min"]["descr"]);
 230          $ret .= gettext(" du vlan ") . $_POST['vlan'];
 231          $ret .= "</td><td>\n";
 232          $dhcp_ip_min_vlan = "dhcp_ip_min_" . $_POST['vlan'];
 233  //        if ($dhcp["$dhcp_ip_min_vlan"]["value"] == "") { $dhcp["$dhcp_ip_min_vlan"]["value"] = 
 234          $ret .= ": <input type=\"text\" SIZE=15 name=\"$dhcp_ip_min_vlan\" value=\"" . $dhcp["$dhcp_ip_min_vlan"]["value"] . "\" maxlength=\"15\">";
 235          if (isset($error['dhcp_ip_min'])) {
 236              $ret .= "<b>" . $error['dhcp_ip_min'] . "</b>";
 237          }
 238          $ret .= "</td></tr>\n";
 239      } else {
 240          if ($nbr_vlan == "0") {
 241              $ret .= "<tr><td>" . gettext($dhcp["dhcp_ip_min"]["descr"]);
 242              $ret .= "</td><td>\n";
 243              $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_ip_min\" value=\"" . $dhcp["dhcp_ip_min"]["value"] . "\" maxlength=\"15\">";
 244              if (isset($error['dhcp_ip_min'])) {
 245                      $ret .= "<b>" . $error['dhcp_ip_min'] . "</b>";
 246              }
 247              $ret .= "</td></tr>\n";
 248          }
 249      }
 250  
 251      // dhcp_begin_range : Debut de la plage
 252      if ($_POST['vlan'] > 0) {
 253          $ret .= "<tr><td>" . gettext($dhcp["dhcp_begin_range"]["descr"]);
 254          $ret .= gettext(" du vlan ") . $_POST['vlan'];
 255          $ret .= "</td><td>\n";
 256          $dhcp_begin_range_vlan = "dhcp_begin_range_" . $_POST['vlan'];
 257          $ret .= ": <input type=\"text\" SIZE=15 name=\"$dhcp_begin_range_vlan\" value=\"" . $dhcp["$dhcp_begin_range_vlan"]["value"] . "\" maxlength=\"15\">";
 258          if (isset($error['dhcp_begin_range'])) {
 259                  $ret .= "<b>" . $error['dhcp_begin_range'] . "</b>";
 260          }
 261          $ret .= "</td></tr>\n";
 262      } else {
 263          if ($nbr_vlan == "0") {
 264              $ret .= "<tr><td>" . gettext($dhcp["dhcp_begin_range"]["descr"]);
 265              $ret .= "</td><td>\n";
 266              $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_begin_range\" value=\"" . $dhcp["dhcp_begin_range"]["value"] . "\" maxlength=\"15\">";
 267              if (isset($error['dhcp_begin_range'])) {
 268                      $ret .= "<b>" . $error['dhcp_begin_range'] . "</b>";
 269              }
 270              $ret .= "</td></tr>\n";
 271          }
 272      }
 273  
 274      // dhcp_end_range : Fin de la plage
 275      if ($_POST['vlan'] > 0) {
 276          $ret .= "<tr><td>" . gettext($dhcp["dhcp_end_range"]["descr"]);
 277          $ret .= gettext(" du vlan ") . $_POST['vlan'];
 278          $ret .= "</td><td>\n";
 279          $dhcp_end_range_vlan = "dhcp_end_range_" . $_POST['vlan'];
 280          $ret .= ": <input type=\"text\" SIZE=15 name=\"$dhcp_end_range_vlan\" value=\"" . $dhcp["$dhcp_end_range_vlan"]["value"] . "\" maxlength=\"15\"";
 281          if (isset($error['dhcp_end_range'])) {
 282                 $ret .= "<b>" . $error['dhcp_end_range'] . "</b>";
 283          }
 284          $ret .= "</td></tr>\n";
 285      } else {
 286          if ($nbr_vlan == "0") {
 287              $ret .= "<tr><td>" . gettext($dhcp["dhcp_end_range"]["descr"]);
 288              $ret .= "</td><td>\n";
 289              $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_end_range\" value=\"" . $dhcp["dhcp_end_range"]["value"] . "\" maxlength=\"15\">";
 290              if (isset($error['dhcp_end_range'])) {
 291                      $ret .= "<b>" . $error['dhcp_end_range'] . "</b>";
 292              }
 293              $ret .= "</td></tr>\n";
 294          }
 295      }
 296  
 297      $ret .= "<tr><td></td><td></td></tr>\n";
 298      // Option autre
 299      if ($_POST['vlan'] > 0) {
 300          $ret .= "<tr><td>" . gettext($dhcp["dhcp_extra_option"]["descr"]);
 301          $ret .= gettext(" du vlan ") . $_POST['vlan'];
 302          $ret .= "</td><td>\n";
 303          $dhcp_extra_option_vlan = "dhcp_extra_option_" . $_POST['vlan'];
 304          $ret .= ": <input type=\"text\" SIZE=30 name=\"$dhcp_extra_option_vlan\" value=\"" . $dhcp["$dhcp_extra_option_vlan"]["value"] . "\" maxlength=\"30\"";
 305          if (isset($error['dhcp_extra_option'])) {
 306                  $ret .= "<b>" . $error['dhcp_extra_option'] . "</b>";
 307          }
 308          $ret .= "</td></tr>\n";
 309      } else {
 310          if ($nbr_vlan == "0") {
 311              $ret .= "<tr><td>" . gettext($dhcp["dhcp_extra_option"]["descr"]);
 312              $ret .= "</td><td>\n";
 313              $ret .= ": <input type=\"text\" SIZE=30 name=\"dhcp_extra_option\" value=\"" . $dhcp["dhcp_extra_option"]["value"] . "\" maxlength=\"30\">";
 314              if (isset($error['dhcp_end_range'])) {
 315                      $ret .= "<b>" . $error['dhcp_end_range'] . "</b>";
 316              }
 317              $ret .= "</td></tr>\n";
 318          }
 319      }
 320  
 321      $ret .= "<tr><td></td><td></td></tr>\n";
 322  
 323      // dhcp_tftp_server : SERVER TFTP
 324      $ret .= "<tr><td>" . gettext($dhcp["dhcp_tftp_server"]["descr"]) . "</td><td>\n";
 325      $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_tftp_server\" value=\"" . $dhcp["dhcp_tftp_server"]["value"] . "\" maxlength=\"15\"";
 326      if ($_POST['vlan'] > 0) {
 327          $ret .= " disabled ";
 328      }
 329      $ret .= ">";
 330      if (isset($error['dhcp_tftp_server'])) {
 331              $ret .= "<b>" . $error['dhcp_tftp_server'] . "</b>";
 332      }
 333      $ret .= "</td></tr>\n";
 334  
 335      // dhcp_unatt_filename fichier de boot PXE
 336      $ret .= "<tr><td>" . gettext($dhcp["dhcp_unatt_filename"]["descr"]) . "</td><td>\n";
 337      $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_unatt_filename\" value=\"" . $dhcp["dhcp_unatt_filename"]["value"] . "\" ";
 338      if ($_POST['vlan'] > 0) {
 339          $ret .= " disabled ";
 340      }
 341      $ret .= ">";
 342      if (isset($error['dhcp_unatt_filename'])) {
 343              $ret .= "<b>" . $error['dhcp_unatt_filename'] . "</b>";
 344      }
 345      $ret .= "</td></tr>\n";
 346  
 347      $ret .= "<tr><td></td><td></td></tr>\n";
 348  
 349      // UNATTENDED
 350      // dhcp_unattended_server
 351      //$ret .= "<tr><td>".gettext($dhcp["dhcp_unatt_server"]["descr"])."</td><td>\n";
 352      //$ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_unatt_server\" value=\"".$dhcp["dhcp_unatt_server"]["value"]."\" maxlength=\"15\">";
 353      //$ret .= "<b>".$error['dhcp_unatt_server']."</b>";
 354      //$ret .= "</td></tr>\n";
 355      // dhcp_unatt_login
 356      $ret .= "<tr><td>" . gettext($dhcp["dhcp_unatt_login"]["descr"]) . "</td><td>\n";
 357      $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_unatt_login\" value=\"" . $dhcp["dhcp_unatt_login"]["value"] . "\" ";
 358      if ($_POST['vlan'] > 0) {
 359          $ret .= " disabled ";
 360      }
 361      $ret .= ">";
 362      if (isset($error['dhcp_unatt_login'])) {
 363              $ret .= "<b>" . $error['dhcp_unatt_login'] . "</b>";
 364      }
 365      $ret .= "</td></tr>\n";
 366  
 367      // dhcp_unatt_pass
 368      $ret .= "<tr><td>" . gettext($dhcp["dhcp_unatt_pass"]["descr"]) . "</td><td>\n";
 369      $ret .= ": <input type=\"text\" SIZE=15 name=\"dhcp_unatt_pass\" value=\"" . $dhcp["dhcp_unatt_pass"]["value"] . "\" ";
 370      if ($_POST['vlan'] > 0) {
 371          $ret .= " disabled ";
 372      }
 373      $ret .= ">";
 374      if (isset($error['dhcp_unatt_pass'])) {
 375           $ret .= "<b>" . $error['dhcp_unatt_pass'] . "</b>";
 376      }
 377      $ret .= "</td></tr>\n";
 378  
 379  
 380  
 381      $ret .= "</table>";
 382      $ret .= "<input type='hidden' name='action' value='newconfig'>\n";
 383      $ret .= "<input type='hidden' name='vlan' value='" . $_POST['vlan'] . "'>\n";
 384      $ret .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Modifier") . "\">\n";
 385      $ret .= "</form>\n";
 386      exec("sudo /usr/share/se3/scripts/makedhcpdconf state", $state);
 387      if ($state[0] == "1") {
 388          $ret .= "<form name=\"stop\" method=\"post\" action=\"config.php\">\n";
 389          $ret .= "<input type='hidden' name='action' value='stop'>\n";
 390          $ret .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Stopper le serveur dhcp") . "\">\n";
 391          $ret .="</form>";
 392      } else {
 393          $ret .= "<form name=\"stop\" method=\"post\" action=\"config.php\">\n";
 394          $ret .= "<input type='hidden' name='action' value='restart'>\n";
 395          $ret .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Red&#233;marrer le serveur dhcp") . "\">\n";
 396          $ret .="</form>";
 397      }
 398  
 399  
 400      return $ret;
 401  }
 402  
 403  /**
 404   * Mise a jour de la conf du dhcp dans la base SQL
 405  
 406   * @Parametres
 407  
 408   * @Return Erreur SQL
 409  
 410   */
 411  function dhcp_update_config() {  // insert range in option service table
 412      require_once  ("ihm.inc.php");
 413      $error = "";
 414  
 415      if ($_POST['vlan'] > 0) {
 416          //verif si le champ existe dans la table sinon on le cree
 417  
 418          $dhcp_min = "dhcp_ip_min_" . $_POST['vlan'];
 419          dhcp_vlan_champ($dhcp_min);
 420          $dhcp_begin = "dhcp_begin_range_" . $_POST['vlan'];
 421          dhcp_vlan_champ($dhcp_begin);
 422          $dhcp_end = "dhcp_end_range_" . $_POST['vlan'];
 423          dhcp_vlan_champ($dhcp_end);
 424          $dhcp_gateway_vlan = "dhcp_gateway_" . $_POST['vlan'];
 425          dhcp_vlan_champ($dhcp_gateway_vlan);
 426          $dhcp_reseau = "dhcp_reseau_" . $_POST['vlan'];
 427          dhcp_vlan_champ($dhcp_reseau);
 428          $dhcp_masque = "dhcp_masque_" . $_POST['vlan'];
 429          dhcp_vlan_champ($dhcp_masque);
 430          $dhcp_extra_option = "dhcp_extra_option_" . $_POST['vlan'];
 431          dhcp_vlan_champ($dhcp_extra_option);
 432      } else {
 433          $dhcp_min = "dhcp_ip_min";
 434          $dhcp_begin = "dhcp_begin_range";
 435          $dhcp_end = "dhcp_end_range";
 436          $dhcp_gateway_vlan = "dhcp_gateway";
 437          $dhcp_extra_option = "dhcp_extra_option";
 438      }
 439  
 440      if ((set_ip_in_lan($_POST["$dhcp_min"])) || ($_POST["$dhcp_min"] == "")) {
 441          $update_query = "UPDATE params SET value='" . $_POST["$dhcp_min"] . "' WHERE name='$dhcp_min'";
 442          mysql_query($update_query);
 443      } else {
 444          $error["$dhcp_min"] = gettext("Cette addresse n'est pas valide : " . $_POST["$dhcp_min"]);
 445      }
 446  
 447      if ((set_ip_in_lan($_POST["$dhcp_begin"])) || ($_POST["$dhcp_begin"] == "")) {
 448          $update_query = "UPDATE params SET value='" . $_POST["$dhcp_begin"] . "' WHERE name='$dhcp_begin'";
 449          mysql_query($update_query);
 450      } else {
 451          $error["$dhcp_begin"] = gettext("Cette addresse n'est pas valide : " . $_POST["$dhcp_begin"]);
 452      }
 453  
 454  
 455      if ((set_ip_in_lan($_POST["$dhcp_end"]) || ($_POST["$dhcp_end"]) == "")) {
 456          $update_query = "UPDATE params SET value='" . $_POST["$dhcp_end"] . "' WHERE name='$dhcp_end'";
 457          mysql_query($update_query);
 458      } else {
 459          $error["$dhcp_end"] = gettext("Cette adresse n'est pas valide : " . $_POST["$dhcp_end"]);
 460      }
 461  
 462  
 463      if ((set_ip_in_lan($_POST["$dhcp_gateway_vlan"])) || ($_POST["$dhcp_gateway_vlan"] == "")) {
 464          $update_query = "UPDATE params SET value='" . $_POST["$dhcp_gateway_vlan"] . "' WHERE name='$dhcp_gateway_vlan'";
 465          mysql_query($update_query);
 466      } else {
 467          $error["$dhcp_gateway_vlan"] = gettext("Cette adresse n'est pas valide : " . $_POST["$dhcp_gateway_vlan"]);
 468      }
 469  
 470      if ((set_ip_in_lan($_POST["$dhcp_reseau"])) || ($_POST["$dhcp_reseau"] == "")) {
 471          $update_query = "UPDATE params SET value='" . $_POST["$dhcp_reseau"] . "' WHERE name='$dhcp_reseau'";
 472          mysql_query($update_query);
 473      } else {
 474          $error["$dhcp_reseau"] = gettext("Cette addresse n'est pas valide : " . $_POST["$dhcp_reseau"]);
 475      }
 476  
 477  
 478      if ((set_ip_in_lan($_POST["$dhcp_masque"])) || ($_POST["$dhcp_masque"] == "")) {
 479          $update_query = "UPDATE params SET value='" . $_POST["$dhcp_masque"] . "' WHERE name='$dhcp_masque'";
 480          mysql_query($update_query);
 481      } else {
 482          $error["$dhcp_masque"] = gettext("Cette addresse n'est pas valide : " . $_POST["$dhcp_masque"]);
 483      }
 484  
 485  //    if (!($_POST["$dhcp_extra_option"]=="")) {
 486      $update_query = "UPDATE params SET value='" . $_POST["$dhcp_extra_option"] . "' WHERE name='$dhcp_extra_option'";
 487      mysql_query($update_query);
 488  //    }
 489      // Si on est dans la conf des vlan cette partie n'est pas modifiable
 490  
 491      if ($_POST['vlan'] < 1) {
 492          if (set_ip_in_lan($_POST['dhcp_dns_server_prim'])) {
 493              $update_query = "UPDATE params SET value='" . $_POST['dhcp_dns_server_prim'] . "' WHERE name='dhcp_dns_server_prim'";
 494              mysql_query($update_query);
 495          } else {
 496              $error["dhcp_dns_server_prim"] = gettext("Cette adresse n'est pas valide :" . $_POST['dhcp_dns_server_prim']);
 497          }
 498  
 499          if ((set_ip_in_lan($_POST['dhcp_dns_server_sec'])) || ($_POST['dhcp_dns_server_sec'] == "")) {
 500              $update_query = "UPDATE params SET value='" . $_POST['dhcp_dns_server_sec'] . "' WHERE name='dhcp_dns_server_sec'";
 501              mysql_query($update_query);
 502          } else {
 503              $error["dhcp_dns_server_sec"] = gettext("Cette adresse n'est pas valide : " . $_POST['dhcp_dns_server_sec']);
 504          }
 505  
 506  
 507          if (preg_match('/,/', $_POST['dhcp_wins'])) {
 508              list($wins_ip_1, $wins_ip_2) = preg_split('/,/', $_POST['dhcp_wins']);
 509              if ((set_ip_in_lan($wins_ip_1)) && (set_ip_in_lan($wins_ip_2))) {
 510                  $update_query = "UPDATE params SET value='" . $_POST['dhcp_wins'] . "' WHERE name='dhcp_wins'";
 511                  mysql_query($update_query);
 512              } else {
 513                  $error["dhcp_wins"] = gettext("Une des adresses n'est pas valide : " . $_POST['dhcp_wins']);
 514              }
 515          } elseif ((set_ip_in_lan($_POST['dhcp_wins'])) || ($_POST['dhcp_wins'] == "")) {
 516              $update_query = "UPDATE params SET value='" . $_POST['dhcp_wins'] . "' WHERE name='dhcp_wins'";
 517              mysql_query($update_query);
 518          } else {
 519              $error["dhcp_wins"] = gettext("Cette adresse n'est pas valide : " . $_POST['dhcp_wins']);
 520          }
 521  
 522          if ((set_ip_in_lan($_POST['dhcp_ntp'])) || ($_POST['dhcp_ntp'] == "")) {
 523              $update_query = "UPDATE params SET value='" . $_POST['dhcp_ntp'] . "' WHERE name='dhcp_ntp'";
 524              mysql_query($update_query);
 525          } else {
 526              $error["dhcp_ntp"] = gettext("Cette adresse n'est pas valide : " . $_POST['dhcp_ntp']);
 527          }
 528          if (preg_match("/^[0-9]+$/", $_POST['dhcp_max_lease'])) {
 529              $update_query = "UPDATE params SET value='" . $_POST['dhcp_max_lease'] . "' WHERE name='dhcp_max_lease'";
 530              mysql_query($update_query);
 531          } else {
 532              $error["dhcp_max_lease"] = gettext("Ce n'est pas un nombre valide : " . $_POST['dhcp_max_lease']);
 533          }
 534  
 535          if (preg_match("/^[0-9]+$/", $_POST['dhcp_default_lease'])) {
 536              $update_query = "UPDATE params SET value='" . $_POST['dhcp_default_lease'] . "' WHERE name='dhcp_default_lease'";
 537              mysql_query($update_query);
 538          } else {
 539              $error["dhcp_default_lease"] = gettext("Ce n'est pas un nombre valide : " . $_POST['dhcp_default_lease']);
 540          }
 541          if (preg_match("/^eth[0-9]+$/", $_POST['dhcp_iface']) || preg_match("/^bond[0-9]+$/", $_POST['dhcp_iface'])) {
 542              $update_query = "UPDATE params SET value='" . $_POST['dhcp_iface'] . "' WHERE name='dhcp_iface'";
 543              mysql_query($update_query);
 544          } else {
 545              $error["dhcp_iface"] = gettext("Ce n'est pas une interface valide : " . $_POST['dhcp_iface']);
 546          }
 547          if ($_POST['dhcp_on_boot']) {
 548              $value = "1";
 549          } else {
 550              $value = "0";
 551          }
 552          $update_query = "UPDATE params SET value='" . $value . "' WHERE name='dhcp_on_boot'";
 553          mysql_query($update_query);
 554  
 555  
 556  
 557          $update_query = "UPDATE params SET value='" . $_POST['dhcp_domain_name'] . "' WHERE name='dhcp_domain_name'";
 558          mysql_query($update_query);
 559  
 560  
 561          // TFTP SERVER
 562          if ((set_ip_in_lan($_POST['dhcp_tftp_server'])) || ($_POST['dhcp_tftp_server'] == "")) {
 563              $update_query = "UPDATE params SET value='" . $_POST['dhcp_tftp_server'] . "' WHERE name='dhcp_tftp_server'";
 564              mysql_query($update_query);
 565          } else {
 566              $error["dhcp_tftp_server"] = gettext("Cette entr&#233;e n'est pas valide :") . $_POST['dhcp_tftp_server'];
 567          }
 568          // unatt
 569          if ((set_ip_in_lan($_POST['dhcp_unatt_server'])) || ($_POST['dhcp_unatt_server'] == "")) {
 570              $update_query = "UPDATE params SET value='" . $_POST['dhcp_unatt_server'] . "' WHERE name='dhcp_unatt_server'";
 571              mysql_query($update_query);
 572          } else {
 573              $error["dhcp_tftp_server"] = gettext("Cette entr&#233;e n'est pas valide :") . $_POST['dhcp_unatt_server'];
 574          }
 575          $update_query = "UPDATE params SET value='" . $_POST['dhcp_unatt_login'] . "' WHERE name='dhcp_unatt_login'";
 576          mysql_query($update_query);
 577          $update_query = "UPDATE params SET value='" . $_POST['dhcp_unatt_pass'] . "' WHERE name='dhcp_unatt_pass'";
 578          mysql_query($update_query);
 579          $update_query = "UPDATE params SET value='" . $_POST['dhcp_unatt_filename'] . "' WHERE name='dhcp_unatt_filename'";
 580          mysql_query($update_query);
 581      }
 582      return $error;
 583  }
 584  
 585  /**
 586   *  Test si l'adresse IP appartient au reseau local
 587  
 588   * @Parametres $ip : Adresse IP a tester
 589  
 590   * @Return  TRUE si oui - FLASE si non
 591  
 592   */
 593  function set_ip_in_lan($ip) {
 594      if (preg_match("/^(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]|[1-9]).)" .
 595                      "{1}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]).)" .
 596                      "{2}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]){1}))$/", $ip)) {
 597          return TRUE;
 598      } else {
 599          return FALSE;
 600      }
 601  }
 602  
 603  /**
 604   * Parse le fichier dhcp.leases
 605  
 606   * @Parametres $file : fichier dhcp.laeses
 607  
 608   * @Return an associativ array : ["hostname"] / ("ip"] / [ "macaddr"] who are in dhcpd.lease and take ["parc"] entry if exist in ldap SORT by hostname
 609  
 610   */
 611  function parse_dhcpd_lease($file) {
 612      require_once("includes/ldap.inc.php");
 613      $lease = file($file);
 614      $compteur_clients = 0;
 615      $client["macaddr"][$compteur_clients] = "";
 616      $client["hostname"][$compteur_clients] = "";
 617  // $client["ip"][$compteur_clients]=$ip[0];
 618      foreach ($lease as $compteur => $ligne) {
 619          if (preg_match("/^lease/", $ligne)) {  // for each "lease" we take IP / Mac Addr / hostname
 620              preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/", $ligne, $ip);   // take IP
 621              $macaddr[0] = gettext("unresolved");
 622              $clienthostname[0] = gettext("unresolved");
 623              $etat = 0;
 624              while (!preg_match("/^}/", $lease[$compteur])) {
 625                  if (preg_match("/binding state active/", $lease[$compteur]))
 626                      $etat = 1; // lease state
 627   if (preg_match("/hardware ethernet/", $lease[$compteur])) { // take mac addr
 628                      preg_match("/[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}/", $lease[$compteur], $macaddr);
 629                  }
 630                  if (preg_match("/client-hostname/", $lease[$compteur])) { // take name
 631                      preg_match("/\"(.*)\"/", $lease[$compteur], $clienthostname);
 632                      $clienthostname[0] = preg_replace("/\"/", "", $clienthostname[0]);
 633                  }
 634                  $compteur = $compteur + 1;
 635              }
 636              if ($etat && ((!in_array($macaddr[0], $client["macaddr"])) && ($macaddr[0] != gettext("unresolved") ) && (!registred($macaddr[0])))) {
 637                  $client["macaddr"][$compteur_clients] = $macaddr[0];
 638                  $client["hostname"][$compteur_clients] = $clienthostname[0];
 639  
 640                  if ($client["hostname"][$compteur_clients] == gettext("unresolved")) {
 641                      $list_computer = search_machines("(&(cn=*)(ipHostNumber=$ip[0]))", "computers");
 642                      if (count($list_computer) > 1) {
 643                          $resolutiondunom = "doublon_ldap";
 644                          $client["hostname"][$compteur_clients] = $resolutiondunom;
 645                      } elseif (count($list_computer) > 0) {
 646                          $resolutiondunom = $list_computer[0]['cn'];
 647                          $client["hostname"][$compteur_clients] = $resolutiondunom;
 648                      }
 649                  }
 650                  $client["ip"][$compteur_clients] = $ip[0];
 651                  $client["parc"][$compteur_clients] = search_parcs($clienthostname[0]);
 652                  $compteur_clients++;
 653              }
 654          }
 655      }
 656      if (is_array($client["ip"])) {
 657          array_multisort($client["hostname"], SORT_ASC, $client["ip"], SORT_ASC, $client["macaddr"], SORT_ASC, $client["parc"]);
 658      } else {
 659          $client = "";
 660      }
 661      return $client;
 662  }
 663  
 664  function my_parse_dhcpd_lease($file) {
 665      $mode_debug = "n";
 666      $mode_fich_debug = "n";
 667  
 668      require_once("includes/ldap.inc.php");
 669      $lease = file($file);
 670      $compteur_clients = 0;
 671  
 672      /*
 673        $client["macaddr"][$compteur_clients]="";
 674        $client["hostname"][$compteur_clients]="";
 675        // $client["ip"][$compteur_clients]=$ip[0];
 676       */
 677  
 678      $client["macaddr"] = array();
 679      $client["ip"] = array();
 680      $client["hostname"] = array();
 681      $client["parc"] = array();
 682  
 683      $tab_recherche_ldap_faite = array();
 684      $liste_noms_en_lease = array();
 685      $liste_noms_ldap = array();
 686      $liste_autres_ip = array();
 687  
 688      if ($mode_fich_debug == "y") {
 689          $fich = fopen('/tmp/parse_dhcpd_lease.txt', 'a+');
 690      }
 691  
 692      foreach ($lease as $compteur => $ligne) {
 693          // for each "lease" we take IP / Mac Addr / hostname
 694          if (preg_match("/^lease/", $ligne)) {
 695              preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/", $ligne, $ip);   // take IP
 696              // Initialisation pour le cas ou les infos dans cette section du dhcpd.leases ne sont pas exmploitables
 697              $macaddr[0] = gettext("unresolved");
 698              $clienthostname[0] = gettext("unresolved");
 699              $etat = 0;
 700  
 701              // On lit le fichier jusqu'a l'accolade fermante suivante
 702              while (!preg_match("/^}/", $lease[$compteur])) {
 703                  if (preg_match("/binding state active/", $lease[$compteur])) {
 704                      $etat = 1;
 705                  } // lease state
 706  
 707                  if (preg_match("/hardware ethernet/", $lease[$compteur])) { // take mac addr
 708                      preg_match("/[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}/", $lease[$compteur], $macaddr);
 709                  }
 710  
 711                  if (preg_match("/client-hostname/", $lease[$compteur])) { // take name
 712                      preg_match("/\"(.*)\"/", $lease[$compteur], $clienthostname);
 713                      $clienthostname[0] = preg_replace("/\"/", "", $clienthostname[0]);
 714                  }
 715  
 716                  $compteur = $compteur + 1;
 717              }
 718  
 719              if ($mode_fich_debug == "y") {
 720                  fwrite($fich, "\n");
 721                  fwrite($fich, "\$ip[0]=" . $ip[0] . "\n");
 722                  fwrite($fich, "\$macaddr[0]=" . $macaddr[0] . "\n");
 723                  fwrite($fich, "\$clienthostname[0]=" . $clienthostname[0] . "\n");
 724              }
 725  
 726              if ($etat == "1") {
 727                  // On a bien 'binding state active' pour cette IP... malheureusement on peut avoir deux ip avec ca pour une meme adresse MAC
 728  
 729                  if ((!registred($macaddr[0])) &&
 730                          ($macaddr[0] != gettext("unresolved"))) {
 731                      // Adresse MAC trouvee dans le leases et pas deja enregistree dans la table se3_dhcp
 732  
 733                      if (!isset($liste_noms_en_lease["$macaddr[0]"])) {
 734                          $liste_noms_en_lease["$macaddr[0]"] = array();
 735                      }
 736                      if (($mode_debug == 'y') ||
 737                              ((strtolower($clienthostname[0]) != 'unresolved') &&
 738                              (!in_array(strtolower($list_computer[$loop]['cn']), $liste_noms_en_lease["$macaddr[0]"])))) {
 739                          $liste_noms_en_lease["$macaddr[0]"][] = strtolower($clienthostname[0]);
 740  
 741                          if ($mode_fich_debug == "y") {
 742                              fwrite($fich, "\$liste_noms_en_lease[\"$macaddr[0]\"][]=" . strtolower($clienthostname[0]) . "\n");
 743                          }
 744                      }
 745  
 746                      if ((!in_array($macaddr[0], $client["macaddr"]))) {
 747                          if ($mode_fich_debug == "y") {
 748                              fwrite($fich, "Adresse Mac non encore traitee.\n");
 749                          }
 750  
 751                          $client["macaddr"][$compteur_clients] = $macaddr[0];
 752                          $client["hostname"][$compteur_clients] = $clienthostname[0];
 753  
 754                          $liste_noms_ldap["$macaddr[0]"] = array();
 755  
 756                          if ($mode_fich_debug == "y") {
 757                              fwrite($fich, "\$client[\"hostname\"][$compteur_clients]=" . $client["hostname"][$compteur_clients] . "\n");
 758                          }
 759  
 760                          if ($client["hostname"][$compteur_clients] == gettext("unresolved")) {
 761                              // Le nom n'a pas ete trouve dans le dhcpd.leases pour cette section
 762  
 763                              if ($mode_fich_debug == "y") {
 764                                  fwrite($fich, "\$client[\"hostname\"][$compteur_clients] est  unresolved.\n");
 765                              }
 766  
 767                              //$list_computer=search_machines("(&(cn=*)(ipHostNumber=$ip[0]))","computers");
 768                              $list_computer = search_machines("(&(cn=*)(macAddress=$macaddr[0]))", "computers");
 769                              if (count($list_computer) > 1) {
 770                                  $resolutiondunom = "doublon_ldap";
 771                                  $client["hostname"][$compteur_clients] = $resolutiondunom;
 772  
 773                                  if ($mode_fich_debug == "y") {
 774                                      fwrite($fich, "\$client[\"hostname\"][$compteur_clients]=" . $resolutiondunom . "\n");
 775                                  }
 776                              } elseif (count($list_computer) > 0) {
 777                                  $resolutiondunom = $list_computer[0]['cn'];
 778                                  $client["hostname"][$compteur_clients] = $resolutiondunom;
 779  
 780                                  if ($mode_fich_debug == "y") {
 781                                      fwrite($fich, "\$client[\"hostname\"][$compteur_clients]=" . $resolutiondunom . "\n");
 782                                  }
 783                              } elseif ($mode_fich_debug == "y") {
 784                                  fwrite($fich, "Adresse Mac non trouvee dans le LDAP.\n");
 785                              }
 786  
 787                              $tab_recherche_ldap_faite[] = $macaddr[0];
 788  
 789                              for ($loop = 0; $loop < count($list_computer); $loop++) {
 790                                  //echo " ".$list_computer[$loop]['cn'];
 791                                  if (($mode_debug == 'y') ||
 792                                          (!in_array(strtolower($list_computer[$loop]['cn']), $liste_noms_ldap["$macaddr[0]"]))) {
 793                                      $liste_noms_ldap["$macaddr[0]"][] = strtolower($list_computer[$loop]['cn']);
 794  
 795                                      if ($mode_fich_debug == "y") {
 796                                          fwrite($fich, "\$liste_noms_ldap[\"$macaddr[0]\"][]=" . strtolower($list_computer[$loop]['cn']) . "\n");
 797                                      }
 798                                  }
 799                              }
 800                          } else {
 801                              // Il y a un hostname dans le lease
 802                              if (!in_array($macaddr[0], $tab_recherche_ldap_faite)) {
 803                                  // On controle quand meme si il y a d'autres noms dans le LDAP (pour affichage)
 804                                  $list_computer = search_machines("(&(cn=*)(macAddress=$macaddr[0]))", "computers");
 805  
 806                                  if (count($list_computer) > 0) {
 807                                      for ($loop = 0; $loop < count($list_computer); $loop++) {
 808                                          //echo " ".$list_computer[$loop]['cn'];
 809                                          if (($mode_debug == 'y') ||
 810                                                  (!in_array(strtolower($list_computer[$loop]['cn']), $liste_noms_ldap["$macaddr[0]"]))) {
 811                                              $liste_noms_ldap["$macaddr[0]"][] = strtolower($list_computer[$loop]['cn']);
 812  
 813                                              if ($mode_fich_debug == "y") {
 814                                                  fwrite($fich, "\$liste_noms_ldap[\"$macaddr[0]\"][]=" . strtolower($list_computer[$loop]['cn']) . "\n");
 815                                              }
 816                                          }
 817                                      }
 818                                  } elseif ($mode_fich_debug == "y") {
 819                                      fwrite($fich, "Adresse Mac non trouvee dans le LDAP.\n");
 820                                  }
 821                              }
 822                          }
 823                          $client["ip"][$compteur_clients] = $ip[0];
 824                          $client["parc"][$compteur_clients] = search_parcs($clienthostname[0]);
 825                          $compteur_clients++;
 826                      } else {
 827                          // On controle quand meme si il y a d'autres noms dans le LDAP (pour affichage)
 828                          if (!in_array($macaddr[0], $tab_recherche_ldap_faite)) {
 829                              $list_computer = search_machines("(&(cn=*)(macAddress=$macaddr[0]))", "computers");
 830  
 831                              if (count($list_computer) > 0) {
 832                                  for ($loop = 0; $loop < count($list_computer); $loop++) {
 833                                      //echo " ".$list_computer[$loop]['cn'];
 834                                      if (($mode_debug == 'y') ||
 835                                              (!in_array(strtolower($list_computer[$loop]['cn']), $liste_noms_ldap["$macaddr[0]"]))) {
 836                                          $liste_noms_ldap["$macaddr[0]"][] = strtolower($list_computer[$loop]['cn']);
 837  
 838                                          if ($mode_fich_debug == "y") {
 839                                              fwrite($fich, "\$liste_noms_ldap[\"$macaddr[0]\"][]=" . strtolower($list_computer[$loop]['cn']) . "\n");
 840                                          }
 841                                      }
 842                                  }
 843                              } elseif ($mode_fich_debug == "y") {
 844                                  fwrite($fich, "Adresse Mac non trouvee dans le LDAP.\n");
 845                              }
 846                          }
 847  
 848                          if (($mode_debug == 'y') ||
 849                                  ((strtolower($clienthostname[0]) != 'unresolved') &&
 850                                  (!in_array(strtolower($clienthostname[0]), $liste_noms_en_lease["$macaddr[0]"])))) {
 851                              $liste_noms_en_lease["$macaddr[0]"][] = strtolower($clienthostname[0]);
 852  
 853                              if ($mode_fich_debug == "y") {
 854                                  fwrite($fich, "\$liste_noms_en_lease[\"$macaddr[0]\"][]=" . strtolower($clienthostname[0]) . "\n");
 855                              }
 856                          }
 857  
 858                          // Et on met a jour l'IP en supposant que la derniere IP recue est la bonne
 859                          $tmp_tab = array();
 860                          $tmp_tab = $client["macaddr"];
 861                          $tmp_tab = array_flip($tmp_tab);
 862                          $indice = $tmp_tab[$macaddr[0]];
 863  
 864                          $liste_autres_ip[$macaddr[0]][] = $client['ip'][$indice];
 865                          $client['ip'][$indice] = $ip[0];
 866                      }
 867                  }
 868              }
 869          }
 870      }
 871  
 872      if (is_array($client["ip"])) {
 873          array_multisort($client["hostname"], SORT_ASC, $client["ip"], SORT_ASC, $client["macaddr"], SORT_ASC, $client["parc"]);
 874  
 875          $client['liste_noms_en_lease'] = $liste_noms_en_lease;
 876          $client['liste_noms_ldap'] = $liste_noms_ldap;
 877          $client['liste_autres_ip'] = $liste_autres_ip;
 878      } else {
 879          $client = "";
 880      }
 881  
 882      if ($mode_fich_debug == "y") {
 883          fwrite($fich, "=============================================\n");
 884          fclose($fich);
 885      }
 886  
 887      return $client;
 888  }
 889  
 890  function my_dhcp_form_lease($parser) {
 891      $mode_debug = "n"; // Si on passe cette variable a 'y', passer la meme variable a 'y' dans my_parse_dhcpd_lease()
 892  
 893      require_once("includes/ldap.inc.php");
 894  
 895      $content .= "<script type='text/javascript'>
 896  function checkAll_baux(){
 897      champs_input=document.getElementsByTagName('input');
 898      for(i=0;i<champs_input.length;i++){
 899          type=champs_input[i].getAttribute('type');
 900          if(type==\"checkbox\"){
 901              champs_input[i].checked=true;
 902          }
 903      }
 904  }
 905  function UncheckAll_baux(){
 906      champs_input=document.getElementsByTagName('input');
 907      for(i=0;i<champs_input.length;i++){
 908          type=champs_input[i].getAttribute('type');
 909          if(type==\"checkbox\"){
 910              champs_input[i].checked=false;
 911          }
 912      }
 913  }
 914  </script>\n";
 915  
 916      $content .= "<form name=\"lease_form\" method=post action=\"baux.php\">\n";
 917      $content .= "<table border=\"1\" width=\"90%\">\n";
 918      $header = "<tr class=\"menuheader\">\n";
 919      $header .="<td align=\"center\"><b>\n" . gettext("Adresse IP");
 920      $header .= "</b></td>\n";
 921      $header .="<td align=\"center\"><b>\n" . gettext("Adresse MAC");
 922      $header .="</b></td>\n";
 923      $header .="<td align=\"center\"><b>\n" . gettext("Nom NETBIOS");
 924      $header .="</b></td>\n";
 925      $header .="<td align=\"center\"><b>\n" . gettext("Parc(s)");
 926      $header .="</b></td>\n";
 927      $header .="<td align=\"center\">\n";
 928      $header .= "<input type='hidden' name='action' value='valid' />\n";
 929      $header .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Valider les actions") . "\" />\n";
 930      $header .= "</td>\n";
 931  //    $content .= "<a href='javascript: checkAll_baux();'><img src='../elements/images/enabled.gif' width='20' height='20' border='0' alt='Tout cocher' /></a> / <a href='javascript:UncheckAll_baux();'><img src='../elements/images/disabled.gif' width='20' height='20' border='0' alt='Tout d&#233;cocher' /></a>\n";
 932      $header .= "</td>\n";
 933      $header .="<td align=\"center\"><b>\n" . gettext("Identifiants admin local");
 934  
 935      $header .="</td></tr>\n";
 936      $content .= $header;
 937      $nbligne = 0;
 938  
 939      foreach ($parser["ip"] as $keys => $value) {
 940          if (!is_recorded_in_dhcp_database($parser["ip"][$keys], $parser["macaddr"][$keys], $parser["hostname"][$keys])) {
 941              $content .= "<tr>\n";
 942  
 943              $content .="<td>\n";
 944              $content .= "<input type=\"text\" maxlength=\"15\" size=\"15\" value=\"" . $parser["ip"][$keys] . "\"  name=\"ip[$keys]\" />\n";
 945              if (($mode_debug == 'y') && (count($parser['liste_autres_ip'][$parser["macaddr"][$keys]]) > 0)) {
 946                  for ($loop = 0; $loop < count($parser['liste_autres_ip'][$parser["macaddr"][$keys]]); $loop++) {
 947                      $content .="<br />\n" . $parser['liste_autres_ip'][$parser["macaddr"][$keys]][$loop];
 948                  }
 949              }
 950  
 951              $content .= "</td>\n";
 952  
 953              $content .="<td align='center'>\n";
 954              $content .= "<input type=\"hidden\" maxlength=\"17\" size=\"17\" value=\"" . $parser["macaddr"][$keys] . "\"  name=\"mac[$keys]\" />\n";
 955              ;
 956              $content .= $parser["macaddr"][$keys];
 957              $content .= "</td>\n";
 958  
 959              $content .="<td>\n";
 960              $content .= "<input type=\"text\" maxlength=\"20\" size=\"20\" value=\"" . $parser["hostname"][$keys] . "\"  name=\"name[$keys]\" />\n";
 961              $content .= "<input type=\"hidden\" maxlength=\"20\" SIZE=\"20\" value=\"" . $parser["hostname"][$keys] . "\"  name=\"oldname[$keys]\">\n";
 962  
 963              if ((count($parser['liste_noms_en_lease'][$parser["macaddr"][$keys]]) > 1) ||
 964                      (count($parser['liste_noms_ldap'][$parser["macaddr"][$keys]]) > 1) ||
 965                      ((isset($parser['liste_noms_en_lease'][$parser["macaddr"][$keys]][0])) && (strtolower($parser['liste_noms_en_lease'][$parser["macaddr"][$keys]][0]) != strtolower($parser["hostname"][$keys]))) ||
 966                      ((isset($parser['liste_noms_ldap'][$parser["macaddr"][$keys]][0])) && (strtolower($parser['liste_noms_ldap'][$parser["macaddr"][$keys]][0]) != strtolower($parser["hostname"][$keys])))
 967              ) {
 968                  $content .="<br />\n";
 969                  $content .="<table border='0'>\n";
 970  
 971                  if (($mode_debug == 'y') ||
 972                          (count($parser['liste_noms_en_lease'][$parser["macaddr"][$keys]]) > 1) ||
 973                          ((isset($parser['liste_noms_en_lease'][$parser["macaddr"][$keys]][0])) && (strtolower($parser['liste_noms_en_lease'][$parser["macaddr"][$keys]][0]) != strtolower($parser["hostname"][$keys])))) {
 974                      $content .="<tr>\n";
 975                      $content .="<th valign='top'>Leases:</th>\n";
 976                      $content .="<td>\n";
 977                      for ($loop = 0; $loop < count($parser['liste_noms_en_lease'][$parser["macaddr"][$keys]]); $loop++) {
 978                          $content .=$parser['liste_noms_en_lease'][$parser["macaddr"][$keys]][$loop] . "<br />\n";
 979                      }
 980                      $content .="</td>\n";
 981                      $content .="</tr>\n";
 982                  }
 983  
 984                  if (($mode_debug == 'y') ||
 985                          (count($parser['liste_noms_ldap'][$parser["macaddr"][$keys]]) > 1) ||
 986                          ((isset($parser['liste_noms_ldap'][$parser["macaddr"][$keys]][0])) && (strtolower($parser['liste_noms_ldap'][$parser["macaddr"][$keys]][0]) != strtolower($parser["hostname"][$keys])))) {
 987                      $content .="<tr>\n";
 988                      $content .="<th valign='top'>Ldap:</th>\n";
 989                      $content .="<td>\n";
 990                      for ($loop = 0; $loop < count($parser['liste_noms_ldap'][$parser["macaddr"][$keys]]); $loop++) {
 991                          $content .=$parser['liste_noms_ldap'][$parser["macaddr"][$keys]][$loop] . "<br />\n";
 992                      }
 993                      $content .="</td>\n";
 994                      $content .="</tr>\n";
 995                  }
 996  
 997                  $content .="</table>\n";
 998              }
 999  
1000              $content .="</td>\n";
1001  
1002              $content .="<td align=\"left\">\n";
1003              $showid = 0;
1004              $listaction = "";
1005              // Est-ce que cette machine est integree ?
1006              if (count(search_machines("(cn=" . $parser["hostname"][$keys] . ")", "computers")) > 0) {
1007                  if (isset($parser["parc"][$keys])) {
1008                      foreach ($parser["parc"][$keys] as $keys2 => $value2) {
1009  
1010                          $content.="<a href=../parcs/show_parc.php?parc=" . $parser["parc"][$keys][$keys2]["cn"] . ">" . $parser["parc"][$keys][$keys2]["cn"] . "</a><br>\n";
1011                      }
1012                  }
1013                  // ajouter a un parc dans lequel la machine n'est  pas ?
1014                  $content .= add_to_parc($parser["parc"][$keys], $keys);
1015                  // est ce que la machine est integree au domaine ?
1016                  if (count(search_machines("(uid=" . $parser["hostname"][$keys] . "$)", "computers")) > 0) {
1017  //                    $listaction .="<OPTION value=\"renommer\">Renommer\n"; 
1018                  } else { // this computer is not recorded on the domain
1019                      $content.="<br><FONT color='red'>" . gettext("Pas au domaine!") . "</FONT>\n";
1020  //                    $listaction .="<OPTION value=\"integrer\">Integrer\n";
1021  //                    $showid = 1;
1022                  }
1023              } else { // this computer is not registered in ldap
1024                  $content.="<FONT color='red'>" . gettext("Non enregistre") . "</FONT>\n";
1025  //                $listaction .="<OPTION value=\"integrer\">Integrer\n";
1026  //                $showid = 1; 
1027              }
1028              //
1029              $content .="</td><td align=\"center\">\n";
1030              $content .="<SELECT  name=\"action_res[$keys]\">";
1031              $content .="<OPTION value=\"none\">" . gettext("Action...");
1032              $content .= $listaction;
1033              $content .= "<OPTION value=\"reserver\">Reserver\n";
1034              $content .="</SELECT>\n";
1035              $content .="</td>\n";
1036              $content .="<td align=\"center\">\n";
1037              if ($showid) {
1038                  $content .= "Admin local : <input type=\"text\" maxlength=\"20\" SIZE=\"15\" value=\"administrateur\"  name=\"localadminname[$keys]\" ><br>\n";
1039                  $content .= "Mot de passe : <input type=\"text\" maxlength=\"20\" SIZE=\"8\" value=\"\"  name=\"localadminpasswd[$keys]\" ><br>\n";
1040              }
1041              $content .="</td></tr>\n";
1042              if ($nbligne++ == 10) {
1043                  $content .=$header;
1044                  $nbligne = 0;
1045              }
1046          }
1047      }
1048      $content .= "</table>\n";
1049  
1050      $content .= "<input type='hidden' name='action' value='valid' />\n";
1051      //$content .= "<input type=\"submit\" name=\"button\" value=\"".gettext("valider")."\">\n";
1052      $content .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Valider les r&#233;servations") . "\" />\n";
1053      $content .= "</form>\n";
1054      return $content;
1055  }
1056  
1057  /**
1058   * MAKE a form with lease info get in dhcpd.lease
1059  
1060   * @Parametres $parser : tableau : ip mac hostname
1061  
1062   * @Return Affichage HTML d'un form a partir du dhcp.leases
1063  
1064   */
1065  function dhcp_form_lease($parser) {
1066      require_once("includes/ldap.inc.php");
1067  
1068      $content .= "<script type='text/javascript'>
1069  function checkAll_baux(){
1070      champs_input=document.getElementsByTagName('input');
1071      for(i=0;i<champs_input.length;i++){
1072          type=champs_input[i].getAttribute('type');
1073          if(type==\"checkbox\"){
1074              champs_input[i].checked=true;
1075          }
1076      }
1077  }
1078  function UncheckAll_baux(){
1079      champs_input=document.getElementsByTagName('input');
1080      for(i=0;i<champs_input.length;i++){
1081          type=champs_input[i].getAttribute('type');
1082          if(type==\"checkbox\"){
1083              champs_input[i].checked=false;
1084          }
1085      }
1086  }
1087  </script>\n";
1088  
1089      $content .= "<form name=\"lease_form\" method=post action=\"baux.php\">\n";
1090      $content .= "<table border=\"1\" width=\"90%\">\n";
1091      $content .="<tr class=\"menuheader\"><td align=\"center\"><b>\n" . gettext("Adresse IP");
1092      $content .= "</b></td><td align=\"center\"><b>\n" . gettext("Adresse MAC");
1093      $content .="</b></td><td align=\"center\"><b>\n" . gettext("Nom NETBIOS");
1094      $content .="</b></td><td align=\"center\"><b>\n" . gettext("Parc(s)");
1095      $content .="</b></td><td align=\"center\"><b>\n" . gettext("Action");
1096      $content .= "</b></td></tr>\n";
1097      $content .= "</b><br />\n";
1098      $content .= "<a href='javascript: checkAll_baux();'><img src='../elements/images/enabled.gif' width='20' height='20' border='0' alt='Tout cocher' /></a> / <a href='javascript:UncheckAll_baux();'><img src='../elements/images/disabled.gif' width='20' height='20' border='0' alt='Tout d&#233;cocher' /></a>\n";
1099      $content .= "</td></tr>\n";
1100      foreach ($parser["ip"] as $keys => $value) {
1101          if (!is_recorded_in_dhcp_database($parser["ip"][$keys], $parser["macaddr"][$keys], $parser["hostname"][$keys])) {
1102              $content .= "<tr><td>\n";
1103              $content .= "<input type=\"text\" maxlength=\"15\" SIZE=\"15\" value=\"" . $parser["ip"][$keys] . "\"  name=\"ip[$keys]\" >\n";
1104              $content .= "</td><td>\n";
1105              $content .= "<input type=\"text\" maxlength=\"17\" SIZE=\"17\" value=\"" . $parser["macaddr"][$keys] . "\"  name=\"mac[$keys]\" >\n";
1106              $content .= "</td><td>\n";
1107              $content .= "<input type=\"text\" maxlength=\"20\" SIZE=\"20\" value=\"" . $parser["hostname"][$keys] . "\"  name=\"name[$keys]\" >\n";
1108              $content .= "<input type=\"hidden\" maxlength=\"20\" SIZE=\"20\" value=\"" . $parser["hostname"][$keys] . "\"  name=\"oldname[$keys]\">\n";
1109              $content .="</td><td align=\"left\">\n";
1110              $showid = 0;
1111              $listaction = "";
1112              // Est-ce que cette machine est integree ?
1113              if (count(search_machines("(cn=" . $parser["hostname"][$keys] . ")", "computers")) > 0) {
1114                  if (isset($parser["parc"][$keys])) {
1115                      foreach ($parser["parc"][$keys] as $keys2 => $value2) {
1116  
1117                          $content.="<a href=../parcs/show_parc.php?parc=" . $parser["parc"][$keys][$keys2]["cn"] . ">" . $parser["parc"][$keys][$keys2]["cn"] . "</a><br>\n";
1118                      }
1119                  }
1120                  // ajouter a un parc dans lequel la machine n'est  pas ?
1121                  $content .= add_to_parc($parser["parc"][$keys], $keys);
1122                  // est ce que la machine est integree au domaine ?
1123                  if (count(search_machines("(uid=" . $parser["hostname"][$keys] . "$)", "computers")) > 0) {
1124                      $listaction .="<OPTION value=\"renommer\">Renommer\n";
1125                  } else { // this computer is not recorded on the domain
1126                      $content.="<FONT color='red'>" . gettext("Pas au domaine!") . "</FONT>\n";
1127                      $listaction .="<OPTION value=\"integrer\">Integrer\n";
1128                      $showid = 1;
1129                  }
1130              } else { // this computer is not registered in ldap
1131                  $content.="<FONT color='red'>" . gettext("Non enregistre") . "</FONT>\n";
1132                  $listaction .="<OPTION value=\"integre\">Integrer\n";
1133                  $showid = 1;
1134              }
1135              //
1136              $content .="</td><td align=\"center\">\n";
1137              $content .="<SELECT  name=\"action_res[$keys]\">";
1138              $content .="<OPTION value=\"none\">" . gettext("Action...");
1139              $content .= $listaction;
1140              $content .= "<OPTION value=\"reserver\">Reserver\n";
1141              $content .="</SELECT>\n";
1142              $content .="</td>\n";
1143              $content .="<td align=\"center\">\n";
1144              if ($showid) {
1145                  $content .= "Admin local : <input type=\"text\" maxlength=\"20\" SIZE=\"15\" value=\"administrateur\"  name=\"localadminname[$keys]\" ><br>\n";
1146                  $content .= "Mot de passe : <input type=\"text\" maxlength=\"20\" SIZE=\"8\" value=\"\"  name=\"localadminpasswd[$keys]\" ><br>\n";
1147              }
1148              $content .="</td></tr>\n";
1149          }
1150      }
1151      $content .= "</table>\n";
1152      $content .= "<input type='hidden' name='action' value='valid'>\n";
1153      //$content .= "<input type=\"submit\" name=\"button\" value=\"".gettext("valider")."\">\n";
1154      $content .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Valider les r&#233;servations") . "\">\n";
1155      $content .= "</form>";
1156      return $content;
1157  }
1158  
1159  /**
1160   * form to modify entry in dhcpd reservation
1161  
1162   * @Parametres $error : Message d'erreur
1163  
1164   * @Return Affichage HTML d'un form
1165  
1166   */
1167  function form_existing_reservation() {
1168      require_once("includes/ldap.inc.php");
1169      require_once  ("ihm.inc.php");
1170      require_once  ("printers.inc.php");
1171      // Recuperation des donnees dans la base SQL
1172  
1173      if (($_GET['order'] == "") || ($_GET['order'] == "ip")) {
1174          $query = "SELECT * FROM `se3_dhcp` ORDER BY INET_ATON(IP) ASC";
1175      } else {
1176          $query = "SELECT * FROM `se3_dhcp` ORDER BY " . $_GET['order'] . " ASC";
1177      }
1178      $result = mysql_query($query);
1179      
1180      
1181      //recup liste ip imprimantes
1182      $liste_imprimantes=search_imprimantes("printer-name=*","printers");
1183      //$resultat=search_imprimantes("printer-name=$mpenc","printers");
1184              for ($loopp=0; $loopp < count($liste_imprimantes); $loopp++) {
1185                      
1186                                          $printer_uri = $liste_imprimantes[$loopp]['printer-uri'];
1187                                          $printer_name = $liste_imprimantes[$loopp]['printer-name'];
1188                                          //echo "liste imp : $printer_name $printer_uri" ;
1189                      continue;
1190                      
1191              }
1192      
1193      
1194      $clef = 0;
1195  
1196      $content .= "<script type='text/javascript'>
1197  function checkAll_reservations(){
1198      champs_input=document.getElementsByTagName('input');
1199      for(i=0;i<champs_input.length;i++){
1200          type=champs_input[i].getAttribute('type');
1201          if(type==\"checkbox\"){
1202              champs_input[i].checked=true;
1203          }
1204      }
1205  }
1206  function UncheckAll_reservations(){
1207      champs_input=document.getElementsByTagName('input');
1208      for(i=0;i<champs_input.length;i++){
1209          type=champs_input[i].getAttribute('type');
1210          if(type==\"checkbox\"){
1211              champs_input[i].checked=false;
1212          }
1213      }
1214  }
1215  </script>\n";
1216  
1217      $content .= "<form name=\"lease_form\" method=post action=\"reservations.php\">\n";
1218      $content .= "<table border=\"1\" width=\"90%\">\n";
1219      $header = "<tr class=\"menuheader\"><td align=\"center\"><b>\n<a href=\"reservations.php?order=ip\">" . gettext("Adresse IP") . "</a>";
1220      $header .= "</b></td><td align=\"center\"><b>\n<a href=\"reservations.php?order=mac\">" . gettext("Adresse MAC") . "</a>";
1221      $header .="</b></td><td align=\"center\"><b>\n<a href=\"reservations.php?order=name\">" . gettext("Nom NETBIOS") . "</a>";
1222      $header .="</b></td><td align=\"center\"><b>\n" . gettext("Parc(s)");
1223      $header .="</b></td><td align=\"center\"><b>\n";
1224      $header .= "<input type='hidden' name='action' value='valid' />\n";
1225      $header .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Valider les actions") . "\" />\n";
1226      $header .= "</td>\n";
1227      $header .="<td align=\"center\"><b>\n" . gettext("identifiants pour l'acces distant");
1228      $header .= "</b><br />\n";
1229      $header .= "</td></tr>\n";
1230      $content .= $header;
1231      $nbligne = 0;
1232      while ($row = mysql_fetch_assoc($result)) {
1233          $content .= "<tr><td>\n";
1234          $content .= "<input type=\"text\" maxlength=\"15\" SIZE=\"15\" value=\"" . $row["ip"] . "\"  name=\"ip[$clef]\">\n";
1235  //        $content .= "<input type=\"hidden\" maxlength=\"15\" SIZE=\"15\" value=\"".$row["ip"]."\"  name=\"oldip[$clef]\">\n";
1236          $content .= "</td><td>\n";
1237          $content .= "<input type=\"text\" maxlength=\"17\" SIZE=\"17\" value=\"" . strtolower($row["mac"]) . "\"  name=\"mac[$clef]\" readonly>\n";
1238          ;
1239          $content .= "</td><td>\n";
1240          $content .= "<input type=\"text\" maxlength=\"20\" SIZE=\"20\" value=\"" . $row["name"] . "\"  name=\"name[$clef]\">\n";
1241          $content .= "<input type=\"hidden\" maxlength=\"20\" SIZE=\"20\" value=\"" . $row["name"] . "\"  name=\"oldname[$clef]\">\n";
1242          
1243          for ($loopp=0; $loopp < count($liste_imprimantes); $loopp++) {
1244                      
1245                                          $printer_uri = $liste_imprimantes[$loopp]['printer-uri'];
1246                                          $printer_name = $liste_imprimantes[$loopp]['printer-name'];
1247                                          //echo "liste imp : $printer_name $printer_uri" ;
1248                                          if (preg_match("/$row[ip]/", $printer_uri)) { 
1249                                              $suisje_printer = "yes";
1250                                              break;    
1251                                          } else { $suisje_printer = "no" ;
1252                                          
1253                                              
1254                                          }
1255                                                  
1256              }
1257          if  ($suisje_printer=="yes") {
1258                          $content.="<br><FONT color='blue'>" . gettext("Imprimante $printer_name") . "</FONT>\n"; 
1259                      }
1260          
1261          $content .="</td><td>\n";
1262          $showid = 0;
1263          $listaction = "";
1264          // Est-ce que cette machine est enregistree ?
1265          $parc[$clef] = search_parcs($row["name"]);
1266          if ((count(search_machines("(cn=" . $row["name"] . ")", "computers"))) > 0) {
1267              if (isset($parc[$clef])) {
1268                  foreach ($parc[$clef] as $keys2 => $value2) {
1269                      $content.="<a href=../parcs/show_parc.php?parc=" . $parc[$clef][$keys2]["cn"] . ">" . $parc[$clef][$keys2]["cn"] . "</a><br>\n";
1270                  }
1271              }
1272              // ajouter a un parc dans lequel la machine n'est  pas ?
1273              $content .= add_to_parc($parc[$clef], $clef);
1274              // windows linux ou imprimante ?
1275              
1276              // est ce que la machine est integree au domaine ?
1277              if (count(search_machines("(uid=" . $row["name"] . "$)", "computers")) > 0) {
1278                  $listaction .="<OPTION value=\"renommer\">Renommer un poste win &#224; distance\n";
1279                  $listaction .="<OPTION value=\"renommer\">Renommer un poste linux &#224; distance\n";
1280                  $listaction .="<OPTION value=\"reintegrer\">R&#233;int&#233;grer\n";
1281              } else { // this computer is not recorded on the domain
1282                  // une imprimante ?
1283                  
1284                  
1285                  
1286                      if  ($suisje_printer=="yes") { 
1287                          $listaction .="<OPTION value=\"renommer-base\">Renommer dans la base\n";
1288                           
1289                      }
1290                      else {
1291                      $content.="<br><FONT color='red'>" . gettext("Pas au domaine!") . "</FONT>\n"; 
1292                      $listaction .="<OPTION value=\"renommer-base\">Renommer dans la base\n";
1293                      $listaction .="<OPTION value=\"renommer-linux\">Renommer un poste linux\n";
1294                      $listaction .="<OPTION value=\"integrer\">Integrer un windows au domaine\n";
1295                  
1296                      }
1297                  $showid = 1;
1298              }
1299          } else { // this computer is not registered in ldap
1300              $list_computer = search_machines("(&(ipHostNumber=" . $row['ip'] . ")(macAddress=" . $row['mac'] . "))", "computers");
1301              if (count($list_computer) > 0) {
1302                  $content.="<FONT color='red'>" . gettext("Autre nom : ") . $list_computer[0]["cn"] . "</FONT>\n";
1303                  $content .= "<input type=\"hidden\" value=\"" . $list_computer[0]["cn"] . "\"  name=\"name[$clef]\">\n";
1304                  $listaction .="<OPTION value=\"actualiser\">Actualiser la reservation\n";
1305              } else {
1306                  $content.="<FONT color='red'>" . gettext("Non enregistr&#233;e") . "</FONT>\n";
1307                  $listaction .="<OPTION value=\"integrer\">Integrer\n";
1308                  $listaction .="<OPTION value=\"renommer-base\">Renommer dans la base\n";
1309                  $showid = 1;
1310              }
1311          }
1312          $content .="</td><td align=\"center\">\n";
1313          $content .="<SELECT  name=\"action_res[$clef]\">";
1314          $content .="<OPTION value=\"none\">" . gettext("Action...");
1315          $content .= $listaction;
1316          $content .= "<OPTION value=\"newip\">Changer l'adresse ip\n";
1317          $content .= "<OPTION value=\"supprimer\">Supprimer la reservation\n";
1318          $content .="</SELECT>\n";
1319          $content .="</td>\n";
1320          $content .="<td align=\"center\">\n";
1321          if ($showid) {
1322              $content .= "Admin local : <input type=\"text\" maxlength=\"20\" SIZE=\"15\" value=\"administrateur\"  name=\"localadminname[$clef]\" ><br>\n";
1323              $content .= "Mot de passe : <input type=\"text\" maxlength=\"20\" SIZE=\"8\" value=\"\"  name=\"localadminpasswd[$clef]\" ><br>\n";
1324          }
1325          $content .="</td></tr>\n";
1326          if ($nbligne++ == 10) {
1327              $content .=$header;
1328              $nbligne = 0;
1329          }
1330          $clef++;
1331      }
1332      $content .= "</table>\n";
1333      $content .= "<input type='hidden' name='action' value='valid'>\n";
1334      $content .= "<input type=\"submit\" name=\"button\" value=\"" . gettext("Valider les modifications") . "\">\n";
1335      $content .= "</form>";
1336  
1337      return $content;
1338  }
1339  
1340  // Return select form whith parc where host is not recorded
1341  
1342  /**
1343   * Return select form whith parc where host is not recorded
1344  
1345   * @Parametres $parcs parc dans lequel on veut ajouter -  $keys
1346  
1347   * @Return Affichage HTML d'un select avec la liste des parcs
1348  
1349   */
1350  function add_to_parc($parcs, $keys) {
1351      require_once("includes/ldap.inc.php");
1352      $liste_parcs = search_machines("objectclass=groupOfNames", "parcs");
1353      if (count($liste_parcs)>0) {
1354          $ret .="<SELECT  name=\"parc[$keys]\">";
1355          $ret .="<OPTION value=\"none\">" . gettext("Ajouter &#224; un parc...");
1356          foreach ($liste_parcs as $keys => $value) {
1357              if (is_array($parcs)) {
1358                  foreach ($parcs as $keys2 => $value2) {
1359                      $parc_tab[] = $parcs[$keys2]["cn"];
1360                  }
1361              } else {
1362                  $parc_tab[] = "";
1363              }
1364              if (!in_array($value['cn'], $parc_tab)) {
1365                  $ret .="<OPTION value=\"" . $value['cn'] . "\">" . $value['cn'] . "\n";
1366              }
1367          }
1368          $ret .= "</SELECT>";
1369      }
1370      return $ret;
1371  }
1372  
1373  /**
1374   *  renvoie l'ip si elle est libre ou une ip fixe libre dans le meme vlan 
1375  
1376   * @Parametres $ip : Adresse IP a tester
1377  
1378   * @Return  adresse ip libre
1379  
1380   */
1381  function get_free_ip($ip) {
1382      include ("config.inc.php");
1383      require_once  ("ihm.inc.php");
1384      $reseau = get_vlan($ip);
1385      $calcul_ip = floatval(sprintf("%u", ip2long($ip)));
1386      $ipmin = floatval(sprintf("%u", $reseau['ipmin']));
1387      $ipmax = floatval(sprintf("%u", $reseau['ipmax']));
1388      $gateway = floatval(sprintf("%u", $reseau['gateway']));
1389      $begin_range = floatval(sprintf("%u", $reseau['begin_range']));
1390      $end_range = floatval(sprintf("%u", $reseau['end_range']));
1391  
1392  
1393      if ((reservation($ip)) or ($ip == long2ip($reseau['gateway'])) or (($calcul_ip >= $begin_range) and ($calcul_ip <= $end_range))) {
1394          $calcul_free = $reseau['ipmin'];
1395          while (floatval(sprintf("%u", $calcul_free)) <= $ipmax) {
1396              if ((fping(long2ip($calcul_free)) == 1) or (reservation(long2ip($calcul_free)) or ($calcul_free == $reseau['gateway']))) {
1397                  $calcul_free++;
1398              } elseif ($calcul_free == $reseau['begin_range']) {
1399                  $calcul_free = $reseau['end_range'] + 1;
1400              } else {
1401  //                print long2ip($calcul_free) . "le coupable<br>";
1402                  return long2ip($calcul_free);
1403                  $ipbuzy=1;
1404                  return $ipbuzy;
1405              }
1406          }
1407      } else {
1408          // l'ip est libre
1409          return $ip;
1410      }
1411  }
1412  
1413  /**
1414   *  renvoie les caracteristiques du vlan correspondant a l'ip 
1415  
1416   * @Parametres $ip : Adresse IP a tester
1417  
1418   * @Return  tableau associatif $reseau
1419  
1420   */
1421  function get_vlan($ip) {
1422      include ("config.inc.php");
1423      require_once  ("ihm.inc.php");
1424      if ($dhcp_vlan == 0) {
1425          return get_network();
1426      } else {
1427          $reseau = get_network();
1428          $calcul_inetaddr = ip2long($ip);
1429          foreach ($reseau as $key => $value) {
1430              if ($calcul_inetaddr == ($calcul_inetaddr & $value['broadcast'])) {
1431                  print "L'ip est sur le vlan : $key<br>";
1432                  return $value;
1433              }
1434          }
1435      }
1436  }
1437  
1438  /**
1439   *  renvoie les caracteristiques de tous vlan 
1440  
1441   * @Parametre aucun
1442  
1443   * @Return  $reseau[$vlan] tableau associatif avec les ip sous forme binaire
1444  
1445   */
1446  function get_network() {
1447      include ("config.inc.php");
1448      require_once ("fonc_outils.inc.php");
1449      require_once  ("ihm.inc.php");
1450      if ($dhcp_vlan == 0) {
1451          $ifconfig = ifconfig();
1452          $reseau['network'] = ip2long($ifconfig['network']);
1453          if (ip2long($dhcp_ip_min) > $reseau['network']) {
1454              $reseau['ipmin'] = ip2long($dhcp_ip_min);
1455          } else {
1456              $reseau['ipmin'] = $reseau['network'] + 51;
1457              $update_query = "UPDATE params SET value='" . long2ip($reseau['ipmin']) . "' WHERE name='dhcp_ip_min'";
1458              mysql_query($update_query);
1459          }
1460          $reseau['mask'] = ip2long($ifconfig['mask']);
1461          $reseau['broadcast'] = ip2long($ifconfig['broadcast']);
1462          $reseau['ipmax'] = $reseau['broadcast'] - 1;
1463          $reseau['begin_range'] = ip2long($dhcp_begin_range);
1464          $reseau['end_range'] = ip2long($dhcp_end_range);
1465          $reseau['gateway'] = ip2long($dhcp_gateway);
1466      } else {
1467          for ($vlan = 0; $vlan <= $dhcp_vlan; $vlan++) {
1468              $nomvar = "dhcp_reseau_" . $vlan;
1469              if (isset($$nomvar)) {
1470                  $reseau[$vlan]['network'] = ip2long($$nomvar);
1471                  $nomvar = "dhcp_ip_min_" . $vlan;
1472                  if (ip2long($$nomvar) > $reseau[$vlan]['network']) {
1473               $reseau[$vlan]['ipmin'] = ip2long($$nomvar);
1474                  } else {
1475                      $reseau[$vlan]['ipmin'] = $reseau[$vlan]['network'] + 51;
1476                      $update_query = "UPDATE params SET value='" . long2ip($reseau[$vlan]['ipmin']) . "' WHERE name='dhcp_ip_min_" . $_POST['vlan'] . "'";
1477                      mysql_query($update_query);
1478                  }
1479                  $nomvar = "dhcp_masque_" . $vlan;
1480                  $reseau[$vlan]['mask'] = ip2long($$nomvar);
1481                  $reseau[$vlan]['broadcast'] = $reseau[$vlan]['network'] | ~$reseau[$vlan]['mask'];
1482                  $reseau[$vlan]['ipmax'] = $reseau[$vlan]['broadcast'] - 1;
1483                  $nomvar = "dhcp_begin_range_" . $vlan;
1484                  $reseau[$vlan]['begin_range'] = ip2long($$nomvar);
1485                  $nomvar = "dhcp_end_range_" . $vlan;
1486                  $reseau[$vlan]['end_range'] = ip2long($$nomvar);
1487                  $nomvar = "dhcp_gateway_" . $vlan;
1488                  $reseau[$vlan]['gateway'] = ip2long($$nomvar);
1489              }
1490          }
1491      }
1492      return $reseau;
1493  }
1494  
1495  /**
1496   * Verifie si l'entree est dans la base SQL
1497  
1498   * @Parametres $ip - $mac - $hostname
1499  
1500   * @Return True - False
1501  
1502   */
1503  function is_recorded_in_dhcp_database($ip, $mac, $hostname) {
1504      require_once  ("ihm.inc.php");
1505      // Recuperation des donnees dans la base SQL
1506      $query = "SELECT * from se3_dhcp where ip='$ip' and mac='$mac' and name='$hostname'";
1507      $result = mysql_query($query);
1508      $resultat = mysql_fetch_assoc($result);
1509      if ($resultat == "") {
1510          return FALSE;
1511      } else {
1512          return TRUE;
1513      }
1514  }
1515  
1516  /**
1517   * Test la presence d'une adresse MAC dans la table se3_dhcp
1518  
1519   * @Parametres  $mac
1520  
1521   * @Return  True - False
1522  
1523   */
1524  function registred($mac) {
1525      require_once  ("ihm.inc.php");
1526      $query = "SELECT * FROM `se3_dhcp` WHERE mac='$mac'";
1527      $result = mysql_query($query);
1528      $resultat = mysql_fetch_assoc($result);
1529      if ($resultat == "") {
1530          return FALSE;
1531      } else {
1532          return TRUE;
1533      }
1534  }
1535  
1536  /**
1537   * Test la presence d'une adresse ip dans la table se3_dhcp
1538  
1539   * @Parametres  $ip
1540  
1541   * @Return  True - False
1542  
1543   */
1544  function reservation($ip) {
1545      require_once  ("ihm.inc.php");
1546      $query = "SELECT * FROM `se3_dhcp` WHERE ip='$ip'";
1547      $result = mysql_query($query);
1548      $resultat = mysql_fetch_assoc($result);
1549      if ($resultat == "") {
1550          return FALSE;
1551      } else {
1552          return TRUE;
1553      }
1554  }
1555  
1556  // add entry in se3_dhcp mysql table for reservation
1557  
1558  /**
1559   * add entry in se3_dhcp mysql table for reservation
1560  
1561   * @Parametres $ip - $mac - $name
1562  
1563   * @Return $ret
1564  
1565   */
1566  function add_reservation($ip, $mac, $name) {
1567      require_once  ("ihm.inc.php");
1568      $ret="";
1569      if (set_ip_in_lan($ip)) {
1570          $oldip= $ip;
1571          $ip = get_free_ip($ip);
1572          if ("$ip" == "") {
1573              $ret = gettext("<FONT color='red'> Attention : Impossible de r&#233;server une ip dans ce vlan </FONT>");
1574          } else {
1575              $error = already_exist($ip, $name, $mac);
1576              if ($error == "") {
1577                    
1578                  $insert_query = "INSERT INTO `se3_dhcp` (`ip`, `mac`, `name`) VALUES ('$ip', '$mac', '$name')";
1579                  mysql_query($insert_query);
1580                  exec("/usr/bin/sudo /usr/share/se3/scripts/integreDomaine.sh ldap $name $ip $mac $admin $adminpasswd");
1581                  if ($ip != $oldip) {
1582                      $ret = gettext("<FONT color='red'> Attention : </FONT>l'adresse choisie pour cette machine est d&#233;j&#224; prise ou elle se situe dans la plage dynamique $name --> $oldip,recherche d'une adresse libre...<br>"); 
1583                    }
1584                  $ret .= gettext("Mise en place d'une r&#233;servation pour la machine  $name --> $ip");
1585              }
1586          }
1587      } else {
1588          $ret = gettext("<FONT color='red'> Attention : l'addresse choisie pour cette machine n'est pas valide </FONT>" . "$name --> $ip");
1589      }
1590      return $ret;
1591  }
1592  
1593  /**
1594   * Test si une reservation existe deja pour cette machine
1595  
1596   * @Parametres $ip : ip de la machine - $name : nom de la machine - $mac : adresse mac de la machine
1597  
1598   * @Return Affichage HTML si la machine existe deja
1599  
1600   */
1601  function already_exist($ip, $name, $mac) {
1602      require_once  ("ihm.inc.php");
1603      // Recuperation des donnees dans la base SQL
1604      $query = "SELECT * from se3_dhcp where ip='$ip'";
1605      $result = mysql_query($query);
1606      $resultat = mysql_fetch_assoc($result);
1607      if ($resultat == "") {
1608          $error = "";
1609      } else {
1610          $error = gettext("Cette adresse ip est d&#233;j&#224; utilis&#233;e : " . $ip) . "\n<br />";
1611      }
1612  
1613      $query = "SELECT * from se3_dhcp where mac='$mac'";
1614      $result = mysql_query($query);
1615      $resultat = mysql_fetch_assoc($result);
1616      if ($resultat == "") {
1617          $error.="";
1618      } else {
1619          $error.=gettext("Cette adresse mac est d&#233;j&#224; utilis&#233;e : " . $mac) . "\n<br />";
1620      }
1621  
1622      $query = "SELECT * from se3_dhcp where name='$name'";
1623      $result = mysql_query($query);
1624      $resultat = mysql_fetch_assoc($result);
1625      if ($resultat == "") {
1626          $error.="";
1627      } else {
1628          $error.=gettext("Ce nom est d&#233;j&#224; utilis&#233; : " . $name) . "\n<br />";
1629      }
1630      return $error;
1631  }
1632  
1633  /**
1634   * Supprime une reservation
1635  
1636   * @Parametres $ip : Ip de la machine - $mac : Adresse mac de la machine - $name : Nom de la machine
1637  
1638   * @Return Message d'erreur SQL en cas de non suppression
1639  
1640   */
1641  function suppr_reservation($ip, $mac, $name) {
1642      require_once  ("ihm.inc.php");
1643      $error = "Suppression de l'entr&#233;e pour la machine $name --> $ip<br>";
1644      $suppr_query = "DELETE FROM `se3_dhcp` where `ip` = '$ip' AND `mac` = '$mac' AND  `name` = '$name'";
1645      mysql_query($suppr_query);
1646      return $error;
1647  }
1648  /**
1649   * renomme une machine sous linux 
1650  
1651   * @Parametres $ip : Ip de la machine - $mac : Adresse mac de la machine - $name : Nom de la machine
1652  
1653   * @Return Message d'erreur SQL en cas de d'echec de l'update
1654  
1655   */
1656  
1657  /**
1658   * renomme une machine sous linux 
1659  
1660   * @Parametres $ip : Ip de la machine - $mac : Adresse mac de la machine - $name : Nom de la machine
1661  
1662   * @Return Message d'erreur SQL en cas de d'echec de l'update
1663  
1664   */
1665  
1666  function renomme_linux($ip, $mac, $name) {
1667  require_once  ("ihm.inc.php");
1668  $ret = "Renommage du poste $ip <br>";
1669  $scriptfile="/tmp/rename.sh";
1670  if (file_exists($scriptfile)) unlink($scriptfile);
1671  
1672  $fich=fopen("$scriptfile","w+");
1673  fwrite($fich,"#!/bin/bash
1674  echo \"$name\" > \"/etc/hostname\"
1675  invoke-rc.d hostname.sh stop 
1676  invoke-rc.d hostname.sh start
1677  
1678  echo \"
1679  127.0.0.1    localhost
1680  127.0.1.1    $name
1681  
1682  # The following lines are desirable for IPv6 capable hosts
1683  ::1      ip6-localhost ip6-loopback
1684  fe00::0  ip6-localnet
1685  ff00::0  ip6-mcastprefix
1686  ff02::1  ip6-allnodes
1687  ff02::2  ip6-allrouters
1688  \" > \"/etc/hosts\"
1689  
1690  reboot");
1691  fclose($fich);
1692  
1693  // Copie du script sur l'esclave avec scp
1694  exec ("/usr/bin/scp $scriptfile root@$ip:/tmp/", $AllOutput, $ReturnValue);
1695          // chmod +x du script bash
1696          exec ("ssh -l root  $ip 'chmod +x $scriptfile ; $scriptfile'", $AllOutput, $ReturnValue);
1697          if ($ReturnValue==0) {
1698                  echo "renommage distant en cours....";
1699  
1700          } else {
1701                  echo "renommage distant impossible ";
1702          }
1703  return $ret;
1704  
1705  }   
1706  
1707  
1708  
1709  
1710  
1711  
1712  
1713  
1714  
1715  
1716  /**
1717   * renomme une reservation et met a jour l'enregistrement ldap
1718  
1719   * @Parametres $ip : Ip de la machine - $mac : Adresse mac de la machine - $name : Nom de la machine
1720  
1721   * @Return Message d'erreur SQL en cas de d'echec de l'update
1722  
1723   */
1724  function renomme_reservation($ip, $mac, $name) {
1725      require_once  ("ihm.inc.php");
1726      $ret = "Modification de l'entr&#233;e pour la machine $name : $ip<br>";
1727      $error = already_exist("ipbidon", $name, "macbidon");
1728      if ($error == "") {
1729          $update_query = "UPDATE se3_dhcp SET name='$name'  where `ip` = '$ip' AND `mac` = '$mac'";
1730          mysql_query($update_query);
1731          $ret .= exec("/usr/bin/sudo /usr/share/se3/scripts/integreDomaine.sh ldap $name $ip $mac");
1732          
1733          return $ret;
1734      } else {
1735          return $error;
1736      }
1737  }
1738  
1739  /**
1740   * change l'ip d'une reservation et met a jour l'enregistrement ldap
1741  
1742   * @Parametres $ip : Ip de la machine - $mac : Adresse mac de la machine - $name : Nom de la machine
1743  
1744   * @Return Message d'erreur SQL en cas de d'echec de l'update
1745  
1746   */
1747  function change_ip_reservation($ip, $mac, $name) {
1748      require_once  ("ihm.inc.php");
1749      $ret = "";
1750      if (set_ip_in_lan($ip)) {
1751          $oldip= $ip;
1752          $ip = get_free_ip($ip);
1753          if ("$ip" == "") {
1754              $ret = gettext("Impossible de reserver une ip dans ce vlan");
1755          } else {
1756              $ret = already_exist($ip, "nombidon", "macbidon");
1757              if ($ret == "") {
1758                  $update_query = "UPDATE se3_dhcp SET ip='$ip'  where `name` = '$name' AND `mac` = '$mac'";
1759                  mysql_query($update_query);
1760                  $ret .= exec("/usr/bin/sudo /usr/share/se3/scripts/integreDomaine.sh ldap $name $ip $mac");
1761                  if ($ip != $oldip) {
1762                      $ret .= gettext("<FONT color='red'> Attention : </FONT>l'adresse choisie pour cette machine est d&#233;j&#224; prise ou elle se situe dans la plage dynamique $name --> $oldip,recherche d'une adresse libre...<br>"); 
1763                    }
1764                  $ret .= gettext("R&#233;servation modifi&#233;e pour la machine $name  : $ip");
1765              }
1766          }
1767      } else {
1768          $ret = gettext("Cette addresse n'est pas valide : " . $ip);
1769      }
1770      return $ret;
1771  }
1772  
1773  /**
1774   * Indique l'etat du serveur DHCP
1775  
1776   * @Parametres
1777   * @Return Affichage HTML sur l'etat
1778  
1779   */
1780  function dhcpd_status() {
1781      exec("sudo /usr/share/se3/scripts/makedhcpdconf state", $ret);
1782      if ($ret[0] == "1") {
1783          $content .= gettext("Le serveur DHCP est : ") . "<FONT color='green'>" . gettext("actif") . "</FONT>";
1784      } else {
1785          $content .= gettext("Le serveur DHCP est : ") . "<FONT color='red'>" . gettext("inactif") . "</FONT>";
1786      }
1787      return $content;
1788  }
1789  
1790  /**
1791   * Redemarre le serveur DHCP
1792  
1793   * @Parametres
1794  
1795   * MongoUpdateBatch@Return
1796  
1797   */
1798  function dhcpd_restart() {
1799      exec("sudo /usr/share/se3/scripts/makedhcpdconf", $ret);
1800  }
1801  
1802  /**
1803   * Stop le serveur DHCP
1804  
1805   * @Parametres
1806   * MongoUpdateBatch@Return
1807  
1808   */
1809  function dhcpd_stop() {
1810      exec("sudo /usr/share/se3/scripts/makedhcpdconf stop", $ret);
1811  }
1812  
1813  /**
1814   * Valide le nom d'une machine
1815  
1816   * @Parametres  $nom : Nom a valider
1817  
1818   * @Return  0 si faux - 1 si Ok
1819  
1820   */
1821  function valid_name($nom) {
1822      $nom = strtoupper($nom);
1823      $l = strlen($nom);
1824      if ($l == 0) {
1825          print gettext("<br><I>le nom doit contenir au moins une lettre</I>");
1826          return 0;
1827      }
1828      if ($l > 63) {
1829          print gettext("<br><I>le nom $nom ne doit pas d&#233;passer 63 caract&#232;res</I>");
1830          return 0;
1831      }
1832      for ($i = 0; $i < $l; $i++) {
1833          $c = substr($nom, $i, 1);
1834          if (!preg_match("/[a-zA-Z0-9_-]/", $c, $tab_err)) {
1835              print gettext("<br><I>caract&#232;re $c incorrect dans hostname $nom </I>");
1836              return 0;
1837          }
1838      }
1839      $prem = substr($nom, 0, 1);
1840      if (!preg_match("/[a-zA-Z0-9]/", $prem, $tab_err)) {
1841          print gettext("<br><I>le nom $nom doit commencer par une lettre ou un chiffre</I>");
1842          return 0;
1843      }
1844      $der = substr($nom, $l - 1, 1);
1845      if (!preg_match("/[a-zA-Z0-9]/", $der, $tab_err)) {
1846          print gettext("<br><I>le nom $nom doit finir par une lettre ou un chiffre</I>");
1847          return 0;
1848      }
1849      return 1;
1850  }
1851  
1852  /**
1853   * validation adresse MAC
1854  
1855   * @Parametres  $mac adresse MAC a tester
1856   * @Return  True si OK - False si adresse MAC pas correcte
1857  
1858   */
1859  function valid_mac($mac) {
1860      $tab_mac = explode(':', $mac); /* transforme adresse mac en tableau de 6 octets */
1861      if (count($tab_mac) <> 6) {
1862          print gettext("<br><I>Attention : une adresse MAC doit avoir la forme xx:xx:xx:xx:xx:xx</I>");
1863          return(0);
1864      }
1865      $mac = strtoupper($mac);
1866      $l = strlen($mac);
1867      for ($i = 0; $i < $l; $i++) {
1868          $c = substr($mac, $i, 1);
1869          if (!preg_match("/[A-F0-9:]/", $c, $tab_err)) {
1870              print gettext("<br><I>caract&#232;re $c incorrect dans adresse mac $mac <I>");
1871              return 0;
1872          }
1873      }
1874      return 1;
1875  }
1876  
1877  /**
1878   * Retourne une adresse MAC formatee en completant par des zeros a gauche
1879  
1880  
1881   * @Parametres $ch_mac: Adresse MAC a traiter
1882   * @Return Retourne une adresse MAC formatee en completant par des zeros a gauche, sinon retourne chaine vide
1883  
1884   */
1885  function format_mac($ch_mac) {
1886      $ch_mac = strtoupper($ch_mac);
1887      $mac_retour = "";
1888      $tab_mac = explode(':', $ch_mac); /* transforme l'adresse mac en tableau de 4 chaines */
1889      if (count($tab_mac) <> 6) {
1890          $z = count($tab_mac);
1891          print gettext("<br><I>Attention : une adresse mac doit avoir la forme xx:xx:xx:xx:xx:xx</I>");
1892          return("");
1893      } else {
1894          for ($i = 0; $i < 6; $i++) {
1895              while (strlen($tab_mac[$i]) < 2)
1896                  $tab_mac[$i] = '0' . $tab_mac[$i];
1897              $mac_retour = $mac_retour . $tab_mac[$i];
1898              if ($i < 5)
1899                  $mac_retour = $mac_retour . ':'; /* on ajoute un point sauf au dernier */
1900          }
1901          /* verification caracteres valides */
1902          if (!preg_match("/[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}/", $mac_retour, $tab_err)) {
1903              print gettext("<br><I>Caract&#232;res interdits dans $mac_retour</I>");
1904              return("");
1905          }
1906  
1907          return($mac_retour);
1908      }
1909  }
1910  
1911  /**
1912   * Validation liste hostname
1913  
1914   * @Parametres $liste_name Nom separes par des espaces
1915  
1916   * @Return  False et message d'erreur - True si Ok
1917  
1918   * @note la liste doit etre une suite de noms de host separes par un espace
1919  
1920   */
1921  function valid_list_name($liste_name) {
1922      $liste_name = trim($liste_name);   /* supprime espaces a droite et a gauche */
1923      if ($liste_name == "")
1924          return 1;
1925      $tab_name = explode(' ', $liste_name); /* transforme la liste de noms en tableau de noms */
1926      $nb_name = count($tab_name);
1927      for ($i = 0; $i < $nb_name; $i++) {
1928          $name = $tab_name[$i];
1929          if (!valid_name($name)) {
1930              print gettext("<I>nom $name incorrect</I>");
1931              return 0;
1932          }
1933      }
1934      return 1;
1935  }
1936  
1937  /**
1938   * Importe dans la base SQL les imports a partit d'un csv
1939  
1940   * @Parametres $tableau : l'import cvs des adresses  IP, Nom, MAC
1941  
1942   * @Return Affichage HTML du resultat
1943  
1944   */
1945  function traite_tableau($tableau) {
1946      $nb_lignes = count($tableau);
1947      $separ = ";";
1948      $z = 0;
1949      $erreur = 0;  // si erreur est vrai en sortie de boucle, annuler transaction
1950      $faux_nom = 1;    // si jamais le nom n'est pas renseigne, on l'invente
1951      // avec un numero
1952      while ($z < $nb_lignes) {
1953          // sauter eventuelle ligne vide
1954          // c'est souvent le cas pour la derniere ligne du presse-papier
1955          if (trim($tableau[$z]) == "")
1956              break;
1957          // decoupage de chaque ligne a partir du separateur |
1958          $tab_ligne = explode($separ, $tableau[$z]);
1959          $ip = trim($tab_ligne[0]);
1960          if (!set_ip_in_lan($ip)) {
1961              print("<br>");
1962              print gettext("Erreur sur adresse ip : $tab_ligne[0]");
1963              $ligne = $z + 1;
1964              print(" Ligne n $ligne");
1965              $erreur = 1;
1966              $z++;
1967              continue;
1968          }
1969  //        $ip=format_ip($ip);
1970          if ($ip == "") {
1971              print("<br>");
1972              print gettext("Erreur sur adresse ip : $tab_ligne[0]");
1973              $ligne = $z + 1;
1974              print gettext(" Ligne n $ligne");
1975              $erreur = 1;
1976              $z++;
1977              continue;
1978          }
1979          $nom = trim($tab_ligne[1]);
1980          if (!valid_name($nom)) {
1981              print("<br>");
1982              print gettext("Erreur sur hostname : $tab_ligne[1] ");
1983              $ligne = $z + 1;
1984              print gettext("Ligne n $ligne");
1985              $erreur = 1;
1986              $z++;
1987              continue;
1988          }
1989          $mac = trim($tab_ligne[2]);
1990          if (!valid_mac($mac)) {
1991              print("<br>");
1992              print gettext("Erreur sur adresse mac : $tab_ligne[2] ");
1993              $ligne = $z + 1;
1994              print gettext("Ligne n $ligne");
1995              $erreur = 1;
1996              $z++;
1997              continue;
1998          }
1999          $mac = format_mac($mac);
2000          if ($mac == "") {
2001              print("<br>");
2002              print gettext("Erreur sur adresse mac : $tab_ligne[2] ");
2003              $ligne = $z + 1;
2004              print gettext("Ligne n $ligne");
2005              $erreur = 1;
2006              $z++;
2007              continue;
2008          }
2009  
2010          require_once  ("ihm.inc.php");
2011          // Recuperation des donnees dans la base SQL
2012          $query = mysql_query("SELECT * from se3_dhcp where mac='$mac'");
2013          $v_count = mysql_num_rows($query);
2014  
2015          if ($v_count <> 0) {
2016              print("<br>");
2017              print gettext("Adresse mac $mac d&#233;ja utilis&#233;e ");
2018              $ligne = $z + 1;
2019              print gettext(" Ligne n $ligne");
2020              $erreur = 1;
2021              $z++;
2022              continue;
2023          }
2024          $query = mysql_query("SELECT * from se3_dhcp where name='$nom'");
2025          $v_count = mysql_num_rows($query);
2026          if ($v_count <> 0) {
2027  //         if (strtolower($query) == strtolower($nom))
2028              print("<br>");
2029              print gettext("Hostname $nom d&#233;ja utilis&#233; ");
2030              $ligne = $z + 1;
2031              print gettext(" Ligne n $ligne");
2032              $erreur = 1;
2033              $z++;
2034              continue;
2035          }
2036          $nominmaj = strtolower($nom);
2037          $query = mysql_query("SELECT * from se3_dhcp where name='$nominmaj'");
2038          $v_count = mysql_num_rows($query);
2039          if ($v_count <> 0) {
2040              print("<br>");
2041              print gettext("Hostname $nominmaj d&#233;ja utilis&#233; ");
2042              $ligne = $z + 1;
2043              print gettext(" Ligne n $ligne");
2044              $erreur = 1;
2045              $z++;
2046              continue;
2047          }
2048          if ($nom == "") {
2049              $nom = "X" . $faux_nom;
2050              $faux_nom++;
2051          }
2052          if (!valid_name($nom)) {
2053              print("<br>");
2054              print gettext("Nom $nom incorrect ");
2055              $ligne = $z + 1;
2056              print gettext(" Ligne n $ligne");
2057              $erreur = 1;
2058              $z++;
2059              continue;
2060          }
2061          // tout est ok, on insere la ligne
2062          add_reservation($ip, $mac, $nom);
2063          $z++;
2064          if ($erreur) {
2065              print("<br><br><b>" . gettext("Erreurs durant le traitement") . "</b><br>");
2066          }
2067      }
2068      print gettext("Traitement termin&#233;<br>");
2069      print gettext("Nb de lignes trait&#233;es : $z");
2070      dhcpd_restart();
2071      $mac = "";
2072      $ip = "";
2073      $nom = "";
2074  }
2075  
2076  /**
2077   * Fonctions: Test la presence de dhcp_vlan dans la table params et en retourne la valeur
2078  
2079   * @Parametres $dhcp_vlan_valeur : Contenu de dhcp_vlan
2080  
2081   * @Return - 0 si pas de vlan - n nombre de vlan
2082  
2083   */
2084  function dhcp_vlan_test() {
2085      include ("config.inc.php");
2086      // si la variable dhcp_vlan n'est pas definie on cree l'entree dans la base sql
2087      if ($dhcp_vlan == "") {
2088          $resultat = mysql_query("INSERT into params set id='NULL', name='dhcp_vlan', value='0', srv_id='0',descr='Nombre de vlan',cat='7'");
2089          return 0;
2090      } else {
2091          return $dhcp_vlan;
2092      }
2093  }
2094  
2095  /**
2096   * Verifie l'existance des champs dans la table params pour les vlans
2097  
2098   * @Parametres $nom_champ : Nom du champ a tester
2099   * MongoUpdateBatch@Return
2100  
2101  
2102   */
2103  function dhcp_vlan_champ($nom_champ) {
2104      require_once ("config.inc.php");
2105      if ($$nom_champ == "") {
2106          $resultat = mysql_query("INSERT into params set id='NULL', name='$nom_champ', value='', srv_id='',descr='',cat='7'");
2107      }
2108  }
2109  
2110  /**
2111   * rename domain client
2112  
2113   * @Parametres $ip - $mac - $name
2114  
2115   * @Return $ret
2116  
2117   */
2118  function renomme_domaine($ip, $oldname, $name) {
2119      require_once  ("ihm.inc.php");
2120      $ret = "<br>\n";
2121      $ret .= exec("/usr/bin/sudo /usr/share/se3/scripts/integreDomaine.sh renomme $name $ip $oldname adminse3 $xppass") . "<br>\n";
2122      $ret .= "<p align='center' style='color:red;'> Attention : Si l'emetteur ne reboote pas tout seul en administrateur local, ouvrez une session administrateur local et lancez <br>c:\\netinst\\shutdown.cmd</p>\n";
2123      return $ret;
2124  }
2125  
2126  /**
2127   * integrate domain client
2128  
2129   * @Parametres $ip - $mac - $name  [ $admin - $adminpasswd ]
2130  
2131   * @Return $ret
2132  
2133   */
2134  function integre_domaine($ip, $mac, $name, $admin, $adminpasswd) {
2135      require_once  ("ihm.inc.php");
2136      if ($adminpasswd == "xxx") {
2137          $adminpasswd = "";
2138      }
2139      //doit-on faire verifier l'existence dans le ldap ?
2140      $ret = "<br>\n";
2141      $ret .= exec("/usr/bin/sudo /usr/share/se3/scripts/integreDomaine.sh rejoint $name $ip $mac $admin $adminpasswd") . "<br>\n";
2142      $ret .= "<p align='center' style='color:red;'> Attention : si l'emetteur ne reboote pas tout seul en administrateur local, ouvrez une session administrateur local et lancez <br>c:\\netinst\\shutdown.cmd </p>\n";
2143      return $ret;
2144  }
2145  
2146  
2147  ?>


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1