From 7c02bdba48deefaaa52701dd9f6efe37d9bf9a8a Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Thu, 23 Apr 2015 16:53:36 +0200 Subject: [PATCH] Use InvalidArgumentException instead of SimpleSAML_Error_Exception when dealing with wrong input parameters. --- lib/SimpleSAML/Utils/Arrays.php | 8 ++-- lib/SimpleSAML/Utils/Auth.php | 7 ++- lib/SimpleSAML/Utils/Config/Metadata.php | 7 ++- lib/SimpleSAML/Utils/Crypto.php | 54 ++++++++++++++-------- lib/SimpleSAML/Utils/HTTP.php | 58 ++++++++++++------------ lib/SimpleSAML/Utils/System.php | 7 +-- lib/SimpleSAML/Utils/Time.php | 6 +-- lib/SimpleSAML/Utils/XML.php | 33 ++++++++------ 8 files changed, 106 insertions(+), 74 deletions(-) diff --git a/lib/SimpleSAML/Utils/Arrays.php b/lib/SimpleSAML/Utils/Arrays.php index ec552bfb1..a620a113f 100644 --- a/lib/SimpleSAML/Utils/Arrays.php +++ b/lib/SimpleSAML/Utils/Arrays.php @@ -37,7 +37,7 @@ class Arrays * @param array $attributes The array containing attributes that we should validate and normalize. * * @return array The normalized attributes array. - * @throws \SimpleSAML_Error_Exception If input is not an array, array keys are not strings or attribute values are + * @throws \InvalidArgumentException If input is not an array, array keys are not strings or attribute values are * not strings. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> @@ -47,20 +47,20 @@ class Arrays { if (!is_array($attributes)) { - throw new \SimpleSAML_Error_Exception('Attributes was not an array. Was: '.print_r($attributes, true).'".'); + throw new \InvalidArgumentException('Attributes was not an array. Was: '.print_r($attributes, true).'".'); } $newAttrs = array(); foreach ($attributes as $name => $values) { if (!is_string($name)) { - throw new \SimpleSAML_Error_Exception('Invalid attribute name: "'.print_r($name, true).'".'); + throw new \InvalidArgumentException('Invalid attribute name: "'.print_r($name, true).'".'); } $values = self::arrayize($values); foreach ($values as $value) { if (!is_string($value)) { - throw new \SimpleSAML_Error_Exception('Invalid attribute value for attribute '.$name. + throw new \InvalidArgumentException('Invalid attribute value for attribute '.$name. ': "'.print_r($value, true).'".'); } } diff --git a/lib/SimpleSAML/Utils/Auth.php b/lib/SimpleSAML/Utils/Auth.php index 16aec172f..089f94ea6 100644 --- a/lib/SimpleSAML/Utils/Auth.php +++ b/lib/SimpleSAML/Utils/Auth.php @@ -15,10 +15,13 @@ class Auth * @param string|NULL $returnTo The URL the user should arrive on after admin authentication. Defaults to null. * * @return string A URL which can be used for admin authentication. + * @throws \InvalidArgumentException If $returnTo is neither a string nor null. */ public static function getAdminLoginURL($returnTo = null) { - assert('is_string($returnTo) || is_null($returnTo)'); + if (!(is_string($returnTo) || is_null($returnTo))) { + throw new \InvalidArgumentException('Invalid input parameters.'); + } if ($returnTo === null) { $returnTo = \SimpleSAML\Utils\HTTP::getSelfURL(); @@ -47,7 +50,7 @@ class Auth * a login page if the current user doesn't have admin access. * * @return void This function will only return if the user is admin. - * @throws SimpleSAML_Error_Exception If no "admin" authentication source was configured. + * @throws \SimpleSAML_Error_Exception If no "admin" authentication source was configured. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> diff --git a/lib/SimpleSAML/Utils/Config/Metadata.php b/lib/SimpleSAML/Utils/Config/Metadata.php index 72c0d0d94..82c47baaa 100644 --- a/lib/SimpleSAML/Utils/Config/Metadata.php +++ b/lib/SimpleSAML/Utils/Config/Metadata.php @@ -72,11 +72,14 @@ class Metadata * @return array An array holding valid contact configuration options. If a key 'name' was part of the input array, * it will try to decompose the name into its parts, and place the parts into givenName and surName, if those are * missing. - * @throws \InvalidArgumentException if the contact does not conform to valid configuration rules for contacts. + * @throws \InvalidArgumentException If $contact is neither a string nor null, or the contact does not conform to + * valid configuration rules for contacts. */ public static function getContact($contact) { - assert('is_array($contact) || is_null($contact)'); + if (!(is_array($contact) || is_null($contact))) { + throw new \InvalidArgumentException('Invalid input parameters'); + } // check the type if (!isset($contact['contactType']) || !in_array($contact['contactType'], self::$VALID_CONTACT_TYPES, true)) { diff --git a/lib/SimpleSAML/Utils/Crypto.php b/lib/SimpleSAML/Utils/Crypto.php index 06c30aa07..e09bbfea5 100644 --- a/lib/SimpleSAML/Utils/Crypto.php +++ b/lib/SimpleSAML/Utils/Crypto.php @@ -16,14 +16,16 @@ class Crypto * @param string $ciphertext The encrypted data to decrypt. * * @return string The decrypted data. - * @throws \SimpleSAML_Error_Exception If the mcrypt module is not loaded or $ciphertext is not a string. + * @htorws \InvalidArgumentException If $ciphertext is not a string. + * @throws \SimpleSAML_Error_Exception If the mcrypt module is not loaded. + * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function aesDecrypt($ciphertext) { if (!is_string($ciphertext)) { - throw new \SimpleSAML_Error_Exception('Input parameter "$ciphertext" must be a string.'); + throw new \InvalidArgumentException('Input parameter "$ciphertext" must be a string.'); } if (!function_exists("mcrypt_encrypt")) { throw new \SimpleSAML_Error_Exception("The mcrypt PHP module is not loaded."); @@ -50,20 +52,23 @@ class Crypto return $clear; } + /** * Encrypt data using AES and the system-wide secret salt as key. * * @param string $data The data to encrypt. * * @return string The encrypted data and IV. - * @throws \SimpleSAML_Error_Exception If the mcrypt module is not loaded or $data is not a string. + * @throws \InvalidArgumentException If $data is not a string. + * @throws \SimpleSAML_Error_Exception If the mcrypt module is not loaded. + * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function aesEncrypt($data) { if (!is_string($data)) { - throw new \SimpleSAML_Error_Exception('Input parameter "$data" must be a string.'); + throw new \InvalidArgumentException('Input parameter "$data" must be a string.'); } if (!function_exists("mcrypt_encrypt")) { throw new \SimpleSAML_Error_Exception('The mcrypt PHP module is not loaded.'); @@ -103,13 +108,15 @@ class Crypto * - 'password': Password for the private key. * * @param \SimpleSAML_Configuration $metadata The metadata array the private key should be loaded from. - * @param bool $required Whether the private key is required. If this is true, a + * @param bool $required Whether the private key is required. If this is true, a * missing key will cause an exception. Defaults to false. - * @param string $prefix The prefix which should be used when reading from the metadata + * @param string $prefix The prefix which should be used when reading from the metadata * array. Defaults to ''. * * @return array|NULL Extracted private key, or NULL if no private key is present. - * @throws \SimpleSAML_Error_Exception If no private key is found in the metadata, or it was not possible to load it. + * @throws \InvalidArgumentException If $required is not boolean or $prefix is not a string. + * @throws \SimpleSAML_Error_Exception If no private key is found in the metadata, or it was not possible to load + * it. * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> @@ -117,7 +124,7 @@ class Crypto public static function loadPrivateKey(\SimpleSAML_Configuration $metadata, $required = false, $prefix = '') { if (!is_bool($required) || !is_string($prefix)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $file = $metadata->getString($prefix.'privatekey', null); @@ -147,6 +154,7 @@ class Crypto return $ret; } + /** * Get public key or certificate from metadata. * @@ -164,22 +172,26 @@ class Crypto * - 'certFingerprint': Array of valid certificate fingerprints. (Only present if this is a certificate.) * * @param \SimpleSAML_Configuration $metadata The metadata. - * @param bool $required Whether the private key is required. If this is TRUE, a missing key + * @param bool $required Whether the private key is required. If this is TRUE, a missing key * will cause an exception. Default is FALSE. - * @param string $prefix The prefix which should be used when reading from the metadata array. + * @param string $prefix The prefix which should be used when reading from the metadata array. * Defaults to ''. * * @return array|NULL Public key or certificate data, or NULL if no public key or certificate was found. + * @throws \InvalidArgumentException If $metadata is not an instance of \SimpleSAML_Configuration, $required is not + * boolean or $prefix is not a string. + * @throws \SimpleSAML_Error_Exception If no private key is found in the metadata, or it was not possible to load + * it. * - * @throws \SimpleSAML_Error_Exception If no private key is found in the metadata, or it was not possible to load it. * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Lasse Birnbaum Jensen */ public static function loadPublicKey(\SimpleSAML_Configuration $metadata, $required = false, $prefix = '') { - assert('is_bool($required)'); - assert('is_string($prefix)'); + if (!is_bool($required) || !is_string($prefix)) { + throw new \InvalidArgumentException('Invalid input parameters.'); + } $keys = $metadata->getPublicKeys(null, false, $prefix); if ($keys !== null) { @@ -226,6 +238,7 @@ class Crypto } } + /** * This function hashes a password with a given algorithm. * @@ -235,16 +248,18 @@ class Crypto * @param string $salt An optional salt to use. * * @return string The hashed password. - * @throws \SimpleSAML_Error_Exception If the algorithm specified is not supported, or the input parameters are not - * strings. + * @throws \InvalidArgumentException If the input parameters are not strings. + * @throws \SimpleSAML_Error_Exception If the algorithm specified is not supported. + * * @see hash_algos() + * * @author Dyonisius Visser, TERENA <visser@terena.org> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function pwHash($password, $algorithm, $salt = null) { if (!is_string($algorithm) || !is_string($password)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } // hash w/o salt @@ -279,14 +294,15 @@ class Crypto * @param string $password The password to check in clear. * * @return boolean True if the hash corresponds with the given password, false otherwise. - * @throws \SimpleSAML_Error_Exception If the algorithm specified is not supported, or the input parameters are not - * strings. + * @throws \InvalidArgumentException If the input parameters are not strings. + * @throws \SimpleSAML_Error_Exception If the algorithm specified is not supported. + * * @author Dyonisius Visser, TERENA <visser@terena.org> */ public static function pwValid($hash, $password) { if (!is_string($hash) || !is_string($password)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } // match algorithm string (e.g. '{SSHA256}', '{MD5}') diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index 1c80fb8ed..7518bfa6d 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -129,7 +129,7 @@ class HTTP * the name, without a value. * * @return void This function never returns. - * @throws \SimpleSAML_Error_Exception If $url is not a string or is empty, or $parameters is not an array. + * @throws \InvalidArgumentException If $url is not a string or is empty, or $parameters is not an array. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Mads Freek Petersen @@ -138,7 +138,7 @@ class HTTP private static function redirect($url, $parameters = array()) { if (!is_string($url) || empty($url) || !is_array($parameters)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } if (!empty($parameters)) { $url = self::addURLParameters($url, $parameters); @@ -226,7 +226,7 @@ class HTTP * array. * * @return string The URL with the new query parameters. - * @throws \SimpleSAML_Error_Exception If $url is not a string or $parameters is not an array. + * @throws \InvalidArgumentException If $url is not a string or $parameters is not an array. * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> @@ -234,7 +234,7 @@ class HTTP public static function addURLParameters($url, $parameters) { if (!is_string($url) || !is_array($parameters)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $queryStart = strpos($url, '?'); @@ -265,14 +265,14 @@ class HTTP * * @return void If there is a session cookie, nothing will be returned. Otherwise, the user will be redirected to a * page telling about the missing cookie. - * @throws \SimpleSAML_Error_Exception If $retryURL is neither a string nor null. + * @throws \InvalidArgumentException If $retryURL is neither a string nor null. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ public static function checkSessionCookie($retryURL = null) { if (!is_string($retryURL) || !is_null($retryURL)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $session = \SimpleSAML_Session::getSessionFromRequest(); @@ -299,7 +299,8 @@ class HTTP * * @return string The normalized URL itself if it is allowed. An empty string if the $url parameter is empty as * defined by the empty() function. - * @throws \SimpleSAML_Error_Exception if the URL is malformed or is not allowed by configuration. + * @throws \InvalidArgumentException If the URL is malformed. + * @throws \SimpleSAML_Error_Exception If the URL is not allowed by configuration. * * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ @@ -348,7 +349,8 @@ class HTTP * @param boolean $getHeaders Whether to also return response headers. Optional. * * @return mixed array if $getHeaders is set, string otherwise - * @throws \SimpleSAML_Error_Exception If the input parameters are invalid or the file or URL cannot be retrieved. + * @throws \InvalidArgumentException If the input parameters are invalid. + * @throws \SimpleSAML_Error_Exception If the file or URL cannot be retrieved. * * @author Andjelko Horvat * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> @@ -357,7 +359,7 @@ class HTTP public static function fetch($url, $context = array(), $getHeaders = false) { if (!is_string($url)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $config = \SimpleSAML_Configuration::getInstance(); @@ -562,7 +564,7 @@ class HTTP * @param array $data The name-value pairs which will be posted to the destination. * * @return string A URL which can be accessed to post the data. - * @throws \SimpleSAML_Error_Exception If $destination is not a string or $data is not an array. + * @throws \InvalidArgumentException If $destination is not a string or $data is not an array. * * @author Andjelko Horvat * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> @@ -570,7 +572,7 @@ class HTTP public static function getPOSTRedirectURL($destination, $data) { if (!is_string($destination) || !is_array($data)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $config = \SimpleSAML_Configuration::getInstance(); @@ -704,7 +706,7 @@ class HTTP * @param string $url The relative URL. * * @return string An absolute URL for the given relative URL. - * @throws \SimpleSAML_Error_Exception If $url is not a string or a valid URL. + * @throws \InvalidArgumentException If $url is not a string or a valid URL. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> @@ -712,14 +714,14 @@ class HTTP public static function normalizeURL($url) { if (!is_string($url)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $url = self::resolveURL($url, self::getSelfURL()); // verify that the URL is to a http or https site if (!preg_match('@^https?://@i', $url)) { - throw new \SimpleSAML_Error_Exception('Invalid URL: '.$url); + throw new \InvalidArgumentException('Invalid URL: '.$url); } return $url; @@ -737,14 +739,14 @@ class HTTP * @param string $query_string The query string which should be parsed. * * @return array The query string as an associative array. - * @throws \SimpleSAML_Error_Exception If $query_string is not a string. + * @throws \InvalidArgumentException If $query_string is not a string. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ public static function parseQueryString($query_string) { if (!is_string($query_string)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $res = array(); @@ -780,14 +782,14 @@ class HTTP * name, without a value. * * @return void This function never returns. - * @throws \SimpleSAML_Error_Exception If $url is not a string or $parameters is not an array. + * @throws \InvalidArgumentException If $url is not a string or $parameters is not an array. * * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function redirectTrustedURL($url, $parameters = array()) { if (!is_string($url) || !is_array($parameters)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $url = self::normalizeURL($url); @@ -812,14 +814,14 @@ class HTTP * name, without a value. * * @return void This function never returns. - * @throws \SimpleSAML_Error_Exception If $url is not a string or $parameters is not an array. + * @throws \InvalidArgumentException If $url is not a string or $parameters is not an array. * * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function redirectUntrustedURL($url, $parameters = array()) { if (!is_string($url) || !is_array($parameters)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $url = self::checkURLAllowed($url); @@ -842,7 +844,7 @@ class HTTP * @param string $base The base URL. Defaults to the base URL of this installation of SimpleSAMLphp. * * @return string An absolute URL for the given relative URL. - * @throws \SimpleSAML_Error_Exception If the base URL cannot be parsed into a valid URL, or the given parameters + * @throws \InvalidArgumentException If the base URL cannot be parsed into a valid URL, or the given parameters * are not strings. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> @@ -855,11 +857,11 @@ class HTTP } if (!is_string($url) || !is_string($base)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } if (!preg_match('/^((((\w+:)\/\/[^\/]+)(\/[^?#]*))(?:\?[^#]*)?)(?:#.*)?/', $base, $baseParsed)) { - throw new \SimpleSAML_Error_Exception('Unable to parse base url: '.$base); + throw new \InvalidArgumentException('Unable to parse base url: '.$base); } $baseDir = dirname($baseParsed[5].'filename'); @@ -922,8 +924,8 @@ class HTTP * @param array|NULL $params Cookie parameters. * @param bool $throw Whether to throw exception if setcookie() fails. * - * @throws \SimpleSAML_Error_Exception If any parameter has an incorrect type or the if the headers were already - * sent and the cookie cannot be set. + * @throws \InvalidArgumentException If any parameter has an incorrect type. + * @throws \SimpleSAML_Error_Exception If the headers were already sent and the cookie cannot be set. * * @author Andjelko Horvat * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> @@ -935,7 +937,7 @@ class HTTP (is_array($params) || is_null($params)) && // $params can be an array or null is_bool($throw)) // $throw must be boolean ) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $default_params = array( @@ -996,7 +998,7 @@ class HTTP * @param string $destination The destination URL. * @param array $data An associative array with the data to be posted to $destination. * - * @throws \SimpleSAML_Error_Exception If $destination is not a string or $data is not an array. + * @throws \InvalidArgumentException If $destination is not a string or $data is not an array. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Andjelko Horvat @@ -1005,7 +1007,7 @@ class HTTP public static function submitPOSTData($destination, $data) { if (!is_string($destination) || !is_array($data)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $config = \SimpleSAML_Configuration::getInstance(); diff --git a/lib/SimpleSAML/Utils/System.php b/lib/SimpleSAML/Utils/System.php index 6a01c591b..8889251a9 100644 --- a/lib/SimpleSAML/Utils/System.php +++ b/lib/SimpleSAML/Utils/System.php @@ -156,8 +156,9 @@ class System * @param string $data The data we should write to the file. * @param int $mode The permissions to apply to the file. Defaults to 0600. * - * @throws \SimpleSAML_Error_Exception If any of the input parameters doesn't have the proper types, or the file - * cannot be saved, permissions cannot be changed or it is not possible to write to the target file. + * @throws \InvalidArgumentException If any of the input parameters doesn't have the proper types. + * @throws \SimpleSAML_Error_Exception If the file cannot be saved, permissions cannot be changed or it is not + * possible to write to the target file. * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> @@ -167,7 +168,7 @@ class System public static function writeFile($filename, $data, $mode = 0600) { if (!is_string($filename) || !is_string($data) || !is_numeric($mode)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters'); + throw new \InvalidArgumentException('Invalid input parameters'); } $tmpFile = self::getTempDir().DIRECTORY_SEPARATOR.rand(); diff --git a/lib/SimpleSAML/Utils/Time.php b/lib/SimpleSAML/Utils/Time.php index 55e156e01..9898f8b50 100644 --- a/lib/SimpleSAML/Utils/Time.php +++ b/lib/SimpleSAML/Utils/Time.php @@ -81,19 +81,19 @@ class Time * time. * * @return int The new timestamp, after the duration is applied. - * @throws \SimpleSAML_Error_Exception If $duration is not a valid ISO 8601 duration or if the input parameters do + * @throws \InvalidArgumentException If $duration is not a valid ISO 8601 duration or if the input parameters do * not have the right data types. */ public static function parseDuration($duration, $timestamp = null) { if (!(is_string($duration) && (is_int($timestamp) || is_null($timestamp)))) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters'); + throw new \InvalidArgumentException('Invalid input parameters'); } // parse the duration. We use a very strict pattern $durationRegEx = '#^(-?)P(?:(?:(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)(?:[.,]\d+)?S)?)?)|(?:(\\d+)W))$#D'; if (!preg_match($durationRegEx, $duration, $matches)) { - throw new \SimpleSAML_Error_Exception('Invalid ISO 8601 duration: '.$duration); + throw new \InvalidArgumentException('Invalid ISO 8601 duration: '.$duration); } $durYears = (empty($matches[2]) ? 0 : (int) $matches[2]); diff --git a/lib/SimpleSAML/Utils/XML.php b/lib/SimpleSAML/Utils/XML.php index 1483a7136..bd09a319d 100644 --- a/lib/SimpleSAML/Utils/XML.php +++ b/lib/SimpleSAML/Utils/XML.php @@ -73,14 +73,14 @@ class XML * - 'decrypt': for decrypted messages. * - 'encrypt': for encrypted messages. * - * @throws \SimpleSAML_Error_Exception If $type is not a string or $message is neither a string nor a \DOMElement. + * @throws \InvalidArgumentException If $type is not a string or $message is neither a string nor a \DOMElement. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ public static function debugSAMLMessage($message, $type) { if (!(is_string($type) && (is_string($message) || $message instanceof \DOMElement))) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + throw new \InvalidArgumentException('Invalid input parameters.'); } $globalConfig = \SimpleSAML_Configuration::getInstance(); @@ -127,13 +127,14 @@ class XML * @param string $indentBase The indentation this element should be assumed to have. Defaults to an empty * string. * - * @throws \SimpleSAML_Error_Exception If $root is not a DOMElement or $indentBase is not a string. + * @throws \InvalidArgumentException If $root is not a DOMElement or $indentBase is not a string. + * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ public static function formatDOMElement(\DOMElement $root, $indentBase = '') { if (!is_string($indentBase)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters'); + throw new \InvalidArgumentException('Invalid input parameters'); } // check what this element contains @@ -214,19 +215,20 @@ class XML * to ''. * * @return string The formatted string. - * @throws \SimpleSAML_Error_Exception If the input does not parse correctly as an XML string or parameters are not - * strings. + * @throws \InvalidArgumentException If the parameters are not strings. + * @throws \DOMException If the input does not parse correctly as an XML string. + * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ public static function formatXMLString($xml, $indentBase = '') { if (!is_string($xml) || !is_string($indentBase)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters'); + throw new \InvalidArgumentException('Invalid input parameters'); } $doc = new \DOMDocument(); if (!$doc->loadXML($xml)) { - throw new \SimpleSAML_Error_Exception('Error parsing XML string.'); + throw new \DOMException('Error parsing XML string.'); } $root = $doc->firstChild; @@ -248,11 +250,14 @@ class XML * * @return array Array with the matching elements in the order they are found. An empty array is * returned if no elements match. + * @throws \InvalidArgumentException If $element is not an instance of DOMElement, $localName is not a string or + * $namespaceURI is not a string. */ public static function getDOMChildren(\DOMElement $element, $localName, $namespaceURI) { - assert('is_string($localName)'); - assert('is_string($namespaceURI)'); + if (!($element instanceof \DOMElement) || !is_string($localName) || !is_string($namespaceURI)) { + throw new \InvalidArgumentException('Invalid input parameters.'); + } $ret = array(); @@ -279,13 +284,15 @@ class XML * @param \DOMElement $element The element we should extract text from. * * @return string The text content of the element. + * @throws \InvalidArgumentException If $element is not an instance of DOMElement. * @throws \SimpleSAML_Error_Exception If the element contains a non-text child node. + * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ public static function getDOMText(\DOMElement $element) { if (!($element instanceof \DOMElement)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters'); + throw new \InvalidArgumentException('Invalid input parameters'); } $txt = ''; @@ -321,7 +328,7 @@ class XML * @param string $nsURI The namespaceURI the element should have. * * @return boolean True if both namespace and local name matches, false otherwise. - * @throws \SimpleSAML_Error_Exception If the namespace shortcut is unknown. + * @throws \InvalidArgumentException If the namespace shortcut is unknown. * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> @@ -349,7 +356,7 @@ class XML // check if it is a valid shortcut if (!array_key_exists($nsURI, $shortcuts)) { - throw new \SimpleSAML_Error_Exception('Unknown namespace shortcut: '.$nsURI); + throw new \InvalidArgumentException('Unknown namespace shortcut: '.$nsURI); } // expand the shortcut -- GitLab