Subversion Repositories ALCASAR

Rev

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