CRM-13823 - Better validation of PSR level
authorColeman Watts <coleman@civicrm.org>
Tue, 17 Nov 2015 21:11:37 +0000 (16:11 -0500)
committerColeman Watts <coleman@civicrm.org>
Tue, 17 Nov 2015 21:26:28 +0000 (16:26 -0500)
CRM/Utils/Check.php
CRM/Utils/Check/Message.php

index 4aeba74f6fa7d3657bd57589479c63b2d17278dd..78337794074d53808cbd36fd07a6842ae1e3f072 100644 (file)
@@ -146,19 +146,27 @@ class CRM_Utils_Check {
   /**
    * Get the integer value (useful for thresholds) of the severity.
    *
-   * @param int|const $severity
+   * @param int|string $severity
    *   the value to look up
    * @param bool $reverse
    *   whether to find the constant from the integer
-   * @return bool
+   * @return string|int
+   * @throws \CRM_Core_Exception
    */
   public static function severityMap($severity, $reverse = FALSE) {
-    // Lowercase string-based severities
-    if (!$reverse) {
+    if ($reverse) {
+      if (isset(self::$severityList[$severity])) {
+        return self::$severityList[$severity];
+      }
+    }
+    else {
+      // Lowercase string-based severities
       $severity = strtolower($severity);
+      if (in_array($severity, self::$severityList)) {
+        return array_search($severity, self::$severityList);
+      }
     }
-
-    return ($reverse) ? self::$severityList[$severity] : array_search($severity, self::$severityList);
+    throw new CRM_Core_Exception('Invalid PSR Severity Level');
   }
 
   /**
index d673091b29fd3ada2c1e717ecceb36f8aab39a8d..caccaa3c01ff2ed61066281253fafd6c61a7e960 100644 (file)
@@ -89,15 +89,21 @@ class CRM_Utils_Check_Message {
    *   The severity of the message. Use PSR-3 log levels.
    *
    * @see Psr\Log\LogLevel
+   *
+   * @throws \CRM_Core_Exception
    */
   public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
     $this->name = $name;
     $this->message = $message;
     $this->title = $title;
-    // Handle non-integer severity levels.
-    if (!CRM_Utils_Rule::integer($level)) {
+    // Convert level to integer
+    if (!CRM_Utils_Rule::positiveInteger($level)) {
       $level = CRM_Utils_Check::severityMap($level);
     }
+    else {
+      // Validate numeric input - this will throw an exception if invalid
+      CRM_Utils_Check::severityMap($level, TRUE);
+    }
     $this->level = $level;
     $this->icon = $icon;
   }