Wettmannstaetten impot update & ofaa oaid scripts
This commit is contained in:
142
scripts/preorder/ofaa-oaid/include/rest.php
Normal file
142
scripts/preorder/ofaa-oaid/include/rest.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
function fetchAccessToken($oauth_url, $client_id, $client_secret, $scopes = ["oaid:codes:read"]) {
|
||||
//echo "called fetchAccessToken $oauth_url\n";
|
||||
global $token_file;
|
||||
global $access_token;
|
||||
|
||||
$headers = [
|
||||
"Accept: application/json",
|
||||
"Content-Type: application/x-www-form-urlencoded",
|
||||
];
|
||||
|
||||
$post_content = [
|
||||
"grant_type" => "client_credentials",
|
||||
"client_id" => $client_id,
|
||||
"client_secret" => $client_secret,
|
||||
"scope" => implode(" ", $scopes),
|
||||
];
|
||||
|
||||
$output = postRestUrl($oauth_url, $headers, $post_content);
|
||||
$output_values = json_decode($output);
|
||||
if(!is_object($output_values) || !property_exists($output_values, "access_token")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$access_token = $output_values->access_token;
|
||||
file_put_contents($token_file, $access_token);
|
||||
|
||||
return $access_token;
|
||||
}
|
||||
|
||||
function getRestUrl($url, $headers, $params = [], $debug = false) {
|
||||
//echo "called getRestUrl $url\n";
|
||||
$ctx_options = ["http" => [
|
||||
"ignore_errors" => true,
|
||||
"method" => "GET",
|
||||
"header" => $headers,
|
||||
]];
|
||||
|
||||
$_url = $url;
|
||||
if(count($params)) {
|
||||
$_url .= "?".http_build_query($params);
|
||||
}
|
||||
|
||||
if($debug) {
|
||||
var_dump($_url, $ctx_options);
|
||||
exit;
|
||||
}
|
||||
|
||||
$ctx = stream_context_create($ctx_options);
|
||||
$output = file_get_contents($_url, false, $ctx);
|
||||
|
||||
foreach($http_response_header as $h_line) {
|
||||
$m = [];
|
||||
if(preg_match('/HTTP\/\d+(?:\.\d+) ([245]\d+)/', $h_line, $m)) {
|
||||
$response_code = $m[1];
|
||||
if(substr($response_code, 0, 1) == "2") {
|
||||
return $output;
|
||||
} elseif($response_code == "401") {
|
||||
return "__err__401";
|
||||
} else {
|
||||
die("getRestUrl(): $h_line");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function putRestUrl($url, $headers, $content, $content_type = "application/x-www-form-urlencoded", $follow_location = true, $debug = false) {
|
||||
return postRestUrl($url, $headers, $content, $content_type, $follow_location, $debug, 'PUT');
|
||||
}
|
||||
|
||||
function postRestUrl($url, $headers, $content, $content_type = "application/x-www-form-urlencoded", $follow_location = true, $debug = false, $method = "POST") {
|
||||
//echo "called postResturl $url\n";
|
||||
$ctx_options = ["http" => [
|
||||
"ignore_errors" => true,
|
||||
"method" => "POST",
|
||||
"header" => $headers,
|
||||
]];
|
||||
|
||||
if(!$follow_location) {
|
||||
$ctx_options["http"]["follow_location"] = false;
|
||||
}
|
||||
|
||||
if($method == "PUT") {
|
||||
$ctx_options["http"]["method"] = "PUT";
|
||||
}
|
||||
|
||||
if($content_type == "application/x-www-form-urlencoded") {
|
||||
$ctx_options["http"]["content"] = http_build_query($content);
|
||||
}
|
||||
if($content_type == "application/json") {
|
||||
$ctx_options["http"]["content"] = json_encode($content);
|
||||
}
|
||||
|
||||
if($debug) {
|
||||
var_dump($url, $ctx_options);
|
||||
exit;
|
||||
}
|
||||
|
||||
$ctx = stream_context_create($ctx_options);
|
||||
$output = file_get_contents($url, false, $ctx);
|
||||
//var_dump($http_response_header);
|
||||
|
||||
if(!$follow_location) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
foreach($http_response_header as $h_line) {
|
||||
$m = [];
|
||||
$status_code_types = "245";
|
||||
if(!$follow_location) {
|
||||
$status_code_types = "2345";
|
||||
}
|
||||
if(preg_match('/HTTP\/\d+(?:\.\d+) ([245]\d+)/', $h_line, $m)) {
|
||||
$response_code = $m[1];
|
||||
if(substr($response_code, 0, 1) == "2") {
|
||||
return $output;
|
||||
} elseif(substr($response_code, 0, 1) == "3") {
|
||||
return $output;
|
||||
} elseif($response_code == "401") {
|
||||
return "__err__401";
|
||||
} else {
|
||||
die("postRestUrl(): $h_line\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function guidv4($data = null) {
|
||||
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
|
||||
$data = $data ?? random_bytes(16);
|
||||
assert(strlen($data) == 16);
|
||||
|
||||
// Set version to 0100
|
||||
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
|
||||
// Set bits 6-7 to 10
|
||||
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
|
||||
|
||||
// Output the 36 character UUID.
|
||||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||||
}
|
||||
Reference in New Issue
Block a user