From f00b5f634775074d33a3c66a177722d40ad70669 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Fri, 26 Feb 2016 09:10:37 +0100
Subject: [PATCH] Move the error-reporting logic in SimpleSAML_Utilities to the
 SimpleSAML_Logger class, and add a new method there to evaluate whether an
 error would be masked or not according to its level.

---
 lib/SimpleSAML/Logger.php    | 63 ++++++++++++++++++++++++++++++++++++
 lib/SimpleSAML/Utilities.php | 34 +++----------------
 www/_include.php             |  2 +-
 3 files changed, 69 insertions(+), 30 deletions(-)

diff --git a/lib/SimpleSAML/Logger.php b/lib/SimpleSAML/Logger.php
index 11d275dd6..f505a4339 100644
--- a/lib/SimpleSAML/Logger.php
+++ b/lib/SimpleSAML/Logger.php
@@ -40,6 +40,24 @@ class SimpleSAML_Logger
      */
     private static $earlyLog = array();
 
+    /**
+     * List of log levels.
+     *
+     * This list is used to restore the log levels after some log levels have been disabled.
+     *
+     * @var array
+     */
+    private static $logLevelStack = array();
+
+    /**
+     * The current mask of log levels disabled.
+     *
+     * Note: this mask is not directly related to the PHP error reporting level.
+     *
+     * @var int
+     */
+    private static $logMask = 0;
+
 
     /**
      * This constant defines the string we set the track ID to while we are fetching the track ID from the session
@@ -256,6 +274,51 @@ class SimpleSAML_Logger
     }
 
 
+    /**
+     * Evaluate whether errors of a certain error level are masked or not.
+     *
+     * @param int $errno The level of the error to check.
+     * @return bool True if the error is masked, false otherwise.
+     */
+    public static function isErrorMasked($errno)
+    {
+        return ($errno & self::$logMask) || !($errno & error_reporting());
+    }
+
+
+    /**
+     * Disable error reporting for the given log levels.
+     *
+     * Every call to this function must be followed by a call to popErrorMask().
+     *
+     * @param int $mask The log levels that should be masked.
+     */
+    public static function maskErrors($mask)
+    {
+        assert('is_int($mask)');
+
+        $currentEnabled = error_reporting();
+        self::$logLevelStack[] = array($currentEnabled, self::$logMask);
+
+        $currentEnabled &= ~$mask;
+        error_reporting($currentEnabled);
+        self::$logMask |= $mask;
+    }
+
+
+    /**
+     * Pop an error mask.
+     *
+     * This function restores the previous error mask.
+     */
+    public static function popErrorMask()
+    {
+        $lastMask = array_pop(self::$logLevelStack);
+        error_reporting($lastMask[0]);
+        self::$logMask = $lastMask[1];
+    }
+
+
     /**
      * Defer a message for later logging.
      *
diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 0aea5f284..d0e33ee4a 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -13,21 +13,7 @@ class SimpleSAML_Utilities
 {
 
     /**
-     * List of log levels.
-     *
-     * This list is used to restore the log levels after some log levels are disabled.
-     *
-     * @var array
-     */
-    private static $logLevelStack = array();
-
-
-    /**
-     * The current mask of disabled log levels.
-     *
-     * Note: This mask is not directly related to the PHP error reporting level.
-     *
-     * @var int
+     * @deprecated This property will be removed in SSP 2.0. Please use SimpleSAML_Logger::isErrorMasked() instead.
      */
     public static $logMask = 0;
 
@@ -629,29 +615,20 @@ class SimpleSAML_Utilities
 
 
     /**
-     * @deprecated This method will be removed in SSP 2.0.
+     * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML_Logger::maskErrors() instead.
      */
     public static function maskErrors($mask)
     {
-        assert('is_int($mask)');
-
-        $currentEnabled = error_reporting();
-        self::$logLevelStack[] = array($currentEnabled, self::$logMask);
-
-        $currentEnabled &= ~$mask;
-        error_reporting($currentEnabled);
-        self::$logMask |= $mask;
+        SimpleSAML_Logger::maskErrors($mask);
     }
 
 
     /**
-     * @deprecated This method will be removed in SSP 2.0.
+     * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML_Logger::popErrorMask() instead.
      */
     public static function popErrorMask()
     {
-        $lastMask = array_pop(self::$logLevelStack);
-        error_reporting($lastMask[0]);
-        self::$logMask = $lastMask[1];
+        SimpleSAML_Logger::popErrorMask();
     }
 
 
@@ -727,5 +704,4 @@ class SimpleSAML_Utilities
     {
         \SimpleSAML\Utils\HTTP::setCookie($name, $value, $params, $throw);
     }
-
 }
diff --git a/www/_include.php b/www/_include.php
index 5c25adeea..875559d1d 100644
--- a/www/_include.php
+++ b/www/_include.php
@@ -59,7 +59,7 @@ function SimpleSAML_error_handler($errno, $errstr, $errfile = null, $errline = 0
         return false;
     }
 
-    if ($errno & SimpleSAML_Utilities::$logMask || !($errno & error_reporting())) {
+    if (SimpleSAML_Logger::isErrorMasked($errno)) {
         // masked error
         return false;
     }
-- 
GitLab