#760: Import de Users via csv
-------------------------+--------------------------------------------------
Reporter: Tsmr | Owner: anonymous
Type: enhancement | Status: closed
Priority: normal | Milestone: 0.80
Component: Autre | Version: 0.68.1
Severity: feature | Resolution: fixed
Keywords: | Cd: 4
-------------------------+--------------------------------------------------
Changes (by walid):
* resolution: => fixed
* status: new => closed
Old description:
> Je ne sais pas si mon code est trés académique, j'en doute même fort,
> mais il doit fonctionner :P
>
> dans le fichier de lang :
>
> {{{
> $lang["csv"][0]="Import csv";
> $lang["csv"][1]="Selectionner votre fichier *.csv";
> $lang["csv"][2]="Importation échouée";
> $lang["csv"][3]="Désolé, mais vous n'avez pas spécifié de chemin
> valide.";
> $lang["csv"][4]="Lecture du fichier reussie.";
> $lang["csv"][5]="Erreur de requête sur";
> $lang["csv"][6]="Entrée importée";
> $lang["csv"][7]="Nombre de valeurs créés";
> }}}
>
> user.import.php
>
> {{{
> include ("_relpos.php");
> $NEEDED_ITEMS=array("user");
> include ($phproot . "/inc/includes.php");
>
> checkRight("user","w");
>
> commonHeader($lang["title"][2],$_SERVER["PHP_SELF"]);
>
> echo "<div align='center'>";
> echo "<form method='post' enctype='multipart/form-data'
> action='import.function.php'>";
> echo "<table class='tab_cadre'>";
> echo "<th colspan='3'>".$lang["csv"][0]." :</th>";
> echo "<tr class='tab_bg_2'>";
> echo "<td>".$lang["csv"][1]." :</td>";
> echo "<td align='center'><input type='file' name='userfile'
> value='userfile'></td>";
> echo "<td align='center'>";
> echo "<input type='submit' value='Envoyer' name='envoyer'>";
> echo "</td>";
> echo "</tr>";
> echo "</table>";
> echo "</form>";
> echo "</div>";
>
> commonFooter();
> }}}
>
> import.function.php
>
> {{{
> <?php
> /*
> * @version $Id: user.php 3798 2006-08-22 15:12:55Z moyo $
> -------------------------------------------------------------------------
> GLPI - Gestionnaire Libre de Parc Informatique
> Copyright (C) 2003-2006 by the INDEPNET Development Team.
>
> http://indepnet.net/ http://glpi-project.org
> -------------------------------------------------------------------------
>
> LICENSE
>
> This file is part of GLPI.
>
> GLPI is free software; you can redistribute it and/or modify
> it under the terms of the GNU General Public License as published by
> the Free Software Foundation; either version 2 of the License, or
> (at your option) any later version.
>
> GLPI is distributed in the hope that it will be useful,
> but WITHOUT ANY WARRANTY; without even the implied warranty of
> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> GNU General Public License for more details.
>
> You should have received a copy of the GNU General Public License
> along with GLPI; if not, write to the Free Software
> Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
> USA
> --------------------------------------------------------------------------
> */
>
> // ----------------------------------------------------------------------
> // Original Author of file:
> // Purpose of file:
> // ----------------------------------------------------------------------
>
> include ("_relpos.php");
> $NEEDED_ITEMS=array("user");
> include ($phproot . "/inc/includes.php");
>
> checkRight("user","w");
>
> commonHeader($lang["title"][2],$_SERVER["PHP_SELF"]);
>
> //recupere le nom du fichier indiqué par l'user
> $fichier=$_FILES["userfile"]["name"];
>
> // ouverture du fichier en lecture
> if ($fichier)
> {
> //ouverture du fichier temporaire
> $fp = fopen ($_FILES["userfile"]["tmp_name"], "r");
> }
> else{
> // fichier inconnu
> echo "<div align='center'><img
> src=\"".$cfg_glpi["root_doc"]."/pics/warning.png\"
> alt=\"warning\"><br><br><b>";
> echo $lang["csv"][2]."<br><br>";
> echo $lang["csv"][3]."<br><br>";
> echo "<a href=\"user.import.php\">".$lang["buttons"][13]."</a>";
> echo "</b></div>";
> commonFooter();
>
> exit();
> }
> // declaration de la variable "cpt" qui permettra de compter le nombre
> d'enregistrement réalisé
> $cpt=0;
>
> echo "<p align='center'>".$lang["csv"][4]."</p>";
>
> // importation
> while (!feof($fp))
> {
> $ligne = fgets($fp,4096);
>
> // on crée un tableau des élements séparés par des points virgule
> $liste = explode(";",$ligne);
>
> $liste[0] = ( isset($liste[0]) ) ? $liste[0] : Null;
> $liste[1] = ( isset($liste[1]) ) ? $liste[1] : Null;
> $liste[2] = ( isset($liste[2]) ) ? $liste[2] : Null;
> $liste[3] = ( isset($liste[3]) ) ? $liste[3] : Null;
> $liste[4] = ( isset($liste[4]) ) ? $liste[4] : Null;
> $liste[5] = ( isset($liste[5]) ) ? $liste[5] : Null;
> $liste[6] = ( isset($liste[6]) ) ? $liste[6] : Null;
> $liste[7] = ( isset($liste[7]) ) ? $liste[7] : Null;
> $liste[8] = ( isset($liste[8]) ) ? $liste[8] : Null;
> $liste[9] = ( isset($liste[9]) ) ? $liste[9] : Null;
> $liste[10] = ( isset($liste[10]) ) ? $liste[10] : Null;
> $liste[11] = ( isset($liste[11]) ) ? $liste[11] : Null;
> $liste[12] = ( isset($liste[12]) ) ? $liste[12] : Null;
> $liste[13] = ( isset($liste[13]) ) ? $liste[13] : Null;
>
> $name=$liste[0];
> $password=$liste[1];
> $password_md5= md5($password);
> $password="";
> $email= $liste[3];
> $phone= $liste[4];
> $phone2= $liste[5];
> $mobile= $liste[6];
> $realname= $liste[7];
> $firstname= $liste[8];
> $location= "0";
> $tracking_order= "no";
> $language= "fr_FR";
> $active= $liste[12];
> $comments= $liste[13];
>
> // pour eviter que le champ "nom" du fichier soit vide
> if ($name!='')
> {
> // nouvel ajout, compteur incrémenté
> $cpt++;
> // requete et insertion ligne par ligne
> $db = new DB;
> $query = "INSERT INTO glpi_users
> (id,name,password,password_md5,email,phone,phone2,mobile,realname,firstname,location,tracking_order,language,active,comments)
> VALUES('','$name','$password','$password_md5','$email','$phone','$phone2','$mobile','$realname','$firstname','$location','$tracking_order','$language','$active','$comments')";
>
> $result= mysql_query($query);
>
> if (mysql_error())
> {
> echo "<div align='center'>";
> echo "<table>";
> echo "<tr>";
> echo "<td align='center'>".$lang["csv"][5]."
> ".$liste[0]."</td>";
> echo "</tr>";
> echo "</table>";
> echo "</div";
> $cpt--;
> }
> else
> {
> echo "<div align='center'>";
> echo "<table>";
> echo "<tr>";
> echo "<td align='center'>".$lang["csv"][6]."</td>";
> echo "<td>".$liste[0]."</td>";
> echo "</tr>";
> echo "</table>";
> echo "</div";
>
> }
> }
> }
> // fermeture du fichier
> fclose($fp);
> echo "<br><br>";
> echo "<div align='center'>";
> echo $lang["csv"][7].": ".$cpt.".";
> echo "<br><br>";
> echo "<a href=\"user.import.php\">".$lang["buttons"][13]."</a>";
> echo "</div";
>
> commonFooter();
>
> ?>
> }}}
New description:
Je ne sais pas si mon code est trés académique, j'en doute même fort,
mais il doit fonctionner :P
dans le fichier de lang :
{{{
$lang["csv"][0]="Import csv";
$lang["csv"][1]="Selectionner votre fichier *.csv";
$lang["csv"][2]="Importation échouée";
$lang["csv"][3]="Désolé, mais vous n'avez pas spécifié de chemin
valide.";
$lang["csv"][4]="Lecture du fichier reussie.";
$lang["csv"][5]="Erreur de requête sur";
$lang["csv"][6]="Entrée importée";
$lang["csv"][7]="Nombre de valeurs créés";
}}}
user.import.php
{{{
include ("_relpos.php");
$NEEDED_ITEMS=array("user");
include ($phproot . "/inc/includes.php");
checkRight("user","w");
commonHeader($lang["title"][2],$_SERVER["PHP_SELF"]);
echo "<div align='center'>";
echo "<form method='post' enctype='multipart/form-data'
action='import.function.php'>";
echo "<table class='tab_cadre'>";
echo "<th colspan='3'>".$lang["csv"][0]." :</th>";
echo "<tr class='tab_bg_2'>";
echo "<td>".$lang["csv"][1]." :</td>";
echo "<td align='center'><input type='file' name='userfile'
value='userfile'></td>";
echo "<td align='center'>";
echo "<input type='submit' value='Envoyer' name='envoyer'>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "</div>";
commonFooter();
}}}
import.function.php
{{{
<?php
/*
* @version $Id: user.php 3798 2006-08-22 15:12:55Z moyo $
-------------------------------------------------------------------------
GLPI - Gestionnaire Libre de Parc Informatique
Copyright (C) 2003-2006 by the INDEPNET Development Team.
http://indepnet.net/ http://glpi-project.org
-------------------------------------------------------------------------
LICENSE
This file is part of GLPI.
GLPI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GLPI; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--------------------------------------------------------------------------
*/
// ----------------------------------------------------------------------
// Original Author of file:
// Purpose of file:
// ----------------------------------------------------------------------
include ("_relpos.php");
$NEEDED_ITEMS=array("user");
include ($phproot . "/inc/includes.php");
checkRight("user","w");
commonHeader($lang["title"][2],$_SERVER["PHP_SELF"]);
//recupere le nom du fichier indiqué par l'user
$fichier=$_FILES["userfile"]["name"];
// ouverture du fichier en lecture
if ($fichier)
{
//ouverture du fichier temporaire
$fp = fopen ($_FILES["userfile"]["tmp_name"], "r");
}
else{
// fichier inconnu
echo "<div align='center'><img
src=\"".$cfg_glpi["root_doc"]."/pics/warning.png\"
alt=\"warning\"><br><br><b>";
echo $lang["csv"][2]."<br><br>";
echo $lang["csv"][3]."<br><br>";
echo "<a href=\"user.import.php\">".$lang["buttons"][13]."</a>";
echo "</b></div>";
commonFooter();
exit();
}
// declaration de la variable "cpt" qui permettra de compter le nombre
d'enregistrement réalisé
$cpt=0;
echo "<p align='center'>".$lang["csv"][4]."</p>";
// importation
while (!feof($fp))
{
$ligne = fgets($fp,4096);
// on crée un tableau des élements séparés par des points virgule
$liste = explode(";",$ligne);
$liste[0] = ( isset($liste[0]) ) ? $liste[0] : Null;
$liste[1] = ( isset($liste[1]) ) ? $liste[1] : Null;
$liste[2] = ( isset($liste[2]) ) ? $liste[2] : Null;
$liste[3] = ( isset($liste[3]) ) ? $liste[3] : Null;
$liste[4] = ( isset($liste[4]) ) ? $liste[4] : Null;
$liste[5] = ( isset($liste[5]) ) ? $liste[5] : Null;
$liste[6] = ( isset($liste[6]) ) ? $liste[6] : Null;
$liste[7] = ( isset($liste[7]) ) ? $liste[7] : Null;
$liste[8] = ( isset($liste[8]) ) ? $liste[8] : Null;
$liste[9] = ( isset($liste[9]) ) ? $liste[9] : Null;
$liste[10] = ( isset($liste[10]) ) ? $liste[10] : Null;
$liste[11] = ( isset($liste[11]) ) ? $liste[11] : Null;
$liste[12] = ( isset($liste[12]) ) ? $liste[12] : Null;
$liste[13] = ( isset($liste[13]) ) ? $liste[13] : Null;
$name=$liste[0];
$password=$liste[1];
$password_md5= md5($password);
$password="";
$email= $liste[3];
$phone= $liste[4];
$phone2= $liste[5];
$mobile= $liste[6];
$realname= $liste[7];
$firstname= $liste[8];
$location= "0";
$tracking_order= "no";
$language= "fr_FR";
$active= $liste[12];
$comments= $liste[13];
// pour eviter que le champ "nom" du fichier soit vide
if ($name!='')
{
// nouvel ajout, compteur incrémenté
$cpt++;
// requete et insertion ligne par ligne
$db = new DB;
$query = "INSERT INTO glpi_users
(id,name,password,password_md5,email,phone,phone2,mobile,realname,firstname,location,tracking_order,language,active,comments)
VALUES('','$name','$password','$password_md5','$email','$phone','$phone2','$mobile','$realname','$firstname','$location','$tracking_order','$language','$active','$comments')";
$result= mysql_query($query);
if (mysql_error())
{
echo "<div align='center'>";
echo "<table>";
echo "<tr>";
echo "<td align='center'>".$lang["csv"][5]."
".$liste[0]."</td>";
echo "</tr>";
echo "</table>";
echo "</div";
$cpt--;
}
else
{
echo "<div align='center'>";
echo "<table>";
echo "<tr>";
echo "<td align='center'>".$lang["csv"][6]."</td>";
echo "<td>".$liste[0]."</td>";
echo "</tr>";
echo "</table>";
echo "</div";
}
}
}
// fermeture du fichier
fclose($fp);
echo "<br><br>";
echo "<div align='center'>";
echo $lang["csv"][7].": ".$cpt.".";
echo "<br><br>";
echo "<a href=\"user.import.php\">".$lang["buttons"][13]."</a>";
echo "</div";
commonFooter();
?>
}}}
See plugin data_injection
Comment:
See data_injection plugin
--
Ticket URL: <https://dev.indepnet.net:8080/glpi/ticket/760>
GLPI <https://dev.indepnet.net:8080/glpi/>
Gestion Libre de Parc Informatique_______________________________________________
Glpi-bugtrack mailing list
[email protected]
https://mail.gna.org/listinfo/glpi-bugtrack