Location: PHPKode > projects > Web-Net-Admin > webnetadmin/serveurs/dhcp/init_dhcp.php
<?
session_start();
function	load_network_host($i, $file, $type, $connexion)
{
  include "mysql/mysql.php";
  $nb_line = count($file);
  $groupe = 1;
  for (; $i < $nb_line; $i++)
    {
      $line = trim($file[$i]);
      if ($line != ";" && $line != "#" && $line != "\n" 
	  && $line != "}" && $line != "{" && $line)
	{
	  if ($line == "group")
	    $groupe++;
	  $tab = explode(" ", $line);
	  if ($tab[0] == "host")
	    {
	      $i++;
	      $line1 = trim($file[++$i]);
	      $line2 = trim($file[++$i]);
	      insert_para_insql($tab[1], $line1, $line2, $groupe, $connexion);
	      $i++;
	    }
	}
    }
  return($groupe);
}

function	insert_para_insql($host, $line1, $line2, $grp, $connexion)
{
  $tab1 = explode(" ", $line1);
  $tab2 = explode(" ", $line2);
  if ($tab1[0] == "hardware" && $tab1[1] == "ethernet")
    {
      if ($tab2[0] == "fixed-address")
	{
	  $ip = $tab2[1];
	  $mac = $tab1[2];
	  $len = strlen($mac) - 1;
	  $mac = substr($mac, 0, $len);
	  $test = check_existence_of_pc($host, $connexion);
	  if ($test)
	    {
	      $req = "UPDATE Machines SET MAC='$mac', Reseau='$grp' "
		."WHERE Nom='$host'";
	      $res = mysql_query($req, $connexion);
	      if (!$res)
		printf("Error : %s <p>", mysql_error($connexion));
	    }
	  else
	    {
	      echo "<font color=red>Warning</font> : La machine ($host) "
		."n'existe pas dans la base mysql<p>";
	    }
	}
    }
}


function	insert_description_network_insql($file, $i, $name, $type, $connexion)
{
  $subnet = $_SESSION["subnet"];
  $count=count($file);
  while($tab[0] != "group" && $tab[0] != "shared-network" && $i < $count)
    {
      $line = trim($file[$i]);
      if ($line)
	{
	  $tab = explode(" ", $line);
	  if ($tab[0] == "subnet")
	    {
	      $subnet++;
	      $base_ip = $tab[1];
	      $mask = $tab[3];
	      $i+=2;
	      while ($line != "}" && $i < $count)
		{
		  $line = trim($file[$i]);
		  if ($line != "}")
		    $option = $option.$line;
		  $i++;
		}
	      $name = mysql_escape_string($name);
	      $op = mysql_escape_string($option);
	      $req = "INSERT INTO DHCP (Nom_network, Base_ip, Numero, Masque, Param_opt)"
		." VALUES ('$name', '$base_ip', '$subnet', '$mask', '$option')";
	      $res = mysql_query($req, $connexion);
	      if (!$res)
		printf("Error : %s <p>", mysql_error($connexion));
	      $option = "";
	    }
	  else if ($tab[0] != "group" && $tab[0] != "shared-network")
	    $i++;
	}
      else if ($tab[0] != "group" && $tab[0] != "shared-network")
	$i++;
    }
  $_SESSION["subnet"] = $subnet;
  return($i);
}


function	load_network_description($file, $type, $nb_line, $connexion)
{
  $_SESSION["subnet"] = 0;
  for ($i=0; $i < $nb_line && $line != "group";)
    {
      $line = trim($file[$i]);
      if ($line != ";" && $line != "#" 
	  && $line != "\n" && $line)
	{
	  $tab = explode(" ", $line);
	  if ($tab[0] == "shared-network")
	    {
	      $name = $tab[1];
	      $i++;
	      $i = insert_description_network_insql($file, $i, $name, $type, $connexion);
	    }
	  else
	    $i++;
	}
      else
	$i++;
    }
  return($i);
}

function	parse_dhcpdfile($path, $type)
{
  require "sortir_variable_obl.php";
  $path = "$path" . "dhcpd.conf";
  $file = file($path);
  $groupe = 0;
  $i = 0;
  if ($file && is_array($file))
    {
      $connexion = mysql_connect($serv_mysql, $mysql_login, $mysql_pass) or die('Connexion au serveur sql impossible<br>');
      mysql_select_db($name_base, $connexion) or die('Sélection de la base de donnée impossible<br>');
      $nb_line = count($file);
      echo "TYPE : $type, nb_line : $nb_line<p>";
      $i = load_network_description($file, $type, $nb_line, $connexion);
      load_network_host($i, $file, $type, $connexion);
      mysql_close($connexion);
    }
  else
    {
      echo "<p><p>Erreur de chargement "
	."du fichier \"dhcpd.conf\", vérifier qu'il existe<p>";
      return (1);
    }
  return(0);
}


function	load_dhcpconf_file($type)
{
  $parse_dhcp = parse_dhcpdfile("tmp_file/", $type);
  if ($parse_dhcp)
    return (1);
  return(0);
}
?>
Return current item: Web-Net-Admin