core.lib.php

来自「phpMyAdmin图形界面化操作,我已经配置好了,只要把解要压缩后的文件放到站」· PHP 代码 · 共 595 行 · 第 1/2 页

PHP
595
字号
<?php/* vim: set expandtab sw=4 ts=4 sts=4: *//** * Core functions used all over the scripts. * This script is distinct from libraries/common.inc.php because this * script is called from /test. * * @version $Id: core.lib.php 11499 2008-08-21 16:45:14Z lem9 $ *//** * checks given $var and returns it if valid, or $default of not valid * given $var is also checked for type being 'similar' as $default * or against any other type if $type is provided * * <code> * // $_REQUEST['db'] not set * echo PMA_ifSetOr($_REQUEST['db'], ''); // '' * // $_REQUEST['sql_query'] not set * echo PMA_ifSetOr($_REQUEST['sql_query']); // null * // $cfg['ForceSSL'] not set * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'boolean'); // false * echo PMA_ifSetOr($cfg['ForceSSL']); // null * // $cfg['ForceSSL'] set to 1 * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'boolean'); // false * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'similar'); // 1 * echo PMA_ifSetOr($cfg['ForceSSL'], false); // 1 * // $cfg['ForceSSL'] set to true * echo PMA_ifSetOr($cfg['ForceSSL'], false, 'boolean'); // true * </code> * * @todo create some testsuites * @uses    PMA_isValid() * @see     PMA_isValid() * @param   mixed   $var        param to check * @param   mixed   $default    default value * @param   mixed   $type       var type or array of values to check against $var * @return  mixed   $var or $default */function PMA_ifSetOr(&$var, $default = null, $type = 'similar'){    if (! PMA_isValid($var, $type, $default)) {        return $default;    }    return $var;}/** * checks given $var against $type or $compare * * $type can be: * - false       : no type checking * - 'scalar'    : whether type of $var is integer, float, string or boolean * - 'numeric'   : whether type of $var is any number repesentation * - 'length'    : whether type of $var is scalar with a string length > 0 * - 'similar'   : whether type of $var is similar to type of $compare * - 'equal'     : whether type of $var is identical to type of $compare * - 'identical' : whether $var is identical to $compare, not only the type! * - or any other valid PHP variable type * * <code> * // $_REQUEST['doit'] = true; * PMA_isValid($_REQUEST['doit'], 'identical', 'true'); // false * // $_REQUEST['doit'] = 'true'; * PMA_isValid($_REQUEST['doit'], 'identical', 'true'); // true * </code> * * NOTE: call-by-reference is used to not get NOTICE on undefined vars, * but the var is not altered inside this function, also after checking a var * this var exists nut is not set, example: * <code> * // $var is not set * isset($var); // false * functionCallByReference($var); // false * isset($var); // true * functionCallByReference($var); // true * </code> * * to avoid this we set this var to null if not isset * * @todo create some testsuites * @todo add some more var types like hex, bin, ...? * @uses    is_scalar() * @uses    is_numeric() * @uses    is_array() * @uses    in_array() * @uses    gettype() * @uses    strtolower() * @see     http://php.net/gettype * @param   mixed   $var        variable to check * @param   mixed   $type       var type or array of valid values to check against $var * @param   mixed   $compare    var to compare with $var * @return  boolean whether valid or not */function PMA_isValid(&$var, $type = 'length', $compare = null){    if (! isset($var)) {        // var is not even set        return false;    }    if ($type === false) {        // no vartype requested        return true;    }    if (is_array($type)) {        return in_array($var, $type);    }    // allow some aliaes of var types    $type = strtolower($type);    switch ($type) {        case 'identic' :            $type = 'identical';            break;        case 'len' :            $type = 'length';            break;        case 'bool' :            $type = 'boolean';            break;        case 'float' :            $type = 'double';            break;        case 'int' :            $type = 'integer';            break;        case 'null' :            $type = 'NULL';            break;    }    if ($type === 'identical') {        return $var === $compare;    }    // whether we should check against given $compare    if ($type === 'similar') {        switch (gettype($compare)) {            case 'string':            case 'boolean':                $type = 'scalar';                break;            case 'integer':            case 'double':                $type = 'numeric';                break;            default:                $type = gettype($compare);        }    } elseif ($type === 'equal') {        $type = gettype($compare);    }    // do the check    if ($type === 'length' || $type === 'scalar') {        $is_scalar = is_scalar($var);        if ($is_scalar && $type === 'length') {            return (bool) strlen($var);        }        return $is_scalar;    }    if ($type === 'numeric') {        return is_numeric($var);    }    if (gettype($var) === $type) {        return true;    }    return false;}/** * 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/** * displays the given error message on phpMyAdmin error page in foreign language, * ends script execution and closes session * * loads language file if not loaded already * * @todo    use detected argument separator (PMA_Config) * @uses    $GLOBALS['session_name'] * @uses    $GLOBALS['text_dir'] * @uses    $GLOBALS['strError'] * @uses    $GLOBALS['available_languages'] * @uses    $GLOBALS['lang'] * @uses    PMA_removeCookie() * @uses    select_lang.lib.php * @uses    $_COOKIE * @uses    substr() * @uses    header() * @uses    http_build_query() * @uses    is_string() * @uses    sprintf() * @uses    vsprintf() * @uses    strtr() * @uses    defined() * @param   string $error_message the error message or named error message * @param   string|array $message_args arguments applied to $error_message * @return  exit */function PMA_fatalError($error_message, $message_args = null){    // it could happen PMA_fatalError() is called before language file is loaded    if (! isset($GLOBALS['available_languages'])) {        $GLOBALS['cfg'] = array(            'DefaultLang'           => 'en-utf-8',            'AllowAnywhereRecoding' => false);        // Loads the language file        require_once './libraries/select_lang.lib.php';        if (isset($strError)) {            $GLOBALS['strError'] = $strError;        } else {            $GLOBALS['strError'] = 'Error';        }        // $text_dir is set in lang/language-utf-8.inc.php        if (isset($text_dir)) {            $GLOBALS['text_dir'] = $text_dir;        }    }    // $error_message could be a language string identifier: strString    if (substr($error_message, 0, 3) === 'str') {        if (isset($$error_message)) {            $error_message = $$error_message;        } elseif (isset($GLOBALS[$error_message])) {            $error_message = $GLOBALS[$error_message];        }    }    if (is_string($message_args)) {        $error_message = sprintf($error_message, $message_args);    } elseif (is_array($message_args)) {        $error_message = vsprintf($error_message, $message_args);    }    $error_message = strtr($error_message, array('<br />' => '[br]'));    // Displays the error message    // (do not use &amp; for parameters sent by header)    $query_params = array(        'lang'  => $GLOBALS['available_languages'][$GLOBALS['lang']][2],        'dir'   => $GLOBALS['text_dir'],        'type'  => $GLOBALS['strError'],        'error' => $error_message,    );    header('Location: ' . (defined('PMA_SETUP') ? '../' : '') . 'error.php?'            . http_build_query($query_params, null, '&'));    // on fatal errors it cannot hurt to always delete the current session    if (isset($GLOBALS['session_name']) && isset($_COOKIE[$GLOBALS['session_name']])) {        PMA_removeCookie($GLOBALS['session_name']);    }    exit;}/** * returns count of tables in given db * * @uses    PMA_DBI_try_query() * @uses    PMA_backquote() * @uses    PMA_DBI_QUERY_STORE() * @uses    PMA_DBI_num_rows() * @uses    PMA_DBI_free_result() * @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) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?