2017-01-27 19:41:02 +00:00
|
|
|
<?php
|
|
|
|
|
2017-01-30 09:39:45 +00:00
|
|
|
require_once __DIR__.'/../../include/php/const';
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
function api_sync(){
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
/* [1] Fetch & generate useful data
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Fetch target url */
|
|
|
|
$url = @file_get_contents(URL_CONF);
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
if( $url === false ){
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Cannot find server's api url", 'api:sync','update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 127;
|
2017-01-27 19:56:02 +00:00
|
|
|
}
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-01-31 18:26:32 +00:00
|
|
|
// remove unwanted spaces or linebreaks
|
2017-02-19 14:44:59 +00:00
|
|
|
$url = preg_replace('/\s/', '', $url).'/sync/';
|
2017-02-19 10:19:40 +00:00
|
|
|
|
2017-01-31 18:26:32 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
/* (2) Fetch machine id */
|
|
|
|
$id_machine = @file_get_contents(ID_CONF);
|
|
|
|
|
|
|
|
if( $id_machine === false ){
|
|
|
|
slog("Cannot find machine id", 'api:sync','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id_machine = (int) preg_replace('/\s/', '', $id_machine);
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Fetch cyclic hash */
|
2017-01-27 19:41:02 +00:00
|
|
|
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
if( strlen($hash) != 128 ){
|
2017-02-19 10:19:40 +00:00
|
|
|
slog("Wrong hash length (".strlen($hash).")", 'api:sync','update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 127;
|
2017-01-27 19:56:02 +00:00
|
|
|
}
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
/* (4) Try new hash if available */
|
|
|
|
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
|
2017-05-09 10:27:09 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
if( $new === false ){
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:sync','update');
|
2017-01-27 19:41:02 +00:00
|
|
|
return 127;
|
2017-01-27 19:56:02 +00:00
|
|
|
}
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
/* (5) Fetch machine unlock code */
|
|
|
|
$unlock_code = @file_get_contents(UNLOCK_CODE);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
if( $unlock_code === false ){
|
|
|
|
slog("Cannot find unlock code", 'api:sync','update');
|
|
|
|
return 127;
|
2017-01-27 19:56:02 +00:00
|
|
|
}
|
2017-02-19 10:19:40 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
$unlock_code = preg_replace('/\s/', '', $unlock_code);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-05-09 10:27:09 +00:00
|
|
|
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-02-19 10:19:40 +00:00
|
|
|
/* [2] Create httpRequest base
|
2017-01-27 19:41:02 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) Set URL */
|
2017-01-28 17:58:49 +00:00
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
/* (2) Set HTTP method -> POST */
|
|
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
|
|
|
2017-02-19 10:19:40 +00:00
|
|
|
/* (3) Additional options */
|
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
2017-07-17 17:10:50 +00:00
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // allow SSL
|
2017-02-19 10:19:40 +00:00
|
|
|
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [3] Manage post data
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Set post data */
|
2017-05-10 12:01:37 +00:00
|
|
|
$postarray = [
|
|
|
|
'id_machine' => $id_machine,
|
|
|
|
'token' => $hash,
|
|
|
|
'unlock' => $unlock_code
|
|
|
|
];
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
/* (2) Add new hash if renew */
|
|
|
|
if( strlen($new) == 128 )
|
|
|
|
$postarray['token'] = $new;
|
|
|
|
|
|
|
|
/* (3) Set postdata raw to curl */
|
2017-01-31 18:26:32 +00:00
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
2017-02-19 10:19:40 +00:00
|
|
|
/* [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;
|
|
|
|
}
|
|
|
|
|
2017-02-19 11:31:36 +00:00
|
|
|
$wh_tok = preg_replace('/\s/', '', $wh_tok);
|
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
/* (2) Set Auth header digest */
|
2017-02-19 10:19:40 +00:00
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
2017-05-10 12:01:37 +00:00
|
|
|
"Authorization: Digest $wh_tok"
|
2017-02-19 10:19:40 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
/* [5] Send and catch request response
|
2017-01-27 19:41:02 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) Send and catch response */
|
2017-01-28 17:58:49 +00:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
2017-01-27 19:41:02 +00:00
|
|
|
$response = curl_exec($curl);
|
|
|
|
|
2017-09-27 07:43:44 +00:00
|
|
|
/* (2) Save request+response to file */
|
|
|
|
file_put_contents(TMP_DIR.'/api.request', $postarray);
|
2017-09-26 14:30:25 +00:00
|
|
|
file_put_contents(TMP_DIR.'/api.response', $response);
|
|
|
|
|
|
|
|
/* (3) Close request */
|
2017-02-19 10:19:40 +00:00
|
|
|
curl_close($curl);
|
2017-05-09 10:27:09 +00:00
|
|
|
|
2017-09-26 14:30:25 +00:00
|
|
|
/* (4) Return response as result */
|
2017-01-27 19:56:02 +00:00
|
|
|
if( $response === false ){
|
2017-01-30 09:59:52 +00:00
|
|
|
slog("Request error", 'api:sync','update');
|
2017-01-27 19:56:02 +00:00
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
|
2017-02-19 10:19:40 +00:00
|
|
|
/* [6] Response management
|
2017-01-27 19:56:02 +00:00
|
|
|
=========================================================*/
|
2017-02-19 11:31:36 +00:00
|
|
|
/* (1) Try to json_decode response */
|
|
|
|
$response_arr = json_decode($response, true);
|
|
|
|
|
|
|
|
// if cannot, abort
|
2017-02-20 20:22:42 +00:00
|
|
|
if( is_null($response_arr) ){
|
|
|
|
slog('Cannot parse HTTP response', 'api:sync', 'update');
|
2017-02-19 11:31:36 +00:00
|
|
|
return 127;
|
2017-02-20 20:22:42 +00:00
|
|
|
}
|
2017-02-19 11:31:36 +00:00
|
|
|
|
|
|
|
/* (2) Check response error */
|
2017-02-20 20:22:42 +00:00
|
|
|
if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){
|
|
|
|
slog('API error not on \'Success\'', 'api:sync', 'update');
|
2017-02-19 11:31:36 +00:00
|
|
|
return 127;
|
2017-02-20 20:22:42 +00:00
|
|
|
}
|
2017-02-19 11:31:36 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
/* [7] Decrement cyclic-hash so request has ran successfully
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Decrement the hash */
|
|
|
|
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
|
|
|
|
|
|
|
|
if( $decr === false ){
|
2017-02-20 20:22:42 +00:00
|
|
|
slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync','update');
|
2017-02-19 14:44:59 +00:00
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:22:42 +00:00
|
|
|
slog('HTTP Response succesfully received', 'api:sync', 'update');
|
2017-02-19 14:44:59 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
/* [8] Deploy received data
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Manage response deployement */
|
2017-05-10 12:01:37 +00:00
|
|
|
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync');
|
2017-05-09 10:27:09 +00:00
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
/* (2) Return state */
|
2017-02-19 10:19:40 +00:00
|
|
|
return ($deploy === true) ? 0 : 127;
|
2017-02-19 14:44:59 +00:00
|
|
|
|
2017-05-09 10:27:09 +00:00
|
|
|
|
2017-01-27 19:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
|
|
|
|
$exec = api_sync();
|
2017-05-09 10:27:09 +00:00
|
|
|
|
|
|
|
if( $exec == 0 ) slog('Success', 'api:sync', 'update');
|
|
|
|
else slog('Failure', 'api:sync', 'update');
|
|
|
|
|
2017-02-20 20:22:42 +00:00
|
|
|
echo $exec;
|
|
|
|
die($exec);
|
2017-05-09 10:27:09 +00:00
|
|
|
|
|
|
|
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
?>
|