File manager - Edit - /home/opticamezl/www/newok/Router.tar
Back
SiteRouterAwareTrait.php 0000644 00000002321 15173161127 0011350 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Defines the trait for a Site Router Aware Class. * * @since 4.2.0 */ trait SiteRouterAwareTrait { /** * @var SiteRouter * @since 4.2.0 */ private $router; /** * Get the site router. * * @return SiteRouter * * @since 4.2.0 * * @throws \UnexpectedValueException May be thrown if the router has not been set. */ public function getSiteRouter(): SiteRouter { if ($this->router) { return $this->router; } throw new \UnexpectedValueException('SiteRouter not set in ' . __CLASS__); } /** * Set the router to use. * * @param SiteRouter $router The router to use. * * @return void * * @since 4.2.0 */ public function setSiteRouter(SiteRouter $router): void { $this->router = $router; } } AdministratorRouter.php 0000644 00000002620 15173161127 0011302 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2007 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router; use Joomla\CMS\Uri\Uri; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Class to create and parse routes * * @since 1.5 */ class AdministratorRouter extends Router { /** * Function to convert a route to an internal URI. * * @param Uri &$uri The uri. * @param bool $setVars Set the parsed data in the internal * storage for current-request-URLs * * @return array * * @since 1.5 */ public function parse(&$uri, $setVars = false) { return []; } /** * Function to convert an internal URI to a route * * @param string $url The internal URL * * @return Uri The absolute search engine friendly URL * * @since 1.5 */ public function build($url) { // Create the URI object $uri = parent::build($url); // Get the path data $route = $uri->getPath(); // Add basepath to the uri $uri->setPath(Uri::root(true) . '/' . basename(JPATH_ADMINISTRATOR) . '/' . $route); return $uri; } } Router.php 0000644 00000030403 15173161127 0006541 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2007 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\Router\Exception\RouteNotFoundException; use Joomla\CMS\Uri\Uri; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Class to create and parse routes * * @since 1.5 */ class Router { /** * Mask for the before process stage * * @var string * @since 3.4 */ public const PROCESS_BEFORE = 'preprocess'; /** * Mask for the during process stage * * @var string * @since 3.4 */ public const PROCESS_DURING = ''; /** * Mask for the after process stage * * @var string * @since 3.4 */ public const PROCESS_AFTER = 'postprocess'; /** * An array of variables * * @var array * @since 1.5 */ protected $vars = []; /** * An array of rules * * @var array * @since 1.5 */ protected $rules = [ 'buildpreprocess' => [], 'build' => [], 'buildpostprocess' => [], 'parsepreprocess' => [], 'parse' => [], 'parsepostprocess' => [], ]; /** * Caching of processed URIs * * @var array * @since 3.3 */ protected $cache = []; /** * Router instances container. * * @var Router[] * @since 1.7 */ protected static $instances = []; /** * Returns the global Router object, only creating it if it * doesn't already exist. * * @param string $client The name of the client * @param array $options An associative array of options * * @return Router A Router object. * * @since 1.5 * * @throws \RuntimeException * * @deprecated 4.0 will be removed in 6.0 * Inject the router or load it from the dependency injection container * Example: Factory::getContainer()->get(SiteRouter::class); */ public static function getInstance($client, $options = []) { if (empty(self::$instances[$client])) { // Create a Router object $classname = 'JRouter' . ucfirst($client); if (!class_exists($classname)) { throw new \RuntimeException(Text::sprintf('JLIB_APPLICATION_ERROR_ROUTER_LOAD', $client), 500); } // Check for a possible service from the container otherwise manually instantiate the class if (Factory::getContainer()->has($classname)) { self::$instances[$client] = Factory::getContainer()->get($classname); } else { self::$instances[$client] = new $classname(); } } return self::$instances[$client]; } /** * Function to convert a route to an internal URI * * @param Uri &$uri The uri. * @param bool $setVars Set the parsed data in the internal * storage for current-request-URLs * * @return array * * @since 1.5 * @throws \Exception */ public function parse(&$uri, $setVars = false) { // Do the preprocess stage of the URL parse process $this->processParseRules($uri, self::PROCESS_BEFORE); // Do the main stage of the URL parse process $this->processParseRules($uri); // Do the postprocess stage of the URL parse process $this->processParseRules($uri, self::PROCESS_AFTER); // Check if all parts of the URL have been parsed. // Otherwise we have an invalid URL if (\strlen($uri->getPath()) > 0) { throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND')); } if ($setVars) { $this->setVars($uri->getQuery(true)); return $this->getVars(); } return $uri->getQuery(true); } /** * Function to convert an internal URI to a route * * @param string|array|Uri $url The internal URL or an associative array * * @return Uri The absolute search engine friendly URL object * * @since 1.5 */ public function build($url) { $key = md5(serialize($url)); if (isset($this->cache[$key])) { return clone $this->cache[$key]; } if ($url instanceof Uri) { $uri = $url; } else { $uri = $this->createUri($url); } // Do the preprocess stage of the URL build process $this->processBuildRules($uri, self::PROCESS_BEFORE); // Do the main stage of the URL build process $this->processBuildRules($uri); // Do the postprocess stage of the URL build process $this->processBuildRules($uri, self::PROCESS_AFTER); $this->cache[$key] = clone $uri; return $uri; } /** * Set a router variable, creating it if it doesn't exist * * @param string $key The name of the variable * @param mixed $value The value of the variable * @param boolean $create If True, the variable will be created if it doesn't exist yet * * @return void * * @since 1.5 */ public function setVar($key, $value, $create = true) { if ($create || \array_key_exists($key, $this->vars)) { $this->vars[$key] = $value; } } /** * Set the router variable array * * @param array $vars An associative array with variables * @param boolean $merge If True, the array will be merged instead of overwritten * * @return void * * @since 1.5 */ public function setVars($vars = [], $merge = true) { if ($merge) { $this->vars = array_merge($this->vars, $vars); } else { $this->vars = $vars; } } /** * Get a router variable * * @param string $key The name of the variable * * @return mixed Value of the variable * * @since 1.5 */ public function getVar($key) { $result = null; if (isset($this->vars[$key])) { $result = $this->vars[$key]; } return $result; } /** * Get the router variable array * * @return array An associative array of router variables * * @since 1.5 */ public function getVars() { return $this->vars; } /** * Attach a build rule * * @param callable $callback The function to be called * @param string $stage The stage of the build process that * this should be added to. Possible values: * 'preprocess', '' for the main build process, * 'postprocess' * * @return void * * @since 1.5 */ public function attachBuildRule(callable $callback, $stage = self::PROCESS_DURING) { if (!\array_key_exists('build' . $stage, $this->rules)) { throw new \InvalidArgumentException(sprintf('The %s stage is not registered. (%s)', $stage, __METHOD__)); } $this->rules['build' . $stage][] = $callback; } /** * Attach a parse rule * * @param callable $callback The function to be called. * @param string $stage The stage of the parse process that * this should be added to. Possible values: * 'preprocess', '' for the main parse process, * 'postprocess' * * @return void * * @since 1.5 */ public function attachParseRule(callable $callback, $stage = self::PROCESS_DURING) { if (!\array_key_exists('parse' . $stage, $this->rules)) { throw new \InvalidArgumentException(sprintf('The %s stage is not registered. (%s)', $stage, __METHOD__)); } $this->rules['parse' . $stage][] = $callback; } /** * Remove a rule * * @param string $type Type of rule to remove (parse or build) * @param callable $rule The rule to be removed. * @param string $stage The stage of the parse process that * this should be added to. Possible values: * 'preprocess', '' for the main parse process, * 'postprocess' * * @return boolean Was a rule removed? * * @since 4.0.0 * @throws \InvalidArgumentException */ public function detachRule($type, $rule, $stage = self::PROCESS_DURING) { if (!\in_array($type, ['parse', 'build'])) { throw new \InvalidArgumentException(sprintf('The %s type is not supported. (%s)', $type, __METHOD__)); } if (!\array_key_exists($type . $stage, $this->rules)) { throw new \InvalidArgumentException(sprintf('The %s stage is not registered. (%s)', $stage, __METHOD__)); } foreach ($this->rules[$type . $stage] as $id => $r) { if ($r == $rule) { unset($this->rules[$type . $stage][$id]); return true; } } return false; } /** * Get all currently attached rules * * @return array All currently attached rules in an array * * @since 4.0.0 */ public function getRules() { return $this->rules; } /** * Process the parsed router variables based on custom defined rules * * @param \Joomla\CMS\Uri\Uri &$uri The URI to parse * @param string $stage The stage that should be processed. * Possible values: 'preprocess', 'postprocess' * and '' for the main parse stage * * @return void * * @since 3.2 */ protected function processParseRules(&$uri, $stage = self::PROCESS_DURING) { if (!\array_key_exists('parse' . $stage, $this->rules)) { throw new \InvalidArgumentException(sprintf('The %s stage is not registered. (%s)', $stage, __METHOD__)); } foreach ($this->rules['parse' . $stage] as $rule) { $rule($this, $uri); } } /** * Process the build uri query data based on custom defined rules * * @param \Joomla\CMS\Uri\Uri &$uri The URI * @param string $stage The stage that should be processed. * Possible values: 'preprocess', 'postprocess' * and '' for the main build stage * * @return void * * @since 3.2 */ protected function processBuildRules(&$uri, $stage = self::PROCESS_DURING) { if (!\array_key_exists('build' . $stage, $this->rules)) { throw new \InvalidArgumentException(sprintf('The %s stage is not registered. (%s)', $stage, __METHOD__)); } foreach ($this->rules['build' . $stage] as $rule) { \call_user_func_array($rule, [&$this, &$uri]); } } /** * Create a uri based on a full or partial URL string * * @param string $url The URI or an associative array * * @return Uri * * @since 3.2 */ protected function createUri($url) { if (!\is_array($url) && substr($url, 0, 1) !== '&') { return new Uri($url); } $uri = new Uri('index.php'); if (\is_string($url)) { $vars = []; if (strpos($url, '&') !== false) { $url = str_replace('&', '&', $url); } parse_str($url, $vars); } else { $vars = $url; } $vars = array_merge($this->getVars(), $vars); foreach ($vars as $key => $var) { if ($var == '') { unset($vars[$key]); } } $uri->setQuery($vars); return $uri; } } SiteRouter.php 0000644 00000043410 15173161127 0007370 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2007 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router; use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Application\SiteApplication; use Joomla\CMS\Component\Router\RouterInterface; use Joomla\CMS\Component\Router\RouterLegacy; use Joomla\CMS\Component\Router\RouterServiceInterface; use Joomla\CMS\Factory; use Joomla\CMS\Menu\AbstractMenu; use Joomla\CMS\Uri\Uri; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Class to create and parse routes for the site application * * @since 1.5 */ class SiteRouter extends Router { /** * Component-router objects * * @var array * * @since 3.3 */ protected $componentRouters = []; /** * @var CMSApplication * * @since 3.4 */ protected $app; /** * Current Menu-Object * * @var AbstractMenu * * @since 3.4 */ protected $menu; /** * Class constructor * * @param CMSApplication $app Application Object * @param AbstractMenu $menu Menu object * * @since 3.4 */ public function __construct(CMSApplication $app = null, AbstractMenu $menu = null) { $this->app = $app ?: Factory::getContainer()->get(SiteApplication::class); $this->menu = $menu ?: $this->app->getMenu(); // Add core rules if ((int) $this->app->get('force_ssl') === 2) { $this->attachParseRule([$this, 'parseCheckSSL'], self::PROCESS_BEFORE); } $this->attachParseRule([$this, 'parseInit'], self::PROCESS_BEFORE); $this->attachBuildRule([$this, 'buildInit'], self::PROCESS_BEFORE); $this->attachBuildRule([$this, 'buildComponentPreprocess'], self::PROCESS_BEFORE); if ($this->app->get('sef', 1)) { if ($this->app->get('sef_suffix')) { $this->attachParseRule([$this, 'parseFormat'], self::PROCESS_BEFORE); $this->attachBuildRule([$this, 'buildFormat'], self::PROCESS_AFTER); } $this->attachParseRule([$this, 'parseSefRoute'], self::PROCESS_DURING); $this->attachBuildRule([$this, 'buildSefRoute'], self::PROCESS_DURING); $this->attachParseRule([$this, 'parsePaginationData'], self::PROCESS_AFTER); $this->attachBuildRule([$this, 'buildPaginationData'], self::PROCESS_AFTER); if ($this->app->get('sef_rewrite')) { $this->attachBuildRule([$this, 'buildRewrite'], self::PROCESS_AFTER); } } $this->attachParseRule([$this, 'parseRawRoute'], self::PROCESS_DURING); $this->attachBuildRule([$this, 'buildBase'], self::PROCESS_AFTER); } /** * Force to SSL * * @param Router &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function parseCheckSSL(&$router, &$uri) { if (strtolower($uri->getScheme()) !== 'https') { // Forward to https $uri->setScheme('https'); $this->app->redirect((string) $uri, 301); } } /** * Do some initial cleanup before parsing the URL * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function parseInit(&$router, &$uri) { // Get the path // Decode URL to convert percent-encoding to unicode so that strings match when routing. $path = urldecode($uri->getPath()); /** * In some environments (e.g. CLI we can't form a valid base URL). In this case we catch the exception thrown * by URI and set an empty base URI for further work. * @todo: This should probably be handled better */ try { $baseUri = Uri::base(true); } catch (\RuntimeException $e) { $baseUri = ''; } // Remove the base URI path. $path = substr_replace($path, '', 0, \strlen($baseUri)); // Check to see if a request to a specific entry point has been made. if (preg_match("#.*?\.php#u", $path, $matches)) { // Get the current entry point path relative to the site path. $scriptPath = realpath($_SERVER['SCRIPT_FILENAME'] ?: str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED'])); $relativeScriptPath = str_replace('\\', '/', str_replace(JPATH_SITE, '', $scriptPath)); // If a php file has been found in the request path, check to see if it is a valid file. // Also verify that it represents the same file from the server variable for entry script. if (is_file(JPATH_SITE . $matches[0]) && ($matches[0] === $relativeScriptPath)) { // Remove the entry point segments from the request path for proper routing. $path = str_replace($matches[0], '', $path); } } // Set the route $uri->setPath(trim($path, '/')); } /** * Parse the format of the request * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function parseFormat(&$router, &$uri) { $route = $uri->getPath(); // Identify format if (!(substr($route, -9) === 'index.php' || substr($route, -1) === '/') && $suffix = pathinfo($route, PATHINFO_EXTENSION)) { $uri->setVar('format', $suffix); $route = str_replace('.' . $suffix, '', $route); $uri->setPath($route); } } /** * Convert a sef route to an internal URI * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function parseSefRoute(&$router, &$uri) { $route = $uri->getPath(); // If the URL is empty, we handle this in the non-SEF parse URL if (empty($route)) { return; } // Parse the application route $segments = explode('/', $route); if (\count($segments) > 1 && $segments[0] === 'component') { $uri->setVar('option', 'com_' . $segments[1]); $uri->setVar('Itemid', null); $route = implode('/', \array_slice($segments, 2)); } else { // Get menu items. $items = $this->menu->getItems(['parent_id', 'access'], [1, null]); $lang_tag = $this->app->getLanguage()->getTag(); $found = null; foreach ($segments as $segment) { $matched = false; foreach ($items as $item) { if ( $item->alias == $segment && (!$this->app->getLanguageFilter() || ($item->language === '*' || $item->language === $lang_tag)) ) { $found = $item; $matched = true; $items = $item->getChildren(); break; } } if (!$matched) { break; } } // Menu links are not valid URLs. Find the first parent that isn't a menulink if ($found && $found->type === 'menulink') { while ($found->hasParent() && $found->type === 'menulink') { $found = $found->getParent(); } if ($found->type === 'menulink') { $found = null; } } if (!$found) { $found = $this->menu->getDefault($lang_tag); } else { $route = trim(substr($route, \strlen($found->route)), '/'); } if ($found) { if ($found->type === 'alias') { $newItem = $this->menu->getItem($found->getParams()->get('aliasoptions')); if ($newItem) { $found->query = array_merge($found->query, $newItem->query); $found->component = $newItem->component; } } $uri->setVar('Itemid', $found->id); $uri->setVar('option', $found->component); } } // Set the active menu item if ($uri->getVar('Itemid')) { $this->menu->setActive($uri->getVar('Itemid')); } // Parse the component route if (!empty($route) && $uri->getVar('option')) { $segments = explode('/', $route); if (\count($segments)) { // Handle component route $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $uri->getVar('option')); $crouter = $this->getComponentRouter($component); $uri->setQuery(array_merge($uri->getQuery(true), $crouter->parse($segments))); } $route = implode('/', $segments); } $uri->setPath($route); } /** * Convert a raw route to an internal URI * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function parseRawRoute(&$router, &$uri) { if ($uri->getVar('Itemid')) { $item = $this->menu->getItem($uri->getVar('Itemid')); } else { $item = $this->menu->getDefault($this->app->getLanguage()->getTag()); } if ($item && $item->type === 'alias') { $newItem = $this->menu->getItem($item->getParams()->get('aliasoptions')); if ($newItem) { $item->query = array_merge($item->query, $newItem->query); $item->component = $newItem->component; } } if (\is_object($item)) { // Set the active menu item $this->menu->setActive($item->id); $uri->setVar('Itemid', $item->id); $uri->setQuery(array_merge($item->query, $uri->getQuery(true))); } } /** * Convert limits for pagination * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function parsePaginationData(&$router, &$uri) { // Process the pagination support $start = $uri->getVar('start'); if ($start !== null) { $uri->setVar('limitstart', $uri->getVar('start')); $uri->delVar('start'); } } /** * Do some initial processing for building a URL * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function buildInit(&$router, &$uri) { $itemid = $uri->getVar('Itemid'); // If no Itemid and option given, merge in the current requests data if (!$itemid && !$uri->getVar('option')) { $uri->setQuery(array_merge($this->getVars(), $uri->getQuery(true))); } // If Itemid is given, but no option, set the option from the menu item if ($itemid && !$uri->getVar('option')) { if ($item = $this->menu->getItem($itemid)) { $uri->setVar('option', $item->component); } } } /** * Run the component preprocess method * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function buildComponentPreprocess(&$router, &$uri) { // Get the query data $query = $uri->getQuery(true); if (!isset($query['option'])) { return; } $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']); $crouter = $this->getComponentRouter($component); $query = $crouter->preprocess($query); // Make sure any menu vars are used if no others are specified if ( isset($query['Itemid']) && (\count($query) === 2 || (\count($query) === 3 && isset($query['lang']))) ) { // Get the active menu item $item = $this->menu->getItem($query['Itemid']); if ($item !== null) { $query = array_merge($item->query, $query); } } $uri->setQuery($query); } /** * Build the SEF route * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function buildSefRoute(&$router, &$uri) { // Get the query data $query = $uri->getQuery(true); if (!isset($query['option'])) { return; } // Get Menu Item $item = empty($query['Itemid']) ? null : $this->menu->getItem($query['Itemid']); // Build the component route $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $query['option']); $crouter = $this->getComponentRouter($component); $parts = $crouter->build($query); $tmp = trim(implode('/', $parts)); // Build the application route if ($item !== null && $query['option'] === $item->component) { if (!$item->home) { $tmp = $tmp ? $item->route . '/' . $tmp : $item->route; } unset($query['Itemid']); } else { $tmp = 'component/' . substr($query['option'], 4) . '/' . $tmp; } // Get the route if ($tmp) { $uri->setPath($uri->getPath() . '/' . $tmp); } // Unset unneeded query information unset($query['option']); // Set query again in the URI $uri->setQuery($query); } /** * Convert limits for pagination * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function buildPaginationData(&$router, &$uri) { $limitstart = $uri->getVar('limitstart'); if ($limitstart !== null && $limitstart !== '') { $uri->setVar('start', (int) $limitstart); } $uri->delVar('limitstart'); } /** * Build the format of the request * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function buildFormat(&$router, &$uri) { $route = $uri->getPath(); // Identify format if (!(substr($route, -9) === 'index.php' || substr($route, -1) === '/') && $format = $uri->getVar('format', 'html')) { $route .= '.' . $format; $uri->setPath($route); $uri->delVar('format'); } } /** * Create a uri based on a full or partial URL string * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function buildRewrite(&$router, &$uri) { // Get the path data $route = $uri->getPath(); // Transform the route if ($route === 'index.php') { $route = ''; } else { $route = str_replace('index.php/', '', $route); } $uri->setPath($route); } /** * Add the basepath to the URI * * @param SiteRouter &$router Router object * @param Uri &$uri URI object to process * * @return void * * @since 4.0.0 */ public function buildBase(&$router, &$uri) { // Add frontend basepath to the uri $uri->setPath(Uri::root(true) . '/' . $uri->getPath()); } /** * Get component router * * @param string $component Name of the component including com_ prefix * * @return RouterInterface Component router * * @since 3.3 */ public function getComponentRouter($component) { if (!isset($this->componentRouters[$component])) { $componentInstance = $this->app->bootComponent($component); if ($componentInstance instanceof RouterServiceInterface) { $this->componentRouters[$component] = $componentInstance->createRouter($this->app, $this->menu); } if (!isset($this->componentRouters[$component])) { $this->componentRouters[$component] = new RouterLegacy(ucfirst(substr($component, 4))); } } return $this->componentRouters[$component]; } /** * Set a router for a component * * @param string $component Component name with com_ prefix * @param object $router Component router * * @return boolean True if the router was accepted, false if not * * @since 3.3 */ public function setComponentRouter($component, $router) { $reflection = new \ReflectionClass($router); if (\in_array('Joomla\\CMS\\Component\\Router\\RouterInterface', $reflection->getInterfaceNames())) { $this->componentRouters[$component] = $router; return true; } else { return false; } } } Exception/RouteNotFoundException.php 0000644 00000002040 15173161127 0013645 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router\Exception; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Exception class defining an error for a missing route * * @since 3.8.0 */ class RouteNotFoundException extends \InvalidArgumentException { /** * Constructor * * @param string $message The Exception message to throw. * @param integer $code The Exception code. * @param \Exception $previous The previous exception used for the exception chaining. * * @since 3.8.0 */ public function __construct($message = '', $code = 404, \Exception $previous = null) { if (empty($message)) { $message = 'URL was not found'; } parent::__construct($message, $code, $previous); } } ApiRouter.php 0000644 00000015535 15173161127 0007204 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router; use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\Router\Exception\RouteNotFoundException; use Joomla\CMS\Uri\Uri; use Joomla\Router\Route; use Joomla\Router\Router; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Joomla! API Router class * * @since 4.0.0 */ class ApiRouter extends Router { /** * The application object * * @var CMSApplicationInterface * @since 4.0.0 */ protected $app; /** * Constructor. * * @param CMSApplicationInterface $app The application object * @param array $maps An optional array of route maps * * @since 1.0 */ public function __construct(CMSApplicationInterface $app, array $maps = []) { $this->app = $app; parent::__construct($maps); } /** * Creates routes map for CRUD * * @param string $baseName The base name of the component. * @param string $controller The name of the controller that contains CRUD functions. * @param array $defaults An array of default values that are used when the URL is matched. * @param bool $publicGets Allow the public to make GET requests. * * @return void * * @since 4.0.0 */ public function createCRUDRoutes($baseName, $controller, $defaults = [], $publicGets = false) { $getDefaults = array_merge(['public' => $publicGets], $defaults); $routes = [ new Route(['GET'], $baseName, $controller . '.displayList', [], $getDefaults), new Route(['GET'], $baseName . '/:id', $controller . '.displayItem', ['id' => '(\d+)'], $getDefaults), new Route(['POST'], $baseName, $controller . '.add', [], $defaults), new Route(['PATCH'], $baseName . '/:id', $controller . '.edit', ['id' => '(\d+)'], $defaults), new Route(['DELETE'], $baseName . '/:id', $controller . '.delete', ['id' => '(\d+)'], $defaults), ]; $this->addRoutes($routes); } /** * Parse the given route and return the name of a controller mapped to the given route. * * @param string $method Request method to match. One of GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE or PATCH * * @return array An array containing the controller and the matched variables. * * @since 4.0.0 * @throws \InvalidArgumentException */ public function parseApiRoute($method = 'GET') { $method = strtoupper($method); $validMethods = ["GET", "POST", "PUT", "DELETE", "HEAD", "TRACE", "PATCH"]; if (!\in_array($method, $validMethods)) { throw new \InvalidArgumentException(sprintf('%s is not a valid HTTP method.', $method)); } // Get the path from the route and remove and leading or trailing slash. $routePath = $this->getRoutePath(); $query = Uri::getInstance()->getQuery(true); // Remove the public key as it is only supported coming from the route definition if (array_key_exists('public', $query)) { unset($query['public']); } // Iterate through all of the known routes looking for a match. foreach ($this->routes as $route) { if (\in_array($method, $route->getMethods())) { if (preg_match($route->getRegex(), ltrim($routePath, '/'), $matches)) { // If we have gotten this far then we have a positive match. $vars = $route->getDefaults(); foreach ($route->getRouteVariables() as $i => $var) { $vars[$var] = $matches[$i + 1]; } $controller = preg_split("/[.]+/", $route->getController()); /** @deprecated 4.3 will be removed in 5.0 * Query parameters will not be merged into route variables from 5.0 */ $vars = array_merge($vars, $query); return [ 'controller' => $controller[0], 'task' => $controller[1], 'vars' => $vars, ]; } } } throw new RouteNotFoundException(sprintf('Unable to handle request for route `%s`.', $routePath)); } /** * Get the path from the route and remove and leading or trailing slash. * * @return string * * @since 4.0.0 */ public function getRoutePath() { // Get the path from the route and remove and leading or trailing slash. $uri = Uri::getInstance(); $path = urldecode($uri->getPath()); /** * In some environments (e.g. CLI we can't form a valid base URL). In this case we catch the exception thrown * by URI and set an empty base URI for further work. * @todo: This should probably be handled better */ try { $baseUri = Uri::base(true); } catch (\RuntimeException $e) { $baseUri = ''; } // Remove the base URI path. $path = substr_replace($path, '', 0, \strlen($baseUri)); // Transform the route $path = $this->removeIndexPhpFromPath($path); return $path; } /** * Removes the index.php from the route's path. * * @param string $path The path * * @return string * * @since 4.0.0 */ private function removeIndexPhpFromPath(string $path): string { // Normalize the path $path = ltrim($path, '/'); // We can only remove index.php if it's present in the beginning of the route if (strpos($path, 'index.php') !== 0) { return $path; } // Edge case: the route is index.php without a trailing slash. Bad idea but we can still map it to a null route. if ($path === 'index.php') { return ''; } // Remove the "index.php/" part of the route and return the result. return substr($path, 10); } /** * Extract routes matching current route from all known routes. * * @return \Joomla\Router\Route[] * * @since 4.0.0 */ public function getMatchingRoutes() { $routePath = $this->getRoutePath(); // Extract routes matching $routePath from all known routes. return array_filter( $this->routes, function ($route) use ($routePath) { return preg_match($route->getRegex(), ltrim($routePath, '/')) === 1; } ); } } Route.php 0000644 00000015212 15173161127 0006360 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2012 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\Uri\Uri; use Joomla\DI\Exception\KeyNotFoundException; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Route handling class * * @since 1.7.0 */ class Route { /** * No change, use the protocol currently used. * * @since 3.9.7 */ public const TLS_IGNORE = 0; /** * Make URI secure using http over TLS (https). * * @since 3.9.7 */ public const TLS_FORCE = 1; /** * Make URI unsecure using plain http (http). * * @since 3.9.7 */ public const TLS_DISABLE = 2; /** * The route object so we don't have to keep fetching it. * * @var Router[] * @since 3.0.1 */ private static $_router = []; /** * Translates an internal Joomla URL to a humanly readable URL. This method builds links for the current active client. * * @param string $url Absolute or Relative URI to Joomla resource. * @param boolean $xhtml Replace & by & for XML compliance. * @param integer $tls Secure state for the resolved URI. Use Route::TLS_* constants * 0: (default) No change, use the protocol currently used in the request * 1: Make URI secure using global secure site URI. * 2: Make URI unsecure using the global unsecure site URI. * @param boolean $absolute Return an absolute URL * * @return string The translated humanly readable URL. * * @since 1.7.0 */ public static function _($url, $xhtml = true, $tls = self::TLS_IGNORE, $absolute = false) { try { /** * @deprecated 3.9 int conversion will be removed in 5.0 * Before 3.9.7 this method silently converted $tls to integer */ if (!is_int($tls)) { @trigger_error( __METHOD__ . '() called with incompatible variable type on parameter $tls.', E_USER_DEPRECATED ); $tls = (int) $tls; } /** * @deprecated 3.9 -1 as valid value will be removed in 5.0 * Before 3.9.7 this method accepted -1. */ if ($tls === -1) { $tls = self::TLS_DISABLE; } $app = Factory::getApplication(); $client = $app->getName(); return static::link($client, $url, $xhtml, $tls, $absolute); } catch (\RuntimeException $e) { /** * @deprecated 3.9 this method will not fail silently from 5.0 * Before 3.9.0 this method failed silently on router error. This B/C will be removed in Joomla 5.0 */ return null; } } /** * Translates an internal Joomla URL to a humanly readable URL. * NOTE: To build link for active client instead of a specific client, you can use <var>Route::_()</var> * * @param string $client The client name for which to build the link. * @param string $url Absolute or Relative URI to Joomla resource. * @param boolean $xhtml Replace & by & for XML compliance. * @param integer $tls Secure state for the resolved URI. Use Route::TLS_* constants * 0: (default) No change, use the protocol currently used in the request * 1: Make URI secure using global secure site URI. * 2: Make URI unsecure using the global unsecure site URI. * @param boolean $absolute Return an absolute URL * * @return string The translated humanly readable URL. * * @throws \RuntimeException * * @since 3.9.0 */ public static function link($client, $url, $xhtml = true, $tls = self::TLS_IGNORE, $absolute = false) { // If we cannot process this $url exit early. if (!\is_array($url) && (strpos($url, '&') !== 0) && (strpos($url, 'index.php') !== 0)) { return $url; } // Get the router instance, only attempt when a client name is given. if ($client && !isset(self::$_router[$client])) { try { self::$_router[$client] = Factory::getContainer()->get(ucfirst($client) . 'Router') ?: Factory::getApplication()::getRouter($client); } catch (KeyNotFoundException $e) { self::$_router[$client] = Factory::getApplication()::getRouter($client); } } // Make sure that we have our router if (!isset(self::$_router[$client])) { throw new \RuntimeException(Text::sprintf('JLIB_APPLICATION_ERROR_ROUTER_LOAD', $client), 500); } // Build route. $uri = self::$_router[$client]->build($url); $scheme = ['path', 'query', 'fragment']; /* * Get the secure/unsecure URLs. * * If the first 5 characters of the BASE are 'https', then we are on an ssl connection over * https and need to set our secure URL to the current request URL, if not, and the scheme is * 'http', then we need to do a quick string manipulation to switch schemes. */ if ($tls === self::TLS_FORCE) { $uri->setScheme('https'); } elseif ($tls === self::TLS_DISABLE) { $uri->setScheme('http'); } // Set scheme if requested or if ($absolute || $tls > 0) { static $scheme_host_port; if (!\is_array($scheme_host_port)) { $uri2 = Uri::getInstance(); $scheme_host_port = [$uri2->getScheme(), $uri2->getHost(), $uri2->getPort()]; } if (is_null($uri->getScheme())) { $uri->setScheme($scheme_host_port[0]); } $uri->setHost($scheme_host_port[1]); $uri->setPort($scheme_host_port[2]); $scheme = array_merge($scheme, ['host', 'port', 'scheme']); } $url = $uri->toString($scheme); // Replace spaces. $url = preg_replace('/\s/u', '%20', $url); if ($xhtml) { $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8'); } return $url; } } SiteRouterAwareInterface.php 0000644 00000001254 15173161127 0012171 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\Router; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for site router aware classes. * * @since 4.2.0 */ interface SiteRouterAwareInterface { /** * Set the router to use. * * @param SiteRouter $router The router to use. * * @return void * * @since 4.2.0 */ public function setSiteRouter(SiteRouter $router): void; }
| ver. 1.4 |
Github
|
.
| PHP 8.3.23 | Generation time: 0 |
proxy
|
phpinfo
|
Settings