]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
LinkDB::filterDay(): check input date format 251/head
authorVirtualTam <virtualtam@flibidi.net>
Sat, 27 Jun 2015 12:57:44 +0000 (14:57 +0200)
committerVirtualTam <virtualtam@flibidi.net>
Wed, 8 Jul 2015 22:44:19 +0000 (00:44 +0200)
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
application/LinkDB.php
application/Utils.php
index.php
tests/LinkDBTest.php
tests/UtilsTest.php

index a673b086b056d45231d3a41e565eb2b6dc026192..827636189aa2aa85d111a2e6ffd793538d93acea 100644 (file)
@@ -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)) {
index 82220bfc4399b69c68d6b9c1e459d12339c33461..a1e97b356c6ed85e418f7515d0697bd3b949c729 100644 (file)
@@ -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;
+}
 ?>
index 561f946e9a52640ab2a4853f201ee92a92daaef5..5771dd8880043d48ada8b2aa1d36ea77d6fd18e0 100644 (file)
--- a/index.php
+++ b/index.php
@@ -957,7 +957,13 @@ function showDaily()
         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.
     foreach($linksToDisplay as $key=>$link)
     {
index ee8dbee3181840efae91014803e2ac809f63e6da..8b0bd23b6c36ad845aff9596585778d3de1d6122 100644 (file)
@@ -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');
     }
 
     /**
index bbba99f2b686bfc6f36f7480cfb2a0a15b2881d1..90392dfba26554c864640f41bae7aad6a7008d5c 100644 (file)
@@ -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'));
+    }
 }
 ?>