From 0876cfa468691334a08714bb4115f3343046897c Mon Sep 17 00:00:00 2001 From: Klaas Eikelboom Date: Wed, 25 Aug 2021 14:34:49 +0200 Subject: [PATCH 1/3] Add a connector that can be used with the ajax endpoint --- CMRF/Connection/AjaxCurl.php | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 CMRF/Connection/AjaxCurl.php diff --git a/CMRF/Connection/AjaxCurl.php b/CMRF/Connection/AjaxCurl.php new file mode 100755 index 0000000..62559b3 --- /dev/null +++ b/CMRF/Connection/AjaxCurl.php @@ -0,0 +1,77 @@ +getProfile(); + + $request = $this->getAPI3Params($call); + // $request['api_key'] = $profile['api_key']; + // $request['key'] = $profile['site_key']; + // $request['version'] = 3; + // $request['entity'] = $call->getEntity(); + // $request['action'] = $call->getAction(); + $post_data = "entity=" . $call->getEntity(); + $post_data .= "&action=" . $call->getAction(); + $post_data .= "&version=3"; + $post_data .= "&json=" . urlencode(json_encode($request)); + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); + curl_setopt($curl, CURLOPT_URL, $profile['url']); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curl, CURLOPT_SSLVERSION, 1); + curl_setopt($curl, CURLOPT_HTTPHEADER, [ + "X-Requested-With: XMLHttpRequest", + "X-Civi-Auth: Bearer {$profile['api_key']}", + "X-Civi-Key: {$profile['site_key']}" + ]); + + $response = curl_exec($curl); + if (curl_error($curl)){ + $call->setStatus(Call::STATUS_FAILED, curl_error($curl)); + return NULL; + } else { + $reply = json_decode($response, true); + if ($reply===NULL) { + $call->setStatus(Call::STATUS_FAILED, curl_error($curl)); + return NULL; + } else { + $status = Call::STATUS_DONE; + if (isset($reply['is_error']) && $reply['is_error']) { + $status = Call::STATUS_FAILED; + } + $call->setReply($reply, $status); + return $reply; + } + } + } +} From c1cf5f824ca35b53489c48e6b6009ad31bc588ac Mon Sep 17 00:00:00 2001 From: Klaas Eikelboom Date: Thu, 26 Aug 2021 17:26:22 +0200 Subject: [PATCH 2/3] Rename connector and document it --- CMRF/Connection/{AjaxCurl.php => CurlAuthX.php} | 11 ++++------- README.md | 12 ++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) rename CMRF/Connection/{AjaxCurl.php => CurlAuthX.php} (86%) create mode 100644 README.md diff --git a/CMRF/Connection/AjaxCurl.php b/CMRF/Connection/CurlAuthX.php similarity index 86% rename from CMRF/Connection/AjaxCurl.php rename to CMRF/Connection/CurlAuthX.php index 62559b3..5022ab3 100755 --- a/CMRF/Connection/AjaxCurl.php +++ b/CMRF/Connection/CurlAuthX.php @@ -2,6 +2,8 @@ /** * Remote CiviCRM connection based on CURL + * Uses the new CiviCRM auth extension. Authentication + * is done with X-Civi-Auth em X-Civi-Key headers * * @author Björn Endres, SYSTOPIA (endres@systopia.de) */ @@ -11,10 +13,10 @@ use CMRF\Core\Call as Call; use CMRF\Core\Connection as Connection; -class AjaxCurl extends Connection { +class CurlAuthX extends Connection { public function getType() { - return 'ajaxcurl'; + return 'curlauthx'; } public function isReady() { @@ -31,11 +33,6 @@ public function executeCall(Call $call) { $profile = $this->getProfile(); $request = $this->getAPI3Params($call); - // $request['api_key'] = $profile['api_key']; - // $request['key'] = $profile['site_key']; - // $request['version'] = 3; - // $request['entity'] = $call->getEntity(); - // $request['action'] = $call->getAction(); $post_data = "entity=" . $call->getEntity(); $post_data .= "&action=" . $call->getAction(); $post_data .= "&version=3"; diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ae818d --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +You can find the general documentation at + +### The CurlAuthX connector +In the latest versions of CiviCRM, the endpoint `https://my-civi.org/sites/all/modules/civicrm/extern/rest.php` is not supported anymore. It is replaced by a new extension `AuthX` that provides a lot of different authentication options. The CurlAuthX connector uses just one of them. + +The AuthX does not have a graphical interface (yet). You can configure it with the `cv` utility. + +* enable the new authx extension `cv ext:enable authx` +* choose the authentication method `cv ev 'Civi::settings()->set("authx_xheader_cred",["api_key"]);'` +* make sitekey mandatory `cv ev 'Civi::settings()->set("authx_guards",["site_key"]);'` +* refresh the cache `cv flush` + From 66caa0935b09005f53531e2a95b86e6d1ae70d9b Mon Sep 17 00:00:00 2001 From: Klaas Eikelboom Date: Thu, 26 Aug 2021 19:40:39 +0200 Subject: [PATCH 3/3] Add forgotten url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ae818d..74bd8e9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -You can find the general documentation at +You can find the general documentation at https://github.com/CiviMRF/documentation. ### The CurlAuthX connector In the latest versions of CiviCRM, the endpoint `https://my-civi.org/sites/all/modules/civicrm/extern/rest.php` is not supported anymore. It is replaced by a new extension `AuthX` that provides a lot of different authentication options. The CurlAuthX connector uses just one of them.