Subversion Repositories ALCASAR

Rev

Rev 1835 | Rev 2415 | 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>
6
<link rel="stylesheet" href="/css/style.css" type="text/css">
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";
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)";
37
}
38
else {
1583 richard 39
  $l_title = "Import and empty the users database";
1300 richard 40
  $l_database_state ="State of the database : number of groups =";
41
  $l_number_of_users = "Number of users";
1583 richard 42
  $l_text_import = "Import from a text file";
1300 richard 43
  $l_text_import_help = "In this file, you must write only the user login one below the other.";
44
  $l_file = "File";
45
  $l_users_group = "Define their group (advisable)";
1583 richard 46
  $l_send = "Import";
47
  $l_empty = "Empty";
1835 raphael.pi 48
  $l_empty_warn = "You are close to empty the whole user database... Would you like to proceed?";
1300 richard 49
  $l_imported_files = "Logins/passwords file imported during the last 24h :";
1583 richard 50
  $l_db_import = "Import from a saved users database file";
1300 richard 51
  $l_db_import_help = "In order to impute the last connections, the actual users database will be automaticly saved.";
1583 richard 52
  $l_db_reset = "Empty the users database";
1300 richard 53
  $l_error_ext_txt = "Error! Please select a file with '.txt' or '.csv' extension";
1583 richard 54
  $l_error_ext_sql = "Error! Please select a file with '.sql' or '.sql.gz' extension";
1300 richard 55
  $l_group_empty = "The group list is empty";
56
  $l_out_title = "   ---  Internet access via ALCASAR  ---  ";
57
  $l_out_login = "Login :";
58
  $l_out_passwd = "Password :";
59
  $l_out_mind = "Don't forget to change your password (a link is on the authentication window)";
60
}
2414 tom.houday 61
 
62
require('/etc/freeradius-web/config.php');
63
 
1300 richard 64
function getImportFileList(){
65
	$importFile = array();
66
	if ($handle = opendir('/tmp')) {
67
		while (false !== ($file = readdir($handle))) {
68
			if ($file != "." && $file != "..") {
69
				$ext = pathinfo($file ,PATHINFO_EXTENSION);
70
				$name = substr($file, 0, -(strlen($ext)+1)); //Retirer les lettres de l'extension ET le point
71
				if ($ext=="pwd"){
72
				$importFile[] = $name;
73
				}
74
			}
75
		}
76
		closedir($handle);
77
	}
78
	return $importFile;
79
}
2414 tom.houday 80
 
1300 richard 81
function GenPassword($nb_car="8")
2414 tom.houday 82
{
1508 richard 83
// Random password
1300 richard 84
	$password = "";
85
	$chaine  = "aAzZeErRtTyYuUIopP152346897mMLkK";
86
	$chaine .= "jJhHgGfFdDsSqQwWxXcCvVbBnN152346897";
2414 tom.houday 87
	while($nb_car != 0) {
1300 richard 88
		$i = rand(0,71);
89
		$password .= $chaine[$i];
2414 tom.houday 90
		$nb_car--;
1300 richard 91
	}
2414 tom.houday 92
	return $password;
93
}
94
 
95
function freeradiusweb_removeDisallowedChars($login)
96
{
97
	if ($login != '')
98
	{
99
		$accent = "âêôûéàèùîáâãäçèéêëìíîïñòóôõöùúûü";
100
		$login = preg_replace("/[^\w$accent\.\/\@\:\-]/",'',$login);
101
	}
102
	if ($login != '' && $config['general_strip_realms'] == 'yes'){
103
		$realm_del = ($config['general_realm_delimiter'] != '') ? $config['general_realm_delimiter'] : '@';
104
		$realm_for = ($config['general_realm_format'] != '') ? $config['general_realm_format'] : 'suffix';
105
		$new = explode($realm_del,$login,2);
106
		if (count($new) == 2)
107
			$login = ($realm_for == 'suffix') ? $new[0] : $new[1];
108
	}
109
 
110
	return $login;
111
}
112
 
1300 richard 113
?>
114
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
115
<tr><th><? echo "$l_title"; ?></th></tr>
116
<tr bgcolor="#FFCC66"><td><img src="/images/pix.gif" width="1" height="2"></td></tr>
117
</TABLE>
118
<TABLE width="100%" border=1 cellspacing=0 cellpadding=1>
119
<tr><td valign="middle" align="left">
120
<CENTER><H3> 
121
<?php
122
echo "$l_database_state";
123
 
124
$LIBpath = "../lib/";
125
if (is_file($LIBpath."sql/drivers/$config[sql_type]/functions.php"))
126
	{
127
	include_once($LIBpath."sql/drivers/$config[sql_type]/functions.php");
128
	}
129
else
130
	{
131
	echo "<b>Could not include SQL library</b><br>\n";
132
	exit();
133
	}
134
include_once($LIBpath.'functions.php');
135
if ($config['sql_use_operators'] == 'true')
136
	{
137
	include($LIBpath."operators.php");
138
	$text = ',op';
139
	$passwd_op = ",':='";
140
	}
141
$link = @da_sql_pconnect($config);
142
if (isset ($_POST ['choix'])) {	$choix = $_POST ['choix']; }
143
	else { $choix = ''; }
144
if ($choix == "raz")
145
	{
1827 raphael.pi 146
	exec ("sudo /usr/local/bin/alcasar-logout.sh all");
147
	exec ("sudo /usr/local/bin/alcasar-mysql.sh --raz");
1300 richard 148
	}
1508 richard 149
// a file is downloaded
1502 richard 150
if(isset($_FILES['import-users']) && ($_FILES['import-users']['name'] !=""))
1300 richard 151
	{
152
	unset($result);
153
	if (isset ($_POST['groupe'])) $group = $_POST ['groupe'];
1583 richard 154
	list($file_name , $extension) = explode("." , $_FILES['import-users']['name'] , 2); 
1508 richard 155
	if ($choix == "csv") // import of a txt file
1300 richard 156
		{
1508 richard 157
		if (($extension != 'csv') && ($extension != 'txt')) $result = $l_error_ext_txt;
1300 richard 158
		else 
159
			{
1583 richard 160
			$destination = '/tmp/import_file.txt';
1827 raphael.pi 161
			exec ("sudo /usr/local/bin/alcasar-mysql.sh --dump");
1300 richard 162
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
163
			$RS_in   = file ($destination);
164
			$da_abort=0;
165
			if ($link)
166
				{
167
				if (is_file($LIBpath."crypt/$config[general_encryption_method].php"))
168
					{
169
					include($LIBpath."crypt/$config[general_encryption_method].php");			
170
					$tmpdate = date("Ymd-his");
1502 richard 171
					$file_out = "/tmp/$tmpdate-$file_name.pwd" ;
1300 richard 172
					$RS_out = fopen ("$file_out", "wb");
173
					foreach ($RS_in as $no => $ligne)
174
						{
175
						if (substr($ligne,0,3) == pack('CCC',239,187,191)) # remove UTF8-BOM 
176
							{
177
								$ligne = substr ($ligne,3);
178
							}
179
						$tligne = split(" ",$ligne);
180
						$login = trim ($tligne[0]);
181
						$password = trim ($tligne[1]);
182
						if ($login != '')
183
							{
184
							if ($password == "")
185
								{
186
								$password = GenPassword();
2414 tom.houday 187
								}
188
							$login = freeradiusweb_removeDisallowedChars($login);
1831 raphael.pi 189
							$login = da_sql_escape_string($link,$login);
190
							$passwd = da_sql_escape_string($link,$passwd);
1300 richard 191
							$passwd = da_encrypt($password);
1508 richard 192
// insertion (login + password) dans la table "radcheck" (si l'usager existe --> changement de mot de passe)
1300 richard 193
							$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);");
194
							if (!$res || !@da_sql_affected_rows($link,$res,$config))
195
								{
196
								echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
197
								$da_abort=1;
198
								}
199
							else
200
								{
1508 richard 201
// create the user informations file
1300 richard 202
								fputs($RS_out,"$l_out_title\r\n\r\n");
203
								fputs($RS_out,"$l_out_login $login   |   $l_out_passwd $password\r\n\r\n");
204
								fputs($RS_out,"$l_out_mind\r\n\r\n");
205
								fputs($RS_out,"--------------------------------------------------------------------------------\r\n\r\n");
206
								}	
1508 richard 207
// insertion de l'usager dans la table "userinfo"
1300 richard 208
							if ($config[sql_use_user_info_table] == 'true' && !$da_abort)
209
								{
210
							$res = @da_sql_query($link,$config, "SELECT username FROM $config[sql_user_info_table] WHERE username = '$login';");
211
							if ($res)
212
								{
213
								if (!@da_sql_num_rows($res,$config))
214
									{
1561 richard 215
									$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_user_info_table] (username) VALUES ('$login');");
1300 richard 216
									if (!$res || !@da_sql_affected_rows($link,$res,$config))
217
										echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
218
									}	
219
								else
220
									echo "<b>User already exists in user info table.</b><br>\n";
221
								}
222
							else
223
								echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
224
								if ($group != '')
225
									{
1831 raphael.pi 226
									$group = da_sql_escape_string($link,$group);
1300 richard 227
									$res = @da_sql_query($link,$config,"SELECT username FROM $config[sql_usergroup_table] WHERE username = '$login' AND groupname = '$group';");
228
									if ($res)
229
										{
230
										if (!@da_sql_num_rows($res,$config))
231
											{
232
											$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_usergroup_table] (username,groupname) VALUES ('$login','$group');");
233
											if (!$res || !@da_sql_affected_rows($link,$res,$config))
234
												echo "<b>Could not add user to group $group. SQL Error</b><br>\n";
235
											} # end if 
236
										else
237
											echo "<b>User already is a member of group $group</b><br>\n";
238
										} # end if
239
									else
240
										echo "<b>Could not add user to group $group: " . da_sql_error($link,$config) . "</b><br>\n";
1508 richard 241
									} // end if ($group)
242
						 		} // end if ($config)		
243
							} // end if ($login !='')
244
						} // end foreach
1300 richard 245
					fclose($RS_out);
1508 richard 246
					} // end if (is file)
247
				} // end if (link)
1583 richard 248
			unlink ($destination);
249
			} // end if (extension = (txt or cvs))
1300 richard 250
		}
1508 richard 251
	else if ($choix == "bdd") // database import
1300 richard 252
		{
1583 richard 253
		if (($extension == 'sql') || ($extension == 'sql.gz')) 
1300 richard 254
			{
1583 richard 255
			$destination = '/tmp/import_file.'.$extension;
1827 raphael.pi 256
			exec ("sudo /usr/local/bin/alcasar-mysql.sh --dump");
1300 richard 257
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
1827 raphael.pi 258
			exec ("sudo /usr/local/bin/alcasar-mysql.sh --import $destination");
1583 richard 259
			//echo "OK : $destination";
260
			unlink ($destination);
1300 richard 261
			}
1583 richard 262
		else $result = $l_error_ext_sql;
1300 richard 263
		}
264
	}
265
if ($link)
266
	{
267
	$res = @da_sql_query($link,$config,"SELECT GroupName FROM radusergroup GROUP BY GroupName");
268
	if ($res)
269
		{
270
		$nb_group = @da_sql_num_rows($res,$config);
271
		echo $nb_group;
272
		}
273
	}
274
echo ", $l_number_of_users = ";
275
if ($link)
276
	{
277
	$res = @da_sql_query($link,$config,"SELECT UserName FROM userinfo");
278
	if ($res)
279
		{
280
		$nb_user = @da_sql_num_rows($res,$config);
281
		echo "$nb_user";
282
		}
283
	}
284
echo "</td></tr><tr><td>";
285
echo "<TABLE width=\"100%\" border=0 cellspacing=0 cellpadding=1>";
286
echo "<tr><td valign=\"middle\" align=\"left\" colspan=\"2\">";
287
echo "<CENTER><H3>$l_text_import</H3></CENTER></td></tr>";
288
echo "<tr><td valign=\"middle\" align=\"left\">";
289
echo "$l_text_import_help<br>";
290
echo "<tr><td valign=\"middle\" align=\"left\">";
291
echo "<br><FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
1583 richard 292
echo "$l_file (.txt or .csv) : <input type=\"file\" name=\"import-users\"><br>";
1300 richard 293
echo "$l_users_group : ";
294
require("../lib/defaults.php");
295
include_once("../lib/$config[general_lib_type]/group_info.php");
296
if (isset($existing_groups)){
297
	echo "<select name=\"groupe\">";
298
	echo "<option value=\"\" selected>";
299
	foreach ($member_groups as $group)
300
		echo "<option value=\"$group\">$group\n";
301
	echo " </select>";
302
	}
303
else echo "$l_group_empty";
304
echo "<br>";
305
echo "<input type='hidden' name='choix' value='csv'>";
306
if (($choix == "csv") && isset($result)) echo "<b>".$result."</b><BR>";
307
echo "<input type=\"submit\" value=\"$l_send\">";
308
echo "</FORM></td>";
309
echo "<td>";
310
$ImportFileList = getImportFileList();
311
if (count($ImportFileList) > 0){
312
	echo "$l_imported_files";
313
	echo "<ul>";
314
	foreach ( $ImportFileList as $ImportFile ) //on parcours le tableau 
315
	{
316
	echo "<li>".$ImportFile." ( <a href=\"import_file.php?file=$ImportFile\">txt</a> - <a href=\"import_file.php?file=$ImportFile&format=pdf\">pdf</a> )</li>";
317
	} 
318
	echo "</ul>";
319
} else {
320
	echo "<br>";
321
}
322
echo "</td></tr></table>";
323
echo "<tr><td valign=\"middle\" align=\"left\">";
324
echo "<H3><CENTER>$l_db_import</CENTER></H3>";
325
echo "$l_db_import_help <br><br>";
326
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
1583 richard 327
echo "$l_file (.sql or .sql.gz) : <input type=\"file\" name=\"import-users\"><br>";
1300 richard 328
echo "<input type='hidden' name='choix' value='bdd'>";
329
if (($choix == "bdd") && isset($result)) echo "<b>".$result."</b><BR>";
330
echo "<input type=\"submit\" value=\"$l_send\">";
331
echo "</FORM>";
332
echo "</td></tr>";
333
echo "<tr><td valign=\"middle\" align=\"left\">";
334
echo "<H3><CENTER>$l_db_reset</CENTER></H3>";
335
echo "$l_db_import_help<br><br>";
1835 raphael.pi 336
echo "<FORM onsubmit=\"return confirm('$l_empty_warn');\" action='$_SERVER[PHP_SELF]' method=POST>";
1300 richard 337
echo "<input type='hidden' name='choix' value='raz'>";
1583 richard 338
echo "<input type=\"submit\" value=\"$l_empty\">";
1300 richard 339
echo "</FORM>";
340
echo "</TD></TR></TABLE>";
341
?>
342
</BODY>
343
</HTML>