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;
|
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;
|
|
|
|
|
2020-01-29 12:53:06 +03:00
|
|
|
use function sprintf;
|
|
|
|
|
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-08-09 11:24:59 +03:00
|
|
|
const CUSTOM_SLUGS_REGEXP = '/[^A-Za-z0-9._~]+/';
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|