From a84699a346945be6c964c4b49c25c5395e0bd206 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Mon, 24 Aug 2015 21:13:53 -0600 Subject: [PATCH 1/3] Switch to extension_loaded instead of function_exists where it makes sense --- phpseclib/Crypt/RSA.php | 2 +- phpseclib/Crypt/Random.php | 8 ++++---- phpseclib/Math/BigInteger.php | 2 +- tests/PhpseclibTestCase.php | 4 ++-- tests/Unit/Math/BigInteger/InternalOpenSSLTest.php | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 78837ddf..1490110e 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -474,7 +474,7 @@ class RSA define('CRYPT_RSA_MODE', self::MODE_INTERNAL); break; // openssl_pkey_get_details - which is used in the only place Crypt/RSA.php uses OpenSSL - was introduced in PHP 5.2.0 - case !function_exists('openssl_pkey_get_details'): + case extension_loaded('openssl') && version_compare(PHP_VERSION, '5.2.0', "<="): define('CRYPT_RSA_MODE', self::MODE_INTERNAL); break; case extension_loaded('openssl') && version_compare(PHP_VERSION, '4.2.0', '>=') && file_exists($this->configFile): diff --git a/phpseclib/Crypt/Random.php b/phpseclib/Crypt/Random.php index 9fb1d15b..4ff93489 100644 --- a/phpseclib/Crypt/Random.php +++ b/phpseclib/Crypt/Random.php @@ -56,7 +56,7 @@ class Random if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // method 1. prior to PHP 5.3 this would call rand() on windows hence the function_exists('class_alias') call. // ie. class_alias is a function that was introduced in PHP 5.3 - if (function_exists('mcrypt_create_iv') && function_exists('class_alias')) { + if (extension_loaded('mcrypt') && function_exists('class_alias')) { return mcrypt_create_iv($length); } // method 2. openssl_random_pseudo_bytes was introduced in PHP 5.3.0 but prior to PHP 5.3.4 there was, @@ -72,12 +72,12 @@ class Random // https://github.com/php/php-src/blob/7014a0eb6d1611151a286c0ff4f2238f92c120d6/win32/winutil.c#L80 // // we're calling it, all the same, in the off chance that the mcrypt extension is not available - if (function_exists('openssl_random_pseudo_bytes') && version_compare(PHP_VERSION, '5.3.4', '>=')) { + if (extension_loaded('openssl') && version_compare(PHP_VERSION, '5.3.4', '>=')) { return openssl_random_pseudo_bytes($length); } } else { // method 1. the fastest - if (function_exists('openssl_random_pseudo_bytes')) { + if (extension_loaded('openssl')) { return openssl_random_pseudo_bytes($length); } // method 2 @@ -95,7 +95,7 @@ class Random // surprisingly slower than method 2. maybe that's because mcrypt_create_iv does a bunch of error checking that we're // not doing. regardless, this'll only be called if this PHP script couldn't open /dev/urandom due to open_basedir // restrictions or some such - if (function_exists('mcrypt_create_iv')) { + if (extension_loaded('mcrypt')) { return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); } } diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index bc7b2269..7e3bf712 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -271,7 +271,7 @@ class BigInteger } } - if (function_exists('openssl_public_encrypt') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { + if (extension_loaded('openssl') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work ob_start(); @phpinfo(); diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index 8f594e04..1a2a8e4a 100644 --- a/tests/PhpseclibTestCase.php +++ b/tests/PhpseclibTestCase.php @@ -59,7 +59,7 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase $value = constant($constant); if ($value !== $expected) { - if (function_exists('runkit_constant_redefine')) { + if (extension_loaded('runkit')) { if (!runkit_constant_redefine($constant, $expected)) { self::markTestSkipped(sprintf( "Failed to redefine constant %s to %s", @@ -88,7 +88,7 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase */ protected static function reRequireFile($filename) { - if (function_exists('runkit_import')) { + if (extension_loaded('runkit')) { $result = runkit_import( sprintf('%s/../phpseclib/%s', __DIR__, $filename), RUNKIT_IMPORT_FUNCTIONS | diff --git a/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php b/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php index 495401fd..d5056b87 100644 --- a/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php +++ b/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php @@ -9,7 +9,7 @@ class Unit_Math_BigInteger_InternalOpenSSLTest extends Unit_Math_BigInteger_Test { public static function setUpBeforeClass() { - if (!function_exists('openssl_public_encrypt')) { + if (!extension_loaded('openssl')) { self::markTestSkipped('openssl_public_encrypt() function is not available.'); } From df6b201ab9f16db11bdf87bbebfe5222ff798e3f Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Tue, 1 Sep 2015 14:17:30 -0400 Subject: [PATCH 2/3] Switch to extension_loaded('gmp') --- phpseclib/Math/BigInteger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 7e3bf712..b2eb4b2d 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3199,7 +3199,7 @@ class BigInteger $x = $this->random($min, $max); // gmp_nextprime() requires PHP 5 >= 5.2.0 per . - if (MATH_BIGINTEGER_MODE == self::MODE_GMP && function_exists('gmp_nextprime')) { + if (MATH_BIGINTEGER_MODE == self::MODE_GMP && extension_loaded('gmp')) { $p = new static(); $p->value = gmp_nextprime($x->value); From 78bac871afbfcbdc656f4644f815e9f0f8291670 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Tue, 1 Sep 2015 14:17:54 -0400 Subject: [PATCH 3/3] Remove conditions for PHP < 5.3.0 for this branch --- phpseclib/Crypt/RSA.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 1490110e..c9150374 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -473,11 +473,7 @@ class RSA case defined('MATH_BIGINTEGER_OPENSSL_DISABLE'): define('CRYPT_RSA_MODE', self::MODE_INTERNAL); break; - // openssl_pkey_get_details - which is used in the only place Crypt/RSA.php uses OpenSSL - was introduced in PHP 5.2.0 - case extension_loaded('openssl') && version_compare(PHP_VERSION, '5.2.0', "<="): - define('CRYPT_RSA_MODE', self::MODE_INTERNAL); - break; - case extension_loaded('openssl') && version_compare(PHP_VERSION, '4.2.0', '>=') && file_exists($this->configFile): + case extension_loaded('openssl') && file_exists($this->configFile): // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work ob_start(); @phpinfo();