Get the Elgg URL segments
public getUrlSegments ( boolean $raw = false ) : string[] | ||
$raw | boolean | If true, the segments will not be HTML escaped |
return | string[] |
/** * Routes the request to a registered page handler * * This function triggers a plugin hook `'route', $identifier` so that plugins can * modify the routing or handle a request. * * @param \Elgg\Http\Request $request The request to handle. * @return boolean Whether the request was routed successfully. * @access private */ public function route(\Elgg\Http\Request $request) { $segments = $request->getUrlSegments(); if ($segments) { $identifier = array_shift($segments); } else { $identifier = ''; // this plugin hook is deprecated. Use elgg_register_page_handler() // to register for the '' (empty string) handler. // allow plugins to override the front page (return true to indicate // that the front page has been served) $result = _elgg_services()->hooks->trigger('index', 'system', null, false); if ($result === true) { elgg_deprecated_notice("The 'index', 'system' plugin has been deprecated. See elgg_front_page_handler()", 1.9); exit; } } // return false to stop processing the request (because you handled it) // return a new $result array if you want to route the request differently $result = array('identifier' => $identifier, 'handler' => $identifier, 'segments' => $segments); if ($this->timer) { $this->timer->begin(['build page']); } $result = $this->hooks->trigger('route', $identifier, $result, $result); if ($result === false) { return true; } if ($identifier != $result['identifier']) { $identifier = $result['identifier']; } else { if ($identifier != $result['handler']) { $identifier = $result['handler']; } } $segments = $result['segments']; $handled = false; ob_start(); if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) { $function = $this->handlers[$identifier]; $handled = call_user_func($function, $segments, $identifier); } $output = ob_get_clean(); $ajax_api = _elgg_services()->ajax; if ($ajax_api->isReady()) { $path = implode('/', $request->getUrlSegments()); $ajax_api->respondFromOutput($output, "path:{$path}"); return true; } echo $output; return $handled || headers_sent(); }
/** * Parses response type to be used as plugin hook type * @return string */ public function parseContext() { $segments = $this->request->getUrlSegments(); $identifier = array_shift($segments); switch ($identifier) { case 'ajax': $page = array_shift($segments); if ($page === 'view') { $view = implode('/', $segments); return "view:{$view}"; } else { if ($page === 'form') { $form = implode('/', $segments); return "form:{$form}"; } } array_unshift($segments, $page); break; case 'action': $action = implode('/', $segments); return "action:{$action}"; } array_unshift($segments, $identifier); $path = implode('/', $segments); return "path:{$path}"; }
/** * Handle request to /serve-icon handler * * @param bool $allow_removing_headers Alter PHP's global headers to allow caching * @return BinaryFileResponse */ public function handleServeIconRequest($allow_removing_headers = true) { $response = new Response(); $response->setExpires($this->getCurrentTime('-1 day')); $response->prepare($this->request); if ($allow_removing_headers) { // clear cache-boosting headers set by PHP session header_remove('Cache-Control'); header_remove('Pragma'); header_remove('Expires'); } $path = implode('/', $this->request->getUrlSegments()); if (!preg_match('~serve-icon/(\\d+)/(.*+)$~', $path, $m)) { return $response->setStatusCode(400)->setContent('Malformatted request URL'); } list(, $guid, $size) = $m; $entity = $this->entities->get($guid); if (!$entity instanceof \ElggEntity) { return $response->setStatusCode(404)->setContent('Item does not exist'); } $thumbnail = $entity->getIcon($size); if (!$thumbnail->exists()) { return $response->setStatusCode(404)->setContent('Icon does not exist'); } $if_none_match = $this->request->headers->get('if_none_match'); if (!empty($if_none_match)) { // strip mod_deflate suffixes $this->request->headers->set('if_none_match', str_replace('-gzip', '', $if_none_match)); } $filenameonfilestore = $thumbnail->getFilenameOnFilestore(); $last_updated = filemtime($filenameonfilestore); $etag = '"' . $last_updated . '"'; $response->setPrivate()->setEtag($etag)->setExpires($this->getCurrentTime('+1 day'))->setMaxAge(86400); if ($response->isNotModified($this->request)) { return $response; } $headers = ['Content-Type' => (new MimeTypeDetector())->getType($filenameonfilestore)]; $response = new BinaryFileResponse($filenameonfilestore, 200, $headers, false, 'inline'); $response->prepare($this->request); $response->setPrivate()->setEtag($etag)->setExpires($this->getCurrentTime('+1 day'))->setMaxAge(86400); return $response; }
/** * Routes the request to a registered page handler * * This function triggers a plugin hook `'route', $identifier` so that plugins can * modify the routing or handle a request. * * @param \Elgg\Http\Request $request The request to handle. * @return boolean Whether the request was routed successfully. * @access private */ public function route(\Elgg\Http\Request $request) { $segments = $request->getUrlSegments(); if ($segments) { $identifier = array_shift($segments); } else { $identifier = ''; // this plugin hook is deprecated. Use elgg_register_page_handler() // to register for the '' (empty string) handler. // allow plugins to override the front page (return true to indicate // that the front page has been served) $result = elgg_trigger_plugin_hook('index', 'system', null, false); if ($result === true) { elgg_deprecated_notice("The 'index', 'system' plugin has been deprecated. See elgg_front_page_handler()", 1.9); exit; } } // return false to stop processing the request (because you handled it) // return a new $result array if you want to route the request differently $result = array('identifier' => $identifier, 'handler' => $identifier, 'segments' => $segments); $result = $this->hooks->trigger('route', $identifier, null, $result); if ($result === false) { return true; } if ($identifier != $result['identifier']) { $identifier = $result['identifier']; } else { if ($identifier != $result['handler']) { $identifier = $result['handler']; } } $segments = $result['segments']; $handled = false; if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) { $function = $this->handlers[$identifier]; $handled = call_user_func($function, $segments, $identifier); } return $handled || headers_sent(); }
/** * Filter a request through the route:rewrite hook * * @param Request $request Elgg request * * @return Request * @access private */ public function allowRewrite(Request $request) { $segments = $request->getUrlSegments(); if ($segments) { $identifier = array_shift($segments); } else { $identifier = ''; } $old = array('identifier' => $identifier, 'segments' => $segments); $new = _elgg_services()->hooks->trigger('route:rewrite', $identifier, $old, $old); if ($new === $old) { return $request; } if (!isset($new['identifier']) || !isset($new['segments']) || !is_string($new['identifier']) || !is_array($new['segments'])) { throw new RuntimeException('rewrite_path handler returned invalid route data.'); } // rewrite request $segments = $new['segments']; array_unshift($segments, $new['identifier']); return $request->setUrlSegments($segments); }