SATS/lib/api/source/request.php

110 lines
2.3 KiB
PHP
Raw Normal View History

<?php
require_once __DIR__.'/../../include/const';
function api_request(){
/* [1] Fetch & generate useful data
=========================================================*/
/* (1) Fetch target url */
$url = @file_get_contents(URL_CONF);
if( $url === false )
return 127;
/* (2) Fetch cyclic hash */
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
if( strlen($hash) != 128 )
return 127;
/* (3) Try new hash if available */
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
if( $new === false )
return 127;
/* (4) Decrement the hash */
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
if( $decr === false )
return 127;
/* (5) Generate the multipart boundary */
$boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary';
/* (6) Fetch data */
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetchdata'));
if( is_null($data) )
$data = "{}";
/* [2] Create httpRequest basis
=========================================================*/
/* (1) Set URL */
$curl = curl_init($url);
/* (2) Set HTTP method -> POST */
curl_setopt($curl, CURLOPT_POST, true);
/* [3] Manage post data
=========================================================*/
/* (1) Set post data */
$postarray = [
'token' => $hash,
'data' => $data
];
/* (2) Add renew if renew */
if( strlen($new) == 128 )
$postarray['renew'] = $new;
/* (3) Parse postfiels to multipart format */
$postraw = "--$boundary";
foreach($postarray as $postkey=>$postvalue)
$postraw .= "\r\ncontent-disposition: form-data; name=\"$postkey\"\r\n\r\n$postvalue\r\n--$boundary";
$postraw .= "--";
/* (4) Set postdata raw to curl */
curl_setopt($curl, CURLOPT_POSTFIELDS, $postraw);
/* [4] Manage headers
=========================================================*/
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: multipart/form-data; boundary=$boundary",
"Content-Length: ".strlen($postraw)
]);
/* [5] Send and catch request response
=========================================================*/
/* (1) Send and catch response */
$response = curl_exec($curl);
/* (2) Close request */
curl_close($curl);
/* (3) Return response as result */
if( $response === false )
return 127;
return $response;
}
echo api_request();
?>