Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created April 9, 2021 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwaidelich/6f39539fc5a42c6b44692b913835fa2c to your computer and use it in GitHub Desktop.
Save bwaidelich/6f39539fc5a42c6b44692b913835fa2c to your computer and use it in GitHub Desktop.
Neos Eel evaluator as XML-RPC service (just a crazy demo, don't use this in production!)
<?php
declare(strict_types=1);
namespace Some\Package;
use GuzzleHttp\Psr7\Response;
use Neos\Eel\Context;
use Neos\Eel\EelEvaluatorInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class EelRpcMiddleware implements MiddlewareInterface
{
private EelEvaluatorInterface $eelEvaluator;
public function __construct(EelEvaluatorInterface $eelEvaluator)
{
$this->eelEvaluator = $eelEvaluator;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
{
// FIXME better way to detect XML RPC calls?
if ($request->getHeaderLine('Content-Type') !== 'text/xml') {
return $next->handle($request);
}
$server = xmlrpc_server_create();
xmlrpc_server_register_method($server, 'eel', function(string $methodName, array $params) {
try {
return $this->eelEvaluator->evaluate($params[0], new Context($params[1] ?? []));
} catch (\Throwable $exception) {
return ['faultCode' => 1, 'faultString' => $exception->getMessage()];
}
});
$response = xmlrpc_server_call_method($server, $request->getBody()->getContents(), null);
xmlrpc_server_destroy($server);
return new Response(200, ['Content-Type' => 'Content-Type: text/xml'], $response);
}
}
Neos:
Flow:
http:
middlewares:
'EelRpc':
middleware: 'Some\Package\EelRpcMiddleware'
position: 'before routing'
@bwaidelich
Copy link
Author

bwaidelich commented Apr 9, 2021

DISCLAIMER: Don't use this example without modifications. Exposing the (whole) power of Eel to the client is unsafe!

Example request payload:

<methodCall>
    <methodName>eel</methodName>
    <params>
        <param>
            <value>
                <string>'your name is: ' + name</string>
            </value>
        </param>
        <param>
            <value>
                <struct>
                    <member>
                        <name>name</name>
                        <value>
                            <string>John</string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </params>
</methodCall>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment