mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Added true unique Utils::uniqueId() / {{ unique_id() }}utilities with length, prefix, and suffix support.
This commit is contained in:
parent
63661a40e3
commit
ae8dfde69d
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
1. [](#new)
|
||||
* Added method `Pages::referrerRoute()` to get the referrer route and language
|
||||
* Added true unique `Utils::uniqueId()` / `{{ unique_id() }}`utilities with length, prefix, and suffix support.
|
||||
2. [](#improved)
|
||||
* Replaced GPL `SVG-Sanitizer` with MIT licensed `DOM-Sanitizer`
|
||||
* `Uri::referrer()` now accepts third parameter, if set to `true`, it returns route without base or language code [#3411](https://github.com/getgrav/grav/issues/3411)
|
||||
|
|
|
|||
|
|
@ -217,6 +217,7 @@ class GravExtension extends AbstractExtension implements GlobalsInterface
|
|||
new TwigFunction('cron', [$this, 'cronFunc']),
|
||||
new TwigFunction('svg_image', [$this, 'svgImageFunction']),
|
||||
new TwigFunction('xss', [$this, 'xssFunc']),
|
||||
new TwigFunction('unique_id', [$this, 'uniqueId']),
|
||||
|
||||
// Translations
|
||||
new TwigFunction('t', [$this, 'translate'], ['needs_environment' => true]),
|
||||
|
|
@ -654,6 +655,20 @@ class GravExtension extends AbstractExtension implements GlobalsInterface
|
|||
return implode(', ', $results_parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 function uniqueId(int $length = 9, array $options = ['prefix' => '', 'suffix' => '']): string
|
||||
{
|
||||
return Utils::uniqueId($length, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return string
|
||||
|
|
|
|||
|
|
@ -628,6 +628,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
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user