这个php函数安全吗?


Is this php function safe

这不是我的完整代码。我只是把相关部分贴出来。在我当前的应用程序中,用户无法访问操纵变量,因此它是安全的,但将来我可能会在他们可以的地方写一些东西,所以当我考虑它时,我想问是否有办法破解这段代码。

define('SMART_TAG_FOLDER','includes/smartTag/');
function loadExternalFunction($functionName,$fields) {
    //get file function should reside in
    $fileName=$functionName;
    $fileEnd=strpos($functionName,'_');
    if ($fileEnd!==false) {
        $fileName=substr($fileName,0,$fileEnd);
    }
    $fileName.='.php';
    //try to load file function should be in
    if (file_exists(SMART_TAG_FOLDER . $fileName)) {
        require_once SMART_TAG_FOLDER . $fileName;
    }
    //if desired function exist then execute
    $functionName='smartTag_'.$functionName;
    if (function_exists($functionName)) {
        //run function
        $evalRun='$value=' . $functionName . '($fields);';
        eval($evalRun);
        return $value;
    }
    return false;
}

我的想法是使用file_exists和function_exists以及向用户定义的变量$function中添加信息,这样应该是安全的。

如果有人知道不使用eval的方法,我很想知道,因为我不喜欢使用eval

你不应该用eval

直接像这样调用函数

$functionName($fields);

如果它们更适合您的需要,您也可以使用其中一个函数:call_user_func()或call_user_func_array()