From 7506eb3b239825927ec3b378bfe66fe3c4140c8b Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Fri, 16 Oct 2009 11:04:08 +0000
Subject: [PATCH] Add support for masking error messages.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1857 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Utilities.php | 51 ++++++++++++++++++++++++++++++++++++
 www/_include.php             |  5 ++++
 2 files changed, 56 insertions(+)

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index d40f28587..99e16c82b 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -9,6 +9,25 @@
  */
 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
+	 */
+	public static $logMask = 0;
+
 
 	/**
 	 * Will return sp.example.org
@@ -2062,6 +2081,38 @@ class SimpleSAML_Utilities {
 		return $tempDir;
 	}
 
+
+	/**
+	 * Disable reporting of 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];
+	}
+
 }
 
 ?>
\ No newline at end of file
diff --git a/www/_include.php b/www/_include.php
index 810f919d4..2f67ef974 100644
--- a/www/_include.php
+++ b/www/_include.php
@@ -32,6 +32,11 @@ set_exception_handler('SimpleSAML_exception_handler');
 /* Log full backtrace on errors and warnings. */
 function SimpleSAML_error_handler($errno, $errstr, $errfile = NULL, $errline = 0, $errcontext = NULL) {
 
+	if ($errno & SimpleSAML_Utilities::$logMask) {
+		/* Masked error. */
+		return FALSE;
+	}
+
 	static $limit = 5;
 	$limit -= 1;
 	if ($limit < 0) {
-- 
GitLab