Subversion Repositories ALCASAR

Rev

Rev 2311 | Rev 2698 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

<?php
/**
 * Print tickets of new users
 *
 * @author    Tom Houdayer
 * @copyright Copyright (C) ALCASAR (http://www.alcasar.net)
 * @license   GPL-3.0
 * @version   $Id: ticket_voucher.php 2388 2017-08-22 22:59:55Z tom.houdayer $
 */

require_once __DIR__ . '/../lib/alcasar/TicketsGenerator.php';
require_once '/etc/freeradius-web/config.php';
require_once '../lib/sql/drivers/'.$config['sql_type'].'/functions.php';;
require_once '../lib/attrshow.php';
require_once '../lib/defaults.php';
require_once '../lib/functions.php';
if ($config['sql_use_operators'] === 'true') {
        require_once '../lib/operators.php';
}


// Get datas from form
if ((!isset($_POST['nbtickets'])) || (!is_numeric($_POST['nbtickets']))){
        header('Location: voucher_new.php');
        exit();
}
$nbTickets  = (int)$_POST['nbtickets'];
$langue_imp = ((isset($_POST['langue_imp'])) ? $_POST['langue_imp'] : 'en');
require_once __DIR__ . '/../lib/langues_imp.php';
require_once __DIR__ . '/../lib/langues.php';

if ($config['sql_use_operators'] === 'true') {
        $text      = ',op';
        $passwd_op = ",':='";
}

// Connect to database
$link = da_sql_pconnect($config);
if (!$link) {
        exit('ERR_DATABASE_CONNECT');
}

$nbFailUsers = 0;



// Generate tickets
$ticketsGenerator = new TicketsGenerator(['language' => $langue_imp]);

ob_start();
for ($i = 0; $i < $nbTickets; $i++) {
        // Generate username and password
        $login  = generateRandomString(8);
        $passwd = generateRandomString(12);
        $password = $passwd;
        
        // Check user exist
        require '../lib/'.$config['general_lib_type'].'/user_info.php';
        if ($user_exists === 'yes') {
                $nbFailUsers++;
                continue;
        }

        // Create user in database
        require '../lib/'.$config['general_lib_type'].'/create_user.php';

        // Get group infos
        if ((isset($group)) && ($group !== '')) {
                $loginUser = $login;
                $login = $group;
                require '../lib/sql/group_info.php';
                $login = $loginUser;
        }

        // Format datas
        $sto_imp = (($sto_imp    === '') ? $l_unlimited : formatTime($sto_imp));
        $mas_imp = (($mas_imp    === '') ? $l_unlimited : formatTime($mas_imp));
        $mds_imp = (($mds_imp    === '') ? $l_unlimited : formatTime($mds_imp));
        $mms_imp = (($mms_imp    === '') ? $l_unlimited : formatTime($mms_imp));
        $exp_imp = (($Expiration === '') ? $l_without   : date('d - m - Y', strtotime($Expiration)));

        // Add user
        $ticketsGenerator->addTicket([
                'username'        => $login,
                'password'        => $password,
                'maxAllSession'   => $mas_imp,
                'sessionTimeout'  => $sto_imp,
                'maxDailySession' => $mds_imp,
                'expiration'      => $exp_imp
        ]);
}
$content_generation = ob_get_clean();

// Generate the PDF
$ret = $ticketsGenerator->output();

if (!$ret) {
        echo $content_generation;
        echo 'Error during tickets report generation';
}


/**
 * Format time in seconds to days/hours/minutes/secondes.
 *
 * @param int $time Time in seconds.
 *
 * @return string Formated time.
 */
function formatTime($time)
{
        $days     = floor($time / 86400); $reste = $time  % 86400;
        $hours    = floor($reste / 3600); $reste = $reste % 3600;
        $minutes  = floor($reste / 60);
        $secondes = $reste % 60;

        return "$days J $hours H $minutes min $secondes s";
}

/**
 * Generate a random string.
 *
 * @param int $length Length of the string.
 *
 * @return string Random string.
 */
function generateRandomString($length = 8)
{
        $string  = '';
        $chars   = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randoms = openssl_random_pseudo_bytes($length);
        for ($i = 0; $i < $length; $i++) {
                $string .= $chars[ord($randoms[$i]) % 62];
        }
        return $string;
}