From: VirtualTam Date: Thu, 9 Jul 2015 19:34:46 +0000 (+0200) Subject: Merge pull request #255 from ArthurHoaro/config X-Git-Tag: v0.5.0~13 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=e92f1ba59edb9fd60b185dc633e64a62dffe3b04;hp=dd484b90b1c15989210d7379e51256d545856d95;p=github%2Fshaarli%2FShaarli.git Merge pull request #255 from ArthurHoaro/config All settings are now stored in config.php --- diff --git a/application/LinkDB.php b/application/LinkDB.php index a673b086..82763618 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -375,7 +375,10 @@ You use the community supported version of the original Shaarli project, by Seba */ public function filterDay($day) { - // TODO: check input format + if (! checkDateFormat('Ymd', $day)) { + throw new Exception('Invalid date format'); + } + $filtered = array(); foreach ($this->links as $l) { if (startsWith($l['linkdate'], $day)) { diff --git a/application/Utils.php b/application/Utils.php index 82220bfc..a1e97b35 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -69,4 +69,19 @@ function sanitizeLink(&$link) $link['description'] = escape($link['description']); $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; +} ?> diff --git a/index.php b/index.php index b73e0b98..236fd4e2 100644 --- a/index.php +++ b/index.php @@ -945,16 +945,22 @@ function showDaily() $days = $LINKSDB->days(); $i = array_search($day,$days); - if ($i==false) { $i=count($days)-1; $day=$days[$i]; } + if ($i===false) { $i=count($days)-1; $day=$days[$i]; } $previousday=''; $nextday=''; if ($i!==false) { - if ($i>1) $previousday=$days[$i-1]; + if ($i>=1) $previousday=$days[$i-1]; if ($ifilterDay($day); + try { + $linksToDisplay = $LINKSDB->filterDay($day); + } catch (Exception $exc) { + error_log($exc); + $linksToDisplay = []; + } + // We pre-format some fields for proper output. foreach($linksToDisplay as $key=>$link) { diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index ee8dbee3..8b0bd23b 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -396,19 +396,22 @@ class LinkDBTest extends PHPUnit_Framework_TestCase /** * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ */ - public function testFilterInvalidDay() + public function testFilterInvalidDayWithChars() { - $this->assertEquals( - 0, - sizeof(self::$privateLinkDB->filterDay('Rainy day, dream away')) - ); + self::$privateLinkDB->filterDay('Rainy day, dream away'); + } - // TODO: check input format - $this->assertEquals( - 6, - sizeof(self::$privateLinkDB->filterDay('20')) - ); + /** + * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ + */ + public function testFilterInvalidDayDigits() + { + self::$privateLinkDB->filterDay('20'); } /** diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index bbba99f2..90392dfb 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -74,5 +74,24 @@ class UtilsTest extends PHPUnit_Framework_TestCase $this->assertTrue(endsWith('å!ùµ', 'ùµ', false)); $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')); + } } ?>