From dba451e93e2c207fb9d75a1d0d42aacdcaa7e6fc Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 19 Feb 2017 15:44:59 +0100 Subject: [PATCH] [Done] Gestion de init|sync fonctionnelle[Todo] Fetcher [Todo] Deploy --- lib/api/source/init.php | 38 ++++++++++++++++++++++++++++---------- lib/api/source/sync.php | 37 ++++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/lib/api/source/init.php b/lib/api/source/init.php index ff01337..b3b6e35 100755 --- a/lib/api/source/init.php +++ b/lib/api/source/init.php @@ -15,7 +15,7 @@ } // remove unwanted spaces or linebreaks - $url = preg_replace('/\s/', '', $url).'/init'; + $url = preg_replace('/\s/', '', $url).'/init/'; /* (2) Fetch machine id */ @@ -26,6 +26,7 @@ return 127; } + $id_machine = (int) preg_replace('/\s/', '', $id_machine); /* (3) Fetch cyclic hash */ @@ -120,9 +121,26 @@ return 127; } + + /* [6] Response management + =========================================================*/ + /* (1) Try to json_decode response */ + $response_arr = json_decode($response, true); + + // if cannot, abort + if( is_null($response_arr) ) + return 127; + + /* (2) Check response error */ + if( !isset($response_arr['error']) || $response_arr['error'] != 0 ) + return 127; - /* [6] Decrement cyclic-hash so request has ran successfully + /* (3) Writes request to temporary pipe */ + file_put_contents(TMP_DIR.'/api.response', $response); + + + /* [7] Decrement cyclic-hash so request has ran successfully =========================================================*/ /* (1) Decrement the hash */ $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); @@ -132,21 +150,21 @@ return 127; } - - /* [6] Response management - =========================================================*/ - /* (1) Writes request to temporary pipe */ - file_put_contents(TMP_DIR.'/api.response', $response); + return 0; - /* (2) Manage response deployement */ + + /* [8] Deploy received data + =========================================================*/ + /* (1) Manage response deployement */ $deploy = syscall(SOURCE_DIR.'/lib/api/deploy init'); - /* (3) Return state */ + /* (2) Return state */ return ($deploy === true) ? 0 : 127; + } - echo api_init(); + die(api_init()); diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index c0768a4..70b5ac3 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -15,7 +15,7 @@ } // remove unwanted spaces or linebreaks - $url = preg_replace('/\s/', '', $url).'/sync'; + $url = preg_replace('/\s/', '', $url).'/sync/'; /* (2) Fetch cyclic hash */ @@ -108,19 +108,6 @@ return 127; } - - - /* [6] Decrement cyclic-hash so request has ran successfully - =========================================================*/ - /* (1) Decrement the hash */ - $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); - - if( $decr === false ){ - slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync','update'); - return 127; - } - - /* [6] Response management =========================================================*/ /* (1) Try to json_decode response */ @@ -137,15 +124,31 @@ /* (3) Writes request to temporary pipe */ file_put_contents(TMP_DIR.'/api.response', $response); - /* (4) Manage response deployement */ + + /* [7] Decrement cyclic-hash so request has ran successfully + =========================================================*/ + /* (1) Decrement the hash */ + $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); + + if( $decr === false ){ + slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:init','update'); + return 127; + } + + return 0; + + /* [8] Deploy received data + =========================================================*/ + /* (1) Manage response deployement */ $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync'); - /* (5) Return state */ + /* (2) Return state */ return ($deploy === true) ? 0 : 127; + } - echo api_request(); + die(api_request());