<?php

header('Content-type: application/json');
$res = new stdClass();

if (!empty($_GET['method']) && !empty($_GET['secret'])) {
    $key = 'EXTERNALSERVICESCONFIG';
    $pass = md5($key.date('YW'));
    if ($_GET['secret'] == $pass) {
        $config = "../serveur/config/clients.ini";
        if (file_exists($config)) {
            $configTab = parse_ini_file($config, true);
            if ($_GET['method'] == 'config') {
                if (isset($configTab[$_GET['clientId']])) {
                    $res->success = true;
                    $res->idClient = $_GET['clientId'];
                    $res->client_key = $configTab[$_GET['clientId']]['key'];
                    $res->client_ip = $configTab[$_GET['clientId']]['ip_agent'];
                }
                else {
                    $res->success = false;
                    $res->message = 'Unknown client';
                }
            }
            else if ($_GET['method'] == 'list') {
                $res->success = true;
                $res->clients = array_keys($configTab);
            }
        }
        else {
            $res->success = false;
            $res->message = 'Config not exists';
        }
    }
    else {
        $res->success = false;
        $res->message = 'Bad secret key';
    }
}
else {
    $res->success = false;
    $res->message = 'Invalid parameters';
}

echo json_encode($res);