2019-10-11 02:35:09 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core;
|
|
|
|
|
2020-01-28 02:41:48 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use DateTimeInterface;
|
2020-06-17 12:01:56 -05:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2020-09-23 12:19:17 -05:00
|
|
|
use Laminas\InputFilter\InputFilter;
|
2019-10-11 02:35:09 -05:00
|
|
|
use PUGX\Shortid\Factory as ShortIdFactory;
|
|
|
|
|
2021-01-10 13:28:52 -06:00
|
|
|
use function Functional\reduce_left;
|
|
|
|
use function is_array;
|
2021-01-30 03:54:04 -06:00
|
|
|
use function lcfirst;
|
2021-01-10 13:28:52 -06:00
|
|
|
use function print_r;
|
2020-01-29 03:53:06 -06:00
|
|
|
use function sprintf;
|
2021-01-11 08:20:26 -06:00
|
|
|
use function str_repeat;
|
2021-01-30 03:54:04 -06:00
|
|
|
use function str_replace;
|
|
|
|
use function ucwords;
|
2020-01-29 03:53:06 -06:00
|
|
|
|
2020-06-20 04:21:37 -05:00
|
|
|
const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15;
|
2020-02-18 11:54:40 -06:00
|
|
|
const DEFAULT_SHORT_CODES_LENGTH = 5;
|
2020-02-18 12:34:01 -06:00
|
|
|
const MIN_SHORT_CODES_LENGTH = 4;
|
2020-06-17 12:01:56 -05:00
|
|
|
const DEFAULT_REDIRECT_STATUS_CODE = StatusCodeInterface::STATUS_FOUND;
|
|
|
|
const DEFAULT_REDIRECT_CACHE_LIFETIME = 30;
|
2020-03-22 11:42:56 -05:00
|
|
|
const LOCAL_LOCK_FACTORY = 'Shlinkio\Shlink\LocalLockFactory';
|
2020-12-02 05:02:49 -06:00
|
|
|
const CUSTOM_SLUGS_REGEXP = '/[^\pL\pN._~]/u'; // Any unicode letter or number, plus ".", "_" and "~" chars
|
2021-02-03 04:53:08 -06:00
|
|
|
const TITLE_TAG_VALUE = '/<title[^>]*>(.*?)<\/title>/i'; // Matches the value inside an html title tag
|
2020-02-18 11:54:40 -06:00
|
|
|
|
|
|
|
function generateRandomShortCode(int $length): string
|
2019-10-11 02:35:09 -05:00
|
|
|
{
|
|
|
|
static $shortIdFactory;
|
|
|
|
if ($shortIdFactory === null) {
|
|
|
|
$shortIdFactory = new ShortIdFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
$alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
return $shortIdFactory->generate($length, $alphabet)->serialize();
|
|
|
|
}
|
2020-01-28 02:41:48 -06: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 03:53:06 -06: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 12:19:17 -05: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 13:28:52 -06:00
|
|
|
|
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);
|
|
|
|
$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 13:28:52 -06:00
|
|
|
}
|
2021-01-30 03:54:04 -06:00
|
|
|
|
|
|
|
function kebabCaseToCamelCase(string $name): string
|
|
|
|
{
|
|
|
|
return lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $name))));
|
|
|
|
}
|