Merge pull request #251 from virtualtam/linkdb/date-format

LinkDB::filterDay(): check input date format
This commit is contained in:
VirtualTam 2015-07-09 01:00:40 +02:00
commit 7f1dfd1c12
5 changed files with 58 additions and 12 deletions

View file

@ -375,7 +375,10 @@ public function filterTags($tags, $casesensitive=false)
*/ */
public function filterDay($day) public function filterDay($day)
{ {
// TODO: check input format if (! checkDateFormat('Ymd', $day)) {
throw new Exception('Invalid date format');
}
$filtered = array(); $filtered = array();
foreach ($this->links as $l) { foreach ($this->links as $l) {
if (startsWith($l['linkdate'], $day)) { if (startsWith($l['linkdate'], $day)) {

View file

@ -69,4 +69,19 @@ function sanitizeLink(&$link)
$link['description'] = escape($link['description']); $link['description'] = escape($link['description']);
$link['tags'] = escape($link['tags']); $link['tags'] = escape($link['tags']);
} }
/**
* Checks if a string represents a valid date
*
* @param string a string-formatted date
* @param format the expected DateTime format of the string
* @return whether the string is a valid date
* @see http://php.net/manual/en/class.datetime.php
* @see http://php.net/manual/en/datetime.createfromformat.php
*/
function checkDateFormat($format, $string)
{
$date = DateTime::createFromFormat($format, $string);
return $date && $date->format($string) == $string;
}
?> ?>

View file

@ -957,7 +957,13 @@ function showDaily()
if ($i<count($days)-1) $nextday=$days[$i+1]; if ($i<count($days)-1) $nextday=$days[$i+1];
} }
$linksToDisplay=$LINKSDB->filterDay($day); try {
$linksToDisplay = $LINKSDB->filterDay($day);
} catch (Exception $exc) {
error_log($exc);
$linksToDisplay = [];
}
// We pre-format some fields for proper output. // We pre-format some fields for proper output.
foreach($linksToDisplay as $key=>$link) foreach($linksToDisplay as $key=>$link)
{ {

View file

@ -396,19 +396,22 @@ public function testFilterUnknownDay()
/** /**
* Use an invalid date format * Use an invalid date format
* @expectedException Exception
* @expectedExceptionMessageRegExp /Invalid date format/
*/ */
public function testFilterInvalidDay() public function testFilterInvalidDayWithChars()
{ {
$this->assertEquals( self::$privateLinkDB->filterDay('Rainy day, dream away');
0, }
sizeof(self::$privateLinkDB->filterDay('Rainy day, dream away'))
);
// TODO: check input format /**
$this->assertEquals( * Use an invalid date format
6, * @expectedException Exception
sizeof(self::$privateLinkDB->filterDay('20')) * @expectedExceptionMessageRegExp /Invalid date format/
); */
public function testFilterInvalidDayDigits()
{
self::$privateLinkDB->filterDay('20');
} }
/** /**

View file

@ -74,5 +74,24 @@ public function testEndsWithSpecialChars()
$this->assertTrue(endsWith('å!ùµ', 'ùµ', false)); $this->assertTrue(endsWith('å!ùµ', 'ùµ', false));
$this->assertTrue(endsWith('µ$åù', 'åù', true)); $this->assertTrue(endsWith('µ$åù', 'åù', true));
} }
/**
* Check valid date strings, according to a DateTime format
*/
public function testCheckValidDateFormat()
{
$this->assertTrue(checkDateFormat('Ymd', '20150627'));
$this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27'));
}
/**
* Check erroneous date strings, according to a DateTime format
*/
public function testCheckInvalidDateFormat()
{
$this->assertFalse(checkDateFormat('Ymd', '2015'));
$this->assertFalse(checkDateFormat('Y-m-d', '2015-06'));
$this->assertFalse(checkDateFormat('Ymd', 'DeLorean'));
}
} }
?> ?>