/**
  * Restaure un item
  * @global type $DB
  * @param type $arrayItem
  */
 function restaurerItem($arrayItem)
 {
     global $DB;
     $date = date('j-m-Y');
     $heure = date('H:m:s');
     //Récupération des informations de configuration
     $configClass = new PluginReformeConfig();
     $config = $configClass->getInfoAdministrative();
     //Récupération des infos de la machine
     $info = $this->getInfoMachine($arrayItem[0], $arrayItem[1]);
     $bon_reforme = null;
     //Modification de la fiche GLPI
     $comment = addslashes($info['commentaire']) . "\nRestauration effectuée par: " . "" . $arrayItem[2] . " le: " . $date . " à: " . $heure;
     //Si nom de la machine non remis
     if ($info["name"] == "") {
         $query = "SELECT name,statut,bon_reforme FROM glpi_plugin_reforme_log " . "WHERE id_item = '{$arrayItem['0']}' AND type_item = '{$arrayItem['1']}' " . "AND vie='1'";
         if ($result = $DB->query($query)) {
             if ($DB->numrows($result) > 0) {
                 $row = $DB->fetch_assoc($result);
                 $info["name"] = $row["name"];
                 $bon_reforme = $row["bon_reforme"];
                 switch ($arrayItem[1]) {
                     case 'Computer':
                         $query1 = "UPDATE glpi_computers SET name='" . $row["name"] . "'," . "states_id='" . $row["statut"] . "' WHERE id='" . $arrayItem[0] . "'";
                         $query2 = "UPDATE glpi_computers SET comment='{$comment}' " . "WHERE id='" . $arrayItem[0] . "'";
                         // instanciation de adldap
                         $cheminAdldap = $this->getAbsolutePathForm() . "/adldap";
                         require_once $cheminAdldap . '/adLDAP.php';
                         //Récupération des AD et réactivation de la machine si présente
                         $LDAPConfig = $this->getLDAPConfig($info["id"]);
                         $domaine = "";
                         //Si la machine appartenait à un domaine on la restaure
                         if ($LDAPConfig != NULL && $this->testerAD($LDAPConfig)) {
                             $serveur[0] = $LDAPConfig['serveur'];
                             $adldap = new adLDAP(array('base_dn' => $LDAPConfig['dc'], 'account_suffix' => $LDAPConfig['suffix'], 'domain_controllers' => $serveur));
                             $adldap->close();
                             $adldap->setAdminUsername($LDAPConfig['login']);
                             $adldap->setAdminPassword($LDAPConfig['passwd']);
                             $adldap->connect();
                             $result = $adldap->computer()->info($info["name"], array("distinguishedname"));
                             if ($result[0]["distinguishedname"][0] != "") {
                                 if ($adldap->user()->activerComputer($result[0]["distinguishedname"][0])) {
                                     $domaine = $LDAPConfig['suffix'];
                                 } else {
                                     $domaine = $LDAPConfig['suffix'] . "-->erreur";
                                 }
                             }
                             $adldap->close();
                         }
                         break;
                     case 'Monitor':
                         $query1 = "UPDATE glpi_monitors SET name='" . $row["name"] . "'," . "states_id='" . $row["statut"] . "' WHERE id='" . $arrayItem[0] . "'";
                         $query2 = "UPDATE glpi_monitors SET comment='{$comment}' " . "WHERE id='" . $arrayItem[0] . "'";
                         break;
                     default:
                         break;
                 }
                 $DB->query($query1);
                 $DB->query($query2);
             }
         }
     }
     // Désactivation du log de réforme
     $query = "UPDATE glpi_plugin_reforme_log SET vie='0' WHERE id_item='" . $arrayItem[0] . "'" . " AND type_item='" . $arrayItem[1] . "'";
     $DB->query($query);
     $stockage = $this->getAbsolutePathForm() . "bon_reforme";
     // Envoie du mail d'annulation de réforme
     $sujet = "Anulation de la reforme de la machine: " . $info["name"];
     $Message_Send = utf8_decode("L\\'objet : <span class=\"Titre\">" . $info["name"] . "</span> a été réformée par erreur<br><br>");
     $Message_Send .= utf8_decode("La machine a été restauré dans la base GLPI par: " . "<span class=\"Tech\">" . $arrayItem[2] . "</span><br><br>");
     $Message_Send .= utf8_decode("<span class=\"glpi\">Ce message a été envoyé " . "de façon automatique par GLPI (Plugin Reforme)<br></span>");
     $Message_Send .= utf8_decode("<span class=\"glpi\">© Viduc 2013 <A " . "HREF=\"http://viduc.sugarbox.fr/\">http://viduc.sugarbox.fr/</A> </span>");
     $this->Send_Mail($config['mail'], $sujet, $Message_Send, $this->getAbsolutePathForm() . 'bon_reforme/' . $bon_reforme . '.pdf', $bon_reforme . '.pdf');
     //Suppression du bon de réforme
     unlink($this->getAbsolutePathForm() . 'bon_reforme/' . $bon_reforme . '.pdf');
 }
 /**
  * Créer un ordinateur dans lAD
  * @global db $DB
  * @param int $id id le d'ordinateur
  * @param string $ad suffix de l'ad
  * @return boolean
  */
 function createComputer($id, $ad)
 {
     global $DB;
     // instanciation de adldap
     $cheminAdldap = $this->getAjaxAbsolutePath() . "adldap/adLDAP.php";
     require_once $cheminAdldap;
     $LDAPConfig = $this->getLDAP($ad);
     $return = false;
     if ($LDAPConfig != NULL && $this->testerAD($LDAPConfig, $cheminAdldap)) {
         // Connection à l'AD
         $serveur[0] = $LDAPConfig['serveur'];
         $adldap = new adLDAP(array('base_dn' => $LDAPConfig['dc'], 'account_suffix' => $LDAPConfig['suffix'], 'domain_controllers' => $serveur));
         $adldap->close();
         $adldap->setAdminUsername($LDAPConfig['login']);
         $adldap->setAdminPassword($LDAPConfig['passwd']);
         $adldap->connect();
         $attributes["cn"] = $this->getItemName($id);
         $container = array("Computers");
         $attributes["container"] = $container;
         $idDomaine = null;
         if ($adldap->computer()->create($attributes)) {
             //modification de la fiche glpi
             $query = "SELECT id FROM glpi_domains WHERE name='" . trim(substr($ad, 1)) . "'";
             if ($result = $DB->query($query)) {
                 if ($DB->numrows($result) > 0) {
                     $row = $DB->fetch_assoc($result);
                     if (!empty($row['id'])) {
                         $idDomaine = $row['id'];
                     }
                 }
             }
             $query = "UPDATE glpi_computers SET ";
             $query .= "domains_id='" . $idDomaine . "'";
             $query .= " WHERE id='" . $id . "'";
             if ($DB->query($query)) {
                 $return = true;
             }
         }
         // Enregistrement de l'action dans la base de données
         $technicien = $_SESSION["glpiname"];
         $date = date('j-m-Y');
         $heure = date('H:m:s');
         $query = "INSERT INTO glpi_plugin_groupead_log VALUES ('','{$id}',\n                     'Computer','{$technicien}','{$date}|{$heure}','createComputer','{$ad}')";
         $DB->query($query);
     }
     return $return;
 }
 function clonerOrdiAD($idOrdinateur, $idCloner, $groupe, $log)
 {
     require_once $this->getAjaxAbsolutePath() . "adldap/adLDAP.php";
     $name = $this->getItemName($idOrdinateur);
     $LDAPConfig = $this->getLDAPConfig($idCloner);
     if ($LDAPConfig != NULL && $this->testerAD($LDAPConfig)) {
         $serveur[0] = $LDAPConfig['serveur'];
         $adldap = new adLDAP(array('base_dn' => $LDAPConfig['dc'], 'account_suffix' => $LDAPConfig['suffix'], 'domain_controllers' => $serveur));
         $adldap->close();
         $adldap->setAdminUsername($LDAPConfig['login']);
         $adldap->setAdminPassword($LDAPConfig['passwd']);
         $adldap->connect();
         $computerInfo = $adldap->computer()->info($name, array("dn"));
         if ($computerInfo["count"] == 0) {
             // si l'ordinateur n'existe pas dans l'ad on le créé
             $attributes["cn"] = $name;
             $container = array("Computers");
             $attributes["container"] = $container;
             if ($adldap->computer()->create($attributes)) {
                 $log = "Ordinateur créé dans l'AD: " . $LDAPConfig['suffix'];
                 $this->setLog($idOrdinateur, 'Computer', $log);
             } else {
                 $log = "Erreur lors de la création de l'ordinateur dans l'AD: " . $LDAPConfig['suffix'];
                 $this->setLog($idOrdinateur, 'Computer', $log);
             }
         } else {
             $log = "L'ordinateur existe déjà dans l'AD: " . $LDAPConfig['suffix'];
             $this->setLog($idOrdinateur, 'Computer', $log);
         }
         if ($groupe != null) {
             $explodeGroupe = explode("|", $groupe);
             if ($explodeGroupe[0] != null) {
                 $explodeGroupe1 = explode(",", $explodeGroupe[0]);
                 foreach ($explodeGroupe1 as $value) {
                     $adldap->group()->addUser($value, "CN=" . $name . ",CN=Computers," . $LDAPConfig['dc']);
                     $log = "L'ordinateur a été ajouté dans le groupe: " . $value . " de l'AD: " . $LDAPConfig['suffix'];
                     $this->setLog($idOrdinateur, 'Computer', $log);
                 }
             }
         }
     }
     return true;
 }