Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw date exception when a fictitious is provided by a URL parameter #22574

Draft
wants to merge 1 commit into
base: 5.x-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public static function factory($dateString, $timezone = null)
$date = self::lastMonth();
} elseif (is_string($dateString) && preg_match('/last[ -]?year/i', urldecode($dateString))) {
$date = self::lastYear();
} else if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', urldecode($dateString))) {
if (!self::isDateStringValid($dateString)) {
throw self::getFictiousDateException($dateString);
}
$date = new Date(strtotime($dateString));
} elseif (
!is_int($dateString)
&& (
Expand Down Expand Up @@ -175,6 +180,23 @@ public static function factory($dateString, $timezone = null)
return Date::factory($timestamp);
}

/**
* Returns if the date string is valid
*
* @param $dateString
* @return Boolean
* @ignore
*/
public static function isDateStringValid($dateString)
{
$ret = date_parse($dateString);
if ($ret['warning_count'] || $ret['error_count']) {
return false;
}
return true;
}


/**
* Returns Date w/ UTC timestamp of time $dateString/$timezone.
* (Only applies to special strings, like 'now','today','yesterday','yesterdaySameTime'.
Expand Down Expand Up @@ -1139,6 +1161,13 @@ public static function secondsToDays($secs)
return $secs / self::NUM_SECONDS_IN_DAY;
}

private static function getFictiousDateException($dateString)
{
$message = Piwik::translate('General_ExceptionFictiousDate');
return new Exception($message . ": $dateString");
}


private static function getInvalidDateFormatException($dateString)
{
$message = Piwik::translate('General_ExceptionInvalidDateFormat', array("YYYY-MM-DD, or 'today' or 'yesterday'", "strtotime", "http://php.net/strtotime"));
Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
"ExceptionIncompatibleClientServerVersions": "Your %1$s client version is %2$s which is incompatible with server version %3$s.",
"ExceptionInvalidAggregateReportsFormat": "Aggregate reports format '%1$s' not valid. Try any of the following instead: %2$s.",
"ExceptionInvalidArchiveTimeToLive": "Today archive time to live must be a number of seconds greater than zero",
"ExceptionFictiousDate": "This date does not exist.",
"ExceptionInvalidDateBeforeFirstWebsite": "The date '%1$s' is a date before first website was online. Try date that's after %2$s (timestamp %3$s).",
"ExceptionInvalidDateFormat": "Date format must be: %1$s or any keyword supported by the %2$s function (see %3$s for more information)",
"ExceptionInvalidDateRange": "The date '%1$s' is not a correct date range. It should have the following format: %2$s.",
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPUnit/Unit/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@ public function testIsLeapYear()
}
}

public function testIsDateStringValid()
{
$this->assertTrue(Date::isDateStringValid('2024-02-29'));
$this->assertFalse(Date::isDateStringValid('2025-02-29'));
$this->assertFalse(Date::isDateStringValid('2025-09-31'));
$this->assertTrue(Date::isDateStringValid('2025-03-31'));
}


public function getLocalizedLongStrings()
{
Expand Down