Convert most instances of function_exists() to extension_loaded() where applicable

This commit is contained in:
Cameron Eagans 2015-09-01 14:32:22 -04:00
parent e391fdbd27
commit 2b75c37cc9
4 changed files with 10 additions and 11 deletions

View File

@ -68,9 +68,8 @@ if (!function_exists('crypt_random_string')) {
function crypt_random_string($length) function crypt_random_string($length)
{ {
if (CRYPT_RANDOM_IS_WINDOWS) { 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. // method 1. prior to PHP 5.3, mcrypt_create_iv() would call rand() on windows
// ie. class_alias is a function that was introduced in PHP 5.3 if (extension_loaded('mcrypt') && version_compare(PHP_VERSION, '5.3.0', '>=')) {
if (function_exists('mcrypt_create_iv') && function_exists('class_alias')) {
return mcrypt_create_iv($length); 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, // 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 // 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 // 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); return openssl_random_pseudo_bytes($length);
} }
} else { } else {
// method 1. the fastest // 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); return openssl_random_pseudo_bytes($length);
} }
// method 2 // 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 // 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 // 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 // restrictions or some such
if (function_exists('mcrypt_create_iv')) { if (extension_loaded('mcrypt')) {
return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
} }
} }

View File

@ -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 // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work
ob_start(); ob_start();
@phpinfo(); @phpinfo();
@ -3236,7 +3236,7 @@ class Math_BigInteger
$x = $this->random($min, $max); $x = $this->random($min, $max);
// gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>. // gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>.
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 = new Math_BigInteger();
$p->value = gmp_nextprime($x->value); $p->value = gmp_nextprime($x->value);

View File

@ -59,7 +59,7 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase
$value = constant($constant); $value = constant($constant);
if ($value !== $expected) { if ($value !== $expected) {
if (function_exists('runkit_constant_redefine')) { if (extension_loaded('runkit')) {
if (!runkit_constant_redefine($constant, $expected)) { if (!runkit_constant_redefine($constant, $expected)) {
self::markTestSkipped(sprintf( self::markTestSkipped(sprintf(
"Failed to redefine constant %s to %s", "Failed to redefine constant %s to %s",
@ -88,7 +88,7 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase
*/ */
protected static function reRequireFile($filename) protected static function reRequireFile($filename)
{ {
if (function_exists('runkit_import')) { if (extension_loaded('runkit')) {
$result = runkit_import( $result = runkit_import(
$filename, $filename,
RUNKIT_IMPORT_FUNCTIONS | RUNKIT_IMPORT_FUNCTIONS |

View File

@ -9,7 +9,7 @@ class Unit_Math_BigInteger_InternalOpenSSLTest extends Unit_Math_BigInteger_Test
{ {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
if (!function_exists('openssl_public_encrypt')) { if (!extension_loaded('openssl')) {
self::markTestSkipped('openssl_public_encrypt() function is not available.'); self::markTestSkipped('openssl_public_encrypt() function is not available.');
} }