aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/php/ini.php
blob: 17e8c54a5738934c511360e0f499f9e93e17cd4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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;
	}
}