-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Trofimov
committed
Sep 5, 2021
1 parent
cb24c76
commit 559a1ef
Showing
26 changed files
with
1,674 additions
and
57 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
class BxDolHTMLPurifierFilterAddBxLinksClass extends HTMLPurifier_Filter | ||
{ | ||
public $name = 'AddBxLinksClass'; | ||
protected $class = BX_DOL_LINK_CLASS; | ||
|
||
public function preFilter($sHtml, $config, $context) | ||
{ | ||
if (false === strstr($sHtml, '<a ')) | ||
return $sHtml; | ||
|
||
$sId = 'bx-links-' . md5(microtime()); | ||
$dom = new DOMDocument(); | ||
@$dom->loadHTML('<?xml encoding="UTF-8"><div id="' . $sId . '">' . $sHtml . '</div>'); | ||
$xpath = new DOMXpath($dom); | ||
|
||
$oLinks = $xpath->evaluate('//a'); | ||
for ($i = 0; $i < $oLinks->length; $i++) { | ||
$oLink = $oLinks->item($i); | ||
|
||
$sClasses = $oLink->getAttribute('class'); | ||
if (!$sClasses || false === strpos($sClasses, $this->class)) | ||
$sClasses = ($sClasses ? $sClasses . ' ' : '') . $this->class; | ||
|
||
$oLink->removeAttribute('class'); | ||
$oLink->setAttribute("class", $sClasses); | ||
} | ||
|
||
if (false === ($s = $dom->saveXML($dom->getElementById($sId)))) // in case of error return original string | ||
return $sHtml; | ||
|
||
return mb_substr($s, 52, -6); // strip added tags | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
class BxDolHTMLPurifierFilterLocalIframe extends HTMLPurifier_Filter | ||
{ | ||
public $name = 'LocalIframe'; | ||
|
||
public function preFilter($sHtml, $config, $context) | ||
{ | ||
if (strstr($sHtml, '<iframe')) { | ||
$sHtml = str_ireplace("</iframe>", "", $sHtml); | ||
if (preg_match_all("/<iframe(.*?)>/si", $sHtml, $aResult)) { | ||
foreach ($aResult[1] as $key => $sItem) { | ||
preg_match('/width="([0-9]+)"/', $sItem, $width); | ||
$iWidth = $width[1]; | ||
preg_match('/height="([0-9]+)"/', $sItem, $height); | ||
$iHeight = $height[1]; | ||
$sDolUrl = preg_quote(BX_DOL_URL_ROOT); | ||
$sIframeUrl = ''; | ||
if (preg_match("#({$sDolUrl}[a-zA-Z0-9_=\-\?\&\/]+)#", $sItem, $aMatches)) | ||
$sIframeUrl = $aMatches[1]; | ||
if (preg_match("#src=\"(((?!//)(?![a-z]+://)(?![a-z]+://))[a-zA-Z0-9_=\-\?\&\/]+)#", $sItem, $aMatches)) | ||
$sIframeUrl = $aMatches[1]; | ||
if ($sIframeUrl) | ||
$sHtml = str_replace($aResult[0][$key], '<img class="LocalIframe" width="' . $iWidth . '" height="' . $iHeight . '" src="' . $sIframeUrl . '">', $sHtml); | ||
} | ||
} | ||
} | ||
return $sHtml; | ||
} | ||
|
||
public function postFilter($sHtml, $config, $context) | ||
{ | ||
$sPostRegex = '#<img class="LocalIframe" ([^>]+)>#'; | ||
$sHtml = preg_replace_callback($sPostRegex, array($this, 'postFilterCallback'), $sHtml); | ||
return $sHtml; | ||
} | ||
|
||
protected function postFilterCallback($aMatches) | ||
{ | ||
return '<iframe frameborder="0" allowfullscreen ' . $aMatches[1] . '></iframe>'; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
class BxDolHTMLPurifierFilterYouTube extends HTMLPurifier_Filter | ||
{ | ||
|
||
/** | ||
* @type string | ||
*/ | ||
public $name = 'YouTube'; | ||
|
||
/** | ||
* @param string $html | ||
* @param HTMLPurifier_Config $config | ||
* @param HTMLPurifier_Context $context | ||
* @return string | ||
*/ | ||
public function preFilter($html, $config, $context) | ||
{ | ||
$pre_regex = '#<object[^>]+>.+?' . | ||
'(?:http:)?//www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s'; | ||
$pre_replace = '<span class="youtube-embed">\1</span>'; | ||
return preg_replace($pre_regex, $pre_replace, $html); | ||
} | ||
|
||
/** | ||
* @param string $html | ||
* @param HTMLPurifier_Config $config | ||
* @param HTMLPurifier_Context $context | ||
* @return string | ||
*/ | ||
public function postFilter($html, $config, $context) | ||
{ | ||
$post_regex = '#<span class="youtube-embed">((?:v|cp)/[A-Za-z0-9\-_=]+)</span>#'; | ||
return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html); | ||
} | ||
|
||
/** | ||
* @param $url | ||
* @return string | ||
*/ | ||
protected function armorUrl($url) | ||
{ | ||
return str_replace('--', '--', $url); | ||
} | ||
|
||
/** | ||
* @param array $matches | ||
* @return string | ||
*/ | ||
protected function postFilterCallback($matches) | ||
{ | ||
$url = $this->armorUrl($matches[1]); | ||
return '<object width="425" height="350" type="application/x-shockwave-flash" ' . | ||
'data="//www.youtube.com/' . $url . '">' . | ||
'<param name="movie" value="//www.youtube.com/' . $url . '"></param>' . | ||
'<!--[if IE]>' . | ||
'<embed src="//www.youtube.com/' . $url . '"' . | ||
'type="application/x-shockwave-flash"' . | ||
'wmode="transparent" width="425" height="350" />' . | ||
'<![endif]-->' . | ||
'</object>'; | ||
} | ||
} | ||
|
||
// vim: et sw=4 sts=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
class BxDolHTMLPurifierFilterYoutubeIframe extends HTMLPurifier_Filter | ||
{ | ||
public $name = 'YouTubeIframe'; | ||
|
||
public function preFilter($html, $config, $context) | ||
{ | ||
if (strstr($html, '<iframe') && (strstr($html, 'youtube.com') || strstr($html, 'youtu.be') || strstr($html, 'youtube-nocookie.com'))) { | ||
$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]; | ||
if (preg_match('/((\/\/www\.youtube\.com\/embed\/)|(\/\/www\.youtube-nocookie\.com\/embed\/)|(\/\/www.youtube.com\/v\/))([a-zA-Z0-9_-]+)/', $item, $id)) { | ||
$id = $id[5]; | ||
$sProto = 0 == strncmp('https', BX_DOL_URL_ROOT, 5) ? 'https' : 'http'; | ||
$html = str_replace($result[0][$key], '<img class="YouTubeIframe" width="' . $width . '" height="' . $height . '" src="' . $sProto . '://www.youtube-nocookie.com/embed/' . $id . '?rel=0">', $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>'; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.