48 private $response_sent =
false;
70 $this->request = $request;
71 $this->hooks = $hooks;
73 $this->transport = $transport;
74 $this->events = $events;
76 $this->headers =
new ResponseHeaderBag();
100 if (!$this->events->trigger(
'init:cookie', $cookie->name, $cookie)) {
104 $symfony_cookie =
new Cookie(
114 $this->headers->setCookie($symfony_cookie);
127 $headers_list = headers_list();
128 foreach ($headers_list as
$header) {
129 if (stripos($header,
'HTTP/1.1') !==
false) {
135 if ($remove_existing) {
136 header_remove(
$name);
140 return $this->headers;
155 $header_bag->add($headers);
157 $response =
new Response(
$content, $status, $header_bag->all());
159 return $this->finalizeResponsePreparation($response, $header_bag);
174 $header_bag->add($headers);
178 return $this->finalizeResponsePreparation($response, $header_bag);
193 $header_bag->add($headers);
201 $header_bag->remove(
'Content-Type');
203 $response =
new JsonResponse(
$content, $status, $header_bag->all());
205 return $this->finalizeResponsePreparation($response, $header_bag);
217 private function finalizeResponsePreparation(Response $response, ResponseHeaderBag $headers) {
219 foreach ($headers->getCookies() as $cookie) {
220 $response->headers->setCookie($cookie);
223 $response->prepare($this->request);
234 public function send(Response $response) {
236 if ($this->response_sent) {
237 if ($this->response_sent !== $response) {
238 _elgg_services()->logger->error(
'Unable to send the following response: ' . PHP_EOL
239 . (
string) $response . PHP_EOL
240 .
'because another response has already been sent: ' . PHP_EOL
241 . (
string) $this->response_sent);
244 if (!$this->events->triggerBefore(
'send',
'http_response', $response)) {
248 $request = $this->request;
249 $method = $request->getRealMethod() ? :
'GET';
250 $path = $request->getElggPath();
252 _elgg_services()->logger->notice(
"Responding to {$method} {$path}");
253 if (!$this->transport->send($response)) {
257 $this->events->triggerAfter(
'send',
'http_response', $response);
258 $this->response_sent = $response;
263 return $this->response_sent;
272 return $this->response_sent;
286 $response = $this->hooks->trigger(
'response', $response_type, $response, $response);
296 $is_xhr = $this->request->isXmlHttpRequest();
299 if (0 === strpos($response_type,
'action:')) {
309 $response->
setForwardURL($this->request->headers->get(
'Referer'));
320 if ($is_xhr && ($is_action || $this->
ajax->isAjax2Request())) {
321 if (!$this->
ajax->isAjax2Request()) {
328 $headers[
'Content-Type'] =
'application/json; charset=UTF-8';
331 if ($response->
isOk()) {
342 return $this->
redirect($redirect_url, $status_code);
366 if ($this->
ajax->isReady()) {
367 return $this->
send($this->
ajax->respondWithError(
$error, $status_code));
370 if ($this->
isXhr()) {
384 $forward_reason = (string) $status_code;
388 if ($this->response_sent) {
390 return $this->response_sent;
394 $params[
'type'] = $forward_reason;
421 if ($this->
ajax->isReady()) {
439 if (!$this->
ajax->isAjax2Request()) {
479 'system_messages' => [
490 $system_messages =
_elgg_services()->systemMessages->dumpRegister();
492 if (isset($system_messages[
'success'])) {
493 $params[
'system_messages'][
'success'] = $system_messages[
'success'];
496 if (isset($system_messages[
'error'])) {
497 $params[
'system_messages'][
'error'] = $system_messages[
'error'];
502 list($service,
$name) = explode(
':', $response_type);
535 $forward_reason = (string) $status_code;
539 if ($this->response_sent) {
542 return $this->response_sent;
555 switch ($status_code) {
563 case 'walled_garden':
565 $status_code = (int) $status_code;
566 if (!$status_code || $status_code < 100 || $status_code > 599) {
572 if ($this->
isXhr()) {
573 if ($status_code < 100 || ($status_code >= 300 && $status_code <= 399) || $status_code > 599) {
579 if (!$this->
isAction() && !$this->
ajax->isAjax2Request()) {
587 $headers = $response->getHeaders();
588 $headers[
'Content-Type'] =
'application/json; charset=UTF-8';
589 $response->setHeaders($headers);
590 return $this->
respond($response);
595 if (!is_int($status_code) || $status_code < 300 || $status_code > 399) {
601 if ($response->isRedirection()) {
604 return $this->
respond($response);
613 $segments = $this->request->getUrlSegments();
615 $identifier = array_shift($segments);
616 switch ($identifier) {
618 $page = array_shift($segments);
619 if (
$page ===
'view') {
620 $view = implode(
'/', $segments);
622 }
else if (
$page ===
'form') {
623 $form = implode(
'/', $segments);
626 array_unshift($segments,
$page);
630 $action = implode(
'/', $segments);
631 return "action:$action";
634 array_unshift($segments, $identifier);
635 $path = implode(
'/', $segments);
644 return $this->request->isXmlHttpRequest();
704 $this->transport = $transport;
713 $unsafe_url = $this->request->headers->get(
'Referer');
715 if ($safe_url !==
false) {
731 if (!preg_match(
'/^(http|https|ftp|sftp|ftps):\/\//',
$url)) {
750 if ($session->isStarted()) {
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
HTTP response builder interface.
respondWithError($error, $status_code=ELGG_HTTP_BAD_REQUEST, array $headers=[])
Send error HTTP response.
if(!$user||!$user->canDelete()) $name
if(!$entity->delete()) $forward_url
getStatusCode()
Returns status code.
$params
Saves global plugin settings.
isRedirection()
Check if response is redirection.
setCookie(\ElggCookie $cookie)
Set a cookie, but allow plugins to customize it first.
getContent()
Returns response body.
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.
getHeaders($remove_existing=true)
Get headers set to apply to all responses.
elgg_get_session()
Gets Elgg's session object.
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.
isOk()
Check if response is OK.
catch(LoginException $e) if($request->isXhr()) $output
getSentResponse()
Returns a response that was sent to the client.
Models the Ajax API service.
wrapAjaxResponse($content= '', $forward_url=null)
Wraps response content in an Ajax2 compatible format.
HTTP response transport interface.
isServerError()
Check if response is server error.
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) if(!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) $view
elgg ajax
Wrapper function for jQuery.ajax which ensures that the url being called is relative to the elgg site...
__construct(Request $request, PluginHooksService $hooks, AjaxService $ajax, ResponseTransport $transport, EventsService $events)
Constructor.
setTransport(ResponseTransport $transport)
Replaces response transport.
prepareRedirectResponse($url, $status=302, array $headers=[])
Creates a redirect response.
isXhr()
Check if the request is an XmlHttpRequest.
const ELGG_JSON_ENCODING
Default JSON encoding.
isSuccessful()
Check if response is successful.
wrapLegacyAjaxResponse($content= '', $forward_url=REFERRER)
Wraps content for compability with legacy Elgg ajax calls.
setHeader($name, $value, $replace=true)
Sets headers to apply to all responses being sent.
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
getSiteRefererUrl()
Ensures the referer header is a site url.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
const ELGG_HTTP_NOT_MODIFIED
const ELGG_HTTP_BAD_REQUEST
isClientError()
Check if response is client error.
makeSecureForwardUrl($url)
Ensure the url has a valid protocol for browser use.
prepareJsonResponse($content= '', $status=200, array $headers=[])
Creates an JSON response.
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_services()
Get the global service provider.
setHeaders(array $headers=[])
Sets additional response headers.
const ELGG_HTTP_SEE_OTHER
$form
List all unvalidated users in the admin area.
setContent($content= '')
Sets response body.
getHeaders()
Returns additional response headers.
respondFromContent($content= '', $status_code=ELGG_HTTP_OK, array $headers=[])
Send OK response.
respond(ResponseBuilder $response)
Send HTTP response.
closeSession()
Closes the session.
prepareResponse($content= '', $status=200, array $headers=[])
Creates an HTTP response.
normalize($content= '')
Normalizes content into serializable data by walking through arrays and objectifying Elgg entities...
getForwardURL()
Returns redirect URL.
setStatusCode($status_code=ELGG_HTTP_OK)
Sets response HTTP status code.