Search code examples
phpiframeyoutubelaravel-4htmlpurifier

Allowing YouTube embed with HTMLPurifier on Laravel 4 and mewebstudio/Purifier


I'm using the mewebstudio/Purifier package service provider HTMLPurifier on Laravel 4.2.

For some reason I can't make the youtube iframe feature work. I even tried the author example for enabling it with no luck.

This is my HTMLPurifier bundle config:

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',
        ),
    ),
);

And this is my test in routes:

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

Everything else works, but this.

Thanks fellow warriors :)


Solution

  • HTMLPurifier already has a filter ready-made for Youtube videos, make sure you use it.

    To use it make sure you have this line on your config:

    'Filter.YouTube' => true
    

    Your final config file would look like this:

    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
            ),
        ),
    );