Subversion Repositories ALCASAR

Rev

Rev 587 | Rev 847 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

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