diff --git a/lib/api/source/deploy.php b/lib/api/source/deploy.php new file mode 100755 index 0000000..f2f4a6e --- /dev/null +++ b/lib/api/source/deploy.php @@ -0,0 +1,40 @@ + diff --git a/lib/api/source/init.php b/lib/api/source/init.php index b3bfcca..ff01337 100755 --- a/lib/api/source/init.php +++ b/lib/api/source/init.php @@ -45,13 +45,14 @@ } /* (5) Fetch machine unlock code */ - $unlock_code = @file_get_contents(ID_CONF); + $unlock_code = @file_get_contents(UNLOCK_CODE); if( $unlock_code === false ){ slog("Cannot find unlock code", 'api:init','update'); return 127; } + $unlock_code = preg_replace('/\s/', '', $unlock_code); @@ -96,6 +97,8 @@ return 127; } + $wh_tok = preg_replace('/\s/', '', $wh_tok); + /* (2) Set Auth header digest */ curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Digest $wh_tok" @@ -136,7 +139,7 @@ file_put_contents(TMP_DIR.'/api.response', $response); /* (2) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy init'); /* (3) Return state */ return ($deploy === true) ? 0 : 127; diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index cdf6845..c0768a4 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -85,6 +85,8 @@ return 127; } + $wh_tok = preg_replace('/\s/', '', $wh_tok); + /* (2) Set Auth header digest */ curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Digest $wh_tok" @@ -121,13 +123,24 @@ /* [6] Response management =========================================================*/ - /* (1) Writes request to temporary pipe */ + /* (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; + + /* (3) Writes request to temporary pipe */ file_put_contents(TMP_DIR.'/api.response', $response); - /* (2) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); + /* (4) Manage response deployement */ + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync'); - /* (3) Return state */ + /* (5) Return state */ return ($deploy === true) ? 0 : 127; } diff --git a/lib/include/bash/const b/lib/include/bash/const index 810980b..42d7fc6 100755 --- a/lib/include/bash/const +++ b/lib/include/bash/const @@ -20,6 +20,7 @@ export ID_CONF="$CONF_DIR/machine.id"; export URL_CONF="$CONF_DIR/api.url"; export AUTH_CONF="$CONF_DIR/auth.list"; export WAREHOUSE_TOK="$CONF_DIR/warehouse.token"; +export UNLOCK_CODE="$CONF_DIR/machine.unlock"; # LOG FILES export BOOT_LOG="$LOG_DIR/boot.log"; diff --git a/lib/include/php/const b/lib/include/php/const index 60a3775..189f15d 100755 --- a/lib/include/php/const +++ b/lib/include/php/const @@ -20,6 +20,7 @@ define('URL_CONF', CONF_DIR.'/api.url'); define('AUTH_CONF', CONF_DIR.'/auth.list'); define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token'); + define('UNLOCK_CODE', CONF_DIR.'/machine.unlock'); # LOG FILES define('BOOT_LOG', LOG_DIR.'/boot.log');