2011-01-19 19 views
11

Tôi làm cách nào để sử dụng HTMLPurifier để lọc xss mà còn cho phép iframe Vimeo và Youtube video?HTMLPurifier iframe Vimeo và Youtube video

require_once 'htmlpurifier/library/HTMLPurifier.auto.php'; 
$config = HTMLPurifier_Config::createDefault(); 
$config->set('HTML.Trusted', true); 

$config->set('Filter.YouTube', true); 
$config->set('HTML.DefinitionID', '1'); 
$config->set('HTML.SafeObject', 'true'); 
$config->set('Output.FlashCompat', 'true'); 

$config->set('HTML.FlashAllowFullScreen', 'true'); 

$purifier = new HTMLPurifier($config); 
$temp = $purifier->purify($temp); 

Trả lời

0

Loại bỏ% HTML.Trusted,% Filter.YouTube và% HTML.DefinitionID. Chúng có thể tương tác kém với SafeObject/FlashCompat.

+0

iframe vẫn bị chặn dù sao, không Metter. Bạn có biết một giải pháp tốt khác với hỗ trợ iframe không? – swamprunner7

+0

Ồ vâng, bạn sẽ phải thêm hỗ trợ Iframe riêng biệt. Đây là một cách có thể thực hiện: http://htmlpurifier.org/phorum/read.php?3,4646,4646#msg-4646 Và tất nhiên chúng tôi hy vọng (cuối cùng) thêm hỗ trợ cho nó đúng trong lõi . –

+0

Tôi đã thử giải pháp này, nhưng tôi có vấn đề, ở đây nhận xét cuối cùng của tôi http://stackoverflow.com/questions/4135755/how-do-i-allow-script-object-param-embed-and-iframe-tags-in -htmlpurifier – swamprunner7

8

Tôi chỉ đọc this blog entry và đã tạo và sử dụng thành công bộ lọc tùy chỉnh. Tôi thực hiện một số thay đổi đối với mã và thêm hỗ trợ Vimeo:

/** 
* Based on: http://sachachua.com/blog/2011/08/drupal-html-purifier-embedding-iframes-youtube/ 
* Iframe filter that does some primitive whitelisting in a somewhat recognizable and tweakable way 
*/ 
class HTMLPurifier_Filter_MyIframe extends HTMLPurifier_Filter 
{ 
    public $name = 'MyIframe'; 

    /** 
    * 
    * @param string $html 
    * @param HTMLPurifier_Config $config 
    * @param HTMLPurifier_Context $context 
    * @return string 
    */ 
    public function preFilter($html, HTMLPurifier_Config $config, HTMLPurifier_Context $context) 
    { 
     $html = preg_replace('#<iframe#i', '<img class="MyIframe"', $html); 
     $html = preg_replace('#</iframe>#i', '</img>', $html); 
     return $html; 
    } 

    /** 
    * 
    * @param string $html 
    * @param HTMLPurifier_Config $config 
    * @param HTMLPurifier_Context $context 
    * @return string 
    */ 
    public function postFilter($html, HTMLPurifier_Config $config, HTMLPurifier_Context $context) 
    { 
     $post_regex = '#<img class="MyIframe"([^>]+?)>#'; 
     return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html); 
    } 

    /** 
    * 
    * @param array $matches 
    * @return string 
    */ 
    protected function postFilterCallback($matches) 
    { 
     // Domain Whitelist 
     $youTubeMatch = preg_match('#src="https?://www.youtube(-nocookie)?.com/#i', $matches[1]); 
     $vimeoMatch = preg_match('#src="http://player.vimeo.com/#i', $matches[1]); 
     if ($youTubeMatch || $vimeoMatch) { 
      $extra = ' frameborder="0"'; 
      if ($youTubeMatch) { 
       $extra .= ' allowfullscreen'; 
      } elseif ($vimeoMatch) { 
       $extra .= ' webkitAllowFullScreen mozallowfullscreen allowFullScreen'; 
      } 
      return '<iframe ' . $matches[1] . $extra . '></iframe>'; 
     } else { 
      return ''; 
     } 
    } 
} 

Thêm bộ lọc để lọc cấu hình HTML của bạn

$config->set('Filter.Custom', array(new HTMLPurifier_Filter_MyIframe())); 
+1

Làm việc tuyệt vời, cảm ơn. – applechief

+1

tôi không làm việc cho tôi lúc đầu. nhưng sau đó tôi cho phép img (vì bộ lọc sử dụng thẻ img để thực hiện phép thuật) và làm việc! $ config-> set ('HTML.Allowed', 'p, a [href | rel | đích], img [class | src | height | width]'); –

28

HTMLPurifier phiên bản 4.4.0 có chỉ thị cấu hình mới cho phép YouTube và Vimeo iframe:

//allow iframes from trusted sources 
$cfg->set('HTML.SafeIframe', true); 
$cfg->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo 
+0

Liệu nó có duy trì thuộc tính 'allowfullscreen' cho YouTube và thuộc tính' webkitAllowFullScreen', 'mozallowfullscreen' và' allowFullScreen' cho Vimeo không? – Sonny

+1

Không, nhưng trong các thử nghiệm của tôi, nó không ảnh hưởng đến trình phát Flash hoặc HTML5. Toàn màn hình hoạt động không có vấn đề gì. Ngoài ra, nó không có trong thông số kỹ thuật, vì vậy nó có thể an toàn để bỏ qua. Xem http://htmlpurifier.org/docs/enduser-customize.html#addThuộc tính để thêm thuộc tính tùy chỉnh. – Malte

+0

Tôi vừa thử điều này, tôi nhận được một iframe trống với "src" –

2

nhiều này nên làm các trick

$text = "<iframe width='560' height='315' src='//www.youtube.com/embed/RGLI7QBUitE?autoplay=1' frameborder='0' allowfullscreen></iframe>"; 

require_once 'htmlpurifier/library/HTMLPurifier.auto.php'; 
$config = HTMLPurifier_Config::createDefault(); 
$config->set('HTML.Trusted', true); 
$config->set('Filter.YouTube', true); 

echo $purifier->purify($text); 
0

Cũng đừng quên đặt

URI.DisableExternalResources: false 

nếu bạn đã thiết lập nó để true trước.

1

Đối với những ai đang gặp khó khăn (làm thế nào để kích hoạt tính năng iframe và allowfullscreen)

$config = \HTMLPurifier_Config::createDefault(); 
    $config->set('HTML.SafeIframe', true); 
    $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo 
    // This line is important allow iframe in allowed elements or it will not work  
    $config->set('HTML.AllowedElements', array('iframe'));// <-- IMPORTANT 
    $config->set('HTML.AllowedAttributes','[email protected],[email protected]'); 

    $def = $config->getHTMLDefinition(true); 
    $def->addAttribute('iframe', 'allowfullscreen', 'Bool'); 

    $purifier = new \HTMLPurifier($config); 
    $purifiedHtml = $purifier->purify($html);