diff options
author | Oleg Pudeyev <oleg@bsdpower.com> | 2012-06-11 16:02:11 -0400 |
---|---|---|
committer | Oleg Pudeyev <oleg@bsdpower.com> | 2012-06-11 16:02:11 -0400 |
commit | 300b420e01b7fc800122d9d12c10679d077e2f8a (patch) | |
tree | 0eb2158c64ce42365c9e5a4b9f0826c408553458 /phpBB/includes | |
parent | 4af503e11bc2c42654cf783f031bdb074fdd91ed (diff) | |
parent | 4468847107103c44936468e6e3c1badeb333ff52 (diff) | |
download | forums-300b420e01b7fc800122d9d12c10679d077e2f8a.tar forums-300b420e01b7fc800122d9d12c10679d077e2f8a.tar.gz forums-300b420e01b7fc800122d9d12c10679d077e2f8a.tar.bz2 forums-300b420e01b7fc800122d9d12c10679d077e2f8a.tar.xz forums-300b420e01b7fc800122d9d12c10679d077e2f8a.zip |
Merge PR #834 branch 'bantu/ticket/10931' into develop
* bantu/ticket/10931:
[ticket/10931] Apply strtolower() correctly, i.e. not on false.
[ticket/10931] Also test get_bytes() and get_string() with false.
[ticket/10931] Make to_numeric function globally available.
[ticket/10931] Make sure get_bytes() always returns either an int or a float.
[ticket/10931] Correctly handle inputs such as '-k' as invalid in get_bytes().
[ticket/10931] Use strict assertSame() instead of assertEquals().
[ticket/10931] Also test for negative values.
[ticket/10931] Also test lower case units in test_get_bytes().
[ticket/10931] Correctly use GNU GPL version 2.
[ticket/10931] Make it clear that we are mocking the ini_get() function.
[ticket/10931] Document that false is also returned if value is not well formed
[ticket/10931] Correct method description of get_string().
[ticket/10931] Let us try ini_get() without error suppression.
[ticket/10931] Unit tests for phpbb_php_ini class.
[ticket/10931] Add wrapper class for ini_get function.
Diffstat (limited to 'phpBB/includes')
-rw-r--r-- | phpBB/includes/functions.php | 13 | ||||
-rw-r--r-- | phpBB/includes/php/ini.php | 175 |
2 files changed, 188 insertions, 0 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 new file mode 100644 index 0000000000..17e8c54a57 --- /dev/null +++ b/phpBB/includes/php/ini.php @@ -0,0 +1,175 @@ +<?php +/** +* +* @package phpBB +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Wrapper class for ini_get function. +* +* Provides easier handling of the different interpretations of ini values. +* +* @package phpBB +*/ +class phpbb_php_ini +{ + /** + * Simple wrapper for ini_get() + * See http://php.net/manual/en/function.ini-get.php + * + * @param string $varname The configuration option name. + * @return bool|string False if configuration option does not exist, + * the configuration option value (string) otherwise. + */ + public function get($varname) + { + return ini_get($varname); + } + + /** + * 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, + * the configuration option value (string) otherwise. + */ + public function get_string($varname) + { + $value = $this->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 = $this->get_string($varname); + + if (empty($value) || strtolower($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, + * false if configuration option value is not numeric, + * 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, + * false if configuration option value is not numeric, + * 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, + * false if configuration option value is not well-formed, + * the configuration option value otherwise. + */ + public function get_bytes($varname) + { + $value = $this->get_string($varname); + + if ($value === false) + { + return false; + } + + if (is_numeric($value)) + { + // Already in bytes. + return phpbb_to_numeric($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; + } + + $value_lower = strtolower($value); + $value_numeric = phpbb_to_numeric($value); + + switch ($value_lower[strlen($value_lower) - 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; + } +} |