From c20c3801a8b16a93e170adc919ebbaf9b4585eca Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 15 Apr 2022 19:57:27 +0200 Subject: [PATCH] Ensured all input dates are changed to the default timezone before being used on any inner layer --- module/Core/functions/functions.php | 43 +++++++++++------------ module/Core/src/Model/ShortUrlEdit.php | 6 ++-- module/Core/src/Model/ShortUrlMeta.php | 6 ++-- module/Core/src/Model/ShortUrlsParams.php | 6 ++-- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php index 567fde47..db9a11b9 100644 --- a/module/Core/functions/functions.php +++ b/module/Core/functions/functions.php @@ -12,6 +12,7 @@ use Laminas\InputFilter\InputFilter; use PUGX\Shortid\Factory as ShortIdFactory; use Shlinkio\Shlink\Common\Util\DateRange; +use function date_default_timezone_get; use function Functional\reduce_left; use function is_array; use function print_r; @@ -32,7 +33,7 @@ function generateRandomShortCode(int $length): string function parseDateFromQuery(array $query, string $dateName): ?Chronos { - return empty($query[$dateName] ?? null) ? null : Chronos::parse($query[$dateName]); + return normalizeDate(empty($query[$dateName] ?? null) ? null : Chronos::parse($query[$dateName])); } function parseDateRangeFromQuery(array $query, string $startDateName, string $endDateName): DateRange @@ -43,29 +44,15 @@ function parseDateRangeFromQuery(array $query, string $startDateName, string $en return buildDateRange($startDate, $endDate); } -function parseDateField(string|DateTimeInterface|Chronos|null $date): ?Chronos +function normalizeDate(string|DateTimeInterface|Chronos|null $date): ?Chronos { - if ($date === null || $date instanceof Chronos) { - return $date; - } + $parsedDate = match (true) { + $date === null || $date instanceof Chronos => $date, + $date instanceof DateTimeInterface => Chronos::instance($date), + default => Chronos::parse($date), + }; - if ($date instanceof DateTimeInterface) { - return Chronos::instance($date); - } - - return Chronos::parse($date); -} - -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); + return $parsedDate?->setTimezone(date_default_timezone_get()); } function getOptionalIntFromInputFilter(InputFilter $inputFilter, string $fieldName): ?int @@ -108,6 +95,18 @@ function isCrawler(string $userAgent): bool return $detector->isCrawler($userAgent); } +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); +} + function fieldWithUtf8Charset(FieldBuilder $field, array $emConfig, string $collation = 'unicode_ci'): FieldBuilder { return match ($emConfig['connection']['driver'] ?? null) { diff --git a/module/Core/src/Model/ShortUrlEdit.php b/module/Core/src/Model/ShortUrlEdit.php index d27d1fe6..325ee339 100644 --- a/module/Core/src/Model/ShortUrlEdit.php +++ b/module/Core/src/Model/ShortUrlEdit.php @@ -12,7 +12,7 @@ use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter; use function array_key_exists; use function Shlinkio\Shlink\Core\getOptionalBoolFromInputFilter; use function Shlinkio\Shlink\Core\getOptionalIntFromInputFilter; -use function Shlinkio\Shlink\Core\parseDateField; +use function Shlinkio\Shlink\Core\normalizeDate; final class ShortUrlEdit implements TitleResolutionModelInterface { @@ -69,8 +69,8 @@ final class ShortUrlEdit implements TitleResolutionModelInterface $this->forwardQueryPropWasProvided = array_key_exists(ShortUrlInputFilter::FORWARD_QUERY, $data); $this->longUrl = $inputFilter->getValue(ShortUrlInputFilter::LONG_URL); - $this->validSince = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE)); - $this->validUntil = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL)); + $this->validSince = normalizeDate($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE)); + $this->validUntil = normalizeDate($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL)); $this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS); $this->validateUrl = getOptionalBoolFromInputFilter($inputFilter, ShortUrlInputFilter::VALIDATE_URL) ?? false; $this->tags = $inputFilter->getValue(ShortUrlInputFilter::TAGS); diff --git a/module/Core/src/Model/ShortUrlMeta.php b/module/Core/src/Model/ShortUrlMeta.php index 86f2c9d1..f43f929d 100644 --- a/module/Core/src/Model/ShortUrlMeta.php +++ b/module/Core/src/Model/ShortUrlMeta.php @@ -12,7 +12,7 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey; use function Shlinkio\Shlink\Core\getOptionalBoolFromInputFilter; use function Shlinkio\Shlink\Core\getOptionalIntFromInputFilter; -use function Shlinkio\Shlink\Core\parseDateField; +use function Shlinkio\Shlink\Core\normalizeDate; use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH; @@ -68,8 +68,8 @@ final class ShortUrlMeta implements TitleResolutionModelInterface } $this->longUrl = $inputFilter->getValue(ShortUrlInputFilter::LONG_URL); - $this->validSince = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE)); - $this->validUntil = parseDateField($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL)); + $this->validSince = normalizeDate($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE)); + $this->validUntil = normalizeDate($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL)); $this->customSlug = $inputFilter->getValue(ShortUrlInputFilter::CUSTOM_SLUG); $this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS); $this->findIfExists = $inputFilter->getValue(ShortUrlInputFilter::FIND_IF_EXISTS); diff --git a/module/Core/src/Model/ShortUrlsParams.php b/module/Core/src/Model/ShortUrlsParams.php index 9abfd10f..95cf4df6 100644 --- a/module/Core/src/Model/ShortUrlsParams.php +++ b/module/Core/src/Model/ShortUrlsParams.php @@ -9,7 +9,7 @@ use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Validation\ShortUrlsParamsInputFilter; use function Shlinkio\Shlink\Common\buildDateRange; -use function Shlinkio\Shlink\Core\parseDateField; +use function Shlinkio\Shlink\Core\normalizeDate; final class ShortUrlsParams { @@ -61,8 +61,8 @@ final class ShortUrlsParams $this->searchTerm = $inputFilter->getValue(ShortUrlsParamsInputFilter::SEARCH_TERM); $this->tags = (array) $inputFilter->getValue(ShortUrlsParamsInputFilter::TAGS); $this->dateRange = buildDateRange( - parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)), - parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)), + normalizeDate($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)), + normalizeDate($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)), ); $this->orderBy = Ordering::fromTuple($inputFilter->getValue(ShortUrlsParamsInputFilter::ORDER_BY)); $this->itemsPerPage = (int) (