SATS/lib/api/source/update.php

165 lines
4.2 KiB
PHP
Raw Permalink Normal View History

<?php
require_once __DIR__.'/../../include/php/const';
function api_request(){
/* [1] Fetch & generate useful data
=========================================================*/
/* (1) Fetch target url */
$url = @file_get_contents(URL_CONF);
if( $url === false ){
slog("Cannot find server's api url", 'api:update','update');
return 127;
}
// remove unwanted spaces or linebreaks
$url = preg_replace('/\s/', '', $url).'/update/';
/* (2) Fetch cyclic hash */
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
if( strlen($hash) != 128 ){
slog("Wrong hash length (".strlen($hash).")", 'api:update','update');
return 127;
}
/* (3) Try new hash if available */
$new_hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
2017-05-08 20:07:20 +00:00
if( $new_hash === false ){
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:update','update');
return 127;
}
/* (4) Fetch data */
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch'), true);
if( is_null($data) ){
slog("api:fetch returned unreadable content", 'api:update','update');
$data = [];
}
2017-05-08 20:07:20 +00:00
/* [2] Create httpRequest base
=========================================================*/
/* (1) Set URL */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
/* (2) Set HTTP method -> POST */
curl_setopt($curl, CURLOPT_POST, true);
/* (3) Additional options */
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // allow SSL
/* [3] Manage post data
=========================================================*/
/* (1) Set post data */
$postarray = [ 'data' => json_encode($data) ];
/* (2) 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:update','update');
return 127;
}
$wh_tok = preg_replace('/\s/', '', $wh_tok);
/* (2) Calculate @new_hash (default value) */
if( strlen($new_hash) != 128 )
$new_hash = $hash;
/* (3) Set Auth header digest */
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Digest {$wh_tok}{$hash}{$new_hash}"
]);
/* [5] Send and catch request response
=========================================================*/
/* (1) Send and catch response */
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
/* (2) Save request+response to file */
file_put_contents(TMP_DIR.'/api.request', $postarray);
file_put_contents(TMP_DIR.'/api.response', $response);
/* (3) Close request */
curl_close($curl);
2017-05-08 20:07:20 +00:00
/* (4) Return response as result */
if( $response === false ){
slog("Request error", 'api:update','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:update', 'update');
return 127;
}
/* (2) Check response error */
if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){
slog('API error not on \'Success\'', 'api:update', 'update');
return 127;
}
/* [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:update','update');
return 127;
}
slog('HTTP Response succesfully received', 'api:update', 'update');
/* [8] Deploy received data
=========================================================*/
/* (1) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy');
2017-05-08 20:07:20 +00:00
/* (2) Return state */
return ($deploy === true) ? 0 : 127;
2017-05-08 20:07:20 +00:00
}
$exec = api_request();
if( $exec == 0 ) slog('Success', 'api:update', 'update');
else slog('Failure', 'api:update', 'update');
2017-05-09 10:27:09 +00:00
echo $exec;
die($exec);
2017-05-08 20:07:20 +00:00
?>