Subversion Repositories ALCASAR

Rev

Rev 2493 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2168 tom.houday 1
<?php
2
/**
3
 * Print tickets of the new user
4
 *
5
 * @author    Tom Houdayer
6
 * @copyright Copyright (C) ALCASAR (http://www.alcasar.net)
7
 * @license   GPL-3.0
8
 * @version   $Id: ticket_user.php 2698 2019-02-05 10:25:12Z tom.houdayer $
9
 */
10
 
11
require_once __DIR__ . '/../lib/alcasar/TicketsGenerator.php';
12
$langue_imp = ((isset($_POST['langue_imp'])) ? $_POST['langue_imp'] : 'en');
13
require_once __DIR__ . '/../lib/langues_imp.php';
2698 tom.houday 14
require_once '/etc/freeradius-web/config.php';
15
require_once __DIR__ . '/../lib/sql/drivers/mysql/functions.php';
2168 tom.houday 16
 
17
// Get datas from form
2698 tom.houday 18
if ((!isset($_POST['username'])) || (!isset($_POST['password']))) {
2168 tom.houday 19
	exit();
20
}
2698 tom.houday 21
$username = $_POST['username'];
22
$password = $_POST['password'];
2168 tom.houday 23
 
2698 tom.houday 24
// Get user attributes
25
$userAttr = [];
26
$link = da_sql_pconnect($config);
27
if ($link) {
28
	$user_uid = da_sql_escape_string($link, $username);
29
	$sql = "SELECT attribute, value FROM (( SELECT attribute, value FROM radcheck WHERE (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration')) AND username = '$user_uid') UNION ( SELECT attribute, value FROM radgroupcheck gr LEFT JOIN radusergroup ug ON gr.groupname = ug.groupname WHERE (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration')) AND username = '$user_uid' ORDER BY ug.priority) UNION ( SELECT attribute, value FROM radgroupcheck WHERE groupname = 'ldap' AND (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration'))) UNION ( SELECT attribute, value FROM radreply WHERE (attribute IN ('Session-Timeout')) AND username = '$user_uid') UNION ( SELECT attribute, value FROM radgroupreply gr LEFT JOIN radusergroup ug ON gr.groupname = ug.groupname WHERE (attribute IN ('Session-Timeout')) AND username = '$user_uid' ORDER BY ug.priority) UNION ( SELECT attribute, value FROM radgroupreply WHERE groupname = 'ldap' AND (attribute IN ('Session-Timeout')))) attrs GROUP BY attribute;";
30
	$res = da_sql_query($link, $config, $sql);
31
	if ($res) {
32
		while ($row = da_sql_fetch_array($res, $config)) {
33
			$userAttr[$row['attribute']] = $row['value'];
34
		}
35
	}
36
}
37
 
38
// Format user attributes
39
$userAttr['Session-Timeout']   = ((isset($userAttr['Session-Timeout']))   ? formatTime($userAttr['Session-Timeout'])   : $l_unlimited);
40
$userAttr['Max-All-Session']   = ((isset($userAttr['Max-All-Session']))   ? formatTime($userAttr['Max-All-Session'])   : $l_unlimited);
41
$userAttr['Max-Daily-Session'] = ((isset($userAttr['Max-Daily-Session'])) ? formatTime($userAttr['Max-Daily-Session']) : $l_unlimited);
42
$userAttr['Expiration'] = ((isset($userAttr['Expiration'])) ? date('d - m - Y', strtotime($userAttr['Expiration'])) : $l_without);
43
 
2168 tom.houday 44
// Generate tickets
45
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);
46
 
2698 tom.houday 47
// Add user ticket
2168 tom.houday 48
$ticketsGenerator->addTicket([
2698 tom.houday 49
	'username'        => $username,
50
	'password'        => $password,
51
	'maxAllSession'   => $userAttr['Max-All-Session'],
52
	'sessionTimeout'  => $userAttr['Session-Timeout'],
53
	'maxDailySession' => $userAttr['Max-Daily-Session'],
54
	'expiration'      => $userAttr['Expiration']
2168 tom.houday 55
]);
56
 
57
// Save the PDF and redirect user to it
2698 tom.houday 58
$filename = 'ticket_' . $username . '.pdf';
2473 tom.houday 59
// Remove accents
60
$filename = strtr(utf8_decode($filename), utf8_decode('ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'), 'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy'); // TODO : manage all UTF-8 chars
2493 tom.houday 61
 
62
$ret = $ticketsGenerator->saveAs($filename);
63
if (!$ret) {
64
	echo 'Error during tickets report generation';
65
	exit();
66
}
67
 
2168 tom.houday 68
header('Location: ' . $filename);
2698 tom.houday 69
 
70
 
71
/**
72
 * Format time in seconds to days/hours/minutes/secondes.
73
 *
74
 * @param int $time Time in seconds.
75
 *
76
 * @return string Formated time.
77
 */
78
function formatTime($time)
79
{
80
	$days     = floor($time / 86400); $rest = $time % 86400;
81
	$hours    = floor($rest /  3600); $rest = $rest %  3600;
82
	$minutes  = floor($rest /    60);
83
	$secondes = $rest % 60;
84
 
85
	$result = '';
86
	if ($days     != 0) $result .= $days.' J ';
87
	if ($hours    != 0) $result .= $hours.' H ';
88
	if ($minutes  != 0) $result .= $minutes.' m ';
89
	if ($secondes != 0) $result .= $secondes.' s ';
90
 
91
	return $result;
92
}