aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNils Adermann <naderman@naderman.de>2011-06-05 03:22:40 +0200
committerNils Adermann <naderman@naderman.de>2011-06-05 03:22:40 +0200
commitb55bfb459e70bd636033b32d65e3436a006acac7 (patch)
treeee99e5b714c28a55344f73f3b5fc68a5a0ec5d7b
parent0a1e7ca02b22bc65d8343df2e571caa6bfef2aac (diff)
parent893d0ae96f4fba26a10b909d39abe952601bca41 (diff)
downloadforums-b55bfb459e70bd636033b32d65e3436a006acac7.tar
forums-b55bfb459e70bd636033b32d65e3436a006acac7.tar.gz
forums-b55bfb459e70bd636033b32d65e3436a006acac7.tar.bz2
forums-b55bfb459e70bd636033b32d65e3436a006acac7.tar.xz
forums-b55bfb459e70bd636033b32d65e3436a006acac7.zip
Merge branch 'develop-olympus' into develop
* develop-olympus: [ticket/10042] GD CAPTCHA: Call phpbb_mt_rand() where required. [ticket/10042] GD CAPTCHA: Round offset to the next pixel. [ticket/10042] Add mt_rand() wrapper which allows swapping $min and $max.
-rw-r--r--phpBB/includes/captcha/captcha_gd.php2
-rw-r--r--phpBB/includes/functions.php16
-rw-r--r--tests/random/mt_rand.php46
3 files changed, 63 insertions, 1 deletions
diff --git a/phpBB/includes/captcha/captcha_gd.php b/phpBB/includes/captcha/captcha_gd.php
index 15f34aa58f..5e61d6a47b 100644
--- a/phpBB/includes/captcha/captcha_gd.php
+++ b/phpBB/includes/captcha/captcha_gd.php
@@ -77,7 +77,7 @@ class captcha
{
$denom = ($code_len - $i);
$denom = max(1.3, $denom);
- $offset[$i] = mt_rand(0, (1.5 * $width_avail) / $denom);
+ $offset[$i] = phpbb_mt_rand(0, (int) round((1.5 * $width_avail) / $denom));
$width_avail -= $offset[$i];
}
diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php
index de150b9fcb..b22634ce88 100644
--- a/phpBB/includes/functions.php
+++ b/phpBB/includes/functions.php
@@ -191,6 +191,22 @@ function unique_id($extra = 'c')
}
/**
+* Wrapper for mt_rand() which allows swapping $min and $max parameters.
+*
+* PHP does not allow us to swap the order of the arguments for mt_rand() anymore.
+* (since PHP 5.3.4, see http://bugs.php.net/46587)
+*
+* @param int $min Lowest value to be returned
+* @param int $max Highest value to be returned
+*
+* @return int Random integer between $min and $max (or $max and $min)
+*/
+function phpbb_mt_rand($min, $max)
+{
+ return ($min > $max) ? mt_rand($max, $min) : mt_rand($min, $max);
+}
+
+/**
* Return formatted string for filesizes
*
* @param int $value filesize in bytes
diff --git a/tests/random/mt_rand.php b/tests/random/mt_rand.php
new file mode 100644
index 0000000000..d6502c4e80
--- /dev/null
+++ b/tests/random/mt_rand.php
@@ -0,0 +1,46 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2011 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+
+class phpbb_random_mt_rand_test extends phpbb_test_case
+{
+ public function test_max_equals_min()
+ {
+ $result = phpbb_mt_rand(42, 42);
+ $this->assertEquals(42, $result);
+ }
+
+ public function test_max_equals_min_negative()
+ {
+ $result = phpbb_mt_rand(-42, -42);
+ $this->assertEquals(-42, $result);
+ }
+
+ public function test_max_greater_min()
+ {
+ $result = phpbb_mt_rand(3, 4);
+ $this->assertGreaterThanOrEqual(3, $result);
+ $this->assertLessThanOrEqual(4, $result);
+ }
+
+ public function test_min_greater_max()
+ {
+ $result = phpbb_mt_rand(4, 3);
+ $this->assertGreaterThanOrEqual(3, $result);
+ $this->assertLessThanOrEqual(4, $result);
+ }
+
+ public function test_min_greater_max_negative()
+ {
+ $result = phpbb_mt_rand(-3, -4);
+ $this->assertGreaterThanOrEqual(-4, $result);
+ $this->assertLessThanOrEqual(-3, $result);
+ }
+}