. // }}} /** * Static wrapper for Symfony's router * * @package GNUsocial * @category URL * * @author Hugo Sales * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ namespace App\Core\Router; use App\Core\Log; use App\Util\Common; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Router as SymfonyRouter; /** * @mixin SymfonyRouter */ abstract class Router { /** * Generates an absolute URL, e.g. "http://example.com/dir/file". */ public const ABSOLUTE_URL = UrlGeneratorInterface::ABSOLUTE_URL; /** * Generates an absolute path, e.g. "/dir/file". */ public const ABSOLUTE_PATH = UrlGeneratorInterface::ABSOLUTE_PATH; /** * Generates a relative path based on the current request path, e.g. "../parent-file". * * @see UrlGenerator::getRelativePath() */ public const RELATIVE_PATH = UrlGeneratorInterface::RELATIVE_PATH; /** * Generates a network path, e.g. "//example.com/dir/file". * Such reference reuses the current scheme but specifies the host. */ public const NETWORK_PATH = UrlGeneratorInterface::NETWORK_PATH; public static ?SymfonyRouter $router = null; public static function setRouter($rtr): void { self::$router = $rtr; } public static function isAbsolute(string $url) { return isset(parse_url($url)['host']); } /** * Generate a URL for route $id with $args replacing the * placeholder route values. Extra params are added as query * string to the URL */ public static function url(string $id, array $args = [], int $type = self::ABSOLUTE_PATH): string { if ($type === self::RELATIVE_PATH) { Log::debug('Requested relative path which is not an absolute path... just saying...'); } return self::$router->generate($id, $args, $type); } public static function sanitizeLocalURL(string $url, array $unset_query_args = []): ?string { try { $parts = parse_url($url); if ($parts === false || (isset($parts['host']) && $parts['host'] !== Common::config('site', 'server'))) { return null; } self::match($parts['path']); if ($unset_query_args !== [] && isset($parts['query'])) { $args = []; parse_str($parts['query'], $args); $args = array_diff_key($args, $unset_query_args); $parts['query'] = http_build_query($args); } return $parts['path'] . (empty($parts['query']) ? '' : ('?' . $parts['query'])) . (empty($parts['fragment']) ? '' : ('#' . $parts['fragment'])); } catch (ResourceNotFoundException) { return null; } } /** * function match($url) throws Symfony\Component\Routing\Exception\ResourceNotFoundException */ public static function __callStatic(string $name, array $args) { return self::$router->{$name}(...$args); } }