3 * Various validation functions
5 * Validation function takes two argument: id for which it is called
6 * and array of fields' values (usually values for entire formset, as defined
8 * The function must always return an array with an error (or error array)
9 * assigned to a form element (formset name or field path). Even if there are
10 * no errors, key must be set with an empty value.
12 * Valdiation functions are assigned in $cfg_db['_validators'] (config_info.inc.php).
14 * @package phpMyAdmin-setup
15 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
20 * Runs validation $validator_id on values $values and returns error list.
23 * o array, keys - field path or formset id, values - array of errors
24 * when $isPostSource is true values is an empty array to allow for error list
25 * cleanup in HTML documen
26 * o false - when no validators match name(s) given by $validator_id
28 * @param string|array $validator_id
29 * @param array $values
30 * @param bool $isPostSource tells whether $values are directly from POST request
33 function validate($validator_id, &$values, $isPostSource)
36 $cf = ConfigFile
::getInstance();
37 $validator_id = (array) $validator_id;
38 $validators = $cf->getDbEntry('_validators');
40 foreach ($validator_id as &$vid) {
41 $vid = $cf->getCanonicalPath($vid);
42 if (isset($validators[$vid])) {
50 // create argument list with canonical paths and remember path mapping
53 foreach ($values as $k => $v) {
54 $k2 = $isPostSource ?
str_replace('-', '/', $k) : $k;
55 $k2 = strpos($k2, '/') ?
$cf->getCanonicalPath($k2) : $k2;
62 foreach ($vids as $vid) {
63 $r = call_user_func($validators[$vid], $vid, $arguments);
66 foreach ($r as $key => $error_list) {
67 // skip empty values if $isPostSource is false
68 if (!$isPostSource && empty($error_list)) {
71 if (!isset($result[$key])) {
72 $result[$key] = array();
74 $result[$key] = array_merge($result[$key], (array)$error_list);
79 // restore original paths
80 $new_result = array();
81 foreach ($result as $k => $v) {
82 $k2 = isset($key_map[$k]) ?
$key_map[$k] : $k;
83 $new_result[$k2] = $v;
85 return empty($new_result) ?
true : $new_result;
89 * Ensures that $php_errormsg variable will be registered in case of an error
90 * and enables output buffering (when $start = true).
91 * Called with $start = false disables output buffering end restores
92 * html_errors and track_errors.
94 * @param boolean $start
96 function test_php_errormsg($start = true)
98 static $old_html_errors, $old_track_errors;
100 $old_html_errors = ini_get('html_errors');
101 $old_track_errors = ini_get('track_errors');
102 ini_set('html_errors', false);
103 ini_set('track_errors', true);
107 ini_set('html_errors', $old_html_errors);
108 ini_set('track_errors', $old_track_errors);
113 * Test database connection
115 * @param string $extension 'mysql' or 'mysqli'
116 * @param string $connect_type 'tcp' or 'socket'
117 * @param string $host
118 * @param string $port
119 * @param string $socket
120 * @param string $user
121 * @param string $pass
122 * @param string $error_key
125 function test_db_connection($extension, $connect_type, $host, $port, $socket, $user, $pass = null, $error_key = 'Server')
127 // test_php_errormsg();
128 $socket = empty($socket) ||
$connect_type == 'tcp' ?
null : ':' . $socket;
129 $port = empty($port) ||
$connect_type == 'socket' ?
null : ':' . $port;
131 if ($extension == 'mysql') {
132 $conn = @mysql_connect
($host . $socket . $port, $user, $pass);
134 $error = PMA_lang('error_connection');
139 $conn = @mysqli_connect
($host, $user, $pass, null, $port, $socket);
141 $error = PMA_lang('error_connection');
146 // test_php_errormsg(false);
147 if (isset($php_errormsg)) {
148 $error .= " - $php_errormsg";
150 return is_null($error) ?
true : array($error_key => $error);
154 * Validate server config
156 * @param string $path
157 * @param array $values
160 function validate_server($path, $values)
162 $result = array('Server' => '', 'Servers/1/user' => '', 'Servers/1/SignonSession' => '', 'Servers/1/SignonURL' => '');
164 if ($values['Servers/1/auth_type'] == 'config' && empty($values['Servers/1/user'])) {
165 $result['Servers/1/user'] = PMA_lang('error_empty_user_for_config_auth');
168 if ($values['Servers/1/auth_type'] == 'signon' && empty($values['Servers/1/SignonSession'])) {
169 $result['Servers/1/SignonSession'] = PMA_lang('error_empty_signon_session');
172 if ($values['Servers/1/auth_type'] == 'signon' && empty($values['Servers/1/SignonURL'])) {
173 $result['Servers/1/SignonURL'] = PMA_lang('error_empty_signon_url');
177 if (!$error && $values['Servers/1/auth_type'] == 'config') {
178 $password = $values['Servers/1/nopassword'] ?
null : $values['Servers/1/password'];
179 $test = test_db_connection($values['Servers/1/extension'], $values['Servers/1/connect_type'], $values['Servers/1/host'], $values['Servers/1/port'], $values['Servers/1/socket'], $values['Servers/1/user'], $password, 'Server');
180 if ($test !== true) {
181 $result = array_merge($result, $test);
188 * Validate pmadb config
190 * @param string $path
191 * @param array $values
194 function validate_pmadb($path, $values)
196 $tables = array('Servers/1/bookmarktable', 'Servers/1/relation', 'Servers/1/table_info', 'Servers/1/table_coords', 'Servers/1/pdf_pages', 'Servers/1/column_info', 'Servers/1/history', 'Servers/1/designer_coords');
197 $result = array('Server_pmadb' => '', 'Servers/1/controluser' => '', 'Servers/1/controlpass' => '');
200 if ($values['Servers/1/pmadb'] == '') {
205 if ($values['Servers/1/controluser'] == '') {
206 $result['Servers/1/controluser'] = PMA_lang('error_empty_pmadb_user');
209 if ($values['Servers/1/controlpass'] == '') {
210 $result['Servers/1/controlpass'] = PMA_lang('error_empty_pmadb_password');
214 $test = test_db_connection($values['Servers/1/extension'], $values['Servers/1/connect_type'], $values['Servers/1/host'], $values['Servers/1/port'], $values['Servers/1/socket'], $values['Servers/1/controluser'], $values['Servers/1/controlpass'], 'Server_pmadb');
215 if ($test !== true) {
216 $result = array_merge($result, $test);
224 * Validates regular expression
226 * @param string $path
227 * @param array $values
230 function validate_regex($path, $values)
232 $result = array($path => '');
234 if ($values[$path] == '') {
241 preg_match($values[$path], '', $matches);
244 test_php_errormsg(false);
246 if (isset($php_errormsg)) {
247 $error = preg_replace('/^preg_match\(\): /', '', $php_errormsg);
248 return array($path => $error);
255 * Validates TrustedProxies field
257 * @param string $path
258 * @param array $values
261 function validate_trusted_proxies($path, $values)
263 $result = array($path => array());
265 if (empty($values[$path])) {
269 if (is_array($values[$path])) {
270 // value already processed by FormDisplay::save
272 foreach ($values[$path] as $ip => $v) {
273 $lines[] = preg_match('/^-\d+$/', $ip)
279 $lines = explode("\n", $values[$path]);
281 foreach ($lines as $line) {
284 // we catch anything that may (or may not) be an IP
285 if (!preg_match("/^(.+):(?:[ ]?)\\w+$/", $line, $matches)) {
286 $result[$path][] = PMA_lang('error_incorrect_value') . ': ' . $line;
289 // now let's check whether we really have an IP address
290 if (filter_var($matches[1], FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) === false
291 && filter_var($matches[1], FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) === false) {
292 $ip = htmlspecialchars(trim($matches[1]));
293 $result[$path][] = PMA_lang('error_incorrect_ip_address', $ip);
303 * Tests integer value
305 * @param string $path
306 * @param array $values
307 * @param bool $allow_neg allow negative values
308 * @param bool $allow_zero allow zero
309 * @param int $max_value max allowed value
310 * @param string $error_lang_key error message key: $GLOBALS["strSetup$error_lang_key"]
311 * @return string empty string if test is successful
313 function test_number($path, $values, $allow_neg, $allow_zero, $max_value, $error_lang_key)
315 if ($values[$path] === '') {
319 if (intval($values[$path]) != $values[$path] ||
(!$allow_neg && $values[$path] < 0) ||
(!$allow_zero && $values[$path] == 0) ||
$values[$path] > $max_value) {
320 return PMA_lang($error_lang_key);
327 * Validates port number
329 * @param string $path
330 * @param array $values
333 function validate_port_number($path, $values)
335 return array($path => test_number($path, $values, false, false, 65536, 'error_incorrect_port'));
339 * Validates positive number
341 * @param string $path
342 * @param array $values
345 function validate_positive_number($path, $values)
347 return array($path => test_number($path, $values, false, false, PHP_INT_MAX
, 'error_nan_p'));
351 * Validates non-negative number
353 * @param string $path
354 * @param array $values
357 function validate_non_negative_number($path, $values)
359 return array($path => test_number($path, $values, false, true, PHP_INT_MAX
, 'error_nan_nneg'));