<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
if($_SESSION['sml_si']!=session_id()) {
    die();
}
require(__DIR__.'/ssp.class.php');
require(__DIR__.'/../../config/config.inc.php');
if(($_SERVER['SERVER_ADDR']==$_SESSION['demo_server_ip']) && ((!empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0] : $_SERVER['REMOTE_ADDR']))!=$_SESSION['demo_developer_ip']) && ($_SESSION['id_user']==$_SESSION['demo_user_id'])) {
    $demo = true;
} else {
    $demo = false;
}

$query = "SELECT u.* FROM sml_users as u";
$table = "( $query ) t";
$primaryKey = 'id';

$columns = array(
    array(
        'db' => 'id',
        'dt' => 'DT_RowId',
        'formatter' => function( $d, $row ) {
            return $d;
        }
    ),
    array( 'db' => 'username',  'dt' =>0 ),
    array( 'db' => 'email',  'dt' =>1, 'formatter' => function( $d, $row ) {
        global $demo;
        if($demo && !empty($d)) {
            $parts = explode('@',$d);
            if(count($parts)==2) {
                return substr($parts[0],0,1).str_repeat('*',max(strlen($parts[0])-1,3)).'@'.str_repeat('*',max(strlen($parts[1]),3));
            }
            return str_repeat('*',max(strlen($d),3));
        } else {
            return $d;
        }
    }),
    array( 'db' => 'role',  'dt' =>2, 'formatter' => function( $d, $row ) {
        return ucfirst($d);
    }),
    array( 'db' => 'active',  'dt' =>3, 'formatter' => function( $d, $row ) {
        if($d) {
            return "<i class='fa fa-check'></i>";
        } else {
            return "<i class='fa fa-times'></i>";
        }
    }),
);

$sql_details = array(
    'user' => DATABASE_USERNAME,
    'pass' => DATABASE_PASSWORD,
    'db' => DATABASE_NAME,
    'host' => DATABASE_HOST);

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
