2019-10-11 02:35:09 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core;
|
|
|
|
|
2023-01-03 06:45:39 -06:00
|
|
|
use BackedEnum;
|
2020-01-28 02:41:48 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use DateTimeInterface;
|
2022-01-10 07:37:44 -06:00
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
|
2024-03-07 03:03:11 -06:00
|
|
|
use GuzzleHttp\Psr7\Query;
|
2024-05-21 11:09:45 -05:00
|
|
|
use Hidehalo\Nanoid\Client as NanoidClient;
|
2021-05-22 08:09:14 -05:00
|
|
|
use Jaybizzle\CrawlerDetect\CrawlerDetect;
|
2022-05-23 14:19:59 -05:00
|
|
|
use Laminas\Filter\Word\CamelCaseToSeparator;
|
2023-01-02 12:28:32 -06:00
|
|
|
use Laminas\Filter\Word\CamelCaseToUnderscore;
|
2020-09-23 12:19:17 -05:00
|
|
|
use Laminas\InputFilter\InputFilter;
|
2024-07-17 12:51:13 -05:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
2021-02-09 15:11:09 -06:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2023-01-25 13:33:07 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlMode;
|
2019-10-11 02:35:09 -05:00
|
|
|
|
2023-11-30 07:34:21 -06:00
|
|
|
use function array_keys;
|
2023-11-29 05:34:13 -06:00
|
|
|
use function array_map;
|
2024-02-26 12:58:46 -06:00
|
|
|
use function array_pad;
|
2023-11-30 02:13:29 -06:00
|
|
|
use function array_reduce;
|
2022-04-15 12:57:27 -05:00
|
|
|
use function date_default_timezone_get;
|
2024-02-26 12:58:46 -06:00
|
|
|
use function explode;
|
2024-02-10 11:19:28 -06:00
|
|
|
use function implode;
|
2021-01-10 13:28:52 -06:00
|
|
|
use function is_array;
|
|
|
|
use function print_r;
|
2021-08-04 11:46:19 -05:00
|
|
|
use function Shlinkio\Shlink\Common\buildDateRange;
|
2024-03-07 03:03:11 -06:00
|
|
|
use function Shlinkio\Shlink\Core\ArrayUtils\map;
|
2020-01-29 03:53:06 -06:00
|
|
|
use function sprintf;
|
2021-01-11 08:20:26 -06:00
|
|
|
use function str_repeat;
|
2024-02-24 16:33:16 -06:00
|
|
|
use function str_replace;
|
2023-01-02 12:28:32 -06:00
|
|
|
use function strtolower;
|
2024-02-24 16:33:16 -06:00
|
|
|
use function trim;
|
2022-05-23 14:19:59 -05:00
|
|
|
use function ucfirst;
|
2020-01-29 03:53:06 -06:00
|
|
|
|
2023-01-25 13:33:07 -06:00
|
|
|
function generateRandomShortCode(int $length, ShortUrlMode $mode = ShortUrlMode::STRICT): string
|
2019-10-11 02:35:09 -05:00
|
|
|
{
|
2024-05-21 11:09:45 -05:00
|
|
|
static $nanoIdClient;
|
|
|
|
if ($nanoIdClient === null) {
|
|
|
|
$nanoIdClient = new NanoidClient();
|
2019-10-11 02:35:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-25 13:33:07 -06:00
|
|
|
$alphabet = $mode === ShortUrlMode::STRICT
|
|
|
|
? '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
: '0123456789abcdefghijklmnopqrstuvwxyz';
|
2024-05-21 11:09:45 -05:00
|
|
|
return $nanoIdClient->formattedId($alphabet, $length);
|
2019-10-11 02:35:09 -05:00
|
|
|
}
|
2020-01-28 02:41:48 -06:00
|
|
|
|
|
|
|
function parseDateFromQuery(array $query, string $dateName): ?Chronos
|
|
|
|
{
|
2022-12-04 13:35:38 -06:00
|
|
|
return normalizeOptionalDate(empty($query[$dateName] ?? null) ? null : Chronos::parse($query[$dateName]));
|
2020-01-28 02:41:48 -06:00
|
|
|
}
|
|
|
|
|
2021-02-09 15:11:09 -06:00
|
|
|
function parseDateRangeFromQuery(array $query, string $startDateName, string $endDateName): DateRange
|
|
|
|
{
|
|
|
|
$startDate = parseDateFromQuery($query, $startDateName);
|
|
|
|
$endDate = parseDateFromQuery($query, $endDateName);
|
|
|
|
|
2021-08-04 11:46:19 -05:00
|
|
|
return buildDateRange($startDate, $endDate);
|
2021-02-09 15:11:09 -06:00
|
|
|
}
|
|
|
|
|
2024-04-13 13:30:00 -05:00
|
|
|
function dateRangeToHumanFriendly(?DateRange $dateRange): string
|
|
|
|
{
|
|
|
|
$startDate = $dateRange?->startDate;
|
|
|
|
$endDate = $dateRange?->endDate;
|
|
|
|
|
|
|
|
return match (true) {
|
|
|
|
$startDate !== null && $endDate !== null => sprintf(
|
|
|
|
'Between %s and %s',
|
|
|
|
$startDate->toDateTimeString(),
|
|
|
|
$endDate->toDateTimeString(),
|
|
|
|
),
|
|
|
|
$startDate !== null => sprintf('Since %s', $startDate->toDateTimeString()),
|
|
|
|
$endDate !== null => sprintf('Until %s', $endDate->toDateTimeString()),
|
|
|
|
default => 'All time',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:35:38 -06:00
|
|
|
/**
|
|
|
|
* @return ($date is null ? null : Chronos)
|
|
|
|
*/
|
2023-11-30 02:13:29 -06:00
|
|
|
function normalizeOptionalDate(string|DateTimeInterface|Chronos|null $date): ?Chronos
|
2020-01-28 02:41:48 -06:00
|
|
|
{
|
2022-04-15 12:57:27 -05:00
|
|
|
$parsedDate = match (true) {
|
|
|
|
$date === null || $date instanceof Chronos => $date,
|
|
|
|
$date instanceof DateTimeInterface => Chronos::instance($date),
|
|
|
|
default => Chronos::parse($date),
|
|
|
|
};
|
2020-01-29 03:53:06 -06:00
|
|
|
|
2022-04-15 12:57:27 -05:00
|
|
|
return $parsedDate?->setTimezone(date_default_timezone_get());
|
2020-01-29 03:53:06 -06:00
|
|
|
}
|
2020-09-23 12:19:17 -05:00
|
|
|
|
2023-11-30 02:13:29 -06:00
|
|
|
function normalizeDate(string|DateTimeInterface|Chronos $date): Chronos
|
2022-12-04 13:35:38 -06:00
|
|
|
{
|
|
|
|
return normalizeOptionalDate($date);
|
|
|
|
}
|
2024-02-24 16:33:16 -06:00
|
|
|
|
|
|
|
function normalizeLocale(string $locale): string
|
|
|
|
{
|
|
|
|
return trim(strtolower(str_replace('_', '-', $locale)));
|
|
|
|
}
|
2024-02-26 12:58:46 -06:00
|
|
|
|
|
|
|
/**
|
2024-03-07 03:03:11 -06:00
|
|
|
* Parse an accept-language-like pattern into a list of locales, optionally filtering out those which do not match a
|
|
|
|
* minimum quality
|
|
|
|
*
|
2024-02-26 12:58:46 -06:00
|
|
|
* @param non-empty-string $acceptLanguage
|
2024-03-07 03:03:11 -06:00
|
|
|
* @return iterable<string>;
|
2024-02-26 12:58:46 -06:00
|
|
|
*/
|
2024-03-07 03:03:11 -06:00
|
|
|
function acceptLanguageToLocales(string $acceptLanguage, float $minQuality = 0): iterable
|
2024-02-26 12:58:46 -06:00
|
|
|
{
|
2024-03-07 03:03:11 -06:00
|
|
|
/** @var array{string, float|null}[] $acceptLanguagesList */
|
|
|
|
$acceptLanguagesList = map(explode(',', $acceptLanguage), static function (string $lang): array {
|
|
|
|
// Split locale/language and quality (en-US;q=0.7) -> [en-US, q=0.7]
|
|
|
|
[$lang, $qualityString] = array_pad(explode(';', $lang), length: 2, value: '');
|
|
|
|
$normalizedLang = normalizeLocale($lang);
|
|
|
|
$quality = Query::parse(trim($qualityString))['q'] ?? 1;
|
|
|
|
|
|
|
|
return [$normalizedLang, (float) $quality];
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($acceptLanguagesList as [$lang, $quality]) {
|
|
|
|
if ($lang !== '*' && $quality >= $minQuality) {
|
|
|
|
yield $lang;
|
|
|
|
}
|
|
|
|
}
|
2024-02-26 12:58:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits a locale into its corresponding language and country codes.
|
|
|
|
* The country code will be null if not present
|
|
|
|
* 'es-AR' -> ['es', 'AR']
|
|
|
|
* 'fr-FR' -> ['fr', 'FR']
|
|
|
|
* 'en' -> ['en', null]
|
|
|
|
*
|
|
|
|
* @return array{string, string|null}
|
|
|
|
*/
|
|
|
|
function splitLocale(string $locale): array
|
|
|
|
{
|
2024-07-31 12:53:05 -05:00
|
|
|
[$lang, $countryCode] = array_pad(explode('-', $locale), length: 2, value: null);
|
|
|
|
return [$lang, $countryCode];
|
2024-02-26 12:58:46 -06:00
|
|
|
}
|
2022-12-04 13:35:38 -06:00
|
|
|
|
2024-07-31 12:53:05 -05:00
|
|
|
/**
|
|
|
|
* @param InputFilter<mixed> $inputFilter
|
|
|
|
*/
|
2020-09-23 12:19:17 -05:00
|
|
|
function getOptionalIntFromInputFilter(InputFilter $inputFilter, string $fieldName): ?int
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return $value !== null ? (int) $value : null;
|
|
|
|
}
|
|
|
|
|
2024-07-31 12:53:05 -05:00
|
|
|
/**
|
|
|
|
* @param InputFilter<mixed> $inputFilter
|
|
|
|
*/
|
2020-09-23 12:19:17 -05:00
|
|
|
function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldName): ?bool
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return $value !== null ? (bool) $value : null;
|
|
|
|
}
|
2021-01-10 13:28:52 -06:00
|
|
|
|
2024-07-31 12:53:05 -05:00
|
|
|
/**
|
|
|
|
* @param InputFilter<mixed> $inputFilter
|
|
|
|
*/
|
2022-12-07 12:06:05 -06:00
|
|
|
function getNonEmptyOptionalValueFromInputFilter(InputFilter $inputFilter, string $fieldName): mixed
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return empty($value) ? null : $value;
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:20:26 -06:00
|
|
|
function arrayToString(array $array, int $indentSize = 4): string
|
2021-01-10 13:28:52 -06:00
|
|
|
{
|
2021-01-11 08:20:26 -06:00
|
|
|
$indent = str_repeat(' ', $indentSize);
|
2023-11-30 07:34:21 -06:00
|
|
|
$names = array_keys($array);
|
2021-01-11 08:20:26 -06:00
|
|
|
$index = 0;
|
|
|
|
|
2023-11-30 07:34:21 -06:00
|
|
|
return array_reduce($names, static function (string $acc, string $name) use (&$index, $indent, $array) {
|
2021-01-11 08:20:26 -06:00
|
|
|
$index++;
|
2023-11-30 07:34:21 -06:00
|
|
|
$messages = $array[$name];
|
2021-01-11 08:20:26 -06:00
|
|
|
|
|
|
|
return $acc . sprintf(
|
|
|
|
"%s%s'%s' => %s",
|
|
|
|
$index === 1 ? '' : "\n",
|
|
|
|
$indent,
|
|
|
|
$name,
|
|
|
|
is_array($messages) ? print_r($messages, true) : $messages,
|
|
|
|
);
|
|
|
|
}, '');
|
2021-01-10 13:28:52 -06:00
|
|
|
}
|
2021-01-30 03:54:04 -06:00
|
|
|
|
2021-05-22 08:09:14 -05:00
|
|
|
function isCrawler(string $userAgent): bool
|
|
|
|
{
|
|
|
|
static $detector;
|
|
|
|
if ($detector === null) {
|
|
|
|
$detector = new CrawlerDetect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $detector->isCrawler($userAgent);
|
|
|
|
}
|
2022-01-10 07:37:44 -06:00
|
|
|
|
2022-04-15 12:57:27 -05:00
|
|
|
function determineTableName(string $tableName, array $emConfig = []): string
|
|
|
|
{
|
|
|
|
$schema = $emConfig['connection']['schema'] ?? null;
|
|
|
|
// $tablePrefix = $emConfig['connection']['table_prefix'] ?? null; // TODO
|
|
|
|
|
|
|
|
if ($schema === null) {
|
|
|
|
return $tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('%s.%s', $schema, $tableName);
|
|
|
|
}
|
|
|
|
|
2022-01-10 07:37:44 -06:00
|
|
|
function fieldWithUtf8Charset(FieldBuilder $field, array $emConfig, string $collation = 'unicode_ci'): FieldBuilder
|
|
|
|
{
|
|
|
|
return match ($emConfig['connection']['driver'] ?? null) {
|
|
|
|
'pdo_mysql' => $field->option('charset', 'utf8mb4')
|
|
|
|
->option('collation', 'utf8mb4_' . $collation),
|
|
|
|
default => $field,
|
|
|
|
};
|
|
|
|
}
|
2022-05-23 14:19:59 -05:00
|
|
|
|
|
|
|
function camelCaseToHumanFriendly(string $value): string
|
|
|
|
{
|
|
|
|
static $filter;
|
|
|
|
if ($filter === null) {
|
|
|
|
$filter = new CamelCaseToSeparator(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return ucfirst($filter->filter($value));
|
|
|
|
}
|
2022-08-14 06:12:10 -05:00
|
|
|
|
2023-01-02 12:28:32 -06:00
|
|
|
function camelCaseToSnakeCase(string $value): string
|
|
|
|
{
|
|
|
|
static $filter;
|
|
|
|
if ($filter === null) {
|
|
|
|
$filter = new CamelCaseToUnderscore();
|
|
|
|
}
|
|
|
|
|
|
|
|
return strtolower($filter->filter($value));
|
|
|
|
}
|
|
|
|
|
2022-08-14 06:12:10 -05:00
|
|
|
function toProblemDetailsType(string $errorCode): string
|
|
|
|
{
|
|
|
|
return sprintf('https://shlink.io/api/error/%s', $errorCode);
|
|
|
|
}
|
2023-01-03 06:45:39 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<BackedEnum> $enum
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
function enumValues(string $enum): array
|
|
|
|
{
|
|
|
|
static $cache;
|
|
|
|
if ($cache === null) {
|
|
|
|
$cache = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cache[$enum] ?? (
|
2023-11-29 05:34:13 -06:00
|
|
|
$cache[$enum] = array_map(static fn (BackedEnum $type) => (string) $type->value, $enum::cases())
|
2023-01-03 06:45:39 -06:00
|
|
|
);
|
|
|
|
}
|
2024-02-10 11:19:28 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<BackedEnum> $enum
|
|
|
|
*/
|
|
|
|
function enumToString(string $enum): string
|
|
|
|
{
|
|
|
|
return sprintf('["%s"]', implode('", "', enumValues($enum)));
|
|
|
|
}
|
2024-07-05 01:52:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Split provided string by comma and return a list of the results.
|
|
|
|
* An empty array is returned if provided value is empty
|
|
|
|
*/
|
|
|
|
function splitByComma(?string $value): array
|
|
|
|
{
|
|
|
|
if ($value === null || trim($value) === '') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_map(trim(...), explode(',', $value));
|
|
|
|
}
|
2024-07-17 12:51:13 -05:00
|
|
|
|
|
|
|
function ipAddressFromRequest(ServerRequestInterface $request): ?string
|
|
|
|
{
|
|
|
|
return $request->getAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR);
|
|
|
|
}
|