Subversion Repositories ALCASAR

Rev

Rev 2699 | 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 2700 2019-02-05 10:34:12Z tom.houdayer $
9
 */
1281 richard 10
 
2168 tom.houday 11
require_once __DIR__ . '/../lib/alcasar/TicketsGenerator.php';
2698 tom.houday 12
$langue_imp = ((isset($_POST['langue_imp'])) ? $_POST['langue_imp'] : 'en');
13
require_once __DIR__ . '/../lib/langues_imp.php';
14
require_once __DIR__ . '/../lib/langues.php';
2168 tom.houday 15
require_once '/etc/freeradius-web/config.php';
2698 tom.houday 16
require_once __DIR__ . '/../lib/sql/drivers/'.$config['sql_type'].'/functions.php';;
17
require_once __DIR__ . '/../lib/attrshow.php';
18
require_once __DIR__ . '/../lib/defaults.php';
19
require_once __DIR__ . '/../lib/functions.php';
2168 tom.houday 20
if ($config['sql_use_operators'] === 'true') {
2698 tom.houday 21
        require_once __DIR__ . '/../lib/operators.php';
22
        $text      = ',op';
23
        $passwd_op = ",':='";
2168 tom.houday 24
}
1281 richard 25
 
2168 tom.houday 26
// Get datas from form
27
if ((!isset($_POST['nbtickets'])) || (!is_numeric($_POST['nbtickets']))){
2698 tom.houday 28
        header('Location: user_new.php');
2168 tom.houday 29
        exit();
1281 richard 30
}
2698 tom.houday 31
$nbTickets = (int)$_POST['nbtickets'];
1281 richard 32
 
2698 tom.houday 33
// Get user attributes
34
$userAttr = [];
35
foreach (['Max-All-Session', 'Session-Timeout', 'Max-Daily-Session', 'Expiration'] as $attrName) {
36
        if (isset($_POST[$attrName]) && !empty($_POST[$attrName])) {
37
                $userAttr[$attrName] = $_POST[$attrName];
38
        }
2168 tom.houday 39
}
1281 richard 40
 
2698 tom.houday 41
if (count($userAttr) < 4) {
42
        // Get group attributes
43
        $link = da_sql_pconnect($config);
44
        if ($link) {
45
                if (isset($_POST['Fgroup']) && !empty($_POST['Fgroup'])) {
46
                        $default_group = 'ldap';
47
                        $group_uid = da_sql_escape_string($link, $_POST['Fgroup']);
48
                        $sql = "SELECT attribute, value FROM (( SELECT attribute, value FROM radgroupcheck WHERE groupname = '$group_uid' AND (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration'))) UNION ( SELECT attribute, value FROM radgroupcheck WHERE groupname = '$default_group' AND (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration'))) UNION ( SELECT attribute, value FROM radgroupreply WHERE groupname = '$group_uid' AND (attribute IN ('Session-Timeout'))) UNION ( SELECT attribute, value FROM radgroupreply WHERE groupname = '$default_group' AND (attribute IN ('Session-Timeout')))) attrs GROUP BY attribute;";
49
                } else {
50
                        $default_group = 'ldap';
51
                        $sql = "SELECT attribute, value FROM (( SELECT attribute, value FROM radgroupcheck WHERE groupname = '$default_group' AND (attribute IN ('Max-All-Session', 'Max-Daily-Session', 'Expiration'))) UNION ( SELECT attribute, value FROM radgroupreply WHERE groupname = '$default_group' AND (attribute IN ('Session-Timeout')))) attrs GROUP BY attribute;";
52
                }
53
                $res = da_sql_query($link, $config, $sql);
54
                if ($res) {
55
                        while ($row = da_sql_fetch_array($res, $config)) {
56
                                if (!isset($userAttr[$row['attribute']])) {
57
                                        $userAttr[$row['attribute']] = $row['value'];
58
                                }
59
                        }
60
                }
61
        }
1281 richard 62
}
63
 
2698 tom.houday 64
// Format user attributes
65
$userAttr['Session-Timeout']   = ((isset($userAttr['Session-Timeout']))   ? formatTime($userAttr['Session-Timeout'])   : $l_unlimited);
66
$userAttr['Max-All-Session']   = ((isset($userAttr['Max-All-Session']))   ? formatTime($userAttr['Max-All-Session'])   : $l_unlimited);
67
$userAttr['Max-Daily-Session'] = ((isset($userAttr['Max-Daily-Session'])) ? formatTime($userAttr['Max-Daily-Session']) : $l_unlimited);
68
$userAttr['Expiration'] = ((isset($userAttr['Expiration'])) ? date('d - m - Y', strtotime($userAttr['Expiration'])) : $l_without);
2168 tom.houday 69
 
70
// Generate tickets
71
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);
72
 
2388 tom.houday 73
ob_start();
2698 tom.houday 74
$nbFailUsers = 0;
2168 tom.houday 75
for ($i = 0; $i < $nbTickets; $i++) {
76
        // Generate username and password
2700 tom.houday 77
        $login  = generateRandomString(8);
78
        $passwd = generateRandomString(12);
79
        $password = $passwd;
2168 tom.houday 80
 
81
        // Check user exist
82
        require '../lib/'.$config['general_lib_type'].'/user_info.php';
83
        if ($user_exists === 'yes') {
84
                $nbFailUsers++;
85
                continue;
1281 richard 86
        }
2168 tom.houday 87
 
88
        // Create user in database
89
        require '../lib/'.$config['general_lib_type'].'/create_user.php';
90
 
2698 tom.houday 91
        // Add user ticket
2168 tom.houday 92
        $ticketsGenerator->addTicket([
2699 tom.houday 93
                'username'        => $login,
2168 tom.houday 94
                'password'        => $password,
2698 tom.houday 95
                'maxAllSession'   => $userAttr['Max-All-Session'],
96
                'sessionTimeout'  => $userAttr['Session-Timeout'],
97
                'maxDailySession' => $userAttr['Max-Daily-Session'],
98
                'expiration'      => $userAttr['Expiration']
2168 tom.houday 99
        ]);
100
}
2388 tom.houday 101
$content_generation = ob_get_clean();
1281 richard 102
 
2168 tom.houday 103
// Generate the PDF
2388 tom.houday 104
$ret = $ticketsGenerator->output();
2168 tom.houday 105
 
2388 tom.houday 106
if (!$ret) {
107
        echo $content_generation;
108
        echo 'Error during tickets report generation';
109
}
2168 tom.houday 110
 
2388 tom.houday 111
 
2168 tom.houday 112
/**
113
 * Format time in seconds to days/hours/minutes/secondes.
114
 *
115
 * @param int $time Time in seconds.
116
 *
117
 * @return string Formated time.
118
 */
119
function formatTime($time)
1281 richard 120
{
2698 tom.houday 121
        $days     = floor($time / 86400); $rest = $time % 86400;
122
        $hours    = floor($rest /  3600); $rest = $rest %  3600;
123
        $minutes  = floor($rest /    60);
124
        $secondes = $rest % 60;
1281 richard 125
 
2698 tom.houday 126
        $result = '';
127
        if ($days     != 0) $result .= $days.' J ';
128
        if ($hours    != 0) $result .= $hours.' H ';
129
        if ($minutes  != 0) $result .= $minutes.' m ';
130
        if ($secondes != 0) $result .= $secondes.' s ';
131
 
132
        return $result;
2168 tom.houday 133
}
1281 richard 134
 
2168 tom.houday 135
/**
136
 * Generate a random string.
137
 *
138
 * @param int $length Length of the string.
139
 *
140
 * @return string Random string.
141
 */
142
function generateRandomString($length = 8)
143
{
144
        $string  = '';
145
        $chars   = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
146
        $randoms = openssl_random_pseudo_bytes($length);
147
        for ($i = 0; $i < $length; $i++) {
148
                $string .= $chars[ord($randoms[$i]) % 62];
149
        }
150
        return $string;
151
}