Subversion Repositories ALCASAR

Rev

Rev 2285 | Rev 2388 | 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 2311 2017-06-26 15:04:47Z 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['sql_use_operators'] === 'true') {
33
	$text      = ',op';
34
	$passwd_op = ",':='";
35
}
1281 richard 36
 
2168 tom.houday 37
// Connect to database
38
$link = da_sql_pconnect($config);
39
if (!$link) {
40
	exit('ERR_DATABASE_CONNECT');
1281 richard 41
}
42
 
2168 tom.houday 43
$nbFailUsers = 0;
44
 
45
 
46
 
47
// Generate tickets
48
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);
49
 
50
 
51
for ($i = 0; $i < $nbTickets; $i++) {
52
	// Generate username and password
53
	$login  = generateRandomString(8);
54
	$passwd = generateRandomString(12);
55
	$password = $passwd;
56
 
57
	// Check user exist
58
	require '../lib/'.$config['general_lib_type'].'/user_info.php';
59
	if ($user_exists === 'yes') {
60
		$nbFailUsers++;
61
		continue;
1281 richard 62
	}
2168 tom.houday 63
 
64
	// Create user in database
65
	require '../lib/'.$config['general_lib_type'].'/create_user.php';
66
 
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;
1281 richard 73
	}
74
 
2168 tom.houday 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)));
1281 richard 81
 
2168 tom.houday 82
	// Add user
83
	$ticketsGenerator->addTicket([
84
		'username'        => $login,
85
		'password'        => $password,
86
		'maxAllSession'   => $mas_imp,
87
		'sessionTimeout'  => $sto_imp,
88
		'maxDailySession' => $mds_imp,
89
		'expiration'      => $exp_imp
90
	]);
91
}
1281 richard 92
 
2168 tom.houday 93
 
94
// Generate the PDF
95
$ticketsGenerator->output();
96
 
97
 
98
/**
99
 * Format time in seconds to days/hours/minutes/secondes.
100
 *
101
 * @param int $time Time in seconds.
102
 *
103
 * @return string Formated time.
104
 */
105
function formatTime($time)
1281 richard 106
{
2168 tom.houday 107
	$days     = floor($time / 86400); $reste = $time  % 86400;
108
	$hours    = floor($reste / 3600); $reste = $reste % 3600;
109
	$minutes  = floor($reste / 60);
110
	$secondes = $reste % 60;
1281 richard 111
 
2168 tom.houday 112
	return "$days J $hours H $minutes min $secondes s";
113
}
1281 richard 114
 
2168 tom.houday 115
/**
116
 * Generate a random string.
117
 *
118
 * @param int $length Length of the string.
119
 *
120
 * @return string Random string.
121
 */
122
function generateRandomString($length = 8)
123
{
124
	$string  = '';
125
	$chars   = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
126
	$randoms = openssl_random_pseudo_bytes($length);
127
	for ($i = 0; $i < $length; $i++) {
128
		$string .= $chars[ord($randoms[$i]) % 62];
129
	}
130
	return $string;
131
}