内爆警告:PHP-Nuke Titanium 函数 deepPurifier 中的数组到字符串转换

问题描述 投票:0回答:2

这在除 8 以外的所有 PHP 版本中都没有任何警告

我认为他们已经用 implode 改变了一些东西,我已经尝试了所有的例子都无济于事。

也许我可以用其他方式完成这件事。我需要一些 PHP 8 的眼睛,因为我是 PHP 8 及更高版本的新手。

我的功能中的警告在以下行中:

警告:数组到字符串的转换

  $test = implode(', ', $data);

当你使用这么多语法相似的不同语言时,很难理解某些事情。我担心这只是我的一个小脑屁。

虽然我有警告,但这段代码似乎一直在工作,我想知道这是否只是 PHP 8 和 8.1 等中的一个错误

function deepPurifier($data) 
{
    global $html_auth, $admin;

    static $config, $purifier;

    # Error check
    if(empty($data) || !isset($data))       
    return $data;

    if(!is_array($data))   
    return stripslashes((string) $data);
    
    // THIS IS WHERE MY WARNING IS
    // warning: array to string conversion 
    $test = implode(', ', $data);

    if(!preg_match('[<|>]', $test)) 
    {
        return $data;
    }
    
    if(!isset($config) || empty($config)) 
    {
        set_include_path(NUKE_BASE_DIR. get_include_path() );
        require_once(NUKE_VENDOR_DIR.'ezyang/htmlpurifier/library/HTMLPurifier/Bootstrap.php'); 
        require_once(NUKE_VENDOR_DIR.'ezyang/htmlpurifier/library/HTMLPurifier.autoload.php');      
        $config = HTMLPurifier_Config::createDefault();
        $config->set('Core.Encoding', 'UTF-8');
        $config->set('HTML.Doctype', 'HTML 4.01 Transitional');
        if(!is_god($admin) || (is_god($admin) && !$html_auth)) 
        {
            $config->set('HTML.Trusted', true);
            $config->set('HTML.SafeObject', true);
            $config->set('HTML.SafeEmbed', true);
            $config->set('HTML.AllowedAttributes','img@height,img@width,img@src,iframe@src,iframe@allowfullscreen');
            $config->set('HTML.AllowedAttributes', 'src, height, width, alt');
            $config->set('HTML.AllowedElements', ['img', 'iframe', 'div', 'script', 'object', 'p', 'span', 'pre', 'b', 'i', 'u', 'strong', 'em', 'sup', 'a', 'img', 'table', 'tr', 'td', 'tbody', 'thead', 'param']);
            $config->set('Output.FlashCompat', true);
            $config->set('Attr.EnableID', true);
            $config->set('Filter.Custom', [new HTMLPurifier_Filter_YouTube()]);
         }
        $def = $config->getHTMLDefinition(true);
        $def->addAttribute('iframe', 'allowfullscreen', 'Bool');
        $purifier = new HTMLPurifier($config);
    }
 # Loop through the data
 foreach ($data as $k => $v) {
         # If its an array
        if (is_array($data[$k])) {
            # Go though this function again
            $data[$k] = array_map('deepStrip', $data[$k]);
        } elseif (is_numeric($v) || empty($v)) {
            $data[$k] = $v;
         } else {
            if (isset($_GET['op']) && $_GET['op'] == 'Configure' && isset($_GET['sub']) && $_GET['sub'] == '11') {
                $data[$k] = $v;
                continue;
            } elseif ($k == 'xsitename' || $k == 'xslogan') {
                $data[$k] = $v;
                continue;
            } elseif (isset($_GET['name'])) {
                # If forum post let it pass to the forum html security
                if ($_GET['name'] == 'Forums' && (isset($_GET['file']) && ($_GET['file'] == 'posting')) && ($k == 'message' || $k == 'subject')) {
                     $data[$k] = $v;
                     continue;
                }
                # If PM let it pass to the forum html security
                if ($_GET['name'] == 'Private_Messages' && ($k == 'message' || $k == 'subject')) {
                     $data[$k] = $v;
                     continue;
                }
                # If SIG let it pass to the forum html security
                if ($_GET['name'] == 'Profile' && (isset($_GET['mode']) && ($_GET['mode'] == 'signature')) && $k == 'signature') {
                    $data[$k] = $v;
                    continue;
                }
            }
            # If its a strip lets purify it
            if (!is_god($admin) || (is_god($admin) && !$html_auth)) {
                    $data[$k] = $purifier->purify($v);
                }
            $data[$k] = str_replace('\n', "\n", (string) $data[$k]);
            # Get the registered globals also
            global ${$k};
            if (isset(${$k}) && !empty(${$k})) {
                ${$k} = $data[$k];
             }
         }
     }
    return $data;
}

var_dump($test);

string(20) "Forums,viewtopic,284" string(20) "Forums,viewtopic,284"

php warnings php-8
2个回答
1
投票

当被内爆的数组嵌套时,警告出现在 PHP 8 中。从文档

$array
参数应该是strings的数组,而不是数组的数组。

例如,以下内容在 PHP 7.4 和 PHP 8.1 中都不会产生警告:

$data = ["a", "b"];
print(implode(" ", $data));

而以下给出了警告

Array to string conversion
(注意第一个数组中的数组):

$data = ["a", "b" => ["c"], "d" => ["e"]];
print(implode(" ", $data));

您可以使用 docker 和不同的 PHP 版本验证行为:

docker run --rm php:7.4 -r 'print(implode(" ", ["a", "b" => ["c"], "d" => ["e"]]));'
docker run --rm php:8.1 -r 'print(implode(" ", ["a", "b" => ["c"], "d" => ["e"]]));'

两者都产生输出:

a Array Array

但是 PHP 8 现在会发出警告:

Warning: Array to string conversion in Command line code on line 1

0
投票

要消除警告,您必须更改以下内容:

来自:

$test = implode(',', $data);

致:

$test  = json_encode($data);

还更改不正确的语法:

来自:

    if(!preg_match('[<|>]', $test)) 
    {
        return $data;
    }

致:

    if(!preg_match('/[<|>]/', $test)) 
    {
        return $data;
    }
© www.soinside.com 2019 - 2024. All rights reserved.