Subversion Repositories ALCASAR

Rev

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