2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Core functions used all over the scripts.
5 * This script is distinct from libraries/common.inc.php because this
6 * script is called from /test.
13 * checks given $var and returns it if valid, or $default of not valid
14 * given $var is also checked for type being 'similar' as $default
15 * or against any other type if $type is provided
18 * // $_REQUEST['db'] not set
19 * echo PMA_ifSetOr($_REQUEST['db'], ''); // ''
20 * // $_REQUEST['sql_query'] not set
21 * echo PMA_ifSetOr($_REQUEST['sql_query']); // null
22 * // $cfg['ForceSSL'] not set
23 * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'boolean'); // false
24 * echo PMA_ifSetOr($cfg['ForceSSL']); // null
25 * // $cfg['ForceSSL'] set to 1
26 * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'boolean'); // false
27 * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'similar'); // 1
28 * echo PMA_ifSetOr($cfg['ForceSSL'], false); // 1
29 * // $cfg['ForceSSL'] set to true
30 * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'boolean'); // true
35 * @param mixed $var param to check
36 * @param mixed $default default value
37 * @param mixed $type var type or array of values to check against $var
38 * @return mixed $var or $default
40 function PMA_ifSetOr(&$var, $default = null, $type = 'similar')
42 if (! PMA_isValid($var, $type, $default)) {
50 * checks given $var against $type or $compare
53 * - false : no type checking
54 * - 'scalar' : whether type of $var is integer, float, string or boolean
55 * - 'numeric' : whether type of $var is any number repesentation
56 * - 'length' : whether type of $var is scalar with a string length > 0
57 * - 'similar' : whether type of $var is similar to type of $compare
58 * - 'equal' : whether type of $var is identical to type of $compare
59 * - 'identical' : whether $var is identical to $compare, not only the type!
60 * - or any other valid PHP variable type
63 * // $_REQUEST['doit'] = true;
64 * PMA_isValid($_REQUEST['doit'], 'identical', 'true'); // false
65 * // $_REQUEST['doit'] = 'true';
66 * PMA_isValid($_REQUEST['doit'], 'identical', 'true'); // true
69 * NOTE: call-by-reference is used to not get NOTICE on undefined vars,
70 * but the var is not altered inside this function, also after checking a var
71 * this var exists nut is not set, example:
74 * isset($var); // false
75 * functionCallByReference($var); // false
76 * isset($var); // true
77 * functionCallByReference($var); // true
80 * to avoid this we set this var to null if not isset
82 * @todo create some testsuites
83 * @todo add some more var types like hex, bin, ...?
90 * @see http://php.net/gettype
91 * @param mixed $var variable to check
92 * @param mixed $type var type or array of valid values to check against $var
93 * @param mixed $compare var to compare with $var
94 * @return boolean whether valid or not
96 function PMA_isValid(&$var, $type = 'length', $compare = null)
99 // var is not even set
103 if ($type === false) {
104 // no vartype requested
108 if (is_array($type)) {
109 return in_array($var, $type);
112 // allow some aliaes of var types
113 $type = strtolower($type);
135 if ($type === 'identical') {
136 return $var === $compare;
139 // whether we should check against given $compare
140 if ($type === 'similar') {
141 switch (gettype($compare)) {
151 $type = gettype($compare);
153 } elseif ($type === 'equal') {
154 $type = gettype($compare);
158 if ($type === 'length' ||
$type === 'scalar') {
159 $is_scalar = is_scalar($var);
160 if ($is_scalar && $type === 'length') {
161 return (bool) strlen($var);
166 if ($type === 'numeric') {
167 return is_numeric($var);
170 if (gettype($var) === $type) {
178 * Removes insecure parts in a path; used before include() or
179 * require() when a part of the path comes from an insecure source
180 * like a cookie or form.
182 * @param string The path to check
184 * @return string The secured path
188 function PMA_securePath($path)
191 $path = preg_replace('@\.\.*@', '.', $path);
197 * displays the given error message on phpMyAdmin error page in foreign language,
198 * ends script execution and closes session
200 * loads language file if not loaded already
202 * @todo use detected argument separator (PMA_Config)
203 * @uses $GLOBALS['session_name']
204 * @uses $GLOBALS['text_dir']
206 * @uses $GLOBALS['available_languages']
207 * @uses $GLOBALS['lang']
208 * @uses $GLOBALS['PMA_Config']->removeCookie()
209 * @uses select_lang.lib.php
213 * @uses http_build_query()
219 * @param string $error_message the error message or named error message
220 * @param string|array $message_args arguments applied to $error_message
223 function PMA_fatalError($error_message, $message_args = null)
225 // it could happen PMA_fatalError() is called before language file is loaded
226 if (! isset($GLOBALS['available_languages'])) {
227 $GLOBALS['cfg'] = array(
228 'DefaultLang' => 'en',
229 'AllowAnywhereRecoding' => false);
231 // Loads the language file
232 require_once './libraries/select_lang.lib.php';
234 // $text_dir is set in po file
235 if (isset($text_dir)) {
236 $GLOBALS['text_dir'] = $text_dir;
240 // $error_message could be a language string identifier: strString
241 if (substr($error_message, 0, 3) === 'str') {
242 if (isset($
$error_message)) {
243 $error_message = $
$error_message;
244 } elseif (isset($GLOBALS[$error_message])) {
245 $error_message = $GLOBALS[$error_message];
249 if (is_string($message_args)) {
250 $error_message = sprintf($error_message, $message_args);
251 } elseif (is_array($message_args)) {
252 $error_message = vsprintf($error_message, $message_args);
254 $error_message = strtr($error_message, array('<br />' => '[br]'));
256 // Displays the error message
257 // (do not use & for parameters sent by header)
258 $query_params = array(
259 'lang' => $GLOBALS['available_languages'][$GLOBALS['lang']][1],
260 'dir' => $GLOBALS['text_dir'],
261 'type' => __('Error'),
262 'error' => $error_message,
264 header('Location: ' . (defined('PMA_SETUP') ?
'../' : '') . 'error.php?'
265 . http_build_query($query_params, null, '&'));
267 // on fatal errors it cannot hurt to always delete the current session
268 if (isset($GLOBALS['session_name']) && isset($_COOKIE[$GLOBALS['session_name']])) {
269 $GLOBALS['PMA_Config']->removeCookie($GLOBALS['session_name']);
276 * Warn or fail on missing extension.
278 * @param string $extension Extension name
279 * @param bool $fatal Whether the error is fatal.
280 / @param string $extra Extra string to append to messsage.
282 function PMA_warnMissingExtension($extension, $fatal = false, $extra = '')
284 $message = sprintf(__('Cannot load [a@http://php.net/%1$s@Documentation][em]%1$s[/em][/a] extension. Please check your PHP configuration.'), $extension);
286 $message .= ' ' . $extra;
289 PMA_fatalError($message);
291 trigger_error($message, E_USER_WARNING
);
296 * returns count of tables in given db
298 * @uses PMA_DBI_try_query()
299 * @uses PMA_backquote()
300 * @uses PMA_DBI_QUERY_STORE()
301 * @uses PMA_DBI_num_rows()
302 * @uses PMA_DBI_free_result()
303 * @param string $db database to count tables for
304 * @return integer count of tables in $db
306 function PMA_getTableCount($db)
308 $tables = PMA_DBI_try_query(
309 'SHOW TABLES FROM ' . PMA_backquote($db) . ';',
310 null, PMA_DBI_QUERY_STORE
);
312 $num_tables = PMA_DBI_num_rows($tables);
314 // for blobstreaming - get blobstreaming tables
315 // for use in determining if a table here is a blobstreaming table
317 // load PMA configuration
318 $PMA_Config = $GLOBALS['PMA_Config'];
320 // if PMA configuration exists
321 if (!empty($PMA_Config))
324 $session_bs_tables = $GLOBALS['PMA_Config']->get('BLOBSTREAMING_TABLES');
326 // if BS tables exist
327 if (isset ($session_bs_tables))
328 while ($data = PMA_DBI_fetch_assoc($tables))
329 foreach ($session_bs_tables as $table_key=>$table_val)
330 // if the table is a blobstreaming table, reduce the table count
331 if ($data['Tables_in_' . $db] == $table_key)
338 } // end if PMA configuration exists
340 PMA_DBI_free_result($tables);
349 * Converts numbers like 10M into bytes
350 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
351 * (renamed with PMA prefix to avoid double definition when embedded
357 * @param string $size
358 * @return integer $size
360 function PMA_get_real_size($size = 0)
366 $scan['gb'] = 1073741824; //1024 * 1024 * 1024;
367 $scan['g'] = 1073741824; //1024 * 1024 * 1024;
368 $scan['mb'] = 1048576;
369 $scan['m'] = 1048576;
374 foreach ($scan as $unit => $factor) {
375 if (strlen($size) > strlen($unit)
376 && strtolower(substr($size, strlen($size) - strlen($unit))) == $unit) {
377 return substr($size, 0, strlen($size) - strlen($unit)) * $factor;
382 } // end function PMA_get_real_size()
385 * merges array recursive like array_merge_recursive() but keyed-values are
386 * always overwritten.
388 * array PMA_array_merge_recursive(array $array1[, array $array2[, array ...]])
390 * @see http://php.net/array_merge
391 * @see http://php.net/array_merge_recursive
392 * @uses func_num_args()
393 * @uses func_get_arg()
395 * @uses call_user_func_array()
396 * @param array array to merge
397 * @param array array to merge
399 * @return array merged array
401 function PMA_array_merge_recursive()
403 switch(func_num_args()) {
408 // when does that happen?
409 return func_get_arg(0);
412 $args = func_get_args();
413 if (!is_array($args[0]) ||
!is_array($args[1])) {
416 foreach ($args[1] as $key2 => $value2) {
417 if (isset($args[0][$key2]) && !is_int($key2)) {
418 $args[0][$key2] = PMA_array_merge_recursive($args[0][$key2],
421 // we erase the parent array, otherwise we cannot override a directive that
422 // contains array elements, like this:
423 // (in config.default.php) $cfg['ForeignKeyDropdownOrder'] = array('id-content','content-id');
424 // (in config.inc.php) $cfg['ForeignKeyDropdownOrder'] = array('content-id');
425 if (is_int($key2) && $key2 == 0) {
428 $args[0][$key2] = $value2;
434 $args = func_get_args();
435 $args[1] = PMA_array_merge_recursive($args[0], $args[1]);
437 return call_user_func_array('PMA_array_merge_recursive', $args);
443 * calls $function vor every element in $array recursively
445 * this function is protected against deep recursion attack CVE-2006-1549,
446 * 1000 seems to be more than enough
448 * @see http://www.php-security.org/MOPB/MOPB-02-2007.html
449 * @see http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-1549
451 * @uses PMA_arrayWalkRecursive()
454 * @param array $array array to walk
455 * @param string $function function to call for every array element
457 function PMA_arrayWalkRecursive(&$array, $function, $apply_to_keys_also = false)
459 static $recursive_counter = 0;
460 if (++
$recursive_counter > 1000) {
461 die('possible deep recursion attack');
463 foreach ($array as $key => $value) {
464 if (is_array($value)) {
465 PMA_arrayWalkRecursive($array[$key], $function, $apply_to_keys_also);
467 $array[$key] = $function($value);
470 if ($apply_to_keys_also && is_string($key)) {
471 $new_key = $function($key);
472 if ($new_key != $key) {
473 $array[$new_key] = $array[$key];
478 $recursive_counter--;
482 * boolean phpMyAdmin.PMA_checkPageValidity(string &$page, array $whitelist)
484 * checks given given $page against given $whitelist and returns true if valid
485 * it ignores optionaly query paramters in $page (script.php?ignored)
491 * @param string &$page page to check
492 * @param array $whitelist whitelist to check page against
493 * @return boolean whether $page is valid or not (in $whitelist or not)
495 function PMA_checkPageValidity(&$page, $whitelist)
497 if (! isset($page) ||
!is_string($page)) {
501 if (in_array($page, $whitelist)) {
503 } elseif (in_array(substr($page, 0, strpos($page . '?', '?')), $whitelist)) {
506 $_page = urldecode($page);
507 if (in_array(substr($_page, 0, strpos($_page . '?', '?')), $whitelist)) {
515 * trys to find the value for the given environment vriable name
517 * searchs in $_SERVER, $_ENV than trys getenv() and apache_getenv()
523 * @uses function_exists()
524 * @uses apache_getenv()
525 * @param string $var_name variable name
526 * @return string value of $var or empty string
528 function PMA_getenv($var_name) {
529 if (isset($_SERVER[$var_name])) {
530 return $_SERVER[$var_name];
531 } elseif (isset($_ENV[$var_name])) {
532 return $_ENV[$var_name];
533 } elseif (getenv($var_name)) {
534 return getenv($var_name);
535 } elseif (function_exists('apache_getenv')
536 && apache_getenv($var_name, true)) {
537 return apache_getenv($var_name, true);