2019-10-11 10:35:09 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core;
|
|
|
|
|
2020-01-28 11:41:48 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use DateTimeInterface;
|
2020-06-17 20:01:56 +03:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2021-05-22 16:09:14 +03:00
|
|
|
use Jaybizzle\CrawlerDetect\CrawlerDetect;
|
2020-09-23 20:19:17 +03:00
|
|
|
use Laminas\InputFilter\InputFilter;
|
2019-10-11 10:35:09 +03:00
|
|
|
use PUGX\Shortid\Factory as ShortIdFactory;
|
2021-02-10 00:11:09 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2019-10-11 10:35:09 +03:00
|
|
|
|
2021-01-10 22:28:52 +03:00
|
|
|
use function Functional\reduce_left;
|
|
|
|
use function is_array;
|
2021-01-30 12:54:04 +03:00
|
|
|
use function lcfirst;
|
2021-01-10 22:28:52 +03:00
|
|
|
use function print_r;
|
2020-01-29 12:53:06 +03:00
|
|
|
use function sprintf;
|
2021-01-11 17:20:26 +03:00
|
|
|
use function str_repeat;
|
2021-01-30 12:54:04 +03:00
|
|
|
use function str_replace;
|
|
|
|
use function ucwords;
|
2020-01-29 12:53:06 +03:00
|
|
|
|
2020-06-20 12:21:37 +03:00
|
|
|
const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15;
|
2020-02-18 20:54:40 +03:00
|
|
|
const DEFAULT_SHORT_CODES_LENGTH = 5;
|
2020-02-18 21:34:01 +03:00
|
|
|
const MIN_SHORT_CODES_LENGTH = 4;
|
2020-06-17 20:01:56 +03:00
|
|
|
const DEFAULT_REDIRECT_STATUS_CODE = StatusCodeInterface::STATUS_FOUND;
|
|
|
|
const DEFAULT_REDIRECT_CACHE_LIFETIME = 30;
|
2020-03-22 19:42:56 +03:00
|
|
|
const LOCAL_LOCK_FACTORY = 'Shlinkio\Shlink\LocalLockFactory';
|
2020-12-02 14:02:49 +03:00
|
|
|
const CUSTOM_SLUGS_REGEXP = '/[^\pL\pN._~]/u'; // Any unicode letter or number, plus ".", "_" and "~" chars
|
2021-02-03 13:53:08 +03:00
|
|
|
const TITLE_TAG_VALUE = '/<title[^>]*>(.*?)<\/title>/i'; // Matches the value inside an html title tag
|
2020-02-18 20:54:40 +03:00
|
|
|
|
|
|
|
function generateRandomShortCode(int $length): string
|
2019-10-11 10:35:09 +03:00
|
|
|
{
|
|
|
|
static $shortIdFactory;
|
|
|
|
if ($shortIdFactory === null) {
|
|
|
|
$shortIdFactory = new ShortIdFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
$alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
return $shortIdFactory->generate($length, $alphabet)->serialize();
|
|
|
|
}
|
2020-01-28 11:41:48 +03:00
|
|
|
|
|
|
|
function parseDateFromQuery(array $query, string $dateName): ?Chronos
|
|
|
|
{
|
|
|
|
return ! isset($query[$dateName]) || empty($query[$dateName]) ? null : Chronos::parse($query[$dateName]);
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:11:09 +03:00
|
|
|
function parseDateRangeFromQuery(array $query, string $startDateName, string $endDateName): DateRange
|
|
|
|
{
|
|
|
|
$startDate = parseDateFromQuery($query, $startDateName);
|
|
|
|
$endDate = parseDateFromQuery($query, $endDateName);
|
|
|
|
|
2021-05-23 12:57:31 +03:00
|
|
|
return match (true) {
|
|
|
|
$startDate === null && $endDate === null => DateRange::emptyInstance(),
|
|
|
|
$startDate !== null && $endDate !== null => DateRange::withStartAndEndDate($startDate, $endDate),
|
|
|
|
$startDate !== null => DateRange::withStartDate($startDate),
|
|
|
|
default => DateRange::withEndDate($endDate),
|
|
|
|
};
|
2021-02-10 00:11:09 +03:00
|
|
|
}
|
|
|
|
|
2020-01-28 11:41:48 +03:00
|
|
|
/**
|
|
|
|
* @param string|DateTimeInterface|Chronos|null $date
|
|
|
|
*/
|
|
|
|
function parseDateField($date): ?Chronos
|
|
|
|
{
|
|
|
|
if ($date === null || $date instanceof Chronos) {
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($date instanceof DateTimeInterface) {
|
|
|
|
return Chronos::instance($date);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Chronos::parse($date);
|
|
|
|
}
|
2020-01-29 12:53:06 +03: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);
|
|
|
|
}
|
2020-09-23 20:19:17 +03:00
|
|
|
|
|
|
|
function getOptionalIntFromInputFilter(InputFilter $inputFilter, string $fieldName): ?int
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return $value !== null ? (int) $value : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldName): ?bool
|
|
|
|
{
|
|
|
|
$value = $inputFilter->getValue($fieldName);
|
|
|
|
return $value !== null ? (bool) $value : null;
|
|
|
|
}
|
2021-01-10 22:28:52 +03:00
|
|
|
|
2021-01-11 17:20:26 +03:00
|
|
|
function arrayToString(array $array, int $indentSize = 4): string
|
2021-01-10 22:28:52 +03:00
|
|
|
{
|
2021-01-11 17:20:26 +03:00
|
|
|
$indent = str_repeat(' ', $indentSize);
|
|
|
|
$index = 0;
|
|
|
|
|
|
|
|
return reduce_left($array, static function ($messages, string $name, $_, string $acc) use (&$index, $indent) {
|
|
|
|
$index++;
|
|
|
|
|
|
|
|
return $acc . sprintf(
|
|
|
|
"%s%s'%s' => %s",
|
|
|
|
$index === 1 ? '' : "\n",
|
|
|
|
$indent,
|
|
|
|
$name,
|
|
|
|
is_array($messages) ? print_r($messages, true) : $messages,
|
|
|
|
);
|
|
|
|
}, '');
|
2021-01-10 22:28:52 +03:00
|
|
|
}
|
2021-01-30 12:54:04 +03:00
|
|
|
|
|
|
|
function kebabCaseToCamelCase(string $name): string
|
|
|
|
{
|
|
|
|
return lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $name))));
|
|
|
|
}
|
2021-05-22 16:09:14 +03:00
|
|
|
|
|
|
|
function isCrawler(string $userAgent): bool
|
|
|
|
{
|
|
|
|
static $detector;
|
|
|
|
if ($detector === null) {
|
|
|
|
$detector = new CrawlerDetect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $detector->isCrawler($userAgent);
|
|
|
|
}
|