socialbox-php/src/Socialbox/Socialbox.php

99 lines
3.8 KiB
PHP
Raw Normal View History

2024-08-31 16:36:22 -04:00
<?php
namespace Socialbox;
use Exception;
use Socialbox\Classes\Configuration;
2024-10-30 15:28:13 -04:00
use Socialbox\Classes\Logger;
use Socialbox\Classes\RpcHandler;
use Socialbox\Classes\Utilities;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\StandardMethods;
use Socialbox\Exceptions\RpcException;
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
{
/**
* Handles the RPC (Remote Procedure Call) requests by parsing the client request,
* executing the appropriate methods, and returning the responses.
*
* @return void
*/
public static function handleRpc(): void
{
try
{
$clientRequest = RpcHandler::getClientRequest();
}
catch(RpcException $e)
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->error('Failed to parse the client request', $e);
http_response_code($e->getCode());
return;
}
2024-10-30 15:28:13 -04:00
Logger::getLogger()->verbose(sprintf('Received %d RPC request(s) from %s', count($clientRequest->getRequests()), $_SERVER['REMOTE_ADDR']));
$results = [];
foreach($clientRequest->getRequests() as $rpcRequest)
{
$method = StandardMethods::tryFrom($rpcRequest->getMethod());
if($method === false)
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->warning('The requested method does not exist');
$response = $rpcRequest->produceError(StandardError::RPC_METHOD_NOT_FOUND, 'The requested method does not exist');
}
else
{
try
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->debug(sprintf('Processing RPC request for method %s', $rpcRequest->getMethod()));
$response = $method->execute($clientRequest, $rpcRequest);
Logger::getLogger()->debug(sprintf('%s method executed successfully', $rpcRequest->getMethod()));
}
catch(StandardException $e)
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->error('An error occurred while processing the RPC request', $e);
$response = $e->produceError($rpcRequest);
}
catch(Exception $e)
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->error('An internal error occurred while processing the RPC request', $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);
}
}
}
if($response !== null)
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->debug(sprintf('Producing response for method %s', $rpcRequest->getMethod()));
$results[] = $response->toArray();
}
}
if(count($results) == 0)
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->verbose('No results to return');
http_response_code(204);
return;
}
if(count($results) == 1)
{
2024-10-30 15:28:13 -04:00
Logger::getLogger()->verbose('Returning single result');
print(json_encode($results[0]));
return;
}
2024-08-31 16:36:22 -04:00
2024-10-30 15:28:13 -04:00
Logger::getLogger()->verbose('Returning multiple results');
print(json_encode($results));
}
2024-08-31 16:36:22 -04:00
}