Subversion Repositories ALCASAR

Rev

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