|
@@ -21,6 +21,7 @@ use Grav\Common\Page\Markdown\Excerpts;
|
|
|
use Grav\Common\Page\Pages;
|
|
|
use Grav\Framework\Flex\Flex;
|
|
|
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
|
|
|
+use Grav\Framework\Media\Interfaces\MediaInterface;
|
|
|
use InvalidArgumentException;
|
|
|
use Negotiation\Accept;
|
|
|
use Negotiation\Negotiator;
|
|
@@ -150,7 +151,7 @@ abstract class Utils
|
|
|
|
|
|
$domain = $domain ?: $grav['config']->get('system.absolute_urls', false);
|
|
|
|
|
|
- return rtrim($uri->rootUrl($domain), '/') . '/' . ($resource ?? '');
|
|
|
+ return rtrim($uri->rootUrl($domain), '/') . '/' . ($resource ?: '');
|
|
|
}
|
|
|
|
|
|
|
|
@@ -166,9 +167,9 @@ abstract class Utils
|
|
|
|
|
|
if ($locator->isStream($path)) {
|
|
|
$path = $locator->findResource($path, true);
|
|
|
- } elseif (!Utils::startsWith($path, GRAV_ROOT)) {
|
|
|
+ } elseif (!static::startsWith($path, GRAV_ROOT)) {
|
|
|
$base_url = Grav::instance()['base_url'];
|
|
|
- $path = GRAV_ROOT . '/' . ltrim(Utils::replaceFirstOccurrence($base_url, '', $path), '/');
|
|
|
+ $path = GRAV_ROOT . '/' . ltrim(static::replaceFirstOccurrence($base_url, '', $path), '/');
|
|
|
}
|
|
|
|
|
|
return $path;
|
|
@@ -628,6 +629,23 @@ abstract class Utils
|
|
|
return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $length);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * Generates a random string with configurable length, prefix and suffix.
|
|
|
+ * Unlike the built-in `uniqid()`, this string is non-conflicting and safe
|
|
|
+ *
|
|
|
+ * @param int $length
|
|
|
+ * @param array $options
|
|
|
+ * @return string
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static function uniqueId(int $length = 13, array $options = []): string
|
|
|
+ {
|
|
|
+ $options = array_merge(['prefix' => '', 'suffix' => ''], $options);
|
|
|
+ $bytes = random_bytes(ceil($length / 2));
|
|
|
+
|
|
|
+ return $options['prefix'] . substr(bin2hex($bytes), 0, $length) . $options['suffix'];
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* Provides the ability to download a file to the browser
|
|
|
*
|
|
@@ -750,13 +768,13 @@ abstract class Utils
|
|
|
if (is_string($http_accept)) {
|
|
|
$negotiator = new Negotiator();
|
|
|
|
|
|
- $supported_types = Utils::getSupportPageTypes(['html', 'json']);
|
|
|
- $priorities = Utils::getMimeTypes($supported_types);
|
|
|
+ $supported_types = static::getSupportPageTypes(['html', 'json']);
|
|
|
+ $priorities = static::getMimeTypes($supported_types);
|
|
|
|
|
|
$media_type = $negotiator->getBest($http_accept, $priorities);
|
|
|
$mimetype = $media_type instanceof Accept ? $media_type->getValue() : '';
|
|
|
|
|
|
- return Utils::getExtensionByMime($mimetype);
|
|
|
+ return static::getExtensionByMime($mimetype);
|
|
|
}
|
|
|
|
|
|
return 'html';
|
|
@@ -791,13 +809,7 @@ abstract class Utils
|
|
|
|
|
|
$media_types = Grav::instance()['config']->get('media.types');
|
|
|
|
|
|
- if (isset($media_types[$extension])) {
|
|
|
- if (isset($media_types[$extension]['mime'])) {
|
|
|
- return $media_types[$extension]['mime'];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $default;
|
|
|
+ return $media_types[$extension]['mime'] ?? $default;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -1555,7 +1567,7 @@ abstract class Utils
|
|
|
|
|
|
switch ($matches[0]) {
|
|
|
case 'self':
|
|
|
- if (null === $object) {
|
|
|
+ if (!$object instanceof MediaInterface) {
|
|
|
throw new RuntimeException(sprintf('Page not available for self@ reference: %s', $path));
|
|
|
}
|
|
|
|
|
@@ -1629,7 +1641,7 @@ abstract class Utils
|
|
|
* @param string $path
|
|
|
* @return string[]|null
|
|
|
*/
|
|
|
- private static function resolveTokenPath(string $path): ?array
|
|
|
+ protected static function resolveTokenPath(string $path): ?array
|
|
|
{
|
|
|
if (strpos($path, '@') !== false) {
|
|
|
$regex = '/^(@\w+|\w+@|@\w+@)([^:]*)(.*)$/u';
|
|
@@ -1739,7 +1751,7 @@ abstract class Utils
|
|
|
{
|
|
|
$enc_url = preg_replace_callback(
|
|
|
'%[^:/@?&=#]+%usD',
|
|
|
- function ($matches) {
|
|
|
+ static function ($matches) {
|
|
|
return urlencode($matches[0]);
|
|
|
},
|
|
|
$url
|
|
@@ -1763,7 +1775,7 @@ abstract class Utils
|
|
|
*
|
|
|
* @param string $string
|
|
|
* @param bool $block Block or Line processing
|
|
|
- * @param null $page
|
|
|
+ * @param PageInterface|null $page
|
|
|
* @return string
|
|
|
* @throws Exception
|
|
|
*/
|