Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / core.lib.php
blob70740b6ad23fbd99e1e6932e37d799f8211f8f3f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
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.
8 * @version $Id$
9 * @package phpMyAdmin
12 /**
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
17 * <code>
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
31 * </code>
33 * @uses PMA_isValid()
34 * @see PMA_isValid()
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)) {
43 return $default;
46 return $var;
49 /**
50 * checks given $var against $type or $compare
52 * $type can be:
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
62 * <code>
63 * // $_REQUEST['doit'] = true;
64 * PMA_isValid($_REQUEST['doit'], 'identical', 'true'); // false
65 * // $_REQUEST['doit'] = 'true';
66 * PMA_isValid($_REQUEST['doit'], 'identical', 'true'); // true
67 * </code>
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:
72 * <code>
73 * // $var is not set
74 * isset($var); // false
75 * functionCallByReference($var); // false
76 * isset($var); // true
77 * functionCallByReference($var); // true
78 * </code>
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, ...?
84 * @uses is_scalar()
85 * @uses is_numeric()
86 * @uses is_array()
87 * @uses in_array()
88 * @uses gettype()
89 * @uses strtolower()
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)
98 if (! isset($var)) {
99 // var is not even set
100 return false;
103 if ($type === false) {
104 // no vartype requested
105 return true;
108 if (is_array($type)) {
109 return in_array($var, $type);
112 // allow some aliaes of var types
113 $type = strtolower($type);
114 switch ($type) {
115 case 'identic' :
116 $type = 'identical';
117 break;
118 case 'len' :
119 $type = 'length';
120 break;
121 case 'bool' :
122 $type = 'boolean';
123 break;
124 case 'float' :
125 $type = 'double';
126 break;
127 case 'int' :
128 $type = 'integer';
129 break;
130 case 'null' :
131 $type = 'NULL';
132 break;
135 if ($type === 'identical') {
136 return $var === $compare;
139 // whether we should check against given $compare
140 if ($type === 'similar') {
141 switch (gettype($compare)) {
142 case 'string':
143 case 'boolean':
144 $type = 'scalar';
145 break;
146 case 'integer':
147 case 'double':
148 $type = 'numeric';
149 break;
150 default:
151 $type = gettype($compare);
153 } elseif ($type === 'equal') {
154 $type = gettype($compare);
157 // do the check
158 if ($type === 'length' || $type === 'scalar') {
159 $is_scalar = is_scalar($var);
160 if ($is_scalar && $type === 'length') {
161 return (bool) strlen($var);
163 return $is_scalar;
166 if ($type === 'numeric') {
167 return is_numeric($var);
170 if (gettype($var) === $type) {
171 return true;
174 return false;
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
186 * @access public
188 function PMA_securePath($path)
190 // change .. to .
191 $path = preg_replace('@\.\.*@', '.', $path);
193 return $path;
194 } // end function
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']
205 * @uses __('Error')
206 * @uses $GLOBALS['available_languages']
207 * @uses $GLOBALS['lang']
208 * @uses $GLOBALS['PMA_Config']->removeCookie()
209 * @uses select_lang.lib.php
210 * @uses $_COOKIE
211 * @uses substr()
212 * @uses header()
213 * @uses http_build_query()
214 * @uses is_string()
215 * @uses sprintf()
216 * @uses vsprintf()
217 * @uses strtr()
218 * @uses defined()
219 * @param string $error_message the error message or named error message
220 * @param string|array $message_args arguments applied to $error_message
221 * @return exit
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 $GLOBALS['strError'] = __('Error');
236 // $text_dir is set in po file
237 if (isset($text_dir)) {
238 $GLOBALS['text_dir'] = $text_dir;
242 // $error_message could be a language string identifier: strString
243 if (substr($error_message, 0, 3) === 'str') {
244 if (isset($$error_message)) {
245 $error_message = $$error_message;
246 } elseif (isset($GLOBALS[$error_message])) {
247 $error_message = $GLOBALS[$error_message];
251 if (is_string($message_args)) {
252 $error_message = sprintf($error_message, $message_args);
253 } elseif (is_array($message_args)) {
254 $error_message = vsprintf($error_message, $message_args);
256 $error_message = strtr($error_message, array('<br />' => '[br]'));
258 // Displays the error message
259 // (do not use &amp; for parameters sent by header)
260 $query_params = array(
261 'lang' => $GLOBALS['available_languages'][$GLOBALS['lang']][1],
262 'dir' => $GLOBALS['text_dir'],
263 'type' => __('Error'),
264 'error' => $error_message,
266 header('Location: ' . (defined('PMA_SETUP') ? '../' : '') . 'error.php?'
267 . http_build_query($query_params, null, '&'));
269 // on fatal errors it cannot hurt to always delete the current session
270 if (isset($GLOBALS['session_name']) && isset($_COOKIE[$GLOBALS['session_name']])) {
271 $GLOBALS['PMA_Config']->removeCookie($GLOBALS['session_name']);
274 exit;
278 * Warn or fail on missing extension.
280 * @param string $extension Extension name
281 * @param bool $fatal Whether the error is fatal.
282 / @param string $extra Extra string to append to messsage.
284 function PMA_warnMissingExtension($extension, $fatal = false, $extra = '')
286 $message = sprintf(__('Cannot load [a@http://php.net/%1$s@Documentation][em]%1$s[/em][/a] extension. Please check your PHP configuration.'), $extension);
287 if ($extra != '') {
288 $message .= ' ' . $extra;
290 if ($fatal) {
291 PMA_fatalError($message);
292 } else {
293 trigger_error($message, E_USER_WARNING);
298 * returns count of tables in given db
300 * @uses PMA_DBI_try_query()
301 * @uses PMA_backquote()
302 * @uses PMA_DBI_QUERY_STORE()
303 * @uses PMA_DBI_num_rows()
304 * @uses PMA_DBI_free_result()
305 * @param string $db database to count tables for
306 * @return integer count of tables in $db
308 function PMA_getTableCount($db)
310 $tables = PMA_DBI_try_query(
311 'SHOW TABLES FROM ' . PMA_backquote($db) . ';',
312 null, PMA_DBI_QUERY_STORE);
313 if ($tables) {
314 $num_tables = PMA_DBI_num_rows($tables);
316 // for blobstreaming - get blobstreaming tables
317 // for use in determining if a table here is a blobstreaming table
319 // load PMA configuration
320 $PMA_Config = $GLOBALS['PMA_Config'];
322 // if PMA configuration exists
323 if (!empty($PMA_Config))
325 // load BS tables
326 $session_bs_tables = $GLOBALS['PMA_Config']->get('BLOBSTREAMING_TABLES');
328 // if BS tables exist
329 if (isset ($session_bs_tables))
330 while ($data = PMA_DBI_fetch_assoc($tables))
331 foreach ($session_bs_tables as $table_key=>$table_val)
332 // if the table is a blobstreaming table, reduce the table count
333 if ($data['Tables_in_' . $db] == $table_key)
335 if ($num_tables > 0)
336 $num_tables--;
338 break;
340 } // end if PMA configuration exists
342 PMA_DBI_free_result($tables);
343 } else {
344 $num_tables = 0;
347 return $num_tables;
351 * Converts numbers like 10M into bytes
352 * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
353 * (renamed with PMA prefix to avoid double definition when embedded
354 * in Moodle)
356 * @uses each()
357 * @uses strlen()
358 * @uses substr()
359 * @param string $size
360 * @return integer $size
362 function PMA_get_real_size($size = 0)
364 if (! $size) {
365 return 0;
368 $scan['gb'] = 1073741824; //1024 * 1024 * 1024;
369 $scan['g'] = 1073741824; //1024 * 1024 * 1024;
370 $scan['mb'] = 1048576;
371 $scan['m'] = 1048576;
372 $scan['kb'] = 1024;
373 $scan['k'] = 1024;
374 $scan['b'] = 1;
376 foreach ($scan as $unit => $factor) {
377 if (strlen($size) > strlen($unit)
378 && strtolower(substr($size, strlen($size) - strlen($unit))) == $unit) {
379 return substr($size, 0, strlen($size) - strlen($unit)) * $factor;
383 return $size;
384 } // end function PMA_get_real_size()
387 * merges array recursive like array_merge_recursive() but keyed-values are
388 * always overwritten.
390 * array PMA_array_merge_recursive(array $array1[, array $array2[, array ...]])
392 * @see http://php.net/array_merge
393 * @see http://php.net/array_merge_recursive
394 * @uses func_num_args()
395 * @uses func_get_arg()
396 * @uses is_array()
397 * @uses call_user_func_array()
398 * @param array array to merge
399 * @param array array to merge
400 * @param array ...
401 * @return array merged array
403 function PMA_array_merge_recursive()
405 switch(func_num_args()) {
406 case 0 :
407 return false;
408 break;
409 case 1 :
410 // when does that happen?
411 return func_get_arg(0);
412 break;
413 case 2 :
414 $args = func_get_args();
415 if (!is_array($args[0]) || !is_array($args[1])) {
416 return $args[1];
418 foreach ($args[1] as $key2 => $value2) {
419 if (isset($args[0][$key2]) && !is_int($key2)) {
420 $args[0][$key2] = PMA_array_merge_recursive($args[0][$key2],
421 $value2);
422 } else {
423 // we erase the parent array, otherwise we cannot override a directive that
424 // contains array elements, like this:
425 // (in config.default.php) $cfg['ForeignKeyDropdownOrder'] = array('id-content','content-id');
426 // (in config.inc.php) $cfg['ForeignKeyDropdownOrder'] = array('content-id');
427 if (is_int($key2) && $key2 == 0) {
428 unset($args[0]);
430 $args[0][$key2] = $value2;
433 return $args[0];
434 break;
435 default :
436 $args = func_get_args();
437 $args[1] = PMA_array_merge_recursive($args[0], $args[1]);
438 array_shift($args);
439 return call_user_func_array('PMA_array_merge_recursive', $args);
440 break;
445 * calls $function vor every element in $array recursively
447 * this function is protected against deep recursion attack CVE-2006-1549,
448 * 1000 seems to be more than enough
450 * @see http://www.php-security.org/MOPB/MOPB-02-2007.html
451 * @see http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-1549
453 * @uses PMA_arrayWalkRecursive()
454 * @uses is_array()
455 * @uses is_string()
456 * @param array $array array to walk
457 * @param string $function function to call for every array element
459 function PMA_arrayWalkRecursive(&$array, $function, $apply_to_keys_also = false)
461 static $recursive_counter = 0;
462 if (++$recursive_counter > 1000) {
463 die('possible deep recursion attack');
465 foreach ($array as $key => $value) {
466 if (is_array($value)) {
467 PMA_arrayWalkRecursive($array[$key], $function, $apply_to_keys_also);
468 } else {
469 $array[$key] = $function($value);
472 if ($apply_to_keys_also && is_string($key)) {
473 $new_key = $function($key);
474 if ($new_key != $key) {
475 $array[$new_key] = $array[$key];
476 unset($array[$key]);
480 $recursive_counter--;
484 * boolean phpMyAdmin.PMA_checkPageValidity(string &$page, array $whitelist)
486 * checks given given $page against given $whitelist and returns true if valid
487 * it ignores optionaly query paramters in $page (script.php?ignored)
489 * @uses in_array()
490 * @uses urldecode()
491 * @uses substr()
492 * @uses strpos()
493 * @param string &$page page to check
494 * @param array $whitelist whitelist to check page against
495 * @return boolean whether $page is valid or not (in $whitelist or not)
497 function PMA_checkPageValidity(&$page, $whitelist)
499 if (! isset($page) || !is_string($page)) {
500 return false;
503 if (in_array($page, $whitelist)) {
504 return true;
505 } elseif (in_array(substr($page, 0, strpos($page . '?', '?')), $whitelist)) {
506 return true;
507 } else {
508 $_page = urldecode($page);
509 if (in_array(substr($_page, 0, strpos($_page . '?', '?')), $whitelist)) {
510 return true;
513 return false;
517 * trys to find the value for the given environment vriable name
519 * searchs in $_SERVER, $_ENV than trys getenv() and apache_getenv()
520 * in this order
522 * @uses $_SERVER
523 * @uses $_ENV
524 * @uses getenv()
525 * @uses function_exists()
526 * @uses apache_getenv()
527 * @param string $var_name variable name
528 * @return string value of $var or empty string
530 function PMA_getenv($var_name) {
531 if (isset($_SERVER[$var_name])) {
532 return $_SERVER[$var_name];
533 } elseif (isset($_ENV[$var_name])) {
534 return $_ENV[$var_name];
535 } elseif (getenv($var_name)) {
536 return getenv($var_name);
537 } elseif (function_exists('apache_getenv')
538 && apache_getenv($var_name, true)) {
539 return apache_getenv($var_name, true);
542 return '';