<?php namespace SmartData; require_once('../bin/smartdata/SmartAPI.php'); require_once('../bin/smartdata/Config.php'); use SmartData\Exception\CustomException; function context($url, $domain, $content) { if (substr( $url, 0, 5 ) != "/docs" && substr( $url, 0, 8 ) != "/openapi") { $url = $url . "/$domain"; header('Content-Type: application/json'); } if (substr( $url, 0, 4 ) == "/get" || substr( $url, 0, 7 ) == "/update") { $url = $url . "/" . $content['smartDataContextId']; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, Config::config()::SMARTDATACONTEXT_API . "$url"); if (substr( $url, 0, 4 ) != "/get" && substr( $url, 0, 5 ) != "/docs" && substr( $url, 0, 8 ) != "/openapi") { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content)); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' )); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $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; } 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); // Validate certificate usage and domain usage $credentials = null; if(isset($json->credentials)) $credentials = Credentials::fromJson($json->credentials); $backend = new Backend($credentials); $json = json_decode($content, true, 512, JSON_BIGINT_AS_STRING); // Pass the request to the smartdatacontext backend and retrieve result echo context($json['command'], $backend->domainInUse(), $json['request']); } } catch (CustomException $e) { http_response_code($e->getHTTPCodeError()); header('X-Message: ' . $e->getMessage(), false); return false; } catch (\Exception $e) { http_response_code(HttpStatusCode::BAD_REQUEST); return false; }