Newer
Older
<?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);
$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);
if ($http_status == 0) {
# Could not connect to the smartdatacontext API
$http_status = 503; # Service Unavailable
$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;
}
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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);
}
}
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;
}