3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
18 protected const METHOD_REGEX
= ';^[a-z][a-zA-Z0-9_]*$;';
21 * Open-ended object. Any public method from this object will be available during this session.
24 * @see \Civi\Pipe\PublicMethods
36 protected function onConnect(string $negotiationFlags): ?
string {
37 \CRM_Core_Session
::useFakeSession();
38 $this->methods
= new PublicMethods();
40 // Convention: Every negotiation-flag should produce exactly one output in the header line.
41 foreach (str_split($negotiationFlags) as $flag) {
44 $flags[$flag] = \CRM_Utils_System
::version();
48 $flags[$flag] = ['jsonrpc-2.0'];
52 $flags[$flag] = function_exists('authx_login') ?
['login'] : ['nologin'];
56 $this->setTrusted(TRUE);
57 $flags[$flag] = 'trusted';
61 $this->setTrusted(FALSE);
62 $flags[$flag] = 'untrusted';
66 // What flags might exist in the future? We don't know! Communicate that we don't know.
72 return json_encode(["Civi::pipe" => $flags]);
78 protected function onRequest(string $requestLine): ?
string {
79 return JsonRpc
::run($requestLine, function(string $method, array $params) {
80 $method = str_replace('.', '_', $method);
81 if (!preg_match(self
::METHOD_REGEX
, $method)) {
82 throw new \
InvalidArgumentException('Method not found', -32601);
85 if (!is_callable([$this->methods
, $method])) {
86 throw new \
InvalidArgumentException('Method not found', -32601);
89 return call_user_func([$this->methods
, $method], $this, $params);
96 protected function onException(string $requestLine, \Throwable
$t): ?
string {
97 $error = JsonRpc
::createResponseError([], $t);
98 return \
json_encode($error);
102 * @param bool $trusted
103 * @return PipeSession
105 public function setTrusted(bool $trusted): PipeSession
{
106 if ($this->trusted
!== NULL && $this->trusted
!== $trusted) {
107 throw new \
CRM_Core_Exception('Cannot modify PipeSession::$trusted after initialization');
109 $this->trusted
= $trusted;
116 public function isTrusted(): bool {
117 // If this gets called when the value is NULL, then you are doing it wrong.
118 return $this->trusted
;