From ca974e2f2a3fc49564d0a595b2d55d04006b9ce5 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:18:00 +0200 Subject: [PATCH 01/15] [ticket/10931] Add wrapper class for ini_get function. Provides easier handling of the different interpretations of ini values. PHPBB3-10931 --- phpBB/includes/php/ini.php | 165 +++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 phpBB/includes/php/ini.php diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php new file mode 100644 index 0000000000..5c2cadb052 --- /dev/null +++ b/phpBB/includes/php/ini.php @@ -0,0 +1,165 @@ +get($varname); + + if ($value === false) + { + return false; + } + + return trim($value); + } + + /** + * Gets configuration option value as a boolean. + * Interprets the string value 'off' as false. + * + * @param string $varname The configuration option name. + * @return bool False if configuration option does not exist. + * False if configuration option is disabled. + * True otherwise. + */ + public function get_bool($varname) + { + $value = strtolower($this->get_string($varname)); + + if (empty($value) || $value == 'off') + { + return false; + } + + return true; + } + + /** + * Gets configuration option value as an integer. + * + * @param string $varname The configuration option name. + * @return bool|int False if configuration option does not exist, + * the configuration option value (integer) otherwise. + */ + public function get_int($varname) + { + $value = $this->get_string($varname); + + if (!is_numeric($value)) + { + return false; + } + + return (int) $value; + } + + /** + * Gets configuration option value as a float. + * + * @param string $varname The configuration option name. + * @return bool|float False if configuration option does not exist, + * the configuration option value (float) otherwise. + */ + public function get_float($varname) + { + $value = $this->get_string($varname); + + if (!is_numeric($value)) + { + return false; + } + + return (float) $value; + } + + /** + * Gets configuration option value in bytes. + * Converts strings like '128M' to bytes (integer or float). + * + * @param string $varname The configuration option name. + * @return bool|int|float False if configuration option does not exist, + * the configuration option value otherwise. + */ + public function get_bytes($varname) + { + $value = strtolower($this->get_string($varname)); + + if ($value === false) + { + return false; + } + + if (is_numeric($value)) + { + return $value; + } + else if (strlen($value) < 2) + { + return false; + } + + $value_numeric = (int) $value; + + switch ($value[strlen($value) - 1]) + { + case 'g': + $value_numeric *= 1024; + case 'm': + $value_numeric *= 1024; + case 'k': + $value_numeric *= 1024; + break; + + default: + // It's not already in bytes (and thus numeric) + // and does not carry a unit. + return false; + } + + return $value_numeric; + } +} From afd6f86892fbbbe06aa0b1295dba361fab29fd5f Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:22:11 +0200 Subject: [PATCH 02/15] [ticket/10931] Unit tests for phpbb_php_ini class. PHPBB3-10931 --- tests/mock/phpbb_php_ini.php | 16 ++++++++ tests/wrapper/phpbb_php_ini_test.php | 58 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/mock/phpbb_php_ini.php create mode 100644 tests/wrapper/phpbb_php_ini_test.php diff --git a/tests/mock/phpbb_php_ini.php b/tests/mock/phpbb_php_ini.php new file mode 100644 index 0000000000..249c1b014a --- /dev/null +++ b/tests/mock/phpbb_php_ini.php @@ -0,0 +1,16 @@ +php_ini = new phpbb_mock_phpbb_php_ini; + } + + public function test_get_string() + { + $this->assertEquals('phpbb', $this->php_ini->get_string(' phpbb ')); + } + + public function test_get_bool() + { + $this->assertEquals(true, $this->php_ini->get_bool('ON')); + $this->assertEquals(true, $this->php_ini->get_bool('on')); + $this->assertEquals(true, $this->php_ini->get_bool('1')); + + $this->assertEquals(false, $this->php_ini->get_bool('OFF')); + $this->assertEquals(false, $this->php_ini->get_bool('off')); + $this->assertEquals(false, $this->php_ini->get_bool('0')); + $this->assertEquals(false, $this->php_ini->get_bool('')); + } + + public function test_get_int() + { + $this->assertEquals(1234, $this->php_ini->get_int('1234')); + $this->assertEquals(false, $this->php_ini->get_int('phpBB')); + } + + public function test_get_float() + { + $this->assertEquals(1234.0, $this->php_ini->get_float('1234')); + $this->assertEquals(false, $this->php_ini->get_float('phpBB')); + } + + public function test_get_bytes() + { + $this->assertEquals(false, $this->php_ini->get_bytes('phpBB')); + $this->assertEquals(false, $this->php_ini->get_bytes('M')); + $this->assertEquals(32 * pow(2, 20), $this->php_ini->get_bytes('32M')); + $this->assertEquals(8 * pow(2, 30), $this->php_ini->get_bytes('8G')); + $this->assertEquals(1234, $this->php_ini->get_bytes('1234')); + } +} From 5bea6ed94658d3302dda54eceaeb326e6f888286 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:40:14 +0200 Subject: [PATCH 03/15] [ticket/10931] Let us try ini_get() without error suppression. PHPBB3-10931 --- phpBB/includes/php/ini.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index 5c2cadb052..92965e7f94 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -34,7 +34,7 @@ class phpbb_php_ini */ public function get($varname) { - return @ini_get($varname); + return ini_get($varname); } /** From 63b2e364929c941d814f6ba493551d458076941a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:45:39 +0200 Subject: [PATCH 04/15] [ticket/10931] Correct method description of get_string(). PHPBB3-10931 --- phpBB/includes/php/ini.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index 92965e7f94..3910700163 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -38,8 +38,7 @@ class phpbb_php_ini } /** - * Gets configuration option value as a string and performs various - * normalisation on the returned value. + * Gets the configuration option value as a trimmed string. * * @param string $varname The configuration option name. * @return bool|string False if configuration option does not exist, From 7501ea825af8d25309f81ed0159c45597086b78b Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:53:17 +0200 Subject: [PATCH 05/15] [ticket/10931] Document that false is also returned if value is not well formed PHPBB3-10931 --- phpBB/includes/php/ini.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index 3910700163..baafee5273 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -82,6 +82,7 @@ class phpbb_php_ini * * @param string $varname The configuration option name. * @return bool|int False if configuration option does not exist, + * false if configuration option value is not numeric, * the configuration option value (integer) otherwise. */ public function get_int($varname) @@ -101,6 +102,7 @@ class phpbb_php_ini * * @param string $varname The configuration option name. * @return bool|float False if configuration option does not exist, + * false if configuration option value is not numeric, * the configuration option value (float) otherwise. */ public function get_float($varname) @@ -121,6 +123,7 @@ class phpbb_php_ini * * @param string $varname The configuration option name. * @return bool|int|float False if configuration option does not exist, + * false if configuration option value is not well-formed, * the configuration option value otherwise. */ public function get_bytes($varname) From 5086366662d78d79bb6daf4b132709d9273c2f8c Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 14:18:21 +0200 Subject: [PATCH 06/15] [ticket/10931] Make it clear that we are mocking the ini_get() function. PHPBB3-10931 --- .../phpbb_php_ini.php => wrapper/phpbb_php_ini_fake.php} | 2 +- tests/wrapper/phpbb_php_ini_test.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename tests/{mock/phpbb_php_ini.php => wrapper/phpbb_php_ini_fake.php} (79%) diff --git a/tests/mock/phpbb_php_ini.php b/tests/wrapper/phpbb_php_ini_fake.php similarity index 79% rename from tests/mock/phpbb_php_ini.php rename to tests/wrapper/phpbb_php_ini_fake.php index 249c1b014a..14ec77c644 100644 --- a/tests/mock/phpbb_php_ini.php +++ b/tests/wrapper/phpbb_php_ini_fake.php @@ -7,7 +7,7 @@ * */ -class phpbb_mock_phpbb_php_ini extends phpbb_php_ini +class phpbb_php_ini_fake extends phpbb_php_ini { function get($varname) { diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 164966fba4..5494f1864d 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -7,7 +7,7 @@ * */ -require_once dirname(__FILE__) . '/../mock/phpbb_php_ini.php'; +require_once dirname(__FILE__) . '/phpbb_php_ini_fake.php'; class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case { @@ -15,7 +15,7 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case public function setUp() { - $this->php_ini = new phpbb_mock_phpbb_php_ini; + $this->php_ini = new phpbb_php_ini_fake; } public function test_get_string() From 80dfa53ee3f04dfdba11efe9eb3f49d739bb602b Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 14:24:30 +0200 Subject: [PATCH 07/15] [ticket/10931] Correctly use GNU GPL version 2. PHPBB3-10931 --- phpBB/includes/php/ini.php | 2 +- tests/wrapper/phpbb_php_ini_fake.php | 2 +- tests/wrapper/phpbb_php_ini_test.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index baafee5273..882464275b 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -3,7 +3,7 @@ * * @package phpBB * @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ diff --git a/tests/wrapper/phpbb_php_ini_fake.php b/tests/wrapper/phpbb_php_ini_fake.php index 14ec77c644..49bc5936e5 100644 --- a/tests/wrapper/phpbb_php_ini_fake.php +++ b/tests/wrapper/phpbb_php_ini_fake.php @@ -3,7 +3,7 @@ * * @package testing * @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 5494f1864d..cdfee802f2 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -3,7 +3,7 @@ * * @package testing * @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ From fb279c9677641db5efa38f24c51db93df004cb46 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 14:31:09 +0200 Subject: [PATCH 08/15] [ticket/10931] Also test lower case units in test_get_bytes(). PHPBB3-10931 --- tests/wrapper/phpbb_php_ini_test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index cdfee802f2..cbb05e98e9 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -50,8 +50,9 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case public function test_get_bytes() { $this->assertEquals(false, $this->php_ini->get_bytes('phpBB')); + $this->assertEquals(false, $this->php_ini->get_bytes('k')); $this->assertEquals(false, $this->php_ini->get_bytes('M')); - $this->assertEquals(32 * pow(2, 20), $this->php_ini->get_bytes('32M')); + $this->assertEquals(32 * pow(2, 20), $this->php_ini->get_bytes('32m')); $this->assertEquals(8 * pow(2, 30), $this->php_ini->get_bytes('8G')); $this->assertEquals(1234, $this->php_ini->get_bytes('1234')); } From 3872abd824c9371af6cbb45e6e9bbfcb31094f98 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 14:35:18 +0200 Subject: [PATCH 09/15] [ticket/10931] Also test for negative values. PHPBB3-10931 --- tests/wrapper/phpbb_php_ini_test.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index cbb05e98e9..84e2b5e92e 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -38,12 +38,14 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case public function test_get_int() { $this->assertEquals(1234, $this->php_ini->get_int('1234')); + $this->assertEquals(-12345, $this->php_ini->get_int('-12345')); $this->assertEquals(false, $this->php_ini->get_int('phpBB')); } public function test_get_float() { $this->assertEquals(1234.0, $this->php_ini->get_float('1234')); + $this->assertEquals(-12345.0, $this->php_ini->get_float('-12345')); $this->assertEquals(false, $this->php_ini->get_float('phpBB')); } @@ -51,9 +53,14 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case { $this->assertEquals(false, $this->php_ini->get_bytes('phpBB')); $this->assertEquals(false, $this->php_ini->get_bytes('k')); + $this->assertEquals(false, $this->php_ini->get_bytes('-k')); $this->assertEquals(false, $this->php_ini->get_bytes('M')); + $this->assertEquals(false, $this->php_ini->get_bytes('-M')); $this->assertEquals(32 * pow(2, 20), $this->php_ini->get_bytes('32m')); + $this->assertEquals(- 32 * pow(2, 20), $this->php_ini->get_bytes('-32m')); $this->assertEquals(8 * pow(2, 30), $this->php_ini->get_bytes('8G')); + $this->assertEquals(- 8 * pow(2, 30), $this->php_ini->get_bytes('-8G')); $this->assertEquals(1234, $this->php_ini->get_bytes('1234')); + $this->assertEquals(-12345, $this->php_ini->get_bytes('-12345')); } } From 44287e57bf9536bd91933347ad64f289ef2a0391 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 14:43:50 +0200 Subject: [PATCH 10/15] [ticket/10931] Use strict assertSame() instead of assertEquals(). PHPBB3-10931 --- tests/wrapper/phpbb_php_ini_test.php | 38 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 84e2b5e92e..5c312300d3 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -20,42 +20,42 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case public function test_get_string() { - $this->assertEquals('phpbb', $this->php_ini->get_string(' phpbb ')); + $this->assertSame('phpbb', $this->php_ini->get_string(' phpbb ')); } public function test_get_bool() { - $this->assertEquals(true, $this->php_ini->get_bool('ON')); - $this->assertEquals(true, $this->php_ini->get_bool('on')); - $this->assertEquals(true, $this->php_ini->get_bool('1')); + $this->assertSame(true, $this->php_ini->get_bool('ON')); + $this->assertSame(true, $this->php_ini->get_bool('on')); + $this->assertSame(true, $this->php_ini->get_bool('1')); - $this->assertEquals(false, $this->php_ini->get_bool('OFF')); - $this->assertEquals(false, $this->php_ini->get_bool('off')); - $this->assertEquals(false, $this->php_ini->get_bool('0')); - $this->assertEquals(false, $this->php_ini->get_bool('')); + $this->assertSame(false, $this->php_ini->get_bool('OFF')); + $this->assertSame(false, $this->php_ini->get_bool('off')); + $this->assertSame(false, $this->php_ini->get_bool('0')); + $this->assertSame(false, $this->php_ini->get_bool('')); } public function test_get_int() { - $this->assertEquals(1234, $this->php_ini->get_int('1234')); - $this->assertEquals(-12345, $this->php_ini->get_int('-12345')); - $this->assertEquals(false, $this->php_ini->get_int('phpBB')); + $this->assertSame(1234, $this->php_ini->get_int('1234')); + $this->assertSame(-12345, $this->php_ini->get_int('-12345')); + $this->assertSame(false, $this->php_ini->get_int('phpBB')); } public function test_get_float() { - $this->assertEquals(1234.0, $this->php_ini->get_float('1234')); - $this->assertEquals(-12345.0, $this->php_ini->get_float('-12345')); - $this->assertEquals(false, $this->php_ini->get_float('phpBB')); + $this->assertSame(1234.0, $this->php_ini->get_float('1234')); + $this->assertSame(-12345.0, $this->php_ini->get_float('-12345')); + $this->assertSame(false, $this->php_ini->get_float('phpBB')); } public function test_get_bytes() { - $this->assertEquals(false, $this->php_ini->get_bytes('phpBB')); - $this->assertEquals(false, $this->php_ini->get_bytes('k')); - $this->assertEquals(false, $this->php_ini->get_bytes('-k')); - $this->assertEquals(false, $this->php_ini->get_bytes('M')); - $this->assertEquals(false, $this->php_ini->get_bytes('-M')); + $this->assertSame(false, $this->php_ini->get_bytes('phpBB')); + $this->assertSame(false, $this->php_ini->get_bytes('k')); + $this->assertSame(false, $this->php_ini->get_bytes('-k')); + $this->assertSame(false, $this->php_ini->get_bytes('M')); + $this->assertSame(false, $this->php_ini->get_bytes('-M')); $this->assertEquals(32 * pow(2, 20), $this->php_ini->get_bytes('32m')); $this->assertEquals(- 32 * pow(2, 20), $this->php_ini->get_bytes('-32m')); $this->assertEquals(8 * pow(2, 30), $this->php_ini->get_bytes('8G')); From e9348b172a5b0661b26a8f3a0fe3368568539edb Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:06:52 +0200 Subject: [PATCH 11/15] [ticket/10931] Correctly handle inputs such as '-k' as invalid in get_bytes(). PHPBB3-10931 --- phpBB/includes/php/ini.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index 882464275b..de1cb5096c 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -137,10 +137,17 @@ class phpbb_php_ini if (is_numeric($value)) { + // Already in bytes. return $value; } else if (strlen($value) < 2) { + // Single character. + return false; + } + else if (strlen($value) < 3 && $value[0] === '-') + { + // Two characters but the first one is a minus. return false; } From 09fb9a9efe048cef102471d1ce79cdeff932776a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:15:08 +0200 Subject: [PATCH 12/15] [ticket/10931] Make sure get_bytes() always returns either an int or a float. PHPBB3-10931 --- phpBB/includes/php/ini.php | 17 +++++++++++++-- tests/wrapper/phpbb_php_ini_test.php | 31 +++++++++++++++++++++------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index de1cb5096c..bbe592a7df 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -138,7 +138,7 @@ class phpbb_php_ini if (is_numeric($value)) { // Already in bytes. - return $value; + return $this->to_numeric($value); } else if (strlen($value) < 2) { @@ -151,7 +151,7 @@ class phpbb_php_ini return false; } - $value_numeric = (int) $value; + $value_numeric = $this->to_numeric($value); switch ($value[strlen($value) - 1]) { @@ -171,4 +171,17 @@ class phpbb_php_ini return $value_numeric; } + + /** + * Casts a numeric string $input to an appropriate numeric type (i.e. integer or float) + * + * @param string $input A numeric string. + * + * @return int|float Integer $input if $input fits integer, + * float $input otherwise. + */ + protected function to_numeric($input) + { + return ($input > PHP_INT_MAX) ? (float) $input : (int) $input; + } } diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 5c312300d3..4d8e583eb8 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -49,18 +49,35 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case $this->assertSame(false, $this->php_ini->get_float('phpBB')); } - public function test_get_bytes() + public function test_get_bytes_invalid() { $this->assertSame(false, $this->php_ini->get_bytes('phpBB')); $this->assertSame(false, $this->php_ini->get_bytes('k')); $this->assertSame(false, $this->php_ini->get_bytes('-k')); $this->assertSame(false, $this->php_ini->get_bytes('M')); $this->assertSame(false, $this->php_ini->get_bytes('-M')); - $this->assertEquals(32 * pow(2, 20), $this->php_ini->get_bytes('32m')); - $this->assertEquals(- 32 * pow(2, 20), $this->php_ini->get_bytes('-32m')); - $this->assertEquals(8 * pow(2, 30), $this->php_ini->get_bytes('8G')); - $this->assertEquals(- 8 * pow(2, 30), $this->php_ini->get_bytes('-8G')); - $this->assertEquals(1234, $this->php_ini->get_bytes('1234')); - $this->assertEquals(-12345, $this->php_ini->get_bytes('-12345')); + } + + /** + * @dataProvider get_bytes_data + */ + public function test_get_bytes($expected, $value) + { + $actual = $this->php_ini->get_bytes($value); + + $this->assertTrue(is_float($actual) || is_int($actual)); + $this->assertEquals($expected, $actual); + } + + static public function get_bytes_data() + { + return array( + array(32 * pow(2, 20), '32m'), + array(- 32 * pow(2, 20), '-32m'), + array(8 * pow(2, 30), '8G'), + array(- 8 * pow(2, 30), '-8G'), + array(1234, '1234'), + array(-12345, '-12345'), + ); } } From cbff02db4f84c83c62bdd1f7450b8afbf39a5270 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:22:55 +0200 Subject: [PATCH 13/15] [ticket/10931] Make to_numeric function globally available. PHPBB3-10931 --- phpBB/includes/functions.php | 13 +++++++++++++ phpBB/includes/php/ini.php | 17 ++--------------- tests/wrapper/phpbb_php_ini_test.php | 1 + 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 95f2cf8d26..ad64471388 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4987,3 +4987,16 @@ function phpbb_pcre_utf8_support() } return $utf8_pcre_properties; } + +/** +* Casts a numeric string $input to an appropriate numeric type (i.e. integer or float) +* +* @param string $input A numeric string. +* +* @return int|float Integer $input if $input fits integer, +* float $input otherwise. +*/ +function phpbb_to_numeric($input) +{ + return ($input > PHP_INT_MAX) ? (float) $input : (int) $input; +} diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index bbe592a7df..02c2b7ccc6 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -138,7 +138,7 @@ class phpbb_php_ini if (is_numeric($value)) { // Already in bytes. - return $this->to_numeric($value); + return phpbb_to_numeric($value); } else if (strlen($value) < 2) { @@ -151,7 +151,7 @@ class phpbb_php_ini return false; } - $value_numeric = $this->to_numeric($value); + $value_numeric = phpbb_to_numeric($value); switch ($value[strlen($value) - 1]) { @@ -171,17 +171,4 @@ class phpbb_php_ini return $value_numeric; } - - /** - * Casts a numeric string $input to an appropriate numeric type (i.e. integer or float) - * - * @param string $input A numeric string. - * - * @return int|float Integer $input if $input fits integer, - * float $input otherwise. - */ - protected function to_numeric($input) - { - return ($input > PHP_INT_MAX) ? (float) $input : (int) $input; - } } diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 4d8e583eb8..418448f102 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -8,6 +8,7 @@ */ require_once dirname(__FILE__) . '/phpbb_php_ini_fake.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case { From 72212077ebecee639c49c473646a38340908d058 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:31:25 +0200 Subject: [PATCH 14/15] [ticket/10931] Also test get_bytes() and get_string() with false. PHPBB3-10931 --- tests/wrapper/phpbb_php_ini_test.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 418448f102..8e08d5c204 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -21,6 +21,7 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case public function test_get_string() { + $this->assertSame(false, $this->php_ini->get_string(false)); $this->assertSame('phpbb', $this->php_ini->get_string(' phpbb ')); } @@ -52,6 +53,7 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case public function test_get_bytes_invalid() { + $this->assertSame(false, $this->php_ini->get_bytes(false)); $this->assertSame(false, $this->php_ini->get_bytes('phpBB')); $this->assertSame(false, $this->php_ini->get_bytes('k')); $this->assertSame(false, $this->php_ini->get_bytes('-k')); From 4468847107103c44936468e6e3c1badeb333ff52 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:45:30 +0200 Subject: [PATCH 15/15] [ticket/10931] Apply strtolower() correctly, i.e. not on false. PHPBB3-10931 --- phpBB/includes/php/ini.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/php/ini.php b/phpBB/includes/php/ini.php index 02c2b7ccc6..17e8c54a57 100644 --- a/phpBB/includes/php/ini.php +++ b/phpBB/includes/php/ini.php @@ -67,9 +67,9 @@ class phpbb_php_ini */ public function get_bool($varname) { - $value = strtolower($this->get_string($varname)); + $value = $this->get_string($varname); - if (empty($value) || $value == 'off') + if (empty($value) || strtolower($value) == 'off') { return false; } @@ -128,7 +128,7 @@ class phpbb_php_ini */ public function get_bytes($varname) { - $value = strtolower($this->get_string($varname)); + $value = $this->get_string($varname); if ($value === false) { @@ -151,9 +151,10 @@ class phpbb_php_ini return false; } + $value_lower = strtolower($value); $value_numeric = phpbb_to_numeric($value); - switch ($value[strlen($value) - 1]) + switch ($value_lower[strlen($value_lower) - 1]) { case 'g': $value_numeric *= 1024;