Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRM_Utils_System::sendResponse(). Fix AssetBuilder's status-code on WP. #14468

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* @method static array synchronizeUsers() Create CRM contacts for all existing CMS users.
* @method static appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_coreResourceList.
* @method static alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_getAssetUrl.
* @method static sendResponse(\Psr\Http\Message\ResponseInterface $response) function to handle RepsoseInterface for delivering HTTP Responses.
*/
class CRM_Utils_System {

Expand Down Expand Up @@ -1861,4 +1862,12 @@ public static function createDefaultCrudLink($crudLinkSpec) {
return NULL;
}

/**
* Return an HTTP Response with appropriate content and status code set.
* @param \Psr\Http\Message\ResponseInterface $response
*/
public static function sendResponse(\Psr\Http\Message\ResponseInterface $response) {
$config = CRM_Core_Config::singleton()->userSystem->sendResponse($response);
}

}
14 changes: 14 additions & 0 deletions CRM/Utils/System/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,4 +945,18 @@ public function synchronizeUsers() {
return [];
}

/**
* Send an HTTP Response base on PSR HTTP RespnseInterface response.
*
* @param \Psr\Http\Message\ResponseInterface $response
*/
public function sendResponse(\Psr\Http\Message\ResponseInterface $response) {
http_response_code($response->getStatusCode());
foreach ($response->getHeaders() as $name => $values) {
CRM_Utils_System::setHttpHeader($name, implode(', ', (array) $values));
}
echo $response->getBody();
CRM_Utils_System::civiExit();
}

}
15 changes: 15 additions & 0 deletions CRM/Utils/System/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,4 +863,19 @@ public function synchronizeUsers() {
];
}

/**
* Send an HTTP Response base on PSR HTTP RespnseInterface response.
*
* @param \Psr\Http\Message\ResponseInterface $response
*/
public function sendResponse(\Psr\Http\Message\ResponseInterface $response) {
// use WordPress function status_header to ensure 404 response is sent
status_header($response->getStatusCode());
foreach ($response->getHeaders() as $name => $values) {
CRM_Utils_System::setHttpHeader($name, implode(', ', (array) $values));
}
echo $response->getBody();
CRM_Utils_System::civiExit();
}

}
12 changes: 1 addition & 11 deletions Civi/Core/AssetBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,7 @@ public function setCacheEnabled($cacheEnabled) {
public static function pageRun() {
// Beg your pardon, sir. Please may I have an HTTP response class instead?
$asset = self::pageRender($_GET);
if (function_exists('http_response_code')) {
// PHP 5.4+
http_response_code($asset['statusCode']);
}
else {
header('X-PHP-Response-Code: ' . $asset['statusCode'], TRUE, $asset['statusCode']);
}

header('Content-Type: ' . $asset['mimeType']);
echo $asset['content'];
\CRM_Utils_System::civiExit();
\CRM_Utils_System::sendResponse(new \GuzzleHttp\Psr7\Response($asset['statusCode'], ['Content-Type' => $asset['mimeType']], $asset['content']));
}

/**
Expand Down