From 2b75c37cc9a5fd5997c00bb642f477eea052adb4 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Tue, 1 Sep 2015 14:32:22 -0400 Subject: [PATCH 1/2] Convert most instances of function_exists() to extension_loaded() where applicable --- phpseclib/Crypt/Random.php | 11 +++++------ phpseclib/Math/BigInteger.php | 4 ++-- tests/PhpseclibTestCase.php | 4 ++-- tests/Unit/Math/BigInteger/InternalOpenSSLTest.php | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/phpseclib/Crypt/Random.php b/phpseclib/Crypt/Random.php index bb968374..964bd2e8 100644 --- a/phpseclib/Crypt/Random.php +++ b/phpseclib/Crypt/Random.php @@ -68,9 +68,8 @@ if (!function_exists('crypt_random_string')) { function crypt_random_string($length) { if (CRYPT_RANDOM_IS_WINDOWS) { - // 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')) { + // method 1. prior to PHP 5.3, mcrypt_create_iv() would call rand() on windows + if (extension_loaded('mcrypt') && version_compare(PHP_VERSION, '5.3.0', '>=')) { 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, @@ -86,12 +85,12 @@ if (!function_exists('crypt_random_string')) { // 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') && version_compare(PHP_VERSION, '5.3.0', '>=')) { return openssl_random_pseudo_bytes($length); } // method 2 @@ -109,7 +108,7 @@ if (!function_exists('crypt_random_string')) { // 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 94362a69..b1057c88 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -266,7 +266,7 @@ class Math_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(); @@ -3236,7 +3236,7 @@ class Math_BigInteger $x = $this->random($min, $max); // gmp_nextprime() requires PHP 5 >= 5.2.0 per . - if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime')) { + if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && (extension_loaded('gmp') && version_compare(PHP_VERSION, '5.2.0', '>='))) { $p = new Math_BigInteger(); $p->value = gmp_nextprime($x->value); diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index 4dc28b9a..12dbb548 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( $filename, RUNKIT_IMPORT_FUNCTIONS | diff --git a/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php b/tests/Unit/Math/BigInteger/InternalOpenSSLTest.php index 7f86fd63..4b297d9f 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 8206061a308c08bbb07a9b4e9450ca5b02775012 Mon Sep 17 00:00:00 2001 From: Cameron Eagans Date: Wed, 2 Sep 2015 15:22:37 -0400 Subject: [PATCH 2/2] Remove unnecessary parens --- 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 b1057c88..fc1fff70 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3236,7 +3236,7 @@ class Math_BigInteger $x = $this->random($min, $max); // gmp_nextprime() requires PHP 5 >= 5.2.0 per . - if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && (extension_loaded('gmp') && version_compare(PHP_VERSION, '5.2.0', '>='))) { + if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && extension_loaded('gmp') && version_compare(PHP_VERSION, '5.2.0', '>=')) { $p = new Math_BigInteger(); $p->value = gmp_nextprime($x->value);