Subversion Repositories ALCASAR

Rev

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