Added date validation in post search queries

This commit is contained in:
Marcin Kurczewski 2014-08-15 10:06:20 +02:00
parent a6f71d73c8
commit e93c447758

View file

@ -292,19 +292,31 @@ class PostSearchParser extends AbstractSearchParser
protected static function parseDate($value) protected static function parseDate($value)
{ {
list ($year, $month, $day) = explode('-', $value . '-0-0'); $value = trim($value);
$year = intval($year); if (preg_match('/^(\d{4})$/', $value, $matches))
$month = intval($month); {
$day = intval($day); $year = intval($matches[1]);
$yearMin = $yearMax = $year; $timeMin = mktime(0, 0, 0, 1, 1, $year);
$monthMin = $monthMax = $month; $timeMax = mktime(0, 0, -1, 1, 1, $year + 1);
$monthMin = $monthMin ?: 1; }
$monthMax = $monthMax ?: 12; elseif (preg_match('/^(\d{4})-(\d{1,2})$/', $value, $matches))
$dayMin = $dayMax = $day; {
$dayMin = $dayMin ?: 1; $year = intval($matches[1]);
$dayMax = $dayMax ?: intval(date('t', mktime(0, 0, 0, $monthMax, 1, $year))); $month = intval($matches[2]);
$timeMin = mktime(0, 0, 0, $monthMin, $dayMin, $yearMin); $timeMin = mktime(0, 0, 0, $month, 1, $year);
$timeMax = mktime(0, 0, -1, $monthMax, $dayMax+1, $yearMax); $timeMax = mktime(0, 0, -1, $month + 1, 1, $year);
}
elseif (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value, $matches))
{
$year = intval($matches[1]);
$month = intval($matches[2]);
$day = intval($matches[3]);
$timeMin = mktime(0, 0, 0, $month, $day, $year);
$timeMax = mktime(0, 0, -1, $month, $day + 1, $year);
}
else
throw new SimpleException('Invalid date format: ' . $value);
return [$timeMin, $timeMax]; return [$timeMin, $timeMax];
} }
} }