From c8830716f1c99260e155c7ddb1a1ad81798fb736 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Thu, 16 Apr 2015 16:08:49 +0200
Subject: [PATCH] Schedule SimpleSAML_Utilities::getLastError() for removal.
 Deprecate it and stop using it.

---
 .../Metadata/MetaDataStorageHandlerMDX.php        |  6 ++++--
 .../Metadata/MetaDataStorageHandlerSerialize.php  | 15 ++++++++++-----
 lib/SimpleSAML/Utilities.php                      | 11 +++--------
 lib/SimpleSAML/Utils/System.php                   | 12 ++++++++----
 4 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerMDX.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerMDX.php
index 7364a6fe9..bf4058475 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerMDX.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerMDX.php
@@ -140,8 +140,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_
 
 		$rawData = file_get_contents($cachefilename);
 		if (empty($rawData)) {
+			$error = error_get_last();
 			throw new Exception('Error reading metadata from cache file "' . $cachefilename . '": ' .
-				SimpleSAML_Utilities::getLastError());
+				$error['message']);
 		}
 
 		$data = unserialize($rawData);
@@ -258,8 +259,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_
 		}
 
 		if (empty($xmldata)) {
+			$error = error_get_last();
 			throw new Exception('Error downloading metadata for "'. $index .'" from "' . $mdx_url . '": ' .
-				SimpleSAML_Utilities::getLastError());
+                $error['message']);
 		}
 
 		$entity = SimpleSAML_Metadata_SAMLParser::parseString($xmldata);
diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php
index 3415cd5cc..fae34c9fb 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php
@@ -164,8 +164,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerSerialize extends SimpleSAML_Met
 
 		$data = @file_get_contents($filePath);
 		if ($data === FALSE) {
+			$error = error_get_last();
 			SimpleSAML_Logger::warning('Error reading file ' . $filePath .
-				': ' . SimpleSAML_Utilities::getLastError());
+				': ' . $error['message']);
 			return NULL;
 		}
 
@@ -199,8 +200,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerSerialize extends SimpleSAML_Met
 			SimpleSAML_Logger::info('Creating directory: ' . $dir);
 			$res = @mkdir($dir, 0777, TRUE);
 			if ($res === FALSE) {
+				$error = error_get_last();
 				SimpleSAML_Logger::error('Failed to create directory ' . $dir .
-					': ' . SimpleSAML_Utilities::getLastError());
+					': ' . $error['message']);
 				return FALSE;
 			}
 		}
@@ -211,15 +213,17 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerSerialize extends SimpleSAML_Met
 
 		$res = file_put_contents($newPath, $data);
 		if ($res === FALSE) {
+			$error = error_get_last();
 			SimpleSAML_Logger::error('Error saving file ' . $newPath .
-				': ' . SimpleSAML_Utilities::getLastError());
+				': ' . $error['message']);
 			return FALSE;
 		}
 
 		$res = rename($newPath, $filePath);
 		if ($res === FALSE) {
+			$error = error_get_last();
 			SimpleSAML_Logger::error('Error renaming ' . $newPath . ' to ' . $filePath .
-				': ' . SimpleSAML_Utilities::getLastError());
+				': ' . $error['message']);
 			return FALSE;
 		}
 
@@ -248,8 +252,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerSerialize extends SimpleSAML_Met
 
 		$res = unlink($filePath);
 		if ($res === FALSE) {
+			$error = error_get_last();
 			SimpleSAML_Logger::error('Failed to delete file ' . $filePath .
-				': ' . SimpleSAML_Utilities::getLastError());
+				': ' . $error['message']);
 		}
 	}
 
diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 148be6ea1..6e3549e95 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -1199,13 +1199,7 @@ class SimpleSAML_Utilities {
 
 
 	/**
-	 * Retrieve last error message.
-	 *
-	 * This function retrieves the last error message. If no error has occurred,
-	 * '[No error message found]' will be returned. If the required function isn't available,
-	 * '[Cannot get error message]' will be returned.
-	 *
-	 * @return string  Last error message.
+	 * @deprecated This function will be removed in SSP 2.0. Please call error_get_last() directly.
 	 */
 	public static function getLastError() {
 
@@ -1915,7 +1909,8 @@ class SimpleSAML_Utilities {
 
 		$data = file_get_contents($path, FALSE, $context);
 		if ($data === FALSE) {
-			throw new SimpleSAML_Error_Exception('Error fetching ' . var_export($path, TRUE) . ':' . self::getLastError());
+			$error = error_get_last();
+			throw new SimpleSAML_Error_Exception('Error fetching ' . var_export($path, TRUE) . ':' . $error['message']);
 		}
 
 		// Data and headers.
diff --git a/lib/SimpleSAML/Utils/System.php b/lib/SimpleSAML/Utils/System.php
index 59da67bdb..bce9285cd 100644
--- a/lib/SimpleSAML/Utils/System.php
+++ b/lib/SimpleSAML/Utils/System.php
@@ -74,8 +74,9 @@ class SimpleSAML_Utils_System
 
         if (!is_dir($tempDir)) {
             if (!mkdir($tempDir, 0700, true)) {
+                $error = error_get_last();
                 throw new SimpleSAML_Error_Exception('Error creating temporary directory "'.$tempDir.
-                    '": '.SimpleSAML_Utilities::getLastError());
+                    '": '.$error['message']);
             }
         } elseif (function_exists('posix_getuid')) {
             // check that the owner of the temp directory is the current user
@@ -117,22 +118,25 @@ class SimpleSAML_Utils_System
 
         $res = @file_put_contents($tmpFile, $data);
         if ($res === false) {
+            $error = error_get_last();
             throw new SimpleSAML_Error_Exception('Error saving file "'.$tmpFile.
-                '": '.SimpleSAML_Utilities::getLastError());
+                '": '.$error['message']);
         }
 
         if (self::getOS() !== self::WINDOWS) {
             if (!chmod($tmpFile, $mode)) {
                 unlink($tmpFile);
+                $error = error_get_last();
                 throw new SimpleSAML_Error_Exception('Error changing file mode of "'.$tmpFile.
-                    '": '.SimpleSAML_Utilities::getLastError());
+                    '": '.$error['message']);
             }
         }
 
         if (!rename($tmpFile, $filename)) {
             unlink($tmpFile);
+            $error = error_get_last();
             throw new SimpleSAML_Error_Exception('Error moving "'.$tmpFile.'" to "'.
-                $filename.'": '.SimpleSAML_Utilities::getLastError());
+                $filename.'": '.$error['message']);
         }
     }
 }
-- 
GitLab