Subversion Repositories ALCASAR

Rev

Rev 2388 | Rev 2699 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1281 richard 1
<?php
2168 tom.houday 2
/**
3
 * Print tickets of new users
4
 *
5
 * @author    Tom Houdayer
6
 * @copyright Copyright (C) ALCASAR (http://www.alcasar.net)
7
 * @license   GPL-3.0
8
 * @version   $Id: ticket_voucher.php 2698 2019-02-05 10:25:12Z tom.houdayer $
9
 */
1281 richard 10
 
2168 tom.houday 11
require_once __DIR__ . '/../lib/alcasar/TicketsGenerator.php';
2698 tom.houday 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';
2168 tom.houday 15
require_once '/etc/freeradius-web/config.php';
2698 tom.houday 16
require_once __DIR__ . '/../lib/sql/drivers/'.$config['sql_type'].'/functions.php';;
17
require_once __DIR__ . '/../lib/attrshow.php';
18
require_once __DIR__ . '/../lib/defaults.php';
19
require_once __DIR__ . '/../lib/functions.php';
2168 tom.houday 20
if ($config['sql_use_operators'] === 'true') {
2698 tom.houday 21
	require_once __DIR__ . '/../lib/operators.php';
22
	$text      = ',op';
23
	$passwd_op = ",':='";
2168 tom.houday 24
}
1281 richard 25
 
2168 tom.houday 26
// Get datas from form
27
if ((!isset($_POST['nbtickets'])) || (!is_numeric($_POST['nbtickets']))){
2698 tom.houday 28
	header('Location: user_new.php');
2168 tom.houday 29
	exit();
1281 richard 30
}
2698 tom.houday 31
$nbTickets = (int)$_POST['nbtickets'];
1281 richard 32
 
2698 tom.houday 33
// Get user attributes
34
$userAttr = [];
35
foreach (['Max-All-Session', 'Session-Timeout', 'Max-Daily-Session', 'Expiration'] as $attrName) {
36
	if (isset($_POST[$attrName]) && !empty($_POST[$attrName])) {
37
		$userAttr[$attrName] = $_POST[$attrName];
38
	}
2168 tom.houday 39
}
1281 richard 40
 
2698 tom.houday 41
if (count($userAttr) < 4) {
42
	// Get group attributes
43
	$link = da_sql_pconnect($config);
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';
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
	}
1281 richard 62
}
63
 
2698 tom.houday 64
// Format user attributes
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);
2168 tom.houday 69
 
70
// Generate tickets
71
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);
72
 
2388 tom.houday 73
ob_start();
2698 tom.houday 74
$nbFailUsers = 0;
2168 tom.houday 75
for ($i = 0; $i < $nbTickets; $i++) {
76
	// Generate username and password
2698 tom.houday 77
	$username = generateRandomString(8);
78
	$password = generateRandomString(12);
2168 tom.houday 79
 
80
	// Check user exist
81
	require '../lib/'.$config['general_lib_type'].'/user_info.php';
82
	if ($user_exists === 'yes') {
83
		$nbFailUsers++;
84
		continue;
1281 richard 85
	}
2168 tom.houday 86
 
87
	// Create user in database
88
	require '../lib/'.$config['general_lib_type'].'/create_user.php';
89
 
2698 tom.houday 90
	// Add user ticket
2168 tom.houday 91
	$ticketsGenerator->addTicket([
2698 tom.houday 92
		'username'        => $username,
2168 tom.houday 93
		'password'        => $password,
2698 tom.houday 94
		'maxAllSession'   => $userAttr['Max-All-Session'],
95
		'sessionTimeout'  => $userAttr['Session-Timeout'],
96
		'maxDailySession' => $userAttr['Max-Daily-Session'],
97
		'expiration'      => $userAttr['Expiration']
2168 tom.houday 98
	]);
99
}
2388 tom.houday 100
$content_generation = ob_get_clean();
1281 richard 101
 
2168 tom.houday 102
// Generate the PDF
2388 tom.houday 103
$ret = $ticketsGenerator->output();
2168 tom.houday 104
 
2388 tom.houday 105
if (!$ret) {
106
	echo $content_generation;
107
	echo 'Error during tickets report generation';
108
}
2168 tom.houday 109
 
2388 tom.houday 110
 
2168 tom.houday 111
/**
112
 * Format time in seconds to days/hours/minutes/secondes.
113
 *
114
 * @param int $time Time in seconds.
115
 *
116
 * @return string Formated time.
117
 */
118
function formatTime($time)
1281 richard 119
{
2698 tom.houday 120
	$days     = floor($time / 86400); $rest = $time % 86400;
121
	$hours    = floor($rest /  3600); $rest = $rest %  3600;
122
	$minutes  = floor($rest /    60);
123
	$secondes = $rest % 60;
1281 richard 124
 
2698 tom.houday 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 ';
130
 
131
	return $result;
2168 tom.houday 132
}
1281 richard 133
 
2168 tom.houday 134
/**
135
 * Generate a random string.
136
 *
137
 * @param int $length Length of the string.
138
 *
139
 * @return string Random string.
140
 */
141
function generateRandomString($length = 8)
142
{
143
	$string  = '';
144
	$chars   = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
145
	$randoms = openssl_random_pseudo_bytes($length);
146
	for ($i = 0; $i < $length; $i++) {
147
		$string .= $chars[ord($randoms[$i]) % 62];
148
	}
149
	return $string;
150
}