<?php
/*
 * Copyright 2005 - 2025 Centreon (https://www.centreon.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 *
 */

if (! isset($centreon)) {
    exit();
}

/**
 * Kick a logged user
 */
const KICK_USER = 'k';

$path = './include/options/session/';
require_once './include/common/common-Func.php';
require_once './class/centreonMsg.class.php';

$action = HtmlSanitizer::createFromString($_GET['o'] ?? '')
    ->removeTags()
    ->sanitize()
    ->getString();

$selectedUserId = filter_var(
    $_GET['user'] ?? null,
    FILTER_VALIDATE_INT
);

$currentPage = filter_var(
    $_GET['p'] ?? $_POST['p'] ?? 0,
    FILTER_VALIDATE_INT
);

if ($selectedUserId) {
    $msg = new CentreonMsg();
    $msg->setTextStyle('bold');
    $msg->setTimeOut('3');

    switch ($action) {
        // logout action
        case KICK_USER:
            // check if the user is allowed to kick this user
            if ($centreon->user->admin == 0) {
                $msg->setText(_('You are not allowed to disconnect this user'));
                break;
            }
            $stmt = $pearDB->prepare('DELETE FROM session WHERE user_id = :userId');
            $stmt->bindValue(':userId', $selectedUserId, PDO::PARAM_INT);
            $stmt->execute();
            $msg->setText(_('User disconnected'));
            break;
    }
}

// Smarty template initialization
$tpl = SmartyBC::createSmartyTemplate($path);

$session_data = [];
$res = $pearDB->query(
    'SELECT session.*, contact_name, contact_admin, contact_auth_type, `contact_ldap_last_sync`
    FROM session, contact
    WHERE contact_id = user_id ORDER BY contact_name, contact_admin'
);
for ($cpt = 0; $r = $res->fetch(); $cpt++) {
    $session_data[$cpt] = [];
    $session_data[$cpt]['class'] = $cpt % 2 ? 'list_one' : 'list_two';
    $session_data[$cpt]['user_id'] = $r['user_id'];
    $session_data[$cpt]['user_alias'] = $r['contact_name'];
    $session_data[$cpt]['admin'] = $r['contact_admin'];
    $session_data[$cpt]['ip_address'] = $r['ip_address'];
    $session_data[$cpt]['last_reload'] = $r['last_reload'];
    $session_data[$cpt]['ldapContact'] = $r['contact_auth_type'];

    $resCP = $pearDB->prepare(
        'SELECT topology_name, topology_page, topology_url_opt FROM topology '
        . 'WHERE topology_page = :topologyPage'
    );
    $resCP->bindValue(':topologyPage', $r['current_page'], PDO::PARAM_INT);
    $resCP->execute();
    $rCP = $resCP->fetch();

    // getting the current users' position in the IHM
    $session_data[$cpt]['current_page'] = $r['current_page'] . $rCP['topology_url_opt'];
    $session_data[$cpt]['topology_name'] = $rCP['topology_name'] != '' ? _($rCP['topology_name']) : $rCP['topology_name'];

    // $centreon->user->admin is a string '1' or '0'
    if ($centreon->user->admin == 1) {
        // adding the link to be able to kick the user
        $session_data[$cpt]['actions']
            = "<a href='./main.php?p=" . $p . '&o=k&user=' . $r['user_id'] . "'>"
                . "<span title='" . _('Disconnect user') . "'>"
                    . returnSvg('www/img/icons/delete.svg', 'var(--icons-fill-color)', 22, 22)
                . '</span>'
            . '</a>';

        // checking if the user account is linked to an LDAP
        if ($r['contact_auth_type'] === 'ldap') {
            // adding the last synchronization time
            if ($r['contact_ldap_last_sync'] > 0) {
                $session_data[$cpt]['last_sync'] = (int) $r['contact_ldap_last_sync'];
            } elseif ($r['contact_ldap_last_sync'] === 0 || $r['contact_ldap_last_sync'] === null) {
                $session_data[$cpt]['last_sync'] = '-';
            }
            $session_data[$cpt]['synchronize']
                = "<a href='#'>"
                    . "<span onclick='submitSync(" . $currentPage . ', "' . $r['user_id'] . "\")' 
                    title='" . _('Synchronize LDAP') . "'>"
                        . returnSvg('www/img/icons/refresh.svg', 'var(--icons-fill-color)', 18, 18)
                    . '</span>'
                . '</a>';
        } else {
            // hiding the synchronization option and details
            $session_data[$cpt]['last_sync'] = '';
            $session_data[$cpt]['synchronize'] = '';
        }
        // adding the column titles
        $tpl->assign('wi_last_sync', _('Last LDAP sync'));
        $tpl->assign('wi_syncLdap', _('Refresh LDAP'));
        $tpl->assign('wi_logoutUser', _('Logout user'));
    }
}

if (isset($msg)) {
    $tpl->assign('msg', $msg);
}

$tpl->assign('session_data', $session_data);
$tpl->assign('isAdmin', $centreon->user->admin);
$tpl->assign('wi_user', _('Users'));
$tpl->assign('wi_where', _('Position'));
$tpl->assign('wi_last_req', _('Last request'));
$tpl->assign('distant_location', _('IP Address'));
$tpl->assign('adminIcon', returnSvg('www/img/icons/admin.svg', 'var(--icons-fill-color)', 17, 17));
$tpl->assign('userIcon', returnSvg('www/img/icons/user.svg', 'var(--icons-fill-color)', 17, 17));
$tpl->display('connected_user.ihtml');
?>

<script>
    //formatting the tags containing a class isTimestamp
    formatDateMoment();

    // ask for confirmation when requesting to resynchronize contact data from the LDAP
    function submitSync(p, contactId) {
        // msg = localized message to be displayed in the confirmation popup
        let msg = "<?= _('All this contact sessions will be closed. Are you sure you want to request a '
            . 'synchronization at the next login of this Contact ?'); ?>";
        // then executing the request and refreshing the page
        if (confirm(msg)) {
            $.ajax({
                url: './api/internal.php?object=centreon_ldap_synchro&action=requestLdapSynchro',
                type: 'POST',
                async: false,
                data: {contactId: contactId},
                success: function(data) {
                    if (data === true) {
                        window.location.href = "?p=" + p;
                    }
                }
            });
        }
    }
</script>
