Subversion Repositories ALCASAR

Rev

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