From ca974e2f2a3fc49564d0a595b2d55d04006b9ce5 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:18:00 +0200 Subject: [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 (limited to '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; + } +} -- cgit v1.2.1 From 5bea6ed94658d3302dda54eceaeb326e6f888286 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:40:14 +0200 Subject: [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(-) (limited to 'phpBB/includes/php/ini.php') 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); } /** -- cgit v1.2.1 From 63b2e364929c941d814f6ba493551d458076941a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:45:39 +0200 Subject: [ticket/10931] Correct method description of get_string(). PHPBB3-10931 --- phpBB/includes/php/ini.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'phpBB/includes/php/ini.php') 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, -- cgit v1.2.1 From 7501ea825af8d25309f81ed0159c45597086b78b Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 13:53:17 +0200 Subject: [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(+) (limited to 'phpBB/includes/php/ini.php') 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) -- cgit v1.2.1 From 80dfa53ee3f04dfdba11efe9eb3f49d739bb602b Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 14:24:30 +0200 Subject: [ticket/10931] Correctly use GNU GPL version 2. PHPBB3-10931 --- phpBB/includes/php/ini.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/php/ini.php') 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 * */ -- cgit v1.2.1 From e9348b172a5b0661b26a8f3a0fe3368568539edb Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:06:52 +0200 Subject: [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(+) (limited to 'phpBB/includes/php/ini.php') 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; } -- cgit v1.2.1 From 09fb9a9efe048cef102471d1ce79cdeff932776a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:15:08 +0200 Subject: [ticket/10931] Make sure get_bytes() always returns either an int or a float. PHPBB3-10931 --- phpBB/includes/php/ini.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/php/ini.php') 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; + } } -- cgit v1.2.1 From cbff02db4f84c83c62bdd1f7450b8afbf39a5270 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:22:55 +0200 Subject: [ticket/10931] Make to_numeric function globally available. PHPBB3-10931 --- phpBB/includes/php/ini.php | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'phpBB/includes/php/ini.php') 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; - } } -- cgit v1.2.1 From 4468847107103c44936468e6e3c1badeb333ff52 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 11 Jun 2012 15:45:30 +0200 Subject: [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(-) (limited to 'phpBB/includes/php/ini.php') 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; -- cgit v1.2.1