2017-01-27 19:41:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__.'/../../include/const';
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
function api_request(){
|
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 ){
|
|
|
|
slog("Cannot find server's api url", 'api:sync');
|
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
|
|
|
|
|
|
|
/* (2) Fetch cyclic hash */
|
|
|
|
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
if( strlen($hash) != 128 ){
|
|
|
|
slog("Wrong cyclic-hash:hash hash length (".strlen($hash).")", 'api:sync');
|
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
|
|
|
|
|
|
|
/* (3) Try new hash if available */
|
|
|
|
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
if( $new === false ){
|
|
|
|
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:sync');
|
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
|
|
|
|
|
|
|
/* (4) Decrement the hash */
|
|
|
|
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
if( $decr === false ){
|
|
|
|
slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync');
|
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
|
|
|
|
|
|
|
/* (5) Generate the multipart boundary */
|
|
|
|
$boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary';
|
|
|
|
|
|
|
|
/* (6) Fetch data */
|
2017-01-27 19:56:02 +00:00
|
|
|
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch'));
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
if( is_null($data) ){
|
|
|
|
slog("api:fetch returned unreadable content", 'api:sync');
|
2017-01-28 16:59:54 +00:00
|
|
|
$data = [];
|
2017-01-27 19:56:02 +00:00
|
|
|
}
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Create httpRequest basis
|
|
|
|
=========================================================*/
|
|
|
|
/* (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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Manage post data
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Set post data */
|
|
|
|
$postarray = [
|
|
|
|
'token' => $hash,
|
2017-01-28 16:59:54 +00:00
|
|
|
'data' => json_encode($data)
|
2017-01-27 19:41:02 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/* (2) Add renew if renew */
|
|
|
|
if( strlen($new) == 128 )
|
|
|
|
$postarray['renew'] = $new;
|
|
|
|
|
|
|
|
/* (3) Parse postfiels to multipart format */
|
2017-01-28 17:58:49 +00:00
|
|
|
$postraw = "--$boundary";
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-01-28 17:58:49 +00:00
|
|
|
foreach($postarray as $postkey=>$postvalue)
|
|
|
|
$postraw .= "\r\nContent-Disposition: form-data; name=\"$postkey\"\r\n\r\n$postvalue\r\n--$boundary";
|
2017-01-27 19:41:02 +00:00
|
|
|
|
2017-01-28 17:58:49 +00:00
|
|
|
$postraw .= "--";
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (4) Set postdata raw to curl */
|
2017-01-28 17:58:49 +00:00
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $postraw);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Manage headers
|
|
|
|
=========================================================*/
|
2017-01-28 17:58:49 +00:00
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
|
|
|
"Content-Type: multipart/form-data; boundary=$boundary"
|
|
|
|
]);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [5] Send and catch request response
|
|
|
|
=========================================================*/
|
|
|
|
/* (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);
|
|
|
|
|
|
|
|
/* (2) Close request */
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
|
|
/* (3) Return response as result */
|
2017-01-27 19:56:02 +00:00
|
|
|
if( $response === false ){
|
|
|
|
slog("Request error", 'api:sync');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
2017-01-28 17:58:49 +00:00
|
|
|
|
|
|
|
/* [6] Decrement cyclic-hash so request has ran successfully
|
|
|
|
=========================================================*/
|
|
|
|
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [6] Response management
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Writes request to temporary pipe */
|
|
|
|
file_put_contents(TMP_DIR.'/api.response', $response);
|
|
|
|
|
|
|
|
/* (2) Manage response deployement */
|
|
|
|
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy');
|
|
|
|
|
|
|
|
/* (3) Return state */
|
|
|
|
return ($deploy === true);
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-27 19:56:02 +00:00
|
|
|
echo api_request();
|
2017-01-27 19:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|