11 use Symfony\Component\HttpFoundation\JsonResponse;
39 $message_filter = [$this,
'prepareResponse'];
49 $version = $this->request->headers->get(
'X-Elgg-Ajax-API');
63 return !$this->response_sent && $this->isAjax2Request();
74 if (!is_string($string)) {
98 $api_response->setData(
$output);
100 $api_response->setData((
object)
$output);
102 $api_response->setData((
object) [
'value' =>
$output]);
105 $api_response = $this->filterApiResponse($api_response, $event_type);
106 $response = $this->buildHttpResponse($api_response);
108 $this->response_sent =
true;
121 $api_response = $this->filterApiResponse($api_response, $event_type);
122 $response = $this->buildHttpResponse($api_response);
124 $this->response_sent =
true;
137 $response =
new JsonResponse([
'error' => $msg], $status);
140 $this->msgs->dumpRegister();
142 $this->response_sent =
true;
156 $api_response->setTtl($this->request->getParam(
'elgg_response_ttl', 0, false));
159 $event_name = AjaxResponse::RESPONSE_EVENT;
160 $api_response = $this->events->triggerResults($event_name, $event_type, [], $api_response);
162 throw new RuntimeException(
"The value returned by event [{$event_name}, {$event_type}] was not an ApiResponse");
166 return $api_response;
177 private function buildHttpResponse(AjaxResponse $api_response): JsonResponse {
178 if ($api_response->isCancelled()) {
179 return new JsonResponse([
'error' =>
'The response was cancelled'], 400);
184 $ttl = $api_response->
getTtl();
187 $response->headers->remove(
'Cache-Control');
192 $response->headers->set(
'Expires', gmdate(
'D, d M Y H:i:s \G\M\T', time() + $ttl));
212 if ($this->request->getParam(
'elgg_fetch_messages',
true)) {
223 if ($this->request->getParam(
'elgg_fetch_deps',
true)) {
224 $response->getData()->_elgg_deps = $this->esm->getImports();
238 $this->allowed_views[
$view] = true;
249 unset($this->allowed_views[
$view]);
258 return array_keys($this->allowed_views);
if(!empty($avatar) &&! $avatar->isValid()) elseif(empty($avatar)) if(! $owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Models the Ajax API service.
respondFromOutput($output, string $event_type='', bool $try_decode=true)
Send a JSON HTTP response with the given output.
respondFromApiResponse(AjaxResponse $api_response, string $event_type='')
Send a JSON HTTP response based on the given API response.
isAjax2Request()
Did the request come from the elgg/Ajax module?
registerView(string $view)
Register a view to be available for ajax calls.
prepareResponse(\Elgg\Event $event)
Prepare the response with additional metadata, like system messages and required ES modules.
isReady()
Is the service ready to respond to the request?
respondWithError(string $msg='', int $status=400)
Send a JSON HTTP 400 response.
getViews()
Returns an array of views allowed for ajax calls.
decodeJson($string)
Attempt to JSON decode the given string.
unregisterView(string $view)
Unregister a view for ajax calls.
__construct(protected EventsService $events, protected SystemMessagesService $msgs, protected Request $request, protected ESMService $esm)
Constructor.
Models an event passed to event handlers.
Exception thrown if an error which can only be found on runtime occurs.
Keeps track of ES modules.
if($email instanceof \Elgg\Email) $object
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
_elgg_services()
Get the global service provider.
getTtl()
Get the max-age for client caching.
getData()
Get the response data, which will be a stdClass object with property "value".
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.