From 5eea5be2106b18ed50c36c4cc3bd64848548e2b0 Mon Sep 17 00:00:00 2001 From: Jim Wigginton Date: Sun, 6 Feb 2011 01:12:20 +0000 Subject: [PATCH] - fopen() can fail even when file_exists() succeeds (thanks, dakitiger!) git-svn-id: http://phpseclib.svn.sourceforge.net/svnroot/phpseclib/trunk@142 21d32557-59b3-4da0-833f-c5933fad653e --- phpseclib/Crypt/Random.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/phpseclib/Crypt/Random.php b/phpseclib/Crypt/Random.php index d9c96b0b..499c8b4b 100644 --- a/phpseclib/Crypt/Random.php +++ b/phpseclib/Crypt/Random.php @@ -61,13 +61,13 @@ function crypt_random($min = 0, $max = 0x7FFFFFFF) } // see http://en.wikipedia.org/wiki//dev/random - // if open_basedir is enabled file_exists() will ouput an "open_basedir restriction in effect" warning, - // so we suppress it. - if (@file_exists('/dev/urandom')) { - static $fp; - if (!$fp) { - $fp = fopen('/dev/urandom', 'rb'); - } + static $urandom = true; + if ($urandom === true) { + // Warning's will be output unles the error suppression operator is used. Errors such as + // "open_basedir restriction in effect", "Permission denied", "No such file or directory", etc. + $urandom = @fopen('/dev/urandom', 'rb'); + } + if (!is_bool($urandom)) { extract(unpack('Nrandom', fread($fp, 4))); // say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this: