Subversion Repositories ALCASAR

Rev

Rev 2853 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1300 richard 1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
<HTML><!-- Written by Rexy, Romero P. & 3abTux -->
3
<HEAD>
4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
<TITLE>Users import</TITLE>
2817 rexy 6
<link rel="stylesheet" href="/css/acc.css" type="text/css">
1300 richard 7
</HEAD>
8
<body>
3028 rexy 9
<div id="ldoverlay" class="overlay">
10
	<div class="lds-spinner" id="spinner"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
11
</div>
1300 richard 12
<?php
1508 richard 13
// Choice of language
1300 richard 14
$Language = 'en';
15
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
16
  $Langue = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
17
  $Language = strtolower(substr(chop($Langue[0]),0,2)); }
18
if($Language == 'fr'){
1583 richard 19
  $l_title = "Importer et vider la base des usagers";
2853 rexy 20
  $l_database_state = "état actuel de la base : nombre de groupes = ";
1300 richard 21
  $l_number_of_users = "Nombre d'usagers";
1583 richard 22
  $l_text_import = "Importer à partir d'un fichier texte";
23
  $l_text_import_help = "Ce fichier ne doit contenir que des noms d'usagers écrits les uns sous les autres.";
1300 richard 24
  $l_file = "Fichier";
1583 richard 25
  $l_users_group = "Définissez leur groupe (conseillé)";
26
  $l_send = "Importer";
27
  $l_empty = "Vider";
1835 raphael.pi 28
  $l_empty_warn = "Vous êtes sur le point de vider entièrement la base de donnée des utilisateurs... Souhaitez vous continuer?";
1300 richard 29
  $l_imported_files = "Fichiers des identifiants/mot_de_passe import&eacute;s durant les derni&egrave;res 24h :";
1583 richard 30
  $l_db_import = "Importer à partir d'une sauvegarde de la base d'usagers";
31
  $l_db_import_help = "Afin de pouvoir imputer les dernières traces de connexion, une sauvegarde de la base actuelle sera automatiquement réalisée.";
32
  $l_db_reset = "Vider la base des usagers";
33
  $l_error_ext_txt = "Erreur! Veuillez sélectionner un fichier avec l'extension '.csv' ou '.txt'";
34
  $l_error_ext_sql = "Erreur! Veuillez sélectionner un fichier avec l'extension '.sql' ou '.sql.gz'";
1300 richard 35
  $l_group_empty = "La liste des groupes est vide";
36
  $l_out_title = "   ---  Accès à Internet via ALCASAR  ---  ";
37
  $l_out_login = "Nom de connexion :";
38
  $l_out_passwd = "Mot de passe :";
39
  $l_out_mind = "Pensez à changer votre mot de passe (lien sur la page d'authentification)";
2853 rexy 40
} else if($Language === 'es') {
41
  $l_title = "Importar y vaciar la base de datos de usuarios.";
42
  $l_database_state ="Estado de la base de datos: cantidad de grupos = ";
43
  $l_number_of_users = "Cantidad de usuarios";
44
  $l_text_import = "Importar desde un archivo de texto";
45
  $l_text_import_help = "En este archivo, debe escribir solo el \"nombre de usuario\" uno debajo del otro.";
46
  $l_file = "Archivo";
47
  $l_users_group = "Definir el grupo (aconsejable)";
48
  $l_send = "Importar";
49
  $l_empty = "Vaciar";
50
  $l_empty_warn = "Está a punto de vaciar toda la base de datos de usuarios ... ¿Desea continuar?";
51
  $l_imported_files = "Archivos de nombres de usuarios / contraseñas importados durante las últimas 24hs: ";
52
  $l_db_import = "Importar desde un archivo de base de datos de usuarios guardado";
53
  $l_db_import_help = "Para atribuir las últimas conexiones, la base de datos de usuarios reales se guardará automáticamente.";
54
  $l_db_reset = "Vaciar la base de datos de usuarios";
55
  $l_error_ext_txt = "¡Error! Seleccione un archivo con la extensión '.txt' o '.csv'";
56
  $l_error_ext_sql = "¡Error! Seleccione un archivo con la extensión '.sql' o '.sql.gz'";
57
  $l_group_empty = "La lista de grupos está vacía.";
58
  $l_out_title = "   ---  Accesos a Internet mediante ALCASAR  ---  ";
59
  $l_out_login = "Nombre de usuario:";
60
  $l_out_passwd = "Contraseña:";
61
  $l_out_mind = "No olvide cambiar su contraseña (hay un enlace en la ventana de autenticación)";
62
} else {
1583 richard 63
  $l_title = "Import and empty the users database";
2853 rexy 64
  $l_database_state ="State of the database : number of groups = ";
1300 richard 65
  $l_number_of_users = "Number of users";
1583 richard 66
  $l_text_import = "Import from a text file";
1300 richard 67
  $l_text_import_help = "In this file, you must write only the user login one below the other.";
68
  $l_file = "File";
69
  $l_users_group = "Define their group (advisable)";
1583 richard 70
  $l_send = "Import";
71
  $l_empty = "Empty";
1835 raphael.pi 72
  $l_empty_warn = "You are close to empty the whole user database... Would you like to proceed?";
1300 richard 73
  $l_imported_files = "Logins/passwords file imported during the last 24h :";
1583 richard 74
  $l_db_import = "Import from a saved users database file";
1300 richard 75
  $l_db_import_help = "In order to impute the last connections, the actual users database will be automaticly saved.";
1583 richard 76
  $l_db_reset = "Empty the users database";
1300 richard 77
  $l_error_ext_txt = "Error! Please select a file with '.txt' or '.csv' extension";
1583 richard 78
  $l_error_ext_sql = "Error! Please select a file with '.sql' or '.sql.gz' extension";
1300 richard 79
  $l_group_empty = "The group list is empty";
80
  $l_out_title = "   ---  Internet access via ALCASAR  ---  ";
81
  $l_out_login = "Login :";
82
  $l_out_passwd = "Password :";
83
  $l_out_mind = "Don't forget to change your password (a link is on the authentication window)";
84
}
2414 tom.houday 85
 
86
require('/etc/freeradius-web/config.php');
87
 
1300 richard 88
function getImportFileList(){
89
	$importFile = array();
90
	if ($handle = opendir('/tmp')) {
91
		while (false !== ($file = readdir($handle))) {
92
			if ($file != "." && $file != "..") {
93
				$ext = pathinfo($file ,PATHINFO_EXTENSION);
94
				$name = substr($file, 0, -(strlen($ext)+1)); //Retirer les lettres de l'extension ET le point
95
				if ($ext=="pwd"){
96
				$importFile[] = $name;
97
				}
98
			}
99
		}
100
		closedir($handle);
101
	}
102
	return $importFile;
103
}
2414 tom.houday 104
 
1300 richard 105
function GenPassword($nb_car="8")
2414 tom.houday 106
{
1508 richard 107
// Random password
1300 richard 108
	$password = "";
109
	$chaine  = "aAzZeErRtTyYuUIopP152346897mMLkK";
110
	$chaine .= "jJhHgGfFdDsSqQwWxXcCvVbBnN152346897";
2414 tom.houday 111
	while($nb_car != 0) {
1300 richard 112
		$i = rand(0,71);
113
		$password .= $chaine[$i];
2414 tom.houday 114
		$nb_car--;
1300 richard 115
	}
2414 tom.houday 116
	return $password;
117
}
118
 
119
function freeradiusweb_removeDisallowedChars($login)
120
{
2415 tom.houday 121
	global $config;
122
 
2414 tom.houday 123
	if ($login != '')
124
	{
125
		$accent = "âêôûéàèùîáâãäçèéêëìíîïñòóôõöùúûü";
126
		$login = preg_replace("/[^\w$accent\.\/\@\:\-]/",'',$login);
127
	}
128
	if ($login != '' && $config['general_strip_realms'] == 'yes'){
129
		$realm_del = ($config['general_realm_delimiter'] != '') ? $config['general_realm_delimiter'] : '@';
130
		$realm_for = ($config['general_realm_format'] != '') ? $config['general_realm_format'] : 'suffix';
131
		$new = explode($realm_del,$login,2);
132
		if (count($new) == 2)
133
			$login = ($realm_for == 'suffix') ? $new[0] : $new[1];
134
	}
135
 
136
	return $login;
137
}
138
 
1300 richard 139
?>
140
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
141
<tr><th><? echo "$l_title"; ?></th></tr>
142
<tr bgcolor="#FFCC66"><td><img src="/images/pix.gif" width="1" height="2"></td></tr>
143
</TABLE>
144
<TABLE width="100%" border=1 cellspacing=0 cellpadding=1>
2853 rexy 145
<tr><td valign="middle" align="center">
146
<H3> 
1300 richard 147
<?php
2853 rexy 148
echo "$l_database_state";
1300 richard 149
$LIBpath = "../lib/";
150
if (is_file($LIBpath."sql/drivers/$config[sql_type]/functions.php"))
151
	{
152
	include_once($LIBpath."sql/drivers/$config[sql_type]/functions.php");
153
	}
154
else
155
	{
156
	echo "<b>Could not include SQL library</b><br>\n";
157
	exit();
158
	}
159
include_once($LIBpath.'functions.php');
160
if ($config['sql_use_operators'] == 'true')
161
	{
162
	include($LIBpath."operators.php");
163
	$text = ',op';
164
	$passwd_op = ",':='";
165
	}
166
$link = @da_sql_pconnect($config);
167
if (isset ($_POST ['choix'])) {	$choix = $_POST ['choix']; }
168
	else { $choix = ''; }
169
if ($choix == "raz")
170
	{
1827 raphael.pi 171
	exec ("sudo /usr/local/bin/alcasar-logout.sh all");
172
	exec ("sudo /usr/local/bin/alcasar-mysql.sh --raz");
1300 richard 173
	}
1508 richard 174
// a file is downloaded
1502 richard 175
if(isset($_FILES['import-users']) && ($_FILES['import-users']['name'] !=""))
1300 richard 176
	{
177
	unset($result);
178
	if (isset ($_POST['groupe'])) $group = $_POST ['groupe'];
1583 richard 179
	list($file_name , $extension) = explode("." , $_FILES['import-users']['name'] , 2); 
1508 richard 180
	if ($choix == "csv") // import of a txt file
1300 richard 181
		{
1508 richard 182
		if (($extension != 'csv') && ($extension != 'txt')) $result = $l_error_ext_txt;
1300 richard 183
		else 
184
			{
1583 richard 185
			$destination = '/tmp/import_file.txt';
1827 raphael.pi 186
			exec ("sudo /usr/local/bin/alcasar-mysql.sh --dump");
1300 richard 187
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
188
			$RS_in   = file ($destination);
189
			$da_abort=0;
190
			if ($link)
191
				{
192
				if (is_file($LIBpath."crypt/$config[general_encryption_method].php"))
193
					{
194
					include($LIBpath."crypt/$config[general_encryption_method].php");			
195
					$tmpdate = date("Ymd-his");
1502 richard 196
					$file_out = "/tmp/$tmpdate-$file_name.pwd" ;
1300 richard 197
					$RS_out = fopen ("$file_out", "wb");
198
					foreach ($RS_in as $no => $ligne)
199
						{
200
						if (substr($ligne,0,3) == pack('CCC',239,187,191)) # remove UTF8-BOM 
201
							{
202
								$ligne = substr ($ligne,3);
203
							}
2415 tom.houday 204
						$tligne = explode(" ",$ligne);
1300 richard 205
						$login = trim ($tligne[0]);
206
						$password = trim ($tligne[1]);
207
						if ($login != '')
208
							{
209
							if ($password == "")
210
								{
211
								$password = GenPassword();
2414 tom.houday 212
								}
213
							$login = freeradiusweb_removeDisallowedChars($login);
1831 raphael.pi 214
							$login = da_sql_escape_string($link,$login);
2415 tom.houday 215
							$passwd = da_encrypt($password);
1831 raphael.pi 216
							$passwd = da_sql_escape_string($link,$passwd);
1508 richard 217
// insertion (login + password) dans la table "radcheck" (si l'usager existe --> changement de mot de passe)
1300 richard 218
							$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_check_table] (attribute,value,username $text) VALUES ('$config[sql_password_attribute]','$passwd','$login' $passwd_op);");
219
							if (!$res || !@da_sql_affected_rows($link,$res,$config))
220
								{
221
								echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
222
								$da_abort=1;
223
								}
224
							else
225
								{
1508 richard 226
// create the user informations file
1300 richard 227
								fputs($RS_out,"$l_out_title\r\n\r\n");
228
								fputs($RS_out,"$l_out_login $login   |   $l_out_passwd $password\r\n\r\n");
229
								fputs($RS_out,"$l_out_mind\r\n\r\n");
230
								fputs($RS_out,"--------------------------------------------------------------------------------\r\n\r\n");
231
								}	
1508 richard 232
// insertion de l'usager dans la table "userinfo"
2415 tom.houday 233
							if ($config['sql_use_user_info_table'] == 'true' && !$da_abort)
1300 richard 234
								{
235
							$res = @da_sql_query($link,$config, "SELECT username FROM $config[sql_user_info_table] WHERE username = '$login';");
236
							if ($res)
237
								{
238
								if (!@da_sql_num_rows($res,$config))
239
									{
1561 richard 240
									$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_user_info_table] (username) VALUES ('$login');");
1300 richard 241
									if (!$res || !@da_sql_affected_rows($link,$res,$config))
242
										echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
243
									}	
244
								else
245
									echo "<b>User already exists in user info table.</b><br>\n";
246
								}
247
							else
248
								echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
2415 tom.houday 249
								if ((isset($group)) && (!empty($group)))
1300 richard 250
									{
1831 raphael.pi 251
									$group = da_sql_escape_string($link,$group);
1300 richard 252
									$res = @da_sql_query($link,$config,"SELECT username FROM $config[sql_usergroup_table] WHERE username = '$login' AND groupname = '$group';");
253
									if ($res)
254
										{
255
										if (!@da_sql_num_rows($res,$config))
256
											{
257
											$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_usergroup_table] (username,groupname) VALUES ('$login','$group');");
258
											if (!$res || !@da_sql_affected_rows($link,$res,$config))
259
												echo "<b>Could not add user to group $group. SQL Error</b><br>\n";
260
											} # end if 
261
										else
262
											echo "<b>User already is a member of group $group</b><br>\n";
263
										} # end if
264
									else
265
										echo "<b>Could not add user to group $group: " . da_sql_error($link,$config) . "</b><br>\n";
1508 richard 266
									} // end if ($group)
267
						 		} // end if ($config)		
268
							} // end if ($login !='')
269
						} // end foreach
1300 richard 270
					fclose($RS_out);
1508 richard 271
					} // end if (is file)
272
				} // end if (link)
1583 richard 273
			unlink ($destination);
274
			} // end if (extension = (txt or cvs))
1300 richard 275
		}
1508 richard 276
	else if ($choix == "bdd") // database import
1300 richard 277
		{
1583 richard 278
		if (($extension == 'sql') || ($extension == 'sql.gz')) 
1300 richard 279
			{
1583 richard 280
			$destination = '/tmp/import_file.'.$extension;
1827 raphael.pi 281
			exec ("sudo /usr/local/bin/alcasar-mysql.sh --dump");
1300 richard 282
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
1827 raphael.pi 283
			exec ("sudo /usr/local/bin/alcasar-mysql.sh --import $destination");
1583 richard 284
			//echo "OK : $destination";
285
			unlink ($destination);
1300 richard 286
			}
1583 richard 287
		else $result = $l_error_ext_sql;
1300 richard 288
		}
289
	}
290
if ($link)
291
	{
292
	$res = @da_sql_query($link,$config,"SELECT GroupName FROM radusergroup GROUP BY GroupName");
293
	if ($res)
294
		{
295
		$nb_group = @da_sql_num_rows($res,$config);
296
		echo $nb_group;
297
		}
298
	}
299
echo ", $l_number_of_users = ";
300
if ($link)
301
	{
302
	$res = @da_sql_query($link,$config,"SELECT UserName FROM userinfo");
303
	if ($res)
304
		{
305
		$nb_user = @da_sql_num_rows($res,$config);
306
		echo "$nb_user";
307
		}
308
	}
2853 rexy 309
echo "</H3></td></tr><tr><td>";
1300 richard 310
echo "<TABLE width=\"100%\" border=0 cellspacing=0 cellpadding=1>";
2853 rexy 311
echo "<tr><td valign=\"middle\" align=\"center\" colspan=\"2\">";
312
echo "<H3>$l_text_import</H3></td></tr>";
1300 richard 313
echo "<tr><td valign=\"middle\" align=\"left\">";
314
echo "$l_text_import_help<br>";
315
echo "<tr><td valign=\"middle\" align=\"left\">";
316
echo "<br><FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
1583 richard 317
echo "$l_file (.txt or .csv) : <input type=\"file\" name=\"import-users\"><br>";
1300 richard 318
echo "$l_users_group : ";
319
require("../lib/defaults.php");
320
include_once("../lib/$config[general_lib_type]/group_info.php");
321
if (isset($existing_groups)){
322
	echo "<select name=\"groupe\">";
323
	echo "<option value=\"\" selected>";
324
	foreach ($member_groups as $group)
325
		echo "<option value=\"$group\">$group\n";
326
	echo " </select>";
327
	}
328
else echo "$l_group_empty";
329
echo "<br>";
330
echo "<input type='hidden' name='choix' value='csv'>";
331
if (($choix == "csv") && isset($result)) echo "<b>".$result."</b><BR>";
3028 rexy 332
echo "<input type=\"submit\" onClick=\"this.disabled=true; document.getElementById('ldoverlay').style.display='block'; value=\"$l_send\">";
1300 richard 333
echo "</FORM></td>";
334
echo "<td>";
335
$ImportFileList = getImportFileList();
336
if (count($ImportFileList) > 0){
337
	echo "$l_imported_files";
338
	echo "<ul>";
339
	foreach ( $ImportFileList as $ImportFile ) //on parcours le tableau 
340
	{
341
	echo "<li>".$ImportFile." ( <a href=\"import_file.php?file=$ImportFile\">txt</a> - <a href=\"import_file.php?file=$ImportFile&format=pdf\">pdf</a> )</li>";
342
	} 
343
	echo "</ul>";
344
} else {
345
	echo "<br>";
346
}
347
echo "</td></tr></table>";
348
echo "<tr><td valign=\"middle\" align=\"left\">";
349
echo "<H3><CENTER>$l_db_import</CENTER></H3>";
350
echo "$l_db_import_help <br><br>";
351
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
1583 richard 352
echo "$l_file (.sql or .sql.gz) : <input type=\"file\" name=\"import-users\"><br>";
1300 richard 353
echo "<input type='hidden' name='choix' value='bdd'>";
354
if (($choix == "bdd") && isset($result)) echo "<b>".$result."</b><BR>";
3028 rexy 355
echo "<input type=\"submit\" onClick=\"this.disabled=true; document.getElementById('ldoverlay').style.display='block'; value=\"$l_send\">";
1300 richard 356
echo "</FORM>";
357
echo "</td></tr>";
358
echo "<tr><td valign=\"middle\" align=\"left\">";
359
echo "<H3><CENTER>$l_db_reset</CENTER></H3>";
360
echo "$l_db_import_help<br><br>";
1835 raphael.pi 361
echo "<FORM onsubmit=\"return confirm('$l_empty_warn');\" action='$_SERVER[PHP_SELF]' method=POST>";
1300 richard 362
echo "<input type='hidden' name='choix' value='raz'>";
3028 rexy 363
echo "<input type=\"submit\" onClick=\"this.disabled=true; document.getElementById('ldoverlay').style.display='block'; value=\"$l_empty\">";
1300 richard 364
echo "</FORM>";
365
echo "</TD></TR></TABLE>";
366
?>
367
</BODY>
368
</HTML>