Subversion Repositories ALCASAR

Rev

Rev 2388 | Rev 2699 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2388 Rev 2698
Line 3... Line 3...
3
 * Print tickets of new users
3
 * Print tickets of new users
4
 *
4
 *
5
 * @author    Tom Houdayer
5
 * @author    Tom Houdayer
6
 * @copyright Copyright (C) ALCASAR (http://www.alcasar.net)
6
 * @copyright Copyright (C) ALCASAR (http://www.alcasar.net)
7
 * @license   GPL-3.0
7
 * @license   GPL-3.0
8
 * @version   $Id: ticket_voucher.php 2388 2017-08-22 22:59:55Z tom.houdayer $
8
 * @version   $Id: ticket_voucher.php 2698 2019-02-05 10:25:12Z tom.houdayer $
9
 */
9
 */
10
 
10
 
11
require_once __DIR__ . '/../lib/alcasar/TicketsGenerator.php';
11
require_once __DIR__ . '/../lib/alcasar/TicketsGenerator.php';
-
 
12
$langue_imp = ((isset($_POST['langue_imp'])) ? $_POST['langue_imp'] : 'en');
-
 
13
require_once __DIR__ . '/../lib/langues_imp.php';
-
 
14
require_once __DIR__ . '/../lib/langues.php';
12
require_once '/etc/freeradius-web/config.php';
15
require_once '/etc/freeradius-web/config.php';
13
require_once '../lib/sql/drivers/'.$config['sql_type'].'/functions.php';;
16
require_once __DIR__ . '/../lib/sql/drivers/'.$config['sql_type'].'/functions.php';;
14
require_once '../lib/attrshow.php';
17
require_once __DIR__ . '/../lib/attrshow.php';
15
require_once '../lib/defaults.php';
18
require_once __DIR__ . '/../lib/defaults.php';
16
require_once '../lib/functions.php';
19
require_once __DIR__ . '/../lib/functions.php';
17
if ($config['sql_use_operators'] === 'true') {
20
if ($config['sql_use_operators'] === 'true') {
18
	require_once '../lib/operators.php';
21
	require_once __DIR__ . '/../lib/operators.php';
-
 
22
	$text      = ',op';
-
 
23
	$passwd_op = ",':='";
19
}
24
}
20
 
25
 
21
 
-
 
22
// Get datas from form
26
// Get datas from form
23
if ((!isset($_POST['nbtickets'])) || (!is_numeric($_POST['nbtickets']))){
27
if ((!isset($_POST['nbtickets'])) || (!is_numeric($_POST['nbtickets']))){
24
	header('Location: voucher_new.php');
28
	header('Location: user_new.php');
25
	exit();
29
	exit();
26
}
30
}
27
$nbTickets  = (int)$_POST['nbtickets'];
31
$nbTickets = (int)$_POST['nbtickets'];
28
$langue_imp = ((isset($_POST['langue_imp'])) ? $_POST['langue_imp'] : 'en');
-
 
29
require_once __DIR__ . '/../lib/langues_imp.php';
-
 
30
require_once __DIR__ . '/../lib/langues.php';
-
 
31
 
32
 
32
if ($config['sql_use_operators'] === 'true') {
33
// Get user attributes
33
	$text      = ',op';
34
$userAttr = [];
-
 
35
foreach (['Max-All-Session', 'Session-Timeout', 'Max-Daily-Session', 'Expiration'] as $attrName) {
-
 
36
	if (isset($_POST[$attrName]) && !empty($_POST[$attrName])) {
34
	$passwd_op = ",':='";
37
		$userAttr[$attrName] = $_POST[$attrName];
-
 
38
	}
35
}
39
}
36
 
40
 
-
 
41
if (count($userAttr) < 4) {
37
// Connect to database
42
	// Get group attributes
38
$link = da_sql_pconnect($config);
43
	$link = da_sql_pconnect($config);
39
if (!$link) {
44
	if ($link) {
-
 
45
		if (isset($_POST['Fgroup']) && !empty($_POST['Fgroup'])) {
-
 
46
			$default_group = 'ldap';
-
 
47
			$group_uid = da_sql_escape_string($link, $_POST['Fgroup']);
-
 
48
			$sql = "SELECT attribute, value FROM (( SELECT attribute, value FROM radgroupcheck WHERE groupname = '$group_uid' AND (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration'))) UNION ( SELECT attribute, value FROM radgroupcheck WHERE groupname = '$default_group' AND (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration'))) UNION ( SELECT attribute, value FROM radgroupreply WHERE groupname = '$group_uid' AND (attribute IN ('Session-Timeout'))) UNION ( SELECT attribute, value FROM radgroupreply WHERE groupname = '$default_group' AND (attribute IN ('Session-Timeout')))) attrs GROUP BY attribute;";
-
 
49
		} else {
-
 
50
			$default_group = 'ldap';
40
	exit('ERR_DATABASE_CONNECT');
51
			$sql = "SELECT attribute, value FROM (( SELECT attribute, value FROM radgroupcheck WHERE groupname = '$default_group' AND (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration'))) UNION ( SELECT attribute, value FROM radgroupreply WHERE groupname = '$default_group' AND (attribute IN ('Session-Timeout')))) attrs GROUP BY attribute;";
-
 
52
		}
-
 
53
		$res = da_sql_query($link, $config, $sql);
-
 
54
		if ($res) {
-
 
55
			while ($row = da_sql_fetch_array($res, $config)) {
-
 
56
				if (!isset($userAttr[$row['attribute']])) {
-
 
57
					$userAttr[$row['attribute']] = $row['value'];
-
 
58
				}
-
 
59
			}
-
 
60
		}
-
 
61
	}
41
}
62
}
42
 
63
 
43
$nbFailUsers = 0;
64
// Format user attributes
44
 
-
 
45
 
-
 
-
 
65
$userAttr['Session-Timeout']   = ((isset($userAttr['Session-Timeout']))   ? formatTime($userAttr['Session-Timeout'])   : $l_unlimited);
-
 
66
$userAttr['Max-All-Session']   = ((isset($userAttr['Max-All-Session']))   ? formatTime($userAttr['Max-All-Session'])   : $l_unlimited);
-
 
67
$userAttr['Max-Daily-Session'] = ((isset($userAttr['Max-Daily-Session'])) ? formatTime($userAttr['Max-Daily-Session']) : $l_unlimited);
-
 
68
$userAttr['Expiration'] = ((isset($userAttr['Expiration'])) ? date('d - m - Y', strtotime($userAttr['Expiration'])) : $l_without);
46
 
69
 
47
// Generate tickets
70
// Generate tickets
48
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);
71
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);
49
 
72
 
50
ob_start();
73
ob_start();
-
 
74
$nbFailUsers = 0;
51
for ($i = 0; $i < $nbTickets; $i++) {
75
for ($i = 0; $i < $nbTickets; $i++) {
52
	// Generate username and password
76
	// Generate username and password
53
	$login  = generateRandomString(8);
77
	$username = generateRandomString(8);
54
	$passwd = generateRandomString(12);
78
	$password = generateRandomString(12);
55
	$password = $passwd;
-
 
56
	
79
	
57
	// Check user exist
80
	// Check user exist
58
	require '../lib/'.$config['general_lib_type'].'/user_info.php';
81
	require '../lib/'.$config['general_lib_type'].'/user_info.php';
59
	if ($user_exists === 'yes') {
82
	if ($user_exists === 'yes') {
60
		$nbFailUsers++;
83
		$nbFailUsers++;
Line 62... Line 85...
62
	}
85
	}
63
 
86
 
64
	// Create user in database
87
	// Create user in database
65
	require '../lib/'.$config['general_lib_type'].'/create_user.php';
88
	require '../lib/'.$config['general_lib_type'].'/create_user.php';
66
 
89
 
67
	// Get group infos
-
 
68
	if ((isset($group)) && ($group !== '')) {
-
 
69
		$loginUser = $login;
-
 
70
		$login = $group;
-
 
71
		require '../lib/sql/group_info.php';
-
 
72
		$login = $loginUser;
-
 
73
	}
-
 
74
 
-
 
75
	// Format datas
-
 
76
	$sto_imp = (($sto_imp    === '') ? $l_unlimited : formatTime($sto_imp));
-
 
77
	$mas_imp = (($mas_imp    === '') ? $l_unlimited : formatTime($mas_imp));
-
 
78
	$mds_imp = (($mds_imp    === '') ? $l_unlimited : formatTime($mds_imp));
-
 
79
	$mms_imp = (($mms_imp    === '') ? $l_unlimited : formatTime($mms_imp));
-
 
80
	$exp_imp = (($Expiration === '') ? $l_without   : date('d - m - Y', strtotime($Expiration)));
-
 
81
 
-
 
82
	// Add user
90
	// Add user ticket
83
	$ticketsGenerator->addTicket([
91
	$ticketsGenerator->addTicket([
84
		'username'        => $login,
92
		'username'        => $username,
85
		'password'        => $password,
93
		'password'        => $password,
86
		'maxAllSession'   => $mas_imp,
94
		'maxAllSession'   => $userAttr['Max-All-Session'],
87
		'sessionTimeout'  => $sto_imp,
95
		'sessionTimeout'  => $userAttr['Session-Timeout'],
88
		'maxDailySession' => $mds_imp,
96
		'maxDailySession' => $userAttr['Max-Daily-Session'],
89
		'expiration'      => $exp_imp
97
		'expiration'      => $userAttr['Expiration']
90
	]);
98
	]);
91
}
99
}
92
$content_generation = ob_get_clean();
100
$content_generation = ob_get_clean();
93
 
101
 
94
// Generate the PDF
102
// Generate the PDF
Line 107... Line 115...
107
 *
115
 *
108
 * @return string Formated time.
116
 * @return string Formated time.
109
 */
117
 */
110
function formatTime($time)
118
function formatTime($time)
111
{
119
{
112
	$days     = floor($time / 86400); $reste = $time  % 86400;
120
	$days     = floor($time / 86400); $rest = $time % 86400;
113
	$hours    = floor($reste / 3600); $reste = $reste % 3600;
121
	$hours    = floor($rest /  3600); $rest = $rest %  3600;
114
	$minutes  = floor($reste / 60);
122
	$minutes  = floor($rest /    60);
115
	$secondes = $reste % 60;
123
	$secondes = $rest % 60;
-
 
124
 
-
 
125
	$result = '';
-
 
126
	if ($days     != 0) $result .= $days.' J ';
-
 
127
	if ($hours    != 0) $result .= $hours.' H ';
-
 
128
	if ($minutes  != 0) $result .= $minutes.' m ';
-
 
129
	if ($secondes != 0) $result .= $secondes.' s ';
116
 
130
 
117
	return "$days J $hours H $minutes min $secondes s";
131
	return $result;
118
}
132
}
119
 
133
 
120
/**
134
/**
121
 * Generate a random string.
135
 * Generate a random string.
122
 *
136
 *