Elgg  Version master
Controller.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Ajax;
4 
7 use Elgg\Request;
8 
15 class Controller {
16 
24  public function __invoke(Request $request) {
25 
26  $type = (string) $request->getParam('type');
27  $segments = explode('/', (string) $request->getParam('segments'));
28  if (empty($segments)) {
29  return elgg_error_response('Ajax pagehandler called with invalid segments', REFERRER, ELGG_HTTP_BAD_REQUEST);
30  }
31 
32  if (elgg_extract(0, $segments) === 'admin') {
33  // protect admin views similar to all admin pages that are protected automatically in the admin_page_handler
35  }
36 
37  $view = '';
38  switch ($type) {
39  case 'view':
40  $view = implode('/', $segments);
41  break;
42  case 'form':
43  $view = 'forms/' . implode('/', $segments);
44  break;
45  default:
46  return elgg_error_response('Ajax pagehandler called with invalid segments', REFERRER, ELGG_HTTP_BAD_REQUEST);
47  }
48 
49  $ajax_api = _elgg_services()->ajax;
50  $allowed_views = $ajax_api->getViews();
51 
52  // cacheable views are always allowed
53  if (!in_array($view, $allowed_views) && !_elgg_services()->simpleCache->isCacheableView($view)) {
54  return elgg_error_response("Ajax view '{$view}' was not registered", REFERRER, ELGG_HTTP_FORBIDDEN);
55  }
56 
57  if (!elgg_view_exists($view)) {
58  return elgg_error_response("Ajax view '{$view}' was not found", REFERRER, ELGG_HTTP_NOT_FOUND);
59  }
60 
61  // pull out GET parameters through filter
62  $vars = [];
63  foreach ($request->getHttpRequest()->query->keys() as $name) {
65  }
66 
67  if (isset($vars['guid'])) {
68  $vars['entity'] = get_entity($vars['guid']);
69  }
70 
71  if (isset($vars['river_id'])) {
72  $vars['item'] = elgg_get_river_item_from_id($vars['river_id']);
73  }
74 
75  $content_type = '';
76  if ($type === 'view') {
78 
79  // Try to guess the mime-type
80  if (_elgg_services()->simpleCache->isCacheableView($view)) {
81  $file = _elgg_services()->views->findViewFile($view, elgg_get_viewtype());
82  $content_type = 'text/html';
83 
84  try {
85  $content_type = _elgg_services()->mimetype->getMimeType($file, $content_type);
86  } catch (InvalidArgumentException $e) {
87  // nothing for now
88  }
89  }
90  } else {
91  $action = implode('/', $segments);
93  }
94 
95  if ($content_type) {
96  elgg_set_http_header("Content-Type: {$content_type}");
97  }
98 
99  return elgg_ok_response($output);
100  }
101 }
$vars
Definition: theme.php:3
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
if(!empty($avatar) &&! $avatar->isValid()) elseif(empty($avatar)) if(! $owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
Definition: upload.php:39
$type
Definition: delete.php:21
catch(AuthenticationException|LoginException $e) if(elgg_is_xhr()) $output
Definition: login.php:86
Controller to handle /ajax requests.
Definition: Controller.php:15
__invoke(Request $request)
Respond to a request.
Definition: Controller.php:24
Exception thrown if an argument is not of the expected type.
Elgg HTTP request.
Definition: Request.php:17
Request container.
Definition: Request.php:12
const ELGG_HTTP_FORBIDDEN
Definition: constants.php:67
const ELGG_HTTP_NOT_FOUND
Definition: constants.php:68
const ELGG_HTTP_BAD_REQUEST
Definition: constants.php:64
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page.
Definition: constants.php:37
elgg_set_http_header(string $header, bool $replace=true)
Set a response HTTP header.
Definition: elgglib.php:36
_elgg_services()
Get the global service provider.
Definition: elgglib.php:343
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:246
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_admin_gatekeeper()
Used at the top of a page to mark it as admin only.
Definition: gatekeepers.php:64
HTTP response builder interface.
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:68
elgg_get_viewtype()
Return the current view type.
Definition: views.php:73
elgg_view_exists(string $view, string $viewtype='', bool $recurse=true)
Returns whether the specified view exists.
Definition: views.php:131
elgg_view_form(string $action, array $form_vars=[], array $body_vars=[])
Definition: views.php:1044
elgg_view(string $view, array $vars=[], string $viewtype='')
Return a parsed view.
Definition: views.php:156
$request
Definition: livesearch.php:12
elgg_ok_response($content='', string|array $message='', ?string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
elgg_error_response(string|array $message='', string $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
elgg_get_river_item_from_id(int $id)
Get river item from its ID.
Definition: river.php:130
$action
Definition: subscribe.php:11
$segments
Definition: admin.php:13