BxDolHTMLPurifierFilterLocalIframe.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class BxDolHTMLPurifierFilterLocalIframe extends HTMLPurifier_Filter
  3. {
  4. public $name = 'LocalIframe';
  5. public function preFilter($sHtml, $config, $context)
  6. {
  7. if (strstr($sHtml, '<iframe')) {
  8. $sHtml = str_ireplace("</iframe>", "", $sHtml);
  9. if (preg_match_all("/<iframe(.*?)>/si", $sHtml, $aResult)) {
  10. foreach ($aResult[1] as $key => $sItem) {
  11. preg_match('/width="([0-9]+)"/', $sItem, $width);
  12. $iWidth = $width[1];
  13. preg_match('/height="([0-9]+)"/', $sItem, $height);
  14. $iHeight = $height[1];
  15. $sDolUrl = preg_quote(BX_DOL_URL_ROOT);
  16. $sIframeUrl = '';
  17. if (preg_match("#({$sDolUrl}[a-zA-Z0-9_=\-\?\&\/]+)#", $sItem, $aMatches))
  18. $sIframeUrl = $aMatches[1];
  19. if (preg_match("#src=\"(((?!//)(?![a-z]+://)(?![a-z]+://))[a-zA-Z0-9_=\-\?\&\/]+)#", $sItem, $aMatches))
  20. $sIframeUrl = $aMatches[1];
  21. if ($sIframeUrl)
  22. $sHtml = str_replace($aResult[0][$key], '<img class="LocalIframe" width="' . $iWidth . '" height="' . $iHeight . '" src="' . $sIframeUrl . '">', $sHtml);
  23. }
  24. }
  25. }
  26. return $sHtml;
  27. }
  28. public function postFilter($sHtml, $config, $context)
  29. {
  30. $sPostRegex = '#<img class="LocalIframe" ([^>]+)>#';
  31. $sHtml = preg_replace_callback($sPostRegex, array($this, 'postFilterCallback'), $sHtml);
  32. return $sHtml;
  33. }
  34. protected function postFilterCallback($aMatches)
  35. {
  36. return '<iframe frameborder="0" allowfullscreen ' . $aMatches[1] . '></iframe>';
  37. }
  38. }