Skip to content
Snippets Groups Projects

Resolve "Data ingress for context information for tiki-wiki"

1 file
+ 21
0
Compare changes
  • Side-by-side
  • Inline
+ 75
1
@@ -13,7 +13,7 @@ function context($url, $domain, $content) {
header('Content-Type: application/json');
}
if (substr( $url, 0, 4 ) == "/get") {
if (substr( $url, 0, 4 ) == "/get" || substr( $url, 0, 7 ) == "/update") {
$url = $url . "/" . $content['smartDataContextId'];
}
@@ -31,6 +31,22 @@ function context($url, $domain, $content) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [];
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$key = strtolower(trim(array_shift($header)));
$headers[$key] = implode($header);
return $len;
}
);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@@ -40,15 +56,73 @@ function context($url, $domain, $content) {
$response = '{"errors": ["SmartDataContext API not available"]}';
}
if (array_key_exists("content-disposition", $headers)) {
header("Content-Type: " . $headers['content-type']);
header("content-disposition: " . $headers['content-disposition']);
}
http_response_code($http_status);
return $response;
}
function handle_unstructured_add() {
// Validate certificate usage and domain usage (certificate only)
$backend = new Backend(null);
$domain = $backend->domainInUse();
// Check if the required URL parameters are set
if (isset($_GET['smartDataContextId'])) {
// Retrieve headers from the original request
$headers = getallheaders();
if (isset($headers['Content-Type'], $headers['Filename'])) {
// Extract the smartdatacontextid from the URL parameters
$smartDataContextId = $_GET['smartDataContextId'];
$targetUrl = Config::config()::SMARTDATACONTEXT_API . "/unstructured/add/$domain/$smartDataContextId";
// Initialize cURL session
$ch = curl_init($targetUrl);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: ' . $headers['Content-Type'],
'Filename: ' . $headers['Filename'],
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$inputData = file_get_contents('php://input');
curl_setopt($ch, CURLOPT_POSTFIELDS, $inputData); // Use php://input to directly stream input
curl_setopt($ch, CURLOPT_INFILESIZE, $_SERVER['CONTENT_LENGTH']); // Set the expected content length
// Execute the cURL request
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_status == 0) {
# Could not connect to the smartdatacontext API
$http_status = 503; # Service Unavailable
$response = '{"errors": ["SmartDataContext API not available"]}';
}
http_response_code($http_status);
return $response;
} else {
http_response_code(400);
}
} else {
http_response_code(400);
}
}
try {
if (array_key_exists('docs', $_GET)) {
echo context("/docs", null, null);
} else if (array_key_exists('openapi', $_GET)) {
echo context("/openapi", null, null);
} else if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['action'] === 'add-unstructured') {
echo handle_unstructured_add();
} else {
$content = file_get_contents('php://input');
$json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
Loading