Skip to content

Commit

Permalink
Merge pull request #15475 from mecachisenros/externUrl
Browse files Browse the repository at this point in the history
dev/core#1100 Add externUrl method and civicrm_alterExternUrl hook
  • Loading branch information
seamuslee001 authored Jan 9, 2020
2 parents 87dab41 + 095e7f1 commit c77f9b1
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CRM/Contribute/Form/ContributionPage/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function preProcess() {

$this->assign('cpageId', $this->_id);

$this->assign('widgetExternUrl', CRM_Utils_System::externUrl('extern/widget', "cpageId={$this->_id}&widgetId={$this->_widget->id}&format=3"));

$config = CRM_Core_Config::singleton();
$title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
$this->_id,
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Payment/PaymentExpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function doTransferCheckout(&$params, $component) {
CRM_Core_Error::fatal(ts('Component is invalid'));
}

$url = $config->userFrameworkResourceURL . "extern/pxIPN.php";
$url = CRM_Utils_System::externUrl('extern/pxIPN');

if ($component == 'event') {
$cancelURL = CRM_Utils_System::url('civicrm/event/register',
Expand Down
17 changes: 1 addition & 16 deletions CRM/Cxn/BAO/Cxn.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,7 @@ class CRM_Cxn_BAO_Cxn extends CRM_Cxn_DAO_Cxn {
* @return string
*/
public static function getSiteCallbackUrl() {
$config = CRM_Core_Config::singleton();

if (preg_match('/^(http|https):/', $config->resourceBase)) {
$civiUrl = $config->resourceBase;
}
else {
$civiUrl = rtrim(CRM_Utils_System::baseURL(), '/') . '/' . ltrim($config->resourceBase, '/');
}

// In practice, this may not be necessary, but we want to prevent
// edge-cases that downgrade security-level below system policy.
if (Civi::settings()->get('enableSSL')) {
$civiUrl = preg_replace('/^http:/', 'https:', $civiUrl);
}

return rtrim($civiUrl, '/') . '/extern/cxn.php';
return CRM_Utils_System::externUrl('extern/cxn', NULL, NULL, TRUE, TRUE);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions CRM/Mailing/BAO/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,8 @@ public function compose(

// push the tracking url on to the html email if necessary
if ($this->open_tracking && $html) {
array_push($html, "\n" . '<img src="' . $config->userFrameworkResourceURL .
"extern/open.php?q=$event_queue_id\" width='1' height='1' alt='' border='0'>"
array_push($html, "\n" . '<img src="' . CRM_Utils_System::externUrl('extern/open', "q=$event_queue_id")
. '" width="1" height="1" alt="" border="0">'
);
}

Expand Down
5 changes: 2 additions & 3 deletions CRM/Mailing/BAO/TrackableURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static function getTrackerURL($url, $mailing_id, $queue_id) {
else {

$hrefExists = FALSE;
$config = CRM_Core_Config::singleton();

$tracker = new CRM_Mailing_BAO_TrackableURL();
if (preg_match('/^href/i', $url)) {
Expand All @@ -73,11 +72,11 @@ public static function getTrackerURL($url, $mailing_id, $queue_id) {
}
$id = $tracker->id;

$redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id";
$redirect = CRM_Utils_System::externUrl('extern/url', "u=$id");
$urlCache[$mailing_id . $url] = $redirect;
}

$returnUrl = "{$urlCache[$mailing_id . $url]}&qid={$queue_id}";
$returnUrl = CRM_Utils_System::externUrl('extern/url', "u=$id&qid=$queue_id");

if ($hrefExists) {
$returnUrl = "href='{$returnUrl}' rel='nofollow'";
Expand Down
35 changes: 35 additions & 0 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,41 @@ public static function url(
return $url;
}

/**
* Generates an extern url.
*
* @param string $path
* The extern path, such as "extern/url".
* @param string $query
* A query string to append to the link.
* @param string $fragment
* A fragment identifier (named anchor) to append to the link.
* @param bool $absolute
* Whether to force the output to be an absolute link (beginning with a
* URI-scheme such as 'http:').
* @param bool $isSSL
* NULL to autodetect. TRUE to force to SSL.
*/
public static function externUrl($path = NULL, $query = NULL, $fragment = NULL, $absolute = TRUE, $isSSL = NULL) {
$query = self::makeQueryString($query);

$url = Civi::paths()->getUrl("[civicrm.root]/{$path}.php", $absolute ? 'absolute' : 'relative', $isSSL)
. ($query ? "?$query" : "")
. ($fragment ? "#$fragment" : "");

$parsedUrl = CRM_Utils_Url::parseUrl($url);
$event = \Civi\Core\Event\GenericHookEvent::create([
'url' => &$parsedUrl,
'path' => $path,
'query' => $query,
'fragment' => $fragment,
'absolute' => $absolute,
'isSSL' => $isSSL,
]);
Civi::service('dispatcher')->dispatch('hook_civicrm_alterExternUrl', $event);
return CRM_Utils_Url::unparseUrl($event->url);
}

/**
* Path of the current page e.g. 'civicrm/contact/view'
*
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Contribute/Page/Widget.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,4 @@ function onReady( ) {
}
</script>
{/literal}
<script type="text/javascript" src="{$config->userFrameworkResourceURL}/extern/widget.php?cpageId={$cpageId}&widgetId={$widget_id}&format=3"></script>
<script type="text/javascript" src="{$widgetExternUrl}"></script>
72 changes: 72 additions & 0 deletions tests/phpunit/CRM/Utils/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,78 @@ public function getURLs() {
];
}

/**
* Test extern url.
*/
public function testExternUrl() {
$siteKey = mt_rand();
$apiKey = mt_rand();
$restUrl = CRM_Utils_System::externUrl('extern/rest', "entity=Contact&action=get&key=$siteKey&api_key=$apiKey");
$this->assertContains('extern/rest.php', $restUrl);
$this->assertContains('?', $restUrl);
$this->assertContains('entity=Contact', $restUrl);
$this->assertContains('action=get', $restUrl);
$this->assertContains("key=$siteKey", $restUrl);
$this->assertContains("api_key=$apiKey", $restUrl);
}

/**
* Test the alterExternUrl hook.
*
* @param string $path
* @param array $expected
*
* @dataProvider getExternURLs
*/
public function testAlterExternUrlHook($path, $expected) {
Civi::service('dispatcher')->addListener('hook_civicrm_alterExternUrl', [$this, 'hook_civicrm_alterExternUrl']);
$externUrl = CRM_Utils_System::externUrl($path, $expected['query']);
$this->assertContains('path/altered/by/hook', $externUrl, 'Hook failed to alter URL path');
$this->assertContains($expected['query'] . '&thisWas=alteredByHook', $externUrl, 'Hook failed to alter URL query');
}

/**
* Hook for alterExternUrl.
*
* @param \Civi\Core\Event\GenericHookEvent $event
* @param string $hookName
*/
public function hook_civicrm_alterExternUrl(\Civi\Core\Event\GenericHookEvent $event, $hookName) {
$this->assertEquals('hook_civicrm_alterExternUrl', $hookName);
$this->assertTrue($event->hasField('url'));
$this->assertTrue($event->hasField('path'));
$this->assertTrue($event->hasField('query'));
$this->assertTrue($event->hasField('fragment'));
$this->assertTrue($event->hasField('absolute'));
$this->assertTrue($event->hasField('isSSL'));
$event->url = $event->url->withPath('path/altered/by/hook');
$event->url = $event->url->withQuery($event->query . '&thisWas=alteredByHook');
}

/**
* Get extern url params for testing.
*
* @return array
*/
public function getExternURLs() {
return [
[
'extern/url',
[
'path' => 'extern/url',
'query' => 'u=1&qid=1',
],
],
[
'extern/open',
[
'path' => 'extern/open',
'query' => 'q=1',
],
],
];
}

/**
* Demonstrate the, um, "flexibility" of isNull
*/
Expand Down

0 comments on commit c77f9b1

Please sign in to comment.