فهرست منبع

HTMLPurifier - filter classes don't exist fix

Alex Trofimov 13 سال پیش
والد
کامیت
17fa57a496
2فایلهای تغییر یافته به همراه39 افزوده شده و 1 حذف شده
  1. 1 1
      inc/utils.inc.php
  2. 38 0
      plugins/htmlpurifier/standalone/HTMLPurifier/Filter/YoutubeIframe.php

+ 1 - 1
inc/utils.inc.php

@@ -607,7 +607,7 @@ function clear_xss($val) {
         $oConfig->set('Output.FlashCompat', 'true');
         $oConfig->set('Output.FlashCompat', 'true');
         $oConfig->set('HTML.FlashAllowFullScreen', 'true');
         $oConfig->set('HTML.FlashAllowFullScreen', 'true');
 
 
-        $oConfig->set('Filter.Custom', array (new HTMLPurifier_Filter_LocalMovie(), new HTMLPurifier_Filter_YouTube(), new HTMLPurifier_Filter_YoutubeIframe()));
+        $oConfig->set('Filter.Custom', array (new HTMLPurifier_Filter_YouTube(), new HTMLPurifier_Filter_YoutubeIframe()));
 
 
         $oDef = $oConfig->getHTMLDefinition(true);
         $oDef = $oConfig->getHTMLDefinition(true);
         $oDef->addAttribute('a', 'target', 'Enum#_blank,_self,_target,_top');
         $oDef->addAttribute('a', 'target', 'Enum#_blank,_self,_target,_top');

+ 38 - 0
plugins/htmlpurifier/standalone/HTMLPurifier/Filter/YoutubeIframe.php

@@ -0,0 +1,38 @@
+<?php
+
+class HTMLPurifier_Filter_YoutubeIframe extends HTMLPurifier_Filter
+{
+    public $name = 'YouTubeIframe';
+
+    public function preFilter($html, $config, $context)
+    {
+        if (strstr($html, '<iframe')) {
+            $html = str_ireplace("</iframe>", "", $html);
+            if (preg_match_all("/<iframe(.*?)>/si", $html, $result)) {
+                foreach ($result[1] as $key => $item) {
+                    preg_match('/width="([0-9]+)"/', $item, $width);
+                    $width = $width[1];
+                    preg_match('/height="([0-9]+)"/', $item, $height);
+                    $height = $height[1];
+                    preg_match('/https?:\/\/www\.youtube\.com\/embed\/(https?:\/\/www.youtube.com\/v\/)?([a-zA-Z0-9_-]+)/', $item, $id);
+                    $id = $id[2];
+                    $html = str_replace($result[0][$key], '<img class="YouTubeIframe" width="' . $width . '" height="' . $height . '" src="http://www.youtube.com/embed/' . $id . '?wmode=opaque">', $html);
+                }
+            }
+        }
+        return $html;
+    }
+
+    public function postFilter($html, $config, $context)
+    {
+        $post_regex = '#<img class="YouTubeIframe" ([^>]+)>#';
+        $html = preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html);
+        return $html;
+    }
+
+    protected function postFilterCallback($matches)
+    {
+        return '<iframe frameborder="0" allowfullscreen ' . $matches[1] . '></iframe>';
+    }
+}
+