允许YouTube在Laravel 4和mewebstudio / Purifier上嵌入HTMLPurifier

时间:2014-07-10 16:15:51

标签: php iframe youtube laravel-4 htmlpurifier

我在Laravel 4.2上使用mewebstudio/Purifier包服务提供商HTMLPurifier。

出于某种原因,我无法使youtube iframe功能正常工作。我甚至尝试过这个例子来启用它而没有运气。

这是我的HTMLPurifier包配置:

return array(
    'encoding' => 'UTF-8',
    'finalize' => true,
    'preload'  => false,
    'settings' => array(
        'default' => array(
            'HTML.Doctype'             => 'XHTML 1.0 Strict',
            'HTML.Allowed'             => 'div[style],b,strong,i,em,a[href|title|style],ul,ol,li,p[style],br,span[style],
                                            img[width|height|alt|src],h1[style],h2[style],h3[style],h4[style],h5[style],table[class|style|summary],tr,td[abbr],tbody,thead',
            'CSS.AllowedProperties'    => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align',
            'HTML.SafeObject'          => true,
            'Output.FlashCompat'       => true,
            'HTML.SafeIframe'          => true,
            'URI.SafeIframeRegexp'     => '%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%',
            'AutoFormat.AutoParagraph' => true,
            'AutoFormat.RemoveEmpty'   => true,
            'HTML.Nofollow'            => true,
            'URI.Host'                 => 'domain.com',
        ),
    ),
);

这是我在路线上的考验:

Route::get('/clean', function() {
    $pured = Purifier::clean('<iframe src="//www.youtube.com/embed/gLUdqi8J0mQ" width="640" height="360" frameborder="0"></iframe>', 'default');
    return $pured;
});

其他一切都有效,但是这个。

感谢战士们的同事们:)

1 个答案:

答案 0 :(得分:-1)

HTMLPurifier已经为Youtube视频准备了一个过滤器,请确保您使用它。

要使用它,请确保您的配置中包含以下行:

'Filter.YouTube' => true

您的最终配置文件如下所示:

return array(
    'encoding' => 'UTF-8',
    'finalize' => true,
    'preload'  => false,
    'settings' => array(
        'default' => array(
            'HTML.Doctype'             => 'XHTML 1.0 Strict',
            'HTML.Allowed'             => 'div[style],b,strong,i,em,a[href|title|style],ul,ol,li,p[style],br,span[style],
                                            img[width|height|alt|src],h1[style],h2[style],h3[style],h4[style],h5[style],table[class|style|summary],tr,td[abbr],tbody,thead',
            'CSS.AllowedProperties'    => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align',
            'HTML.SafeObject'          => true,
            'Output.FlashCompat'       => true,
            'HTML.SafeIframe'          => true,
            'URI.SafeIframeRegexp'     => '%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%',
            'AutoFormat.AutoParagraph' => true,
            'AutoFormat.RemoveEmpty'   => true,
            'HTML.Nofollow'            => true,
            'URI.Host'                 => 'domain.com',
            'Filter.YouTube'           => true
        ),
    ),
);