maj
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* @package Grav\Common
|
||||
*
|
||||
* @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
|
||||
* @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
@@ -49,7 +49,9 @@ use Grav\Common\Twig\Twig;
|
||||
use Grav\Framework\DI\Container;
|
||||
use Grav\Framework\Psr7\Response;
|
||||
use Grav\Framework\RequestHandler\RequestHandler;
|
||||
use Grav\Framework\Route\Route;
|
||||
use Grav\Framework\Session\Messages;
|
||||
use InvalidArgumentException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
@@ -62,6 +64,7 @@ use function get_class;
|
||||
use function in_array;
|
||||
use function is_callable;
|
||||
use function is_int;
|
||||
use function is_string;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
@@ -343,7 +346,7 @@ class Grav extends Container
|
||||
* Please use this method instead of calling `die();` or `exit();`. Note that you need to create a response object.
|
||||
*
|
||||
* @param ResponseInterface $response
|
||||
* @return void
|
||||
* @return never-return
|
||||
*/
|
||||
public function close(ResponseInterface $response): void
|
||||
{
|
||||
@@ -395,7 +398,7 @@ class Grav extends Container
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @return void
|
||||
* @return never-return
|
||||
* @deprecated 1.7 Do not use
|
||||
*/
|
||||
public function exit(ResponseInterface $response): void
|
||||
@@ -408,9 +411,9 @@ class Grav extends Container
|
||||
*
|
||||
* Please use this method instead of calling `header("Location: {$url}", true, 302); exit();`.
|
||||
*
|
||||
* @param string $route Internal route.
|
||||
* @param Route|string $route Internal route.
|
||||
* @param int|null $code Redirection code (30x)
|
||||
* @return void
|
||||
* @return never-return
|
||||
*/
|
||||
public function redirect($route, $code = null): void
|
||||
{
|
||||
@@ -422,7 +425,7 @@ class Grav extends Container
|
||||
/**
|
||||
* Returns redirect response object from Grav.
|
||||
*
|
||||
* @param string $route Internal route.
|
||||
* @param Route|string $route Internal route.
|
||||
* @param int|null $code Redirection code (30x)
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
@@ -431,37 +434,47 @@ class Grav extends Container
|
||||
/** @var Uri $uri */
|
||||
$uri = $this['uri'];
|
||||
|
||||
// Clean route for redirect
|
||||
$route = preg_replace("#^\/[\\\/]+\/#", '/', $route);
|
||||
if (is_string($route)) {
|
||||
// Clean route for redirect
|
||||
$route = preg_replace("#^\/[\\\/]+\/#", '/', $route);
|
||||
|
||||
if (null === $code) {
|
||||
// Check for redirect code in the route: e.g. /new/[301], /new[301]/route or /new[301].html
|
||||
$regex = '/.*(\[(30[1-7])\])(.\w+|\/.*?)?$/';
|
||||
preg_match($regex, $route, $matches);
|
||||
if ($matches) {
|
||||
$route = str_replace($matches[1], '', $matches[0]);
|
||||
$code = $matches[2];
|
||||
}
|
||||
}
|
||||
|
||||
if ($uri::isExternal($route)) {
|
||||
$url = $route;
|
||||
} else {
|
||||
$url = rtrim($uri->rootUrl(), '/') . '/';
|
||||
|
||||
if ($this['config']->get('system.pages.redirect_trailing_slash', true)) {
|
||||
$url .= trim($route, '/'); // Remove trailing slash
|
||||
} else {
|
||||
$url .= ltrim($route, '/'); // Support trailing slash default routes
|
||||
}
|
||||
}
|
||||
} elseif ($route instanceof Route) {
|
||||
$url = $route->toString(true);
|
||||
} else {
|
||||
throw new InvalidArgumentException('Bad $route');
|
||||
}
|
||||
|
||||
if ($code < 300 || $code > 399) {
|
||||
$code = null;
|
||||
}
|
||||
|
||||
if (null === $code) {
|
||||
// Check for redirect code in the route: e.g. /new/[301], /new[301]/route or /new[301].html
|
||||
$regex = '/.*(\[(30[1-7])\])(.\w+|\/.*?)?$/';
|
||||
preg_match($regex, $route, $matches);
|
||||
if ($matches) {
|
||||
$route = str_replace($matches[1], '', $matches[0]);
|
||||
$code = $matches[2];
|
||||
}
|
||||
}
|
||||
|
||||
if ($code === null) {
|
||||
$code = $this['config']->get('system.pages.redirect_default_code', 302);
|
||||
}
|
||||
|
||||
if ($uri::isExternal($route)) {
|
||||
$url = $route;
|
||||
} else {
|
||||
$url = rtrim($uri->rootUrl(), '/') . '/';
|
||||
|
||||
if ($this['config']->get('system.pages.redirect_trailing_slash', true)) {
|
||||
$url .= trim($route, '/'); // Remove trailing slash
|
||||
} else {
|
||||
$url .= ltrim($route, '/'); // Support trailing slash default routes
|
||||
}
|
||||
if ($uri->extension() === 'json') {
|
||||
return new Response(200, ['Content-Type' => 'application/json'], json_encode(['code' => $code, 'redirect' => $url], JSON_THROW_ON_ERROR));
|
||||
}
|
||||
|
||||
return new Response($code, ['Location' => $url]);
|
||||
@@ -639,6 +652,7 @@ class Grav extends Container
|
||||
* @param array $args
|
||||
* @return mixed|null
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function __call($method, $args)
|
||||
{
|
||||
$closure = $this->{$method} ?? null;
|
||||
@@ -715,18 +729,36 @@ class Grav extends Container
|
||||
*/
|
||||
public function fallbackUrl($path)
|
||||
{
|
||||
$this->fireEvent('onPageFallBackUrl');
|
||||
|
||||
/** @var Uri $uri */
|
||||
$uri = $this['uri'];
|
||||
|
||||
/** @var Config $config */
|
||||
$config = $this['config'];
|
||||
|
||||
$uri_extension = strtolower($uri->extension());
|
||||
$fallback_types = $config->get('system.media.allowed_fallback_types', null);
|
||||
$path_parts = Utils::pathinfo($path);
|
||||
|
||||
/** @var Pages $pages */
|
||||
$pages = $this['pages'];
|
||||
$page = $pages->find($path_parts['dirname'], true);
|
||||
|
||||
$uri_extension = strtolower($uri->extension() ?? '');
|
||||
$fallback_types = $config->get('system.media.allowed_fallback_types');
|
||||
$supported_types = $config->get('media.types');
|
||||
|
||||
$parsed_url = parse_url(rawurldecode($uri->basename()));
|
||||
$media_file = $parsed_url['path'];
|
||||
|
||||
$event = new Event([
|
||||
'uri' => $uri,
|
||||
'page' => &$page,
|
||||
'filename' => &$media_file,
|
||||
'extension' => $uri_extension,
|
||||
'allowed_fallback_types' => &$fallback_types,
|
||||
'media_types' => &$supported_types
|
||||
]);
|
||||
|
||||
$this->fireEvent('onPageFallBackUrl', $event);
|
||||
|
||||
// Check whitelist first, then ensure extension is a valid media type
|
||||
if (!empty($fallback_types) && !in_array($uri_extension, $fallback_types, true)) {
|
||||
return false;
|
||||
@@ -735,16 +767,8 @@ class Grav extends Container
|
||||
return false;
|
||||
}
|
||||
|
||||
$path_parts = pathinfo($path);
|
||||
|
||||
/** @var Pages $pages */
|
||||
$pages = $this['pages'];
|
||||
$page = $pages->find($path_parts['dirname'], true);
|
||||
|
||||
if ($page) {
|
||||
$media = $page->media()->all();
|
||||
$parsed_url = parse_url(rawurldecode($uri->basename()));
|
||||
$media_file = $parsed_url['path'];
|
||||
|
||||
// if this is a media object, try actions first
|
||||
if (isset($media[$media_file])) {
|
||||
@@ -774,11 +798,9 @@ class Grav extends Container
|
||||
}
|
||||
Utils::download($page->path() . DIRECTORY_SEPARATOR . $uri->basename(), $download);
|
||||
}
|
||||
|
||||
// Nothing found
|
||||
return false;
|
||||
}
|
||||
|
||||
return $page ?? false;
|
||||
// Nothing found
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user