8 use InvalidArgumentException;
10 use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
11 use Symfony\Component\HttpFoundation\Response;
12 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
45 private $response_sent =
false;
61 $this->request = $request;
62 $this->hooks = $hooks;
64 $this->transport = $transport;
65 $this->headers =
new ResponseHeaderBag();
89 $headers_list = headers_list();
90 foreach ($headers_list as
$header) {
91 if (stripos(
$header,
'HTTP/1.1') !==
false) {
97 if ($remove_existing) {
102 return $this->headers;
115 $header_bag->add($headers);
116 $response =
new Response(
$content, $status, $header_bag->all());
117 $response->prepare($this->request);
132 $header_bag->add($headers);
133 $response =
new SymfonyRedirectResponse(
$url, $status, $header_bag->all());
134 $response->prepare($this->request);
144 public function send(Response $response) {
146 if ($this->response_sent) {
147 if ($this->response_sent !== $response) {
148 _elgg_services()->logger->error(
'Unable to send the following response: ' . PHP_EOL
149 . (
string) $response . PHP_EOL
150 .
'because another response has already been sent: ' . PHP_EOL
151 . (
string) $this->response_sent);
158 if (!$this->transport->send($response)) {
163 $this->response_sent = $response;
166 return $this->response_sent;
174 return $this->response_sent;
187 $response = $this->hooks->trigger(
'response', $response_type, $response, $response);
190 .
"return an instanceof " . ResponseBuilder::class);
197 $is_xhr = $this->request->isXmlHttpRequest();
200 if (0 === strpos($response_type,
'action:')) {
210 $response->
setForwardURL($this->request->headers->get(
'Referer'));
221 if ($is_xhr && $is_action && !$this->ajax->isAjax2Request()) {
227 if ($is_xhr && $is_action) {
230 $headers[
'Content-Type'] =
'application/json; charset=UTF-8';
240 return $this->
redirect($redirect_url, $status_code);
243 if ($this->ajax->isReady() && $response->
isSuccessful()) {
264 if ($this->ajax->isReady()) {
265 return $this->
send($this->ajax->respondWithError(
$error, $status_code));
268 if ($this->
isXhr()) {
282 $forward_reason = (string) $status_code;
286 if ($this->response_sent) {
288 return $this->response_sent;
291 $params[
'type'] = $forward_reason;
310 if ($this->ajax->isReady()) {
313 return $this->
send($this->ajax->respondFromOutput(
$content, $hook_type));
338 'system_messages' => [
349 $system_messages =
_elgg_services()->systemMessages->dumpRegister();
351 if (isset($system_messages[
'success'])) {
352 $params[
'system_messages'][
'success'] = $system_messages[
'success'];
355 if (isset($system_messages[
'error'])) {
356 $params[
'system_messages'][
'error'] = $system_messages[
'error'];
361 list($service,
$name) = explode(
':', $response_type);
382 $secure_referrer =
function () {
383 $unsafe_url = $this->request->headers->get(
'Referer');
385 if ($safe_url !==
false) {
395 if (!preg_match(
'/^(http|https|ftp|sftp|ftps):\/\//',
$forward_url)) {
412 'location' => $location,
415 $forward_reason = (string) $status_code;
419 if ($this->response_sent) {
422 return $this->response_sent;
435 switch ($status_code) {
443 case 'walled_garden':
445 $status_code = (int) $status_code;
446 if (!$status_code || $status_code < 100 || $status_code > 599) {
452 if ($this->
isXhr()) {
453 if ($status_code < 100 || ($status_code >= 300 && $status_code <= 399) || $status_code > 599) {
459 if (!$this->
isAction() && !$this->ajax->isAjax2Request()) {
467 $headers = $response->getHeaders();
468 $headers[
'Content-Type'] =
'application/json; charset=UTF-8';
469 $response->setHeaders($headers);
470 return $this->
respond($response);
475 if (!is_int($status_code) || $status_code < 300 || $status_code > 399) {
481 if ($response->isRedirection()) {
484 return $this->
respond($response);
493 $segments = $this->request->getUrlSegments();
495 $identifier = array_shift($segments);
496 switch ($identifier) {
498 $page = array_shift($segments);
499 if ($page ===
'view') {
500 $view = implode(
'/', $segments);
502 }
else if ($page ===
'form') {
503 $form = implode(
'/', $segments);
506 array_unshift($segments, $page);
510 $action = implode(
'/', $segments);
511 return "action:$action";
514 array_unshift($segments, $identifier);
515 $path = implode(
'/', $segments);
524 return $this->request->isXmlHttpRequest();
$content
Set robots.txt action.
if(! $owner||!($owner instanceof ElggUser)||! $owner->canEdit()) $error
if($guid==elgg_get_logged_in_user_guid()) $name
ui datepicker calendar ui state default
Models the Ajax API service.
getHeaders($remove_existing=true)
Get headers set to apply to all responses.
prepareResponse($content='', $status=200, array $headers=array())
Creates an HTTP response.
stringify($content='')
Stringify/serialize response data.
redirect($forward_url=REFERRER, $status_code=ELGG_HTTP_FOUND)
Prepares a redirect response.
setHeader($name, $value, $replace=true)
Sets headers to apply to all responses being sent.
wrapLegacyAjaxResponse($content='', $forward_url=REFERRER)
Wraps content for compability with legacy Elgg ajax calls.
parseContext()
Parses response type to be used as plugin hook type.
getSentResponse()
Returns a response that was sent to the client.
normalize($content='')
Normalizes content into serializable data by walking through arrays and objectifying Elgg entities.
respondFromContent($content='', $status_code=ELGG_HTTP_OK, array $headers=[])
Send OK response.
respondWithError($error, $status_code=ELGG_HTTP_BAD_REQUEST, array $headers=[])
Send error HTTP response.
__construct(Request $request, PluginHooksService $hooks, AjaxService $ajax, ResponseTransport $transport)
Constructor.
send(Response $response)
Send a response.
isXhr()
Check if the request is an XmlHttpRequest.
isAction()
Check if the requested path is an action.
prepareRedirectResponse($url, $status=302, array $headers=array())
Creates a redirect response.
respond(ResponseBuilder $response)
Send HTTP response.
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
const ELGG_JSON_ENCODING
Default JSON encoding.
elgg_trigger_after_event($event, $object_type, $object=null)
Trigger an "After event" indicating a process has finished.
const ELGG_HTTP_NOT_MODIFIED
const ELGG_HTTP_SEE_OTHER
elgg_trigger_before_event($event, $object_type, $object=null)
Trigger a "Before event" indicating a process is about to begin.
const ELGG_HTTP_BAD_REQUEST
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
if(! $entity->delete()) $forward_url
HTTP response builder interface.
setStatusCode($status_code=ELGG_HTTP_OK)
Sets response HTTP status code.
isRedirection()
Check if response is redirection.
setHeaders(array $headers=[])
Sets additional response headers.
isSuccessful()
Check if response is successful.
getContent()
Returns response body.
setForwardURL($forward_url=REFERRER)
Sets redirect URL.
getStatusCode()
Returns status code.
setContent($content='')
Sets response body.
getHeaders()
Returns additional response headers.
getForwardURL()
Returns redirect URL.
isNotModified()
Check if response has been modified.
isServerError()
Check if response is server error.
isClientError()
Check if response is client error.
HTTP response transport interface.
elgg_view_resource($name, array $vars=[])
Render a resource view.
elgg_normalize_site_url($unsafe_url)
From untrusted input, get a site URL safe for forwarding.