Subversion Repositories ALCASAR

Rev

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