2017-02-19 10:19:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__.'/../../include/php/const';
|
|
|
|
|
|
|
|
function api_init(){
|
|
|
|
|
|
|
|
/* [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:init','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove unwanted spaces or linebreaks
|
2017-02-19 14:44:59 +00:00
|
|
|
$url = preg_replace('/\s/', '', $url).'/init/';
|
2017-02-19 10:19:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (2) Fetch machine id */
|
|
|
|
$id_machine = @file_get_contents(ID_CONF);
|
|
|
|
|
|
|
|
if( $id_machine === false ){
|
|
|
|
slog("Cannot find machine id", 'api:init','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
$id_machine = (int) preg_replace('/\s/', '', $id_machine);
|
2017-02-19 10:19:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (3) Fetch cyclic hash */
|
|
|
|
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
|
|
|
|
|
|
|
|
if( strlen($hash) != 128 ){
|
|
|
|
slog("Wrong hash length (".strlen($hash).")", 'api:init','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Try new hash if available */
|
|
|
|
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
|
|
|
|
|
|
|
|
if( $new === false ){
|
|
|
|
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:init','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Fetch machine unlock code */
|
2017-02-19 11:31:36 +00:00
|
|
|
$unlock_code = @file_get_contents(UNLOCK_CODE);
|
2017-02-19 10:19:40 +00:00
|
|
|
|
|
|
|
if( $unlock_code === false ){
|
|
|
|
slog("Cannot find unlock code", 'api:init','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2017-02-19 11:31:36 +00:00
|
|
|
$unlock_code = preg_replace('/\s/', '', $unlock_code);
|
2017-02-19 10:19:40 +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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [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:init','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2017-02-19 11:31:36 +00:00
|
|
|
$wh_tok = preg_replace('/\s/', '', $wh_tok);
|
|
|
|
|
2017-02-19 10:19:40 +00:00
|
|
|
/* (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:init','update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
|
|
|
|
/* [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;
|
2017-02-19 10:19:40 +00:00
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
/* (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);
|
2017-02-19 10:19:40 +00:00
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
|
|
|
|
/* [7] Decrement cyclic-hash so request has ran successfully
|
2017-02-19 10:19:40 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (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;
|
|
|
|
}
|
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-02-19 10:19:40 +00:00
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
/* [8] Deploy received data
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Manage response deployement */
|
2017-02-19 11:31:36 +00:00
|
|
|
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy init');
|
2017-02-19 10:19:40 +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-02-19 10:19:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-19 14:44:59 +00:00
|
|
|
die(api_init());
|
2017-02-19 10:19:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|