📄 common.lib.php
字号:
<?php/* $Id: common.lib.php 10051 2007-03-02 17:22:14Z lem9 $ */// vim: expandtab sw=4 ts=4 sts=4:/** * Misc stuff and functions used by almost all the scripts. * Among other things, it contains the advanced authentication work. *//** * Order of sections for common.lib.php: * * the include of libraries/defines_mysql.lib.php must be after the connection * to db to get the MySql version * * the authentication libraries must be before the connection to db * * ... so the required order is: * * LABEL_definition_of_functions * - definition of functions * LABEL_variables_init * - init some variables always needed * LABEL_parsing_config_file * - parsing of the config file * LABEL_loading_language_file * - loading language file * LABEL_theme_setup * - setting up themes * * - load of mysql extension (if necessary) label_loading_mysql * - loading of an authentication library label_ * - db connection * - authentication work * - load of the libraries/defines_mysql.lib.php library to get the MySQL * release number *//** * For now, avoid warnings of E_STRICT mode * (this must be done before function definitions) */if (defined('E_STRICT')) { $old_error_reporting = error_reporting(0); if ($old_error_reporting & E_STRICT) { error_reporting($old_error_reporting ^ E_STRICT); } else { error_reporting($old_error_reporting); } unset($old_error_reporting);}/** * Avoid object cloning errors */@ini_set('zend.ze1_compatibility_mode',false);/** * Avoid problems with magic_quotes_runtime */@ini_set('magic_quotes_runtime',false);/******************************************************************************//* definition of functions LABEL_definition_of_functions *//** * Removes insecure parts in a path; used before include() or * require() when a part of the path comes from an insecure source * like a cookie or form. * * @param string The path to check * * @return string The secured path * * @access public * @author Marc Delisle (lem9@users.sourceforge.net) */function PMA_securePath($path){ // change .. to . $path = preg_replace('@\.\.*@', '.', $path); return $path;} // end function/** * returns count of tables in given db * * @param string $db database to count tables for * @return integer count of tables in $db */function PMA_getTableCount($db){ $tables = PMA_DBI_try_query( 'SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE); if ($tables) { $num_tables = PMA_DBI_num_rows($tables); PMA_DBI_free_result($tables); } else { $num_tables = 0; } return $num_tables;}/** * Converts numbers like 10M into bytes * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas * (renamed with PMA prefix to avoid double definition when embedded * in Moodle) * * @param string $size * @return integer $size */function PMA_get_real_size($size = 0){ if (!$size) { return 0; } $scan['MB'] = 1048576; $scan['Mb'] = 1048576; $scan['M'] = 1048576; $scan['m'] = 1048576; $scan['KB'] = 1024; $scan['Kb'] = 1024; $scan['K'] = 1024; $scan['k'] = 1024; while (list($key) = each($scan)) { if ((strlen($size) > strlen($key)) && (substr($size, strlen($size) - strlen($key)) == $key)) { $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; break; } } return $size;} // end function PMA_get_real_size()/** * loads php module * * @uses PHP_OS * @uses extension_loaded() * @uses ini_get() * @uses function_exists() * @uses ob_start() * @uses phpinfo() * @uses strip_tags() * @uses ob_get_contents() * @uses ob_end_clean() * @uses preg_match() * @uses strtoupper() * @uses substr() * @uses dl() * @param string $module name if module to load * @return boolean success loading module */function PMA_dl($module){ static $dl_allowed = null; if (extension_loaded($module)) { return true; } if (null === $dl_allowed) { if (!@ini_get('safe_mode') && @ini_get('enable_dl') && @function_exists('dl')) { ob_start(); phpinfo(INFO_GENERAL); /* Only general info */ $a = strip_tags(ob_get_contents()); ob_end_clean(); if (preg_match('@Thread Safety[[:space:]]*enabled@', $a)) { if (preg_match('@Server API[[:space:]]*\(CGI\|CLI\)@', $a)) { $dl_allowed = true; } else { $dl_allowed = false; } } else { $dl_allowed = true; } } else { $dl_allowed = false; } } if (!$dl_allowed) { return false; } /* Once we require PHP >= 4.3, we might use PHP_SHLIB_SUFFIX here */ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $module_file = 'php_' . $module . '.dll'; } elseif (PHP_OS=='HP-UX') { $module_file = $module . '.sl'; } else { $module_file = $module . '.so'; } return @dl($module_file);}/** * merges array recursive like array_merge_recursive() but keyed-values are * always overwritten. * * array PMA_array_merge_recursive(array $array1[, array $array2[, array ...]]) * * @see http://php.net/array_merge * @see http://php.net/array_merge_recursive * @uses func_num_args() * @uses func_get_arg() * @uses is_array() * @uses call_user_func_array() * @param array array to merge * @param array array to merge * @param array ... * @return array merged array */function PMA_array_merge_recursive(){ switch(func_num_args()) { case 0 : return false; break; case 1 : // when does that happen? return func_get_arg(0); break; case 2 : $args = func_get_args(); if (!is_array($args[0]) || !is_array($args[1])) { return $args[1]; } foreach ($args[1] as $key2 => $value2) { if (isset($args[0][$key2]) && !is_int($key2)) { $args[0][$key2] = PMA_array_merge_recursive($args[0][$key2], $value2); } else { // we erase the parent array, otherwise we cannot override a directive that // contains array elements, like this: // (in config.default.php) $cfg['ForeignKeyDropdownOrder'] = array('id-content','content-id'); // (in config.inc.php) $cfg['ForeignKeyDropdownOrder'] = array('content-id'); if (is_int($key2) && $key2 == 0) { unset($args[0]); } $args[0][$key2] = $value2; } } return $args[0]; break; default : $args = func_get_args(); $args[1] = PMA_array_merge_recursive($args[0], $args[1]); array_shift($args); return call_user_func_array('PMA_array_merge_recursive', $args); break; }}/** * calls $function for every element in $array recursively * * this function is protected against deep recursion attack CVE-2006-1549, * 1000 seems to be more than enough * * @see http://www.php-security.org/MOPB/MOPB-02-2007.html * @see http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-1549 * * @param array $array array to walk * @param string $function function to call for every array element */function PMA_arrayWalkRecursive(&$array, $function, $apply_to_keys_also = false){ static $recursive_counter = 0; if (++$recursive_counter > 1000) { die('possible deep recursion attack'); } foreach ($array as $key => $value) { if (is_array($value)) { PMA_arrayWalkRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } $recursive_counter++;}/** * boolean phpMyAdmin.PMA_checkPageValidity(string &$page, array $whitelist) * * checks given given $page against given $whitelist and returns true if valid * it ignores optionaly query paramters in $page (script.php?ignored) * * @uses in_array() * @uses urldecode() * @uses substr() * @uses strpos() * @param string &$page page to check * @param array $whitelist whitelist to check page against * @return boolean whether $page is valid or not (in $whitelist or not) */function PMA_checkPageValidity(&$page, $whitelist){ if (! isset($page) || !is_string($page)) { return false; } if (in_array($page, $whitelist)) { return true; } elseif (in_array(substr($page, 0, strpos($page . '?', '?')), $whitelist)) { return true; } else { $_page = urldecode($page); if (in_array(substr($_page, 0, strpos($_page . '?', '?')), $whitelist)) { return true; } } return false;}/** * trys to find the value for the given environment vriable name * * searchs in $_SERVER, $_ENV than trys getenv() and apache_getenv() * in this order * * @param string $var_name variable name * @return string value of $var or empty string */function PMA_getenv($var_name) { if (isset($_SERVER[$var_name])) { return $_SERVER[$var_name]; } elseif (isset($_ENV[$var_name])) { return $_ENV[$var_name]; } elseif (getenv($var_name)) { return getenv($var_name); } elseif (function_exists('apache_getenv') && apache_getenv($var_name, true)) { return apache_getenv($var_name, true); } return '';}/** * include here only libraries which contain only function definitions * no code im main()!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -