<?php
// Increase the script timeout value
set_time_limit(200);
$DEBUG=0;

//Get current time
    $mtime = microtime();
//Split seconds and microseconds
    $mtime = explode(" ",$mtime);
//Create one value for start time
    $mtime = $mtime[1] + $mtime[0];
//Write start time into a variable
    $tstart = $mtime; 
?>

<style type="text/css"> 

H1 {
  FONT-SIZE: 12pt; 
  COLOR: #000000;
  LINE-HEIGHT: 16pt; 
  FONT-FAMILY: "Trebuchet MS", Trebuchet, Arial, Helvetica, sans-serif
}

body {
  FONT-SIZE: 9pt; 
  COLOR: #000000; 
  LINE-HEIGHT: 12pt; 
  FONT-FAMILY: Verdana, Geneva, Arial, Helvetica, sans-serif; 
  TEXT-ALIGN: left; 
  TEXT-DECORATION: none;
}
</style>

<?php

//include "include_config.php";

//include ("../dyn/session.inc");
require_once "DB.php";
include ("../dyn/connexion.php");
// * custom *
if(file_exists("../dyn/custom/var.inc")) {
   include("../dyn/custom/var.inc");
}
else {
  include("../dyn/var.inc");
}
//*
$db=& DB :: connect($dsn, $db_option);
if (DB :: isError($db)) {
    die($db->getMessage());
} else {
  if($DEBUG==1)
    echo "La base ".$dsn['database']." est connectée.<br>";
}

// Process the form
$user_name = $_POST['user_name'];
$verbose = "";
$ddram = false;
$physicalmem=0;
$logicalmem=0;
$input = $_POST['add'];
$input = explode("\n", $input);
$uuid = $_POST["uuid"];
$software_audit = $_POST["software_audit"];
if (!is_null($_POST["verbose"])) {$verbose = $_POST["verbose"];} else {$verbose="off";}
$systemname = $_POST["systemname"];

echo "User: " . $user_name . "<br />\n\n";
echo "Verbose: " . $verbose . "<br />\n\n";
echo "System: " . $systemname . "<br />\n\n";
echo "UUID: " . $uuid . "<br /><br />";

$numero="";
do {
  $back=false;
  $idstation=0;
  // Rechercher station et la creer si besoin est
  if ($uuid!="") {
    if (is_numeric($uuid))
    $sql = "SELECT idstation FROM station WHERE numero = '$uuid'";
    else
    $sql = "SELECT idstation FROM station WHERE nomstation = '$systemname'";
    if ($verbose == "y"){echo $sql . "<br />\n\n";}
    $idstation = $db->getOne($sql);
    if (DB :: isError($idstation))
      die($idstation->getMessage().$sql);
    else {
      if ($DEBUG == 1)
          echo " la requete ".$sql." est exécutée<br>";
      if ((!$idstation && !is_numeric($uuid)) || $numero ) { //creation station si nouveau nom ou nouveau numero
        if ($systemname!="") {  //(même si dans ce cas nomstation deja présent)
          $staval="";
          $staval["idstation"]=$db->nextID("station");
          $idstation=$staval["idstation"];
          $staval["nomstation"]=$systemname;
          $staval["idservice"]=3502;  //"dsit (serveur)" temporairement pour visibilité poste ds tab
          if (!$numero || !is_numeric($numero) || strlen($numero)!=7) { //si pas de numero dans base registre ou numero non valide
            //enregistrement WMI et attribution numero WMI
            $numero="WMI%'";
            $sql_numerostation="select max(numero) from station where numero like '";
            $temp = $db->getone($sql_numerostation.$numero);
            if (is_null($temp))
              $numero="WMI0001";
            else  
              $numero="WMI".sprintf("%04u",intval(substr($temp,3))+1);
            $staval["numero"]=$numero;
          } else $staval["numero"]=$numero;  //numero present dans bdr -> utilisé 
          $staval["idetatstation"]=1; //affecte
          $staval["idservice"]=$db->getOne("select idservice from service where libservice='DSIT (WMI)'");
          $staval["idbatiment"]=$db->getOne("select idbatiment from batiment where libbatiment='ANNEXE PL DE LA COUR'");
          $res= $db->autoExecute("station",$staval,DB_AUTOQUERY_INSERT);
          if (DB :: isError($res))
            echo($res->getDebugInfo().' '.$res->getMessage());
          else{
            if ($DEBUG == 1)
              echo "La requête de mise à jour est effectuée.<br>";
          } 
          //ajouter interv enregistrement WMI station
          $intervalF="";
          $intervalF['idintervention']=$db->nextID("intervention");
          $intervalF['idstation']=$idstation;
          $intervalF['observations']="Enregistrement WMI du poste.";
          $intervalF['idtype']=$db->getOne("select idtype from typeintervention where libtype='Enregistrement'");
          $intervalF['dateouverture']=date("Y-m-d");
          $intervalF['idetatintervention']=3;  //cloturée
          $res= $db->autoExecute("intervention",$intervalF,DB_AUTOQUERY_INSERT);
          if (DB :: isError($res))
            echo($res->getDebugInfo().' '.$res->getMessage());
          else{
            if ($DEBUG == 1)
              echo "La requête de mise à jour est effectuée.<br>";
          }
        } else $idstation=0;  //pas d'enregistrement si nomstation vide
      } else {  //maj nomstation si besoin
        if (is_numeric($uuid)) {
          if ($idstation) { //numero trouvé dans table station
            if (strtoupper ($db->getOne("select nomstation from station where idstation = ".$idstation))!=$systemname) {
              $cle = "idstation = ".$idstation;
              $staval="";
              $staval["nomstation"]=$systemname;
              $res= $db->autoExecute("station",$staval,DB_AUTOQUERY_UPDATE,$cle);  
              if (DB :: isError($res))
              echo($res->getDebugInfo().' '.$res->getMessage());
            }
          } else { //numero dans base registre non retrouvé dans table station (eronné ou nouveau)
            $numero=$uuid;
            $uuid=$systemname;
            $back=true;
          }
        } 
      }
    }
  } else $idstation=0;  //pas d'enregistrement si uuid vide
} while ($back);
$sql = "";
if ($idstation) { //si station identifiee ou creee (nomstation present)
  $affectation=($db->getOne("select idetatstation from station where idstation = ".$idstation)==1);
  foreach ($input as $split) {
    //Network
    if (substr($split, 0, 7) == "network")  { insert_network($split); }
    // System submit
    if (substr($split, 0, 6) == "system") { insert_system($split); }
    // Processor
    if (substr($split, 0, 9) == "processor"){ insert_processor($split); }
    // Memory
    if (substr($split, 0, 6) == "memory")   { insert_memory($split); }
    // Video
    if (substr($split, 0, 5) == "video")    { insert_video($split); }
    // Hard Drives
    if (substr($split, 0, 9) == "harddrive"){ insert_harddrive($split); }
    // Monitor - System Table
    if (substr($split, 0, 11) == "monitor_sys" && $affectation){ insert_monitor($split); }
    // Optical Drives
    if (substr($split, 0, 7) == "optical"){insert_optical($split); }
    // Floppy Drive
    if (substr($split, 0, 6) == "floppy"){ insert_floppy($split); }
    // Keyboard
    if (substr($split, 0, 8) == "keyboard" && $affectation){ insert_keyboard($split); }
    // Mouse
    if (substr($split, 0, 5) == "mouse" && $affectation){ insert_mouse($split); }
    // Sound
    if (substr($split, 0, 5) == "sound"){ insert_sound($split); }
    //Software  autorisation enregistrement logiciels et clés meme si station non affectee
    if (substr($split, 0, 8) == "software"){ insert_software($split); }
    // CD Keys
    if (substr($split, 0, 7) == "ms_keys"){ insert_ms_keys($split); }
  }
}
if (!$physicalmem && $logicalmem) { //presence de memoire physique mais pas logique
  $logicalmem =floor(round(floatval($logicalmem/16))*16); //conversion en multiple de 16
  insert_memory("memory^^^Logical^^^".$logicalmem."^^^");
}
  
function insert_network ($split) {
    global $idstation, $verbose, $db;
    $extended = explode('^^^',$split);
    if ($verbose == "y"){echo "<h2>Reseau</h2><br />";}
    $net_mac_address = trim($extended[1]);
    $net_description = trim($extended[2]);
    $net_adapter_type = trim($extended[3]);    //ethernet 802.3
    $net_manufacturer = substr(trim($extended[4]),0,50);
    if ($net_description!="") {
      $idmarque=0;
      if ($net_manufacturer) {
        $idmarque=$db->getOne("select idmarque from marque where libmarque = '".$net_manufacturer."'");
        if (!is_numeric($idmarque)) {  //marque à créer 
          $idmarque=$db->nextId("marque");
          $nval="";
          $nval["idmarque"]=$idmarque;
          $nval["libmarque"]=$net_manufacturer; 
          $res= $db->autoExecute("marque",$nval,DB_AUTOQUERY_INSERT);
          if (DB :: isError($res))
            echo($res->getDebugInfo().' '.$res->getMessage());
        }
      }
      $net_description=str_replace(" pour gestion intégrale de PC (3C905C-TX)","",$net_description);
      $net_description=str_replace(" For Complete PC Management NIC (3C905C-TX)","",$net_description); 
      if (strpos($net_description,"-",0)>5)
        $temp=explode("-",$net_description);
      else
        $temp=array($net_description);
      $net_description=rtrim($temp[0]);
      $net_description=str_replace("Carte réseau ","",$net_description);
      $net_description=str_replace("(R)","",$net_description);
      $net_description=substr($net_description,0,80);
      $idmodele=0;
      $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$net_description."' and idmarque = ".$idmarque);
      if (!is_numeric($idmodele)) {  //modele à créer
        $idmodele=$db->nextId("modeleelement");
        $nval="";
        $nval["idmodele"]=$idmodele;
        $nval["libmodele"]=$net_description;          
        $nval["idmarque"]=$idmarque; 
        $nval["nature"]="cartereseau"; 
        $nval["classe"]="composant"; 
        $nval["rank"]=8; 
        $nval["typecomp"]=$net_adapter_type; 
        $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
      $idelement=0;
      $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = ".$idstation." and (adressemac = '".$net_mac_address."' or datescan='0000-00-00')");
      if (!is_numeric($idelement)) {  //element à créer
        $idelement=$db->nextId("element");
        $nval="";
        $nval["idelement"]=$idelement;
        $nval["idstation"]=$idstation; 
        $nval["idmodele"]=$idmodele; 
        $nval["adressemac"]=$net_mac_address; 
        $nval["datescan"]=date("Y-m-d"); 
        $nval["idetatelement"]=1;   //affecte
        $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      } else {  //mise à jour element
        $cle= "idelement = ".$idelement;
        $nval="";
        $nval["datescan"]=date("Y-m-d"); 
        $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
          if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
    }
}

function insert_system ($split) {
    global $idstation, $verbose, $db, $numero, $logicalmem, $affectation;
    $extended = explode('^^^',$split);
  if ($verbose == "y"){echo "<h2>Systeme</h2><br />";}
  $ip_address = trim($extended[1]);
  $net_user_name = trim($extended[2]);
  $system_model = trim($extended[3]);
  $system_system_type = trim($extended[4]);
  $system_id_number = trim($extended[5]);
  $system_vendor = trim($extended[6]);
  $net_domain_role = trim($extended[7]);
  $system_os_name = trim($extended[8]);
  $date_system_install = trim($extended[9]);
  $system_service_pack = trim($extended[10]);
  $system_os_key = trim($extended[11]);
  $logicalmem = trim($extended[12]); //a utiliser pour test incohérence NT (banques apparaissent 2 fois ex:A0,A1,A2,A3 au lieu de A0,A1)
  $staval="";
  //maj ou creation ip
  if ($affectation) { //controle adresse ip si station affectée
     $idip=0;
     $idip=$db->getOne("select idip from tcpip where idstation = ".$idstation." and ipmaitre = 'v'");
     $adresse=explode(".",$ip_address);
     if (!is_numeric($idip)) {  //adresse à créer 
       $idip=$db->nextId("tcpip");
       $nval="";
       $nval["idip"]=$idip;
       $nval["idstation"]=$idstation; 
       $nval["adresseip"]=$ip_address; 
       $nval["ipa"]=$adresse[0]; 
       $nval["ipb"]=$adresse[1]; 
       $nval["ipc"]=$adresse[2]; 
       $nval["ipd"]=$adresse[3]; 
       $nval["ipmaitre"]="v"; 
       $res= $db->autoExecute("tcpip",$nval,DB_AUTOQUERY_INSERT);
       if (DB :: isError($res))
         echo($res->getDebugInfo().' '.$res->getMessage());
     } else {  //mise à jour adresse
       $cle= "idip = ".$idip;
       $nval="";
       $nval["adresseip"]=$ip_address; 
       $nval["ipa"]=$adresse[0]; 
       $nval["ipb"]=$adresse[1]; 
       $nval["ipc"]=$adresse[2]; 
       $nval["ipd"]=$adresse[3]; 
       $res= $db->autoExecute("tcpip",$nval,DB_AUTOQUERY_UPDATE,$cle);
       if (DB :: isError($res))
         echo($res->getDebugInfo().' '.$res->getMessage());
     }
  }
  //recherche id utilisateur
  if (strstr($numero,"WMI")) {  //si station wmi 
    $login=explode("/",$net_user_name);  // ex: MAIRIE-ARLES/f.durand  \ converti en / dans audit.vbs
    $idutilisateur=0;
    if (isset($login[1])) { //utilisateur loggé lors scan
      $idutilisateur=$db->getOne("select idutilisateur from personnel where login = '".$login[1]."'");
      if (is_numeric($idutilisateur)) {  //utilisateur loggé reconnu sur station wmi
        $staval["idutilisateur"]=$idutilisateur;    //ajout utilisateur
      }
    }
  }
  //recherche modele 
  if (stristr($system_model,"to be") || stristr($system_model,"system") ||
$system_model=="") { //pas de modele
    $idmodele=1; //assembleur
  } else {
    $system_model=substr($system_model,0,80);
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$system_model."'");
    if (!is_numeric($idmodele)) {  //modele a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele; 
      $nval["libmodele"]=$system_model; 
      $nval["nature"]="station"; 
      $nval["classe"]="station"; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } 
  }
  $staval["idmodele"]=$idmodele;
  //typestation
  if ($system_system_type=="Notebook") 
    $idtypestation= $db->getOne("select idtypestation from typestation where libtypestation = 'Portable'");
  else {
    switch ($net_domain_role) {
    case "Member Server":
    case "Primary Domain Controller":
    case "Backup Domain Controller":
      $idtypestation= $db->getOne("select idtypestation from typestation where libtypestation = 'Serveur'");
      break;
    default:
      $idtypestation= $db->getOne("select idtypestation from typestation where libtypestation = 'Client'");
    }
  }
  $staval["idtypestation"]=$idtypestation;
  //numeroserie
  if ($db->getOne("select numserie from station where idstation = ".$idstation)/*==""*/)
    if (!(stristr($system_id_number,"to be") || substr($system_id_number,0,3)=="SYS" || $system_id_number=="" || stristr($system_id_number,"EVAL") || stristr($system_id_number,"00000") || stristr($system_id_number,"none") || stristr($system_id_number,"serial"))) {
      $staval["numserie"]=$system_id_number;
    }
  //marque
  if (!(stristr($system_vendor,"to be") || stristr($system_vendor,"system") || $system_vendor=="")) {
    $system_vendor=substr(trim($system_vendor),0,50);
    $idmarque=0;
    $idmarque=$db->getOne("select idmarque from marque where libmarque = '".$system_vendor."'");
    if (!is_numeric($idmarque)) {  //marque a creer
      $idmarque=$db->nextId("marque");
      $nval="";
      $nval["idmarque"]=$idmarque;
      $nval["libmarque"]=$system_vendor; 
      $res= $db->autoExecute("marque",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $staval["idmarque"]=$idmarque;
  } 
  //systeme exploitation
  $system_os_name = str_replace("(R)","",$system_os_name);
  $system_os_name = str_replace("Microsoft ","",$system_os_name);
  $system_os_name = str_replace("Standard Edition","SE",$system_os_name);
  if ($system_service_pack!="" && $system_service_pack!="0.0" && $system_service_pack!=".") {
    if (strstr($system_os_name,"Ubuntu")) 
      $system_os_name .= " ".$system_service_pack;
    else
      $system_os_name .= " SP".$system_service_pack;
  }
  $idsysteme=0;
  $idsysteme=$db->getOne("select idmodele from modeleelement where libmodele = '".$system_os_name."'");
  if (!is_numeric($idsysteme)) {  //systeme a creer
    $idsysteme=$db->nextId("modeleelement");
    $nval="";
    $nval["idmodele"]=$idsysteme;
    $nval["libmodele"]=$system_os_name; 
    $nval["nature"]="systeme"; 
    $nval["classe"]="logiciel"; 
    $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
    if (DB :: isError($res))
      echo($res->getDebugInfo().' '.$res->getMessage());
  }
  $staval["idsysteme"]=$idsysteme;   
  $date_system_install=explode("/",$date_system_install);
    $staval["dateatelier"]=$date_system_install[0]."-".$date_system_install[1]."-".$date_system_install[2];
  $staval["seriesysteme"]=$system_os_key;
  $staval["datescan"]=date("Y-m-d"); 
  //mise a jour station
  $cle= "idstation = ".$idstation;
  $res= $db->autoExecute("station",$staval,DB_AUTOQUERY_UPDATE,$cle);
  if (DB :: isError($res))
    echo($res->getDebugInfo().' '.$res->getMessage());
}

function insert_processor ($split){
    global $idstation, $verbose, $db, $ddram;
    $extended = explode('^^^',$split);
    if ($verbose == "y"){echo "<h2>Processor</h2><br />";}
    $processor_current_clock_speed = trim($extended[1]);
    $processor_device_id = trim($extended[2]);
    $processor_ext_clock = trim($extended[3]);
    $processor_manufacturer = substr(trim($extended[4]),0,50);
    $processor_name = trim($extended[5]);
    $processor_socket_designation = trim($extended[6]);
  if ($processor_device_id=="CPU0") { // || $processor_device_id=="CPU2") {
    //frequence bus
    if ($processor_ext_clock==0)
      $processor_ext_clock=133;
    else
      $processor_ext_clock =floor(round(floatval($processor_ext_clock*3/100))*100/3);
    //nom processeur
    $processor_name=str_replace("(R)","",$processor_name);
    $processor_name=str_replace("(TM)","",$processor_name);
    $processor_name=str_replace("processor ","",$processor_name);
    $processor_name=str_replace("Processeur ","",$processor_name);
    $processor_name=str_replace("Intel ","",$processor_name);
    $processor_name=str_replace("CPU ","",$processor_name);
    $processor_name=str_replace("family ","",$processor_name);
    if (stristr($processor_name,"hz")) {
      $index=strpos($processor_name,"Hz");
      $processor_name=rtrim(substr($processor_name,0,strlen($processor_name)-8));
    }
    //frequence processeur
    switch ($processor_current_clock_speed) {
    case 37421:
    case 37427:
      $processor_current_clock_speed=2400;
      break;
    case 37554:
    case 37687:
      $processor_current_clock_speed=2530;
      break;
    case 37692:
      $processor_current_clock_speed=2660;
    }
    if ($processor_current_clock_speed>=1200)
      if ($processor_current_clock_speed>=1700)
        if ($processor_current_clock_speed>=1800) //>1800   66
          $processor_current_clock_speed =floor(round(floatval($processor_current_clock_speed*3/200))*200/3);
        else //1700-1800   33
          $processor_current_clock_speed =floor(round(floatval($processor_current_clock_speed*3/100))*100/3);
      else  //1200-1700   66
        $processor_current_clock_speed =floor(round(floatval($processor_current_clock_speed*3/200))*200/3);
    else  //<1200   33
      $processor_current_clock_speed =floor(round(floatval($processor_current_clock_speed*3/100))*100/3);
    if ($processor_current_clock_speed==2066) $processor_current_clock_speed=2100;
    if ($processor_name=="Pentium M") {
      if ($processor_current_clock_speed==600) $processor_current_clock_speed=1600;
      if ($processor_current_clock_speed==800) $processor_current_clock_speed=1730;
    }
    $ddram=($processor_current_clock_speed>1500);
    //processeur inconnu
    if (stristr($processor_name,"inconnu")) {
      switch ($processor_current_clock_speed) {
      case 550:
      case 600:
      case 650:
      case 666:
      case 933:
      case 1000:
        $processor_name ="Pentium III";
        break;
//      case 2400:    mis en commentaire pour CeleronD320 reconnu inconnu(34721)
//        $processor_name ="Pentium 4";
        break;
      default:
        $processor_name = "Celeron";
      }
    }
    //socket et designation
    switch ($processor_name) {
    case "Pentium M":
    case "Pentium 4 Mobile":
    case "Xeon":
      $processor_name.= " ".Frequence($processor_current_clock_speed)."(".$processor_ext_clock.")";
      break;
    case "Pentium II":
    case "Pentium III":
    case "Celeron":
    case "Pentium 4": { 
      if (strstr($processor_socket_designation,"370")) 
        $processor_socket_designation="Socket370";
      else if (stristr($processor_socket_designation,"478"))
        $processor_socket_designation="Socket478"; 
      else if (stristr($processor_socket_designation,"slot"))
        if ($processor_current_clock_speed<700) 
          $processor_socket_designation="Slot1";
        else 
          $processor_socket_designation="Socket370";
      else if (stristr($processor_socket_designation,"775") || stristr($processor_socket_designation,"CPUSocket"))
        $processor_socket_designation="Socket775";
      else if ($processor_current_clock_speed<1500)
        $processor_socket_designation="Socket370";
      else 
        $processor_socket_designation="Socket478";
    }
    default: //autres processeurs
      $processor_name.= " ".Frequence($processor_current_clock_speed)."(".$processor_ext_clock.") ".$processor_socket_designation;
    }
    //enregistrer proc
    $idmarque=0;
    if ($processor_manufacturer) {
      $idmarque=$db->getOne("select idmarque from marque where libmarque = '".$processor_manufacturer."'");
      if (!is_numeric($idmarque)) {  //marque à créer 
        $idmarque=$db->nextId("marque");
        $nval="";
        $nval["idmarque"]=$idmarque;
        $nval["libmarque"]=$processor_manufacturer; 
        $res= $db->autoExecute("marque",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
    }
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$processor_name."'");
    if (!is_numeric($idmodele)) {  //processeur a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$processor_name; 
      $nval["idmarque"]=$idmarque; 
      $nval["connecteur"]=$processor_socket_designation;
      $nval["frequence"]=$processor_current_clock_speed; 
      $nval["vitesse"]=$processor_ext_clock; 
      $nval["nature"]="processeur"; 
      $nval["classe"]="composant"; 
      $nval["rank"]=1; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = ".$idstation);
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
  }
}

function Frequence($freq) {
  if ($freq>=1000)
   return substr($freq,0,1).".".substr($freq,1,2)."GHz";
  else
   return $freq."MHz";
}

function insert_memory ($split){
    global $idstation, $verbose, $db, $ddram, $logicalmem, $physicalmem;
    $extended = explode('^^^',$split);
    if ($verbose == "y"){echo "<h2>Mémoire</h2><br />";}
    $memory_bank = trim($extended[1]);
    $memory_capacity = trim($extended[2]);
    $memory_speed = trim($extended[3]);
  if ($memory_capacity>0) {
    $total=$db->getOne("select sum(taille) from modeleelement,element where modeleelement.idmodele=element.idmodele and nature='memoire' and datescan='".date("Y-m-d")."' and idstation=".$idstation." group by idstation");
    if (!$total) $total=0;
    $old_capacity=$db->getOne("select taille from modeleelement,element where modeleelement.idmodele=element.idmodele and idstation = ".$idstation." and adressemac = '".$memory_bank."'");
    if (!$old_capacity) $old_capacity=0;
    $next_capacity=$total-$old_capacity+$memory_capacity-40;
echo $next_capacity."<".$logicalmem." ou ".$memory_capacity."<".$old_capacity." ou ".$memory_capacity.">=256";
    if (($next_capacity<$logicalmem)||($memory_capacity<$old_capacity)||($memory_capacity>=256)) {
echo "ajout memoire";
      //si somme des memoires avec marge de 40 inférieure à cap totale ou si memoire deja scannee avec taille sup ou
      //memoire d'au moins 256 ko (sous XP pas de bug NT mais parfois carte video integree) -> OK
      $physicalmem+=$memory_capacity;
      if ($memory_speed<133) $memory_speed=0;
      if ($ddram) $memory_type="DDRAM"; else $memory_type="SDRAM";
      $memory_designation= $memory_type." ".$memory_capacity."Mo";
      if ($memory_speed>0) $memory_designation.=" (".$memory_speed.")";
      //enregistrer memoire
      $idmodele=0;
      $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$memory_designation."'");
      if (!is_numeric($idmodele)) {  //memoire a creer
        $idmodele=$db->nextId("modeleelement");
        $nval="";
        $nval["idmodele"]=$idmodele;
        $nval["libmodele"]=$memory_designation; 
        $nval["typecomp"]=$memory_type;
        if ($memory_speed>0) $nval["frequence"]=$memory_speed; 
        $nval["taille"]=$memory_capacity; 
        $nval["nature"]="memoire"; 
        $nval["classe"]="composant"; 
        $nval["rank"]=2; 
        $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
      $idelement=0;
      $idelement=$db->getOne("select idelement from element where idstation = ".$idstation." and (adressemac = '".$memory_bank."' or (idmodele = ".$idmodele." and datescan='0000-00-00'))");
      if (!is_numeric($idelement)) {  //element à créer
        $idelement=$db->nextId("element");
        $nval="";
        $nval["idelement"]=$idelement;
        $nval["idstation"]=$idstation; 
        $nval["idmodele"]=$idmodele; 
        $nval["adressemac"]=$memory_bank; 
        $nval["datescan"]=date("Y-m-d"); 
        $nval["idetatelement"]=1;   //affecte
        $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      } else {  //mise à jour element
        $cle= "idelement = ".$idelement;
        $nval="";
        $nval["idmodele"]=$idmodele; 
        $nval["adressemac"]=$memory_bank; 
        $nval["datescan"]=date("Y-m-d"); 
        $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
          if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
    }
  }
}

function insert_video ($split){
    global $idstation, $verbose, $db;
    $extended = explode('^^^',$split);
    if ($verbose == "y"){echo "<h2>Carte Video</h2><br />";}
    $video_adapter_ram = trim($extended[1]);    
    $video_caption = trim($extended[2]);    
    $video_current_horizontal_res = trim($extended[3]); 
    $video_current_number_colours = trim($extended[4]); 
    $video_current_refresh_rate = trim($extended[5]);   
    $video_current_vertical_res = trim($extended[6]);   
    $video_driver_version = trim($extended[7]); 
    $video_device_id = trim($extended[8]);
  switch ($video_driver_version) {
  case "1.03.05":
  case "2.00.17":
  case "4.1024.330.0010":
    $video_caption="S3 VIRGE";
    break;
  case "4.00.1381.1270":
    $video_caption="SIS 6326";
  }
  if (($video_device_id=="VideoController1" || strstr($video_device_id,"VGA Linux")) && $video_caption!="") {
    if ($video_adapter_ram=="") $video_adapter_ram=0;
    $video_caption=str_replace("(Microsoft Corporation)","",$video_caption);
    $video_caption=str_replace("(Toshiba)","",$video_caption);
    $video_caption=str_replace("(Français)","",$video_caption);
    $video_caption=str_replace("(R)","",$video_caption);
    $video_caption=str_replace("(TM)","",$video_caption);
    $video_caption=str_replace("Chipset Family","",$video_caption);
    $video_caption=str_replace("Display Driver","",$video_caption);
    $video_config=$video_current_horizontal_res."x".$video_current_vertical_res;
    switch ($video_current_number_colours) {
    case 65536:
      $video_current_number_colours="65K";
      break;
    case 16777216:
      $video_current_number_colours="16M";
      break;
    case 4294967296:
      $video_current_number_colours="4MM";
    }
    $video_config.=" ".$video_current_number_colours;
    if ($video_current_refresh_rate>1)
      $video_config.=" ".$video_current_refresh_rate."Hz";
    //enregistrer video
    $video_caption=substr($video_caption,0,80);
    $idmodele=0;
    if ($video_adapter_ram) {
      $video_caption.=" (".strval($video_adapter_ram)."Mo)";
    }
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$video_caption."' and nature = 'cartevideo'");// and taille=".$video_adapter_ram);
    if (!is_numeric($idmodele)) {  //video a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$video_caption; 
      $nval["taille"]=$video_adapter_ram; 
      $nval["nature"]="cartevideo"; 
      $nval["classe"]="composant"; 
      $nval["rank"]=6; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = ".$idstation);
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["adressemac"]=$video_config; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["adressemac"]=$video_config; 
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
  }
}

function insert_monitor ($split){
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Moniteur</h2><br />";}
    $extended = explode('^^^',$split);
    $monitor_model = trim($extended[1]);
    $monitor_serial = trim($extended[2]);
  if (!(stristr($monitor_model,"Écran Plug-and-Play") || $monitor_model=="")) {
    $monitor_serial=str_replace(" ","",$monitor_serial);
    if (stristr($monitor_serial,"not found") || stristr($monitor_serial,"notfound")) $monitor_serial="";
    $idelement=0;
    $idelement=$db->getOne("select idelement from element inner join modeleelement on element.idmodele=
    modeleelement.idmodele where nature = 'ecran' and idstation = ".$idstation);
    if (!is_numeric($idelement)) {  //ecran absent
      //enregistrer ecran
      $monitor_model=substr($monitor_model,0,80);
      $idmodele=0;
      $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$monitor_model."'");
      if (!is_numeric($idmodele)) {  //ecran a creer
        $idmodele=$db->nextId("modeleelement");
        $nval="";
        $nval["idmodele"]=$idmodele;
        $nval["libmodele"]=$monitor_model; 
        $nval["nature"]="ecran"; 
        $nval["classe"]="peripherique"; 
        $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele;
      $nval["numserie"]=$monitor_serial; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //ecran existe: ajouter noserie si existe et absent ds base
      if ($monitor_serial!="") {
        $serial=0;
        $serial=$db->getOne("select numserie from element where idelement = ".$idelement);
        if ($serial=="") {  //noserie absent
          $cle= "idelement = ".$idelement;
          $nval="";
          $nval["numserie"]=$monitor_serial; 
          $nval["datescan"]=date("Y-m-d"); 
          $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
          if (DB :: isError($res))
            echo($res->getDebugInfo().' '.$res->getMessage());
        }
      }
    }
    }
}

function Drive_Size($size) {
  if ($size<1000) 
    return strval(round($size/50)*50)."Mo";
  else
    return sprintf("%.1f",$size/1024)."Go";
}

function insert_harddrive ($split){
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Disque Dur</h2><br />";}
    $extended = explode('^^^',$split);
    $hard_drive_index = trim($extended[1]);
    $hard_drive_interface_type = trim($extended[2]);
    $hard_drive_manufacturer = trim($extended[3]);
    $hard_drive_model = trim($extended[4]);
    $hard_drive_partitions = trim($extended[5]);
  $hard_drive_size = trim($extended[6]);
  $c_free_space = trim($extended[7]);
  if ((strtoupper($hard_drive_interface_type)=="IDE" || strtoupper($hard_drive_interface_type)=="SCSI") && $hard_drive_size>500) {
    if ($hard_drive_model=="") { //nt
      if (stristr($hard_drive_manufacturer,"standard"))
        $hard_drive_model= "Disque dur";
      else
        $hard_drive_model= $hard_drive_manufacturer; 
    } 
    $hard_drive_model=str_replace(" Disk","",$hard_drive_model);
    $hard_drive_model=str_replace(" Device","",$hard_drive_model);
    $hard_drive_model.=" ".Drive_Size($hard_drive_size);
    $complement = "#".$hard_drive_interface_type.$hard_drive_index." ".$hard_drive_partitions."part(s)";
    if ($hard_drive_index==0 && $c_free_space) {
      $complement .= " (".Drive_Size($c_free_space)." libres sur C:)"; 
    }
    //enregistrer disque dur
    $hard_drive_model=substr($hard_drive_model,0,80);
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$hard_drive_model."' and taille=".$hard_drive_size);
    if (!is_numeric($idmodele)) {  //disque a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$hard_drive_model; 
      $nval["taille"]=$hard_drive_size; 
      $nval["nature"]="disquedur"; 
      $nval["classe"]="composant"; 
      $nval["rank"]=3; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = 
    ".$idstation." and (adressemac like '#".$hard_drive_interface_type.$hard_drive_index."%' or datescan='0000-00-00')");
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["adressemac"]=$complement; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["adressemac"]=$complement; 
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
  }
}

function insert_optical ($split) {
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Lecteur Optique</h2><br />";}
    $extended = explode('^^^',$split);
    $optical_drive_caption = trim($extended[1]);
    $optical_drive_drive = trim($extended[2]);
    $optical_device_id = trim($extended[3]);
  if ($optical_drive_caption == "Cdrom Device" || $optical_drive_caption == "") { //nt
    if ($optical_device_id=="/DEVICE/CDROM0") {
      $optical_drive_caption = "Lecteur CD";
      $typelect="CD";
    } else if ($optical_device_id=="/DEVICE/CDROM1") {
      $optical_drive_caption = "Graveur CD";
      $typelecteur="CDRW";
    }  else {
      $optical_drive_caption = "";
    }
  } else { //xp
    if (stristr($optical_drive_caption,"AXV") || stristr($optical_drive_caption,"NERO"))
      $optical_drive_caption="";
    else if (stristr($optical_drive_caption,"CD-ROM") || stristr($optical_drive_caption,"CRD") || stristr($optical_drive_caption,"TEAC CD")) 
      $typelecteur="CD";
    else if (stristr($optical_drive_caption,"CD-WRITER") || stristr($optical_drive_caption,"CD-R/RW") || stristr($optical_drive_caption,"CD-RW") || stristr($optical_drive_caption,"CDRW") || stristr($optical_drive_caption,"CRW")) 
      $typelecteur="CDRW";
    else if (stristr($optical_drive_caption,"COMBO") || stristr($optical_drive_caption,"DW") || stristr($optical_drive_caption,"RW/DVD") || stristr($optical_drive_caption,"CDW/DVD")) 
      $typelecteur="CDRWDVD";
    else if (stristr($optical_drive_caption,"DVD-ROM")) 
      $typelecteur="DVD";
    else if (stristr($optical_drive_caption,"DVD-RW") || stristr($optical_drive_caption,"DVDRAM") || stristr($optical_drive_caption,"DVD+RW") || stristr($optical_drive_caption,"DVDR")) 
      $typelecteur="DVDRW";
    else 
      $typelecteur="CD";
  }
  if ($optical_drive_caption!="") {
    //enregistrer lect optique
    $optical_drive_caption=substr($optical_drive_caption,0,80);
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$optical_drive_caption."'");
    if (!is_numeric($idmodele)) {  //lect optique a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$optical_drive_caption; 
      $nval["typecomp"]=$typelecteur; 
      $nval["connecteur"]="IDE"; 
      $nval["integre"]="v"; 
      $nval["nature"]="lecteuroptique"; 
      $nval["classe"]="composant"; 
      $nval["rank"]=4; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = ".$idstation);
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["adressemac"]=$optical_drive_drive; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["adressemac"]=$optical_drive_drive; 
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
  }
}

function insert_floppy ($split){
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Lecteur Disquettes</h2><br />";}
    $extended = explode('^^^',$split);
    $floppy_description = trim($extended[1]);
  if ($floppy_description=="Lecteur de disquettes" || $floppy_description=="Floppy disk drive") {
    //enregistrer lect disquettes
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = 'Lecteur disquettes 3\'1/2'"); //laisser \' au lieu de ` car cree par traitementwmi
    if (!is_numeric($idmodele)) {
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]="Lecteur disquettes 3'1/2";
      $nval["nature"]="floppy"; 
      $nval["classe"]="peripherique"; 
      $nval["rank"]=5; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = 
    ".$idstation);
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
  }
}

function insert_keyboard ($split) {
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Keyboard</h2><br />";}
    $extended = explode('^^^',$split);
    $keyboard_description = trim($extended[1]);
  if (strstr($keyboard_description,"102"))
    $keyboard_description="Clavier PC/AT étendu (101/102 touches)";
  else if (!strstr($keyboard_description,"USB"))
    $keyboard_description="Clavier USB";
  else if (!strstr($keyboard_description,"PIH"))
    $keyboard_description="Clavier PIH";
  else if (!strstr($keyboard_description,"84"))
    $keyboard_description="Clavier ".$keyboard_description;
  if ($keyboard_description!="") {
    //enregistrer clavier
    $keyboard_description=substr($keyboard_description,0,80);
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$keyboard_description."'");
    if (!is_numeric($idmodele)) {  //clavier a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$keyboard_description; 
      $nval["nature"]="clavier"; 
      $nval["classe"]="peripherique"; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = 
    ".$idstation);
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    }
}

function insert_mouse ($split) {
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Mouse</h2><br />";}
    $extended = explode('^^^',$split);
    $mouse_description = trim($extended[1]);
    $mouse_port = trim($extended[2]);
  if ($mouse_description=="Périphérique d`interface utilisateur USB")
    $mouse_description="Souris USB";
  if ($mouse_description!="") {
    //enregistrer souris
    $mouse_description=substr($mouse_description,0,80);
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$mouse_description."'");
    if (!is_numeric($idmodele)) {  //souris a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$mouse_description; 
      $nval["connecteur"]=$mouse_port; 
      $nval["nature"]="souris"; 
      $nval["classe"]="peripherique"; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = 
    ".$idstation);
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
  }
}

function insert_sound ($split) {
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Sound</h2><br />";}
    $extended = explode('^^^',$split);
    $sound_name = trim($extended[1]);
  if ($sound_name!="") {
    $sound_name=str_replace("(R)","",$sound_name);
    $sound_name=str_replace("(r)","",$sound_name);
    //enregistrer carte son
    $sound_name=substr($sound_name,0,80);
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$sound_name."' and nature = 'carteson'");
    if (!is_numeric($idmodele)) {  //carte son a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$sound_name; 
      $nval["nature"]="carteson"; 
      $nval["classe"]="composant"; 
      $nval["rank"]=7; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    $idelement=0;
    $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = 
    ".$idstation);
    if (!is_numeric($idelement)) {  //element à créer
      $idelement=$db->nextId("element");
      $nval="";
      $nval["idelement"]=$idelement;
      $nval["idstation"]=$idstation; 
      $nval["idmodele"]=$idmodele; 
      $nval["datescan"]=date("Y-m-d"); 
      $nval["idetatelement"]=1;   //affecte
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    } else {  //mise à jour element
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    }
}

function insert_software ($split) {
    global $idstation, $verbose, $db;
    $extended = explode('^^^',$split);
    if ($verbose == "y"){echo "<h2>Logiciel</h2><br />";}
    $software_name = trim($extended[1]);
    $software_version = trim($extended[2]);
    $software_install_date = trim($extended[3]);
    $software_publisher = substr(trim($extended[4]),0,50);
  if ($software_name!="") {
    if (strstr($software_install_date,"/"))
      $software_install_date=str_replace("/","-",$software_install_date);
    else
      if (is_numeric($software_install_date))
        $software_install_date=substr($software_install_date,0,4)."-".
substr($software_install_date,4,2)."-".substr($software_install_date,6,2);
      else
        $software_install_date="0000-00-00";
    //enregistrer logiciel
    $idediteur=0;
    if ($software_publisher) {
      $idediteur=$db->getOne("select idmarque from marque where libmarque = '".$software_publisher."'");
      if (!is_numeric($idediteur)) {  //editeur à créer 
        $idediteur=$db->nextId("marque");
        $nval="";
        $nval["idmarque"]=$idediteur;
        $nval["libmarque"]=$software_publisher;
        $nval["publisheronly"]=1;
        $res= $db->autoExecute("marque",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
    }
    $software_name=substr($software_name,0,80);
    $idmodele=0;
    $idmodele=$db->getOne("select idmodele from modeleelement where libmodele = '".$software_name."' and nature = 'logiciel'");
    if (!is_numeric($idmodele)) {  //logiciel a creer
      $idmodele=$db->nextId("modeleelement");
      $nval="";
      $nval["idmodele"]=$idmodele;
      $nval["libmodele"]=$software_name; 
      $nval["idmarque"]=$idediteur; 
//      $nval["integre"]="f";     par defaut
      $nval["nature"]="logiciel"; 
      $nval["classe"]="logiciel"; 
      $res= $db->autoExecute("modeleelement",$nval,DB_AUTOQUERY_INSERT);
      if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
    if ($db->getOne("select integre from modeleelement where idmodele = ".$idmodele)=="v") {
    //n'enregistrer que les logiciels autorisés
      $idelement=0;
      $idelement=$db->getOne("select idelement from element where idmodele = ".$idmodele." and idstation = ".$idstation);
      if (!is_numeric($idelement)) {  //element à créer
        $idelement=$db->nextId("element");
        $nval="";
        $nval["idelement"]=$idelement;
        $nval["idstation"]=$idstation; 
        $nval["idmodele"]=$idmodele; 
        $nval["version"]=$software_version;
        $nval["dateatelier"]=$software_install_date; 
        $nval["datescan"]=date("Y-m-d"); 
        $nval["idetatelement"]=1;   //affecte
        $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_INSERT);
        if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      } else {  //mise à jour element
        $cle= "idelement = ".$idelement;
        $nval="";
        $nval["version"]=$software_version;
        $nval["dateatelier"]=$software_install_date; 
        $nval["datescan"]=date("Y-m-d"); 
        $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
          if (DB :: isError($res))
          echo($res->getDebugInfo().' '.$res->getMessage());
      }
    }
  } 
}

function insert_ms_keys ($split) {
    global $idstation, $verbose, $db;
    if ($verbose == "y"){echo "<h2>Numero Serie</h2><br />";}
    $extended = explode('^^^',$split);
    $ms_keys_cd_key = trim($extended[1]);
    $ms_keys_key_type = trim($extended[2]);
  if ($ms_keys_cd_key!="") {
    $key_word=explode("_",$ms_keys_key_type);
    $idelement=0;
    $idelement=$db->getOne("select idelement from element inner join modeleelement on element.idmodele=
    modeleelement.idmodele where libmodele like '%".
    $key_word[0]."%".$key_word[1]."%' and idstation = ".$idstation);
    if (is_numeric($idelement)) {  //logiciel existe
      $cle= "idelement = ".$idelement;
      $nval="";
      $nval["numserie"]=$ms_keys_cd_key;
      $nval["datescan"]=date("Y-m-d"); 
      $res= $db->autoExecute("element",$nval,DB_AUTOQUERY_UPDATE,$cle);
        if (DB :: isError($res))
        echo($res->getDebugInfo().' '.$res->getMessage());
    }
  }
}

echo "<a href=\"javascript:window.close()\" name=\"clicktoclose\">Close</a>";

//Get current time as we did at start
    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
//Store end time in a variable
    $tend = $mtime;
//Calculate the difference
    $totaltime = ($tend - $tstart);
//Output result
    echo "<br />&nbsp;<br />Page was generated in " . round($totaltime,2) . " seconds !";

if (isset($_GET['fs'])) {
//  echo "Thankyou. Your PC has now been audited. <a href=\"JavaScript:window.close();\">Close this window</a>.";
} else {
//  header("Location: index.php");
}

// deconnexion
$db->disconnect();
if ($DEBUG == 1)
  echo "La base ".$dsn['database']." est déconnectée.<br>";

function nslookup ($ip) {
 $host = split('Name:',`nslookup $ip`);
 return ( trim (isset($host[1]) ? str_replace ("\n".'Address:  '.$ip, '', $host[1]) : $ip));
}

?>