10 use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
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);
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);
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()) {
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();
const ELGG_JSON_ENCODING
Default JSON encoding.
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $error
HTTP response builder interface.
respondWithError($error, $status_code=ELGG_HTTP_BAD_REQUEST, array $headers=[])
Send error HTTP response.
if(!$entity->delete()) $forward_url
getStatusCode()
Returns status code.
isRedirection()
Check if response is redirection.
getContent()
Returns response body.
if($guid==elgg_get_logged_in_user_guid()) $name
elgg_view_resource($name, array $vars=[])
Render a resource view.
redirect($forward_url=REFERRER, $status_code=ELGG_HTTP_FOUND)
Prepares a redirect response.
isAction()
Check if the requested path is an action.
const ELGG_HTTP_SEE_OTHER
getHeaders($remove_existing=true)
Get headers set to apply to all responses.
isNotModified()
Check if response has been modified.
elgg_normalize_site_url($unsafe_url)
From untrusted input, get a site URL safe for forwarding.
stringify($content= '')
Stringify/serialize response data.
prepareResponse($content= '', $status=200, array $headers=array())
Creates an HTTP response.
getSentResponse()
Returns a response that was sent to the client.
elgg_trigger_before_event($event, $object_type, $object=null)
Trigger a "Before event" indicating a process is about to begin.
Models the Ajax API service.
const ELGG_HTTP_BAD_REQUEST
HTTP response transport interface.
isServerError()
Check if response is server error.
elgg ajax
Wrapper function for jQuery.ajax which ensures that the url being called is relative to the elgg site...
isXhr()
Check if the request is an XmlHttpRequest.
isSuccessful()
Check if response is successful.
wrapLegacyAjaxResponse($content= '', $forward_url=REFERRER)
Wraps content for compability with legacy Elgg ajax calls.
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
setHeader($name, $value, $replace=true)
Sets headers to apply to all responses being sent.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
isClientError()
Check if response is client error.
const ELGG_HTTP_NOT_MODIFIED
setForwardURL($forward_url=REFERRER)
Sets redirect URL.
$content
Set robots.txt action.
send(Response $response)
Send a response.
parseContext()
Parses response type to be used as plugin hook type.
elgg_trigger_after_event($event, $object_type, $object=null)
Trigger an "After event" indicating a process has finished.
setHeaders(array $headers=[])
Sets additional response headers.
setContent($content= '')
Sets response body.
getHeaders()
Returns additional response headers.
prepareRedirectResponse($url, $status=302, array $headers=array())
Creates a redirect response.
respondFromContent($content= '', $status_code=ELGG_HTTP_OK, array $headers=[])
Send OK response.
respond(ResponseBuilder $response)
Send HTTP response.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
normalize($content= '')
Normalizes content into serializable data by walking through arrays and objectifying Elgg entities...
__construct(Request $request, PluginHooksService $hooks, AjaxService $ajax, ResponseTransport $transport)
Constructor.
getForwardURL()
Returns redirect URL.
setStatusCode($status_code=ELGG_HTTP_OK)
Sets response HTTP status code.