Subversion Repositories ALCASAR

Rev

Rev 2311 | Rev 2698 | 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 2388 2017-08-22 22:59: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['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
 
2388 tom.houday 50
ob_start();
2168 tom.houday 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
}
2388 tom.houday 92
$content_generation = ob_get_clean();
1281 richard 93
 
2168 tom.houday 94
// Generate the PDF
2388 tom.houday 95
$ret = $ticketsGenerator->output();
2168 tom.houday 96
 
2388 tom.houday 97
if (!$ret) {
98
	echo $content_generation;
99
	echo 'Error during tickets report generation';
100
}
2168 tom.houday 101
 
2388 tom.houday 102
 
2168 tom.houday 103
/**
104
 * Format time in seconds to days/hours/minutes/secondes.
105
 *
106
 * @param int $time Time in seconds.
107
 *
108
 * @return string Formated time.
109
 */
110
function formatTime($time)
1281 richard 111
{
2168 tom.houday 112
	$days     = floor($time / 86400); $reste = $time  % 86400;
113
	$hours    = floor($reste / 3600); $reste = $reste % 3600;
114
	$minutes  = floor($reste / 60);
115
	$secondes = $reste % 60;
1281 richard 116
 
2168 tom.houday 117
	return "$days J $hours H $minutes min $secondes s";
118
}
1281 richard 119
 
2168 tom.houday 120
/**
121
 * Generate a random string.
122
 *
123
 * @param int $length Length of the string.
124
 *
125
 * @return string Random string.
126
 */
127
function generateRandomString($length = 8)
128
{
129
	$string  = '';
130
	$chars   = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
131
	$randoms = openssl_random_pseudo_bytes($length);
132
	for ($i = 0; $i < $length; $i++) {
133
		$string .= $chars[ord($randoms[$i]) % 62];
134
	}
135
	return $string;
136
}