2024-08-31 16:36:22 -04:00
|
|
|
<?php
|
|
|
|
|
2024-09-13 13:52:38 -04:00
|
|
|
namespace Socialbox;
|
|
|
|
|
2024-09-24 15:01:46 -04:00
|
|
|
use Exception;
|
2024-09-27 14:21:35 -04:00
|
|
|
use Socialbox\Classes\Configuration;
|
2024-09-13 13:52:38 -04:00
|
|
|
use Socialbox\Classes\RpcHandler;
|
2024-09-27 14:21:35 -04:00
|
|
|
use Socialbox\Classes\Utilities;
|
2024-09-13 13:52:38 -04:00
|
|
|
use Socialbox\Enums\StandardError;
|
|
|
|
use Socialbox\Enums\StandardMethods;
|
|
|
|
use Socialbox\Exceptions\RpcException;
|
2024-09-24 15:01:46 -04:00
|
|
|
use Socialbox\Exceptions\StandardException;
|
2024-08-31 16:36:22 -04:00
|
|
|
|
2024-08-31 17:11:25 -04:00
|
|
|
class Socialbox
|
2024-08-31 16:36:22 -04:00
|
|
|
{
|
2024-09-13 13:52:38 -04:00
|
|
|
public static function handleRpc(): void
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$clientRequest = RpcHandler::getClientRequest();
|
|
|
|
}
|
|
|
|
catch(RpcException $e)
|
|
|
|
{
|
|
|
|
http_response_code($e->getCode());
|
|
|
|
print($e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach($clientRequest->getRequests() as $rpcRequest)
|
|
|
|
{
|
|
|
|
$method = StandardMethods::tryFrom($rpcRequest->getMethod());
|
|
|
|
|
|
|
|
if($method === false)
|
|
|
|
{
|
2024-09-24 14:20:49 -04:00
|
|
|
$response = $rpcRequest->produceError(StandardError::RPC_METHOD_NOT_FOUND, 'The requested method does not exist');
|
2024-09-24 15:01:46 -04:00
|
|
|
}
|
2024-09-27 14:21:35 -04:00
|
|
|
else
|
2024-09-24 15:01:46 -04:00
|
|
|
{
|
2024-09-27 14:21:35 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$response = $method->execute($clientRequest, $rpcRequest);
|
|
|
|
}
|
|
|
|
catch(StandardException $e)
|
|
|
|
{
|
|
|
|
$response = $e->produceError($rpcRequest);
|
|
|
|
}
|
|
|
|
catch(Exception $e)
|
|
|
|
{
|
|
|
|
if(Configuration::getConfiguration()['security']['display_internal_exceptions'])
|
|
|
|
{
|
|
|
|
$response = $rpcRequest->produceError(StandardError::INTERNAL_SERVER_ERROR, Utilities::throwableToString($e));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$response = $rpcRequest->produceError(StandardError::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2024-09-24 15:01:46 -04:00
|
|
|
}
|
|
|
|
|
2024-09-13 13:52:38 -04:00
|
|
|
if($response !== null)
|
|
|
|
{
|
2024-09-27 14:21:35 -04:00
|
|
|
$results[] = $response->toArray();
|
2024-09-13 13:52:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 14:21:35 -04:00
|
|
|
if(count($results) == 0)
|
|
|
|
{
|
|
|
|
http_response_code(204);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($results) == 1)
|
2024-09-13 13:52:38 -04:00
|
|
|
{
|
2024-09-27 14:21:35 -04:00
|
|
|
print(json_encode($results[0]));
|
2024-09-13 13:52:38 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-08-31 16:36:22 -04:00
|
|
|
|
2024-09-27 14:21:35 -04:00
|
|
|
print(json_encode($results));
|
2024-09-13 13:52:38 -04:00
|
|
|
}
|
2024-08-31 16:36:22 -04:00
|
|
|
}
|