Subversion Repositories ALCASAR

Rev

Rev 584 | Rev 768 | 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])));
584 richard 157
						if ($login != '')
158
							{
159
							$password = GenPassword();		
160
							$passwd = da_encrypt($password);
161
							$passwd = da_sql_escape_string($passwd);
324 richard 162
/* insertion (login + password) dans la table "radcheck" (si l'usager existe --> changement de mot de passe) */
584 richard 163
							$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);");
164
							if (!$res || !@da_sql_affected_rows($link,$res,$config))
324 richard 165
								{
584 richard 166
								echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
167
								$da_abort=1;
324 richard 168
								}
169
							else
170
								{
584 richard 171
								creatlog ($login,$password,$service,$RS_out);
172
								/*echo $login." : ".$password." , ";*/
173
								}	
174
/* insertion de l'usager dans la table "userinfo" */
175
							if ($config[sql_use_user_info_table] == 'true' && !$da_abort)
176
								{
177
							$res = @da_sql_query($link,$config, "SELECT username FROM $config[sql_user_info_table] WHERE username = '$login';");
178
							if ($res)
179
								{
180
								if (!@da_sql_num_rows($res,$config))
324 richard 181
									{
584 richard 182
									$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_user_info_table] (username,department) VALUES ('$login','$service');");
183
									if (!$res || !@da_sql_affected_rows($link,$res,$config))
184
										echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
185
									}	
186
								else
187
									echo "<b>User already exists in user info table.</b><br>\n";
188
								}
189
							else
190
								echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
191
								if ($group != '')
192
									{
193
									$group = da_sql_escape_string($group);
194
									$res = @da_sql_query($link,$config,"SELECT username FROM $config[sql_usergroup_table] WHERE username = '$login' AND groupname = '$group';");
195
									if ($res)
324 richard 196
										{
584 richard 197
										if (!@da_sql_num_rows($res,$config))
198
											{
199
											$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_usergroup_table] (username,groupname) VALUES ('$login','$group');");
200
											if (!$res || !@da_sql_affected_rows($link,$res,$config))
201
												echo "<b>Could not add user to group $group. SQL Error</b><br>\n";
202
											} # end if 
203
										else
204
											echo "<b>User already is a member of group $group</b><br>\n";
205
										} # end if
324 richard 206
									else
584 richard 207
										echo "<b>Could not add user to group $group: " . da_sql_error($link,$config) . "</b><br>\n";
208
									} # end if ($group)
209
						 		} # end if ($config)		
210
							} # end if ($login !='')
211
						} # end foreach
324 richard 212
					fclose($RS_out);
584 richard 213
					} # end if (is file)
214
				} # end if (link)
324 richard 215
			}
216
		}
217
	else if ($choix == "bdd")
218
//import d'une Bdd
219
		{
534 richard 220
		if ($extension != '.sql') $result = $l_error_ext_sql;
324 richard 221
		else 
222
			{
386 franck 223
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --dump");
324 richard 224
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
386 franck 225
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --import $destination");
324 richard 226
			}
227
		}
228
	}
229
if ($link)
230
	{
231
	$res = @da_sql_query($link,$config,"SELECT GroupName FROM radusergroup GROUP BY GroupName");
232
	if ($res)
233
		{
234
		$nb_group = @da_sql_num_rows($res,$config);
235
		echo $nb_group;
236
		}
237
	}
238
echo ", $l_number_of_users = ";
239
if ($link)
240
	{
241
	$res = @da_sql_query($link,$config,"SELECT UserName FROM userinfo");
242
	if ($res)
243
		{
244
		$nb_user = @da_sql_num_rows($res,$config);
245
		echo "$nb_user";
246
		}
247
	}
248
echo "</td></tr><tr><td>";
249
echo "<TABLE width=\"100%\" border=0 cellspacing=0 cellpadding=1>";
250
echo "<tr><td valign=\"middle\" align=\"left\" colspan=\"2\">";
251
echo "<CENTER><H3>$l_text_import</H3></CENTER></td></tr>";
252
echo "<tr><td valign=\"middle\" align=\"left\">";
253
echo "$l_text_import_help<br>";
254
echo "<tr><td valign=\"middle\" align=\"left\">";
255
echo "<br><FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
256
echo "$l_file (.txt) : <input type=\"file\" name=\"import-users\"><br>";
257
echo "$l_users_service : <input type=\"input\" name=\"service\" value=\"\"><br>";
587 richard 258
echo "$l_users_group : ";
259
require("../lib/defaults.php");
260
include_once("../lib/$config[general_lib_type]/group_info.php");
261
if (isset($existing_groups)){
262
	echo "<select name=\"groupe\">";
263
	echo "<option value=\"\" selected>";
264
	foreach ($member_groups as $group)
265
		echo "<option value=\"$group\">$group\n";
266
	echo " </select>";
267
	}
268
else echo "$l_group_empty";
269
echo "<br>";
324 richard 270
echo "<input type='hidden' name='choix' value='csv'>";
534 richard 271
if (($choix == "csv") && isset($result)) echo "<b>".$result."</b><BR>";
324 richard 272
echo "<input type=\"submit\" value=\"$l_send\">";
273
echo "</FORM></td>";
274
echo "<td>";
275
$ImportFileList = getImportFileList();
276
if (count($ImportFileList) > 0){
277
	echo "$l_imported_files";
278
	echo "<ul>";
279
	foreach ( $ImportFileList as $ImportFile ) //on parcours le tableau 
280
	{
281
	echo "<li>".$ImportFile." ( <a href=\"import_file.php?file=$ImportFile\">txt</a> - <a href=\"import_file.php?file=$ImportFile&format=pdf\">pdf</a> )</li>";
282
	} 
283
	echo "</ul>";
284
} else {
285
	echo "<br>";
286
}
287
echo "</td></tr></table>";
288
echo "<tr><td valign=\"middle\" align=\"left\">";
289
echo "<H3><CENTER>$l_db_import</CENTER></H3>";
290
echo "$l_db_import_help <br><br>";
291
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
292
echo "$l_file (.sql) : <input type=\"file\" name=\"import-users\"><br>";
293
echo "<input type='hidden' name='choix' value='bdd'>";
534 richard 294
if (($choix == "bdd") && isset($result)) echo "<b>".$result."</b><BR>";
324 richard 295
echo "<input type=\"submit\" value=\"$l_send\">";
296
echo "</FORM>";
297
echo "</td></tr>";
298
echo "<tr><td valign=\"middle\" align=\"left\">";
299
echo "<H3><CENTER>$l_db_reset</CENTER></H3>";
300
echo "$l_db_import_help<br><br>";
301
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST>";
302
echo "<input type='hidden' name='choix' value='raz'>";
303
echo "<input type=\"submit\" value=\"$l_send\">";
304
echo "</FORM>";
305
echo "</TD></TR></TABLE>";
306
?>
307
</BODY>
308
</HTML>
309
<?php