Subversion Repositories ALCASAR

Rev

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