Subversion Repositories ALCASAR

Rev

Rev 2168 | Rev 2311 | 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 2285 2017-06-20 08:15:55Z tom.houdayer $
9
 */
1281 richard 10
 
2168 tom.houday 11
require_once __DIR__ . '/../lib/alcasar/TicketsGenerator.php';
12
require_once '/etc/freeradius-web/config.php';
13
require_once '../lib/sql/drivers/'.$config['sql_type'].'/functions.php';;
14
require_once '../lib/attrshow.php';
15
require_once '../lib/defaults.php';
16
require_once '../lib/functions.php';
17
if ($config['sql_use_operators'] === 'true') {
18
	require_once '../lib/operators.php';
19
}
1281 richard 20
 
2168 tom.houday 21
 
22
// Get datas from form
23
if ((!isset($_POST['nbtickets'])) || (!is_numeric($_POST['nbtickets']))){
24
	header('Location: voucher_new.php');
25
	exit();
1281 richard 26
}
2168 tom.houday 27
$nbTickets  = (int)$_POST['nbtickets'];
28
$langue_imp = ((isset($_POST['langue_imp'])) ? $_POST['langue_imp'] : 'en');
29
require_once __DIR__ . '/../lib/langues_imp.php';
2285 tom.houday 30
require_once __DIR__ . '/../lib/langues.php';
1281 richard 31
 
2168 tom.houday 32
if ($config['general_lib_type'] === 'sql' && $config['sql_use_operators'] === 'true') {
33
	$show_ops = 1;
34
	$colspan  = 2;
1281 richard 35
} else {
2168 tom.houday 36
	$show_ops = 0;
37
	$colspan  = 1;
1281 richard 38
}
2168 tom.houday 39
if ($config['sql_use_operators'] === 'true') {
40
	$text      = ',op';
41
	$passwd_op = ",':='";
42
}
1281 richard 43
 
2168 tom.houday 44
// Connect to database
45
$link = da_sql_pconnect($config);
46
if (!$link) {
47
	exit('ERR_DATABASE_CONNECT');
1281 richard 48
}
49
 
2168 tom.houday 50
$nbFailUsers = 0;
51
 
52
 
53
 
54
// Generate tickets
55
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);
56
 
57
 
58
for ($i = 0; $i < $nbTickets; $i++) {
59
	// Generate username and password
60
	$login  = generateRandomString(8);
61
	$passwd = generateRandomString(12);
62
	$password = $passwd;
63
 
64
	// Check user exist
65
	require '../lib/'.$config['general_lib_type'].'/user_info.php';
66
	if ($user_exists === 'yes') {
67
		$nbFailUsers++;
68
		continue;
1281 richard 69
	}
2168 tom.houday 70
 
71
	// Create user in database
72
	require '../lib/'.$config['general_lib_type'].'/create_user.php';
73
 
74
	// Get group infos
75
	if ((isset($group)) && ($group !== '')) {
76
		$loginUser = $login;
77
		$login = $group;
78
		require '../lib/sql/group_info.php';
79
		$login = $loginUser;
1281 richard 80
	}
81
 
2168 tom.houday 82
	// Format datas
83
	$sto_imp = (($sto_imp    === '') ? $l_unlimited : formatTime($sto_imp));
84
	$mas_imp = (($mas_imp    === '') ? $l_unlimited : formatTime($mas_imp));
85
	$mds_imp = (($mds_imp    === '') ? $l_unlimited : formatTime($mds_imp));
86
	$mms_imp = (($mms_imp    === '') ? $l_unlimited : formatTime($mms_imp));
87
	$exp_imp = (($Expiration === '') ? $l_without   : date('d - m - Y', strtotime($Expiration)));
1281 richard 88
 
2168 tom.houday 89
	// Add user
90
	$ticketsGenerator->addTicket([
91
		'username'        => $login,
92
		'password'        => $password,
93
		'maxAllSession'   => $mas_imp,
94
		'sessionTimeout'  => $sto_imp,
95
		'maxDailySession' => $mds_imp,
96
		'expiration'      => $exp_imp
97
	]);
98
}
1281 richard 99
 
2168 tom.houday 100
 
101
// Generate the PDF
102
$ticketsGenerator->output();
103
 
104
 
105
/**
106
 * Format time in seconds to days/hours/minutes/secondes.
107
 *
108
 * @param int $time Time in seconds.
109
 *
110
 * @return string Formated time.
111
 */
112
function formatTime($time)
1281 richard 113
{
2168 tom.houday 114
	$days     = floor($time / 86400); $reste = $time  % 86400;
115
	$hours    = floor($reste / 3600); $reste = $reste % 3600;
116
	$minutes  = floor($reste / 60);
117
	$secondes = $reste % 60;
1281 richard 118
 
2168 tom.houday 119
	return "$days J $hours H $minutes min $secondes s";
120
}
1281 richard 121
 
2168 tom.houday 122
/**
123
 * Generate a random string.
124
 *
125
 * @param int $length Length of the string.
126
 *
127
 * @return string Random string.
128
 */
129
function generateRandomString($length = 8)
130
{
131
	$string  = '';
132
	$chars   = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
133
	$randoms = openssl_random_pseudo_bytes($length);
134
	for ($i = 0; $i < $length; $i++) {
135
		$string .= $chars[ord($randoms[$i]) % 62];
136
	}
137
	return $string;
138
}