POST */ curl_setopt($curl, CURLOPT_POST, true); /* (3) Additional options */ curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); /* [3] Manage post data =========================================================*/ /* (1) Set post data */ $postarray = [ 'id_machine' => $id_machine, 'token' => $hash, 'unlock' => $unlock_code ]; /* (2) Add new hash if renew */ if( strlen($new) == 128 ) $postarray['token'] = $new; /* (3) Set postdata raw to curl */ curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray); /* [4] Manage authentication =========================================================*/ /* (1) Fetch warehouse token */ $wh_tok = @file_get_contents(WAREHOUSE_TOK); if( $wh_tok === false ){ slog("Cannot find warehouse token", 'api:sync','update'); return 127; } $wh_tok = preg_replace('/\s/', '', $wh_tok); /* (2) Set Auth header digest */ curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Digest $wh_tok" ]); /* [5] Send and catch request response =========================================================*/ /* (1) Send and catch response */ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); /* (2) Close request */ curl_close($curl); /* (3) Return response as result */ if( $response === false ){ slog("Request error", 'api:sync','update'); 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) ){ slog('Cannot parse HTTP response', 'api:sync', 'update'); return 127; } /* (2) Check response error */ if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){ slog('API error not on \'Success\'', 'api:sync', 'update'); return 127; } /* (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'); if( $decr === false ){ slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync','update'); return 127; } slog('HTTP Response succesfully received', 'api:sync', 'update'); /* [8] Deploy received data =========================================================*/ /* (1) Manage response deployement */ $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync'); /* (2) Return state */ return ($deploy === true) ? 0 : 127; } $exec = api_sync(); if( $exec == 0 ) slog('Success', 'api:sync', 'update'); else slog('Failure', 'api:sync', 'update'); echo $exec; die($exec); ?>