-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin_import_users_csv.php
344 lines (323 loc) · 16.6 KB
/
admin_import_users_csv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* admin_import_user_csv.php
* script d'importation d'utilisateurs à partir d'un fichier CSV
* Ce script fait partie de l'application GRR
* Dernière modification : $Date: 2009-09-29 18:02:56 $
* @author Laurent Delineau <[email protected]>
* @copyright Copyright 2003-2008 Laurent Delineau
* @link http://www.gnu.org/licenses/licenses.html
* @package root
* @version $Id: admin_import_users_csv.php,v 1.8 2009-09-29 18:02:56 grr Exp $
* @filesource
*
* This file is part of GRR.
*
* GRR 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.
*
* GRR 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 GRR; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* $Log: admin_import_users_csv.php,v $
* Revision 1.8 2009-09-29 18:02:56 grr
* *** empty log message ***
*
* Revision 1.7 2009-04-14 12:59:17 grr
* *** empty log message ***
*
* Revision 1.6 2009-03-24 13:30:07 grr
* *** empty log message ***
*
* Revision 1.5 2009-02-27 13:28:19 grr
* *** empty log message ***
*
* Revision 1.4 2008-11-16 22:00:58 grr
* *** empty log message ***
*
*
*/
include "include/admin.inc.php";
$grr_script_name = "admin_import_users_csv.php";
$back = '';
if (isset($_SERVER['HTTP_REFERER'])) $back = my_htmlspecialcharacters($_SERVER['HTTP_REFERER']);
if ((authGetUserLevel(getUserName(),-1) < 6) and (authGetUserLevel(getUserName(),-1,'user') != 1))
{
$day = date("d");
$month = date("m");
$year = date("Y");
showAccessDenied($day, $month, $year, '',$back);
exit();
}
# print the page header
print_header("","","","",$type="with_session", $page="admin");
?>
<p>| <a href="admin_user.php"><?php echo get_vocab("back");?></a> |</p>
<?php
$reg_data = isset($_POST["reg_data"]) ? $_POST["reg_data"] : NULL;
$is_posted = isset($_POST["is_posted"]) ? $_POST["is_posted"] : NULL;
$test_login_existant='';
$test_nom_prenom_existant='';
$test_login='';
if ($reg_data != 'yes') {
// $long_max : doit être plus grand que la plus grande ligne trouvée dans le fichier CSV
$long_max = 8000;
if ($is_posted != '1') {
?>
<form enctype="multipart/form-data" action="admin_import_users_csv.php" method="post" >
<?php $csvfile=""; ?>
<p><?php echo get_vocab("admin_import_users_csv0"); ?><input type="file" name="csvfile" /></p>
<div><input type="hidden" name="is_posted" value="1" />
<p><?php echo get_vocab("admin_import_users_csv1"); ?>
<input type="checkbox" name="en_tete" value="yes" checked="checked" /></p>
<input type="submit" value="<?php echo get_vocab("submit");?>" /><br />
</div></form><div>
<?php
echo get_vocab("admin_import_users_csv2");
echo get_vocab("admin_import_users_csv3")."</div>";
}
if ($is_posted == '1') {
$valid = isset($_POST["valid"]) ? $_POST["valid"] : NULL;
$en_tete = isset($_POST["en_tete"]) ? $_POST["en_tete"] : NULL;
$csv_file = isset($_FILES["csvfile"]) ? $_FILES["csvfile"] : NULL;
echo "<form enctype=\"multipart/form-data\" action=\"admin_import_users_csv.php\" method=\"post\" >";
if($csv_file['tmp_name'] != "") {
$fp = @fopen($csv_file['tmp_name'], "r");
if(!$fp) {
echo get_vocab("admin_import_users_csv4")."</form>";
} else {
$row = 0;
echo "<table border=\"1\"><tr><td><p>".get_vocab("login")."</p></td>
<td><p>".get_vocab("name")."</p></td>
<td><p>".get_vocab("first_name")."</p></td>
<td><p>".get_vocab("pwd")."</p></td>
<td><p>".get_vocab("email")."</p></td>
<td><p>".get_vocab("type")."</p></td>
<td><p>".get_vocab("statut")."</p></td>
<td><p>".get_vocab("authentification")."</p></td>
</tr>";
$valid = 1;
while(!feof($fp)) {
if ($en_tete == 'yes') {
$data = fgetcsv ($fp, $long_max, ";");
$en_tete = 'no';
}
$data = fgetcsv ($fp, $long_max, ";");
$num = count ($data);
if ($num == 8) {
$row++;
echo "<tr>";
for ($c=0; $c<$num; $c++) {
switch ($c) {
case 0:
//login
$test_login = preg_replace("/([A-Za-z0-9_@. -])/","",$data[$c]);
if ($test_login=="") {
$data[$c] = strtoupper($data[$c]);
$test = grr_sql_count(grr_sql_query("SELECT login FROM ".TABLE_PREFIX."_utilisateurs WHERE login='$data[$c]'"));
if ($test!='0') {
echo "<td><p><font color=\"red\">$data[$c]</font></p>\n";
echo "<input type=\"hidden\" name=\"reg_stat[$row]\" value=\"existant\" />\n";
$test_login_existant = "oui";
$login_exist = "oui";
$login_valeur = $data[$c];
} else {
echo "<td><p>$data[$c]</p>\n";
echo "<input type=\"hidden\" name=\"reg_stat[$row]\" value=\"nouveau\" />\n";
$login_exist = "non";
}
$data_login = htmlentities($data[$c]);
echo "<input type=\"hidden\" name=\"reg_login[$row]\" value=\"$data_login\" /></td>\n";
} else {
echo "<td><font color=\"red\">???</font></td>\n";
$valid = 0;
}
break;
case 1:
//Nom
$test_nom_prenom_existant = 'no';
if (preg_match ("`^.{1,30}$`", $data[$c])) {
$test_nom = protect_data_sql($data[$c]);
$test_prenom = protect_data_sql($data[$c+1]);
$test_nom_prenom = grr_sql_count(grr_sql_query("SELECT nom FROM ".TABLE_PREFIX."_utilisateurs WHERE (nom='$test_nom' and prenom = '$test_prenom')"));
if ($test_nom_prenom!='0') {
$test_nom_prenom_existant = 'yes';
echo "<td><p><font color=\"blue\">$data[$c]</font></p>";
} else {
echo "<td><p>$data[$c]</p>";
}
$data_nom = htmlentities($data[$c]);
echo "<input type=\"hidden\" name=\"reg_nom[$row]\" value=\"$data_nom\" /></td>";
} else {
echo "<td><font color=\"red\">???</font></td>";
}
break;
case 2:
//Prenom
if (preg_match ("`^.{1,30}$`", $data[$c])) {
if ($test_nom_prenom_existant == 'yes') {
echo "<td><p><font color=\"blue\">$data[$c]</font></p>";
} else {
echo "<td><p>$data[$c]</p>";
}
$data_prenom = htmlentities($data[$c]);
echo "<input type=\"hidden\" name=\"reg_prenom[$row]\" value=\"$data_prenom\" /></td>";
} else {
echo "<td><font color=\"red\">???</font></td>";
$valid = 0;
}
break;
case 3:
// Mot de passe
if ((preg_match ("`^.{".$pass_leng.",30}$`", $data[$c])) or ($data[$c]=='')){
$data_mdp = htmlentities($data[$c]);
echo "<td><p>$data[$c] </p>";
echo "<input type=\"hidden\" name=\"reg_mdp[$row]\" value=\"$data_mdp\" /></td>";
} else {
echo "<td><font color=\"red\">???</font></td>";
$valid = 0;
}
break;
case 4:
// Adresse E-mail
if ((preg_match ("`^.{1,100}$`", $data[$c])) or ($data[$c]=='')){
$data_email = htmlentities($data[$c]);
echo "<td><p>$data[$c] </p>";
echo "<input type=\"hidden\" name=\"reg_email[$row]\" value=\"$data_email\" /></td>";
} else if ($data[$c]=='-') {
echo "<td><font color=\"red\">???</font>";
echo "<input type=\"hidden\" name=\"reg_email[$row]\" value=\"\" /></td>";
} else {
echo "<td><font color=\"red\">???</font></td>";
$valid = 0;
}
break;
case 5:
// Type d'utilisateur : quatre valeurs autorisées : visiteur, utilisateur, administrateur, gestionnaire_utilisateur
// Si c'est un gestionnaire d'utilisateurs qui importe, seuls les types visiteur et utilisateur sont autorisés
if (authGetUserLevel(getUserName(),-1) >= 6)
$filtre = "(visiteur|utilisateur|administrateur|gestionnaire_utilisateur)";
else
$filtre = "`(visiteur|utilisateur)`";
if (preg_match ($filtre, $data[$c])) {
$data_type_user = htmlentities($data[$c]);
echo "<td><p>$data[$c] </p>";
echo "<input type=\"hidden\" name=\"reg_type_user[$row]\" value=\"$data_type_user\" /></td>";
} else {
echo "<td><font color=\"red\">???</font></td>";
$valid = 0;
}
break;
case 6:
// statut: deux valeurs autorisées : actif ou inactif
if (preg_match ("`(actif|inactif)`", $data[$c])) {
$data_statut = htmlentities($data[$c]);
echo "<td><p>$data[$c] </p>";
echo "<input type=\"hidden\" name=\"reg_statut[$row]\" value=\"$data_statut\" /></td>";
} else {
echo "<td><font color=\"red\">???</font></td>";
$valid = 0;
}
break;
case 7:
// Type d'authentification : deux valeurs autorisées : local ou ext
if (preg_match ("`(local|ext)`", $data[$c])) {
$data_type_auth = htmlentities($data[$c]);
if (($data_mdp == "") and ($data_type_auth == "local")) {
echo "<td><font color=\"red\">local -> mot de passe incorrect</font></td>";
$valid = 0;
} else if (($data_mdp != "") and ($data_type_auth == "ext")) {
echo "<td><font color=\"red\">ext -> mot de passe incorrect</font></td>";
$valid = 0;
} else {
echo "<td><p>$data[$c] </p>";
echo "<input type=\"hidden\" name=\"reg_type_auth[$row]\" value=\"$data_type_auth\" /></td>";
}
} else {
echo "<td><font color=\"red\">???</font></td>";
$valid = 0;
}
break;
}
}
echo "</tr>";
}
}
fclose($fp);
echo "</table>";
echo "<p>".get_vocab("admin_import_users_csv5")."$row ".get_vocab("admin_import_users_csv6")."</p>\n";
if ($row > 0) {
if ($test_login_existant == "oui") {
echo get_vocab("admin_import_users_csv7");
}
if ($test_nom_prenom_existant == 'yes') {
echo get_vocab("admin_import_users_csv8");
}
if ($valid == '1') {
echo "<div><input type=\"submit\" value=\"".get_vocab("submit")."\" />\n";
echo "<input type=\"hidden\" name=\"nb_row\" value=\"$row\" />\n";
echo "<input type=\"hidden\" name=\"reg_data\" value=\"yes\" />\n";
echo "</div></form>";
} else {
echo get_vocab("admin_import_users_csv9");
echo "</form>";
}
} else {
echo "<p>".get_vocab("admin_import_users_csv10")."</p></form>";
}
}
} else {
echo "<p>".get_vocab("admin_import_users_csv11")."</p></form>";
}
}
} else {
// Restriction dans le cas d'une démo
VerifyModeDemo();
// Phase d'enregistrement des données
$nb_row = isset($_POST["nb_row"]) ? $_POST["nb_row"] : NULL;
$reg_stat = isset($_POST["reg_stat"]) ? $_POST["reg_stat"] : NULL;
$reg_login = isset($_POST["reg_login"]) ? $_POST["reg_login"] : NULL;
$reg_nom = isset($_POST["reg_nom"]) ? $_POST["reg_nom"] : NULL;
$reg_prenom = isset($_POST["reg_prenom"]) ? $_POST["reg_prenom"] : NULL;
$reg_email = isset($_POST["reg_email"]) ? $_POST["reg_email"] : NULL;
$reg_mdp = isset($_POST["reg_mdp"]) ? $_POST["reg_mdp"] : NULL;
$reg_type_user = isset($_POST["reg_type_user"]) ? $_POST["reg_type_user"] : NULL;
$reg_statut = isset($_POST["reg_statut"]) ? $_POST["reg_statut"] : NULL;
$reg_type_auth = isset($_POST["reg_type_auth"]) ? $_POST["reg_type_auth"] : NULL;
$nb_row++;
for ($row=1; $row<$nb_row; $row++) {
if ($reg_type_auth[$row]!="ext")
$reg_mdp[$row] = md5(unslashes($reg_mdp[$row]));
// On nettoie les windozeries
$reg_nom[$row] = protect_data_sql(corriger_caracteres($reg_nom[$row]));
$reg_prenom[$row] = protect_data_sql(corriger_caracteres($reg_prenom[$row]));
$reg_email[$row] = protect_data_sql(corriger_caracteres($reg_email[$row]));
$test_login = grr_sql_count(grr_sql_query("SELECT login FROM ".TABLE_PREFIX."_utilisateurs WHERE login='$reg_login[$row]'"));
if ($test_login == 0) {
$regdata = grr_sql_query("INSERT INTO ".TABLE_PREFIX."_utilisateurs SET nom='".$reg_nom[$row]."',prenom='".$reg_prenom[$row]."',login='".$reg_login[$row]."',email='".$reg_email[$row]."',password='".protect_data_sql($reg_mdp[$row])."',statut='".$reg_type_user[$row]."',etat='".$reg_statut[$row]."',source='".$reg_type_auth[$row]."'");
} else {
$regdata = grr_sql_query("UPDATE ".TABLE_PREFIX."_utilisateurs SET nom='".$reg_nom[$row]."',prenom='".$reg_prenom[$row]."',email='".$reg_email[$row]."',password='".protect_data_sql($reg_mdp[$row])."',statut='".$reg_type_user[$row]."',etat='".$reg_statut[$row]."',source='".$reg_type_auth[$row]."' WHERE login='".$reg_login[$row]."'");
}
if (!$regdata) {
echo "<p><font color=\"red\">".$reg_login[$row].get_vocab("deux_points").get_vocab("message_records_error")."</font></p>";
} else {
if ($reg_stat[$row] == "nouveau") {
echo "<p>".$reg_login[$row].get_vocab("deux_points").get_vocab("admin_import_users_csv12")."</p>";
} else {
echo "<p>".$reg_login[$row].get_vocab("deux_points").get_vocab("message_records")."</p>";
}
}
}
}
?>
</body>
</html>