SMMP/public_html/test/client/background.php

95 lines
2.4 KiB
PHP
Executable File

<?php define('__ROOT__', dirname(dirname(dirname(__FILE__))) );
require_once __ROOT__.'/autoloader.php';
use \api\core\ModuleRequest;
use \manager\sessionManager;
use \test\client\api;
debug();
/* [1] Gestion de l'enregistrement
=========================================================*/
if( isset($_POST['code']) && isset($_POST['action']) ){
array_push($_SESSION['history'],[
'code' => json_decode( $_POST['code'], true ),
'action' => json_decode( $_POST['action'], true )
]);
$data = '';
foreach($_SESSION['history'] as $h=>$entry)
$data .= "<span>".$entry['code']." - ".$entry['action']."</span><br>";
echo json_encode( [
'ModuleError' => 0,
'data' => $data
] );
}
/* [2] Gestion de la synchronisation
=========================================================*/
if( isset($_POST['sync']) ){
$api = new api();
/* (1) On effectue la synchro */
$token = sessionManager::secure_hash( $_COOKIE['mac_password'], $_COOKIE['mac_count'] );
// Renouvellement du TOKEN
if( $_COOKIE['mac_count'] == 1 ){
$response = $api->send('sync-renew', [
'@warehouse' => '643405e1004845c2291c244faab192e3ad493b9dec147c8030475794b0f2fa23',
'@token' => $token,
'@data' => json_encode( $_SESSION['history'] ),
'@renew' => sessionManager::secure_hash( $_COOKIE['mac_password'], 10)
]);
$parsedResponse = json_decode( $response, true );
// On change les données locales uniquement si on a une réponse positive
if( $parsedResponse['ModuleError'] == 0 ){
$_COOKIE['mac_count'] = 9;
setcookie('mac_count', $_COOKIE['mac_count'], time()+3600*24*365, '/');
}
}else{
$response = $api->send('sync', [
'@warehouse' => '643405e1004845c2291c244faab192e3ad493b9dec147c8030475794b0f2fa23',
'@token' => $token,
'@data' => json_encode( $_SESSION['history'] )
]);
$parsedResponse = json_decode( $response, true );
// On change les données locales uniquement si on a une réponse positive
if( $parsedResponse['ModuleError'] == 0 ){
$_COOKIE['mac_count']--;
setcookie('mac_count', $_COOKIE['mac_count'], time()+3600*24*365, '/');
}
}
$toReturn = [
'response' => $parsedResponse,
'count' => $_COOKIE['mac_count'],
'token' => $token,
'status' => $parsedResponse['ModuleError'],
'data' => ($parsedResponse['ModuleError']==0) ? $parsedResponse['data'] : 'void'
];
echo json_encode($toReturn);
}
?>