Skip to content

Instantly share code, notes, and snippets.

@cybersholt
Last active May 13, 2017 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybersholt/f8045e64d5fe857c40292fa9a4e4775e to your computer and use it in GitHub Desktop.
Save cybersholt/f8045e64d5fe857c40292fa9a4e4775e to your computer and use it in GitHub Desktop.
Uddeim BBcode Bug
<?php
function uddeIMbbcode_replace( $string, $config ) {
if ( $config->allowbb > 0 ) {
// replace font formatting [b] [i] [u] [color= [size=
// bold
$string = preg_replace( "/(\[b\])(.*?)(\[\/b\])/si", "<span style=\"font-weight: bold\">\\2</span>", $string );
// underline
$string = preg_replace( "/(\[u\])(.*?)(\[\/u\])/si", "<span style=\"text-decoration: underline\">\\2</span>", $string );
// italic
$string = preg_replace( "/(\[i\])(.*?)(\[\/i\])/si", "<span style=\"font-style: italic\">\\2</span>", $string );
// ol
$string = preg_replace( "/(\[ol\])(.*?)(\[\/ol\])/si", "<ol>\\2</ol>", $string );
// ul
$string = preg_replace( "/(\[ul\])(.*?)(\[\/ul\])/si", "<ul>\\2</ul>", $string );
// li
$string = preg_replace( "/(\[li\])(.*?)(\[\/li\])/si", "<li>\\2</li>", $string );
// max size is 7
$string = preg_replace( "/\[size=([1-7])\](.+?)\[\/size\]/si", "<font size=\\1\">\\2</font>", $string );
// color
$string = preg_replace( "%\[color=#(.{1,6}?)\](.*?)\[/color\]%si", "<span style=\"color: #\\1\">\\2</span>", $string );
// more font formatters opened than closed
// do {
// $string.="</span>";
// close them
// } while (substr_count($string,"<span") > substr_count($string,"</span>"));
while ( substr_count( $string, "<span" ) > substr_count( $string, "</span>" ) ) {
$string .= "</span>";
}
// more font formatters closed than opened (less likely case)
// do {
// $string="<span>".$string;
// add a dummy container to balance that out
// } while (substr_count($string,"<span") < substr_count($string,"</span>"));
while ( substr_count( $string, "<span" ) < substr_count( $string, "</span>" ) ) {
$string = "<span>" . $string;
}
}
if ( $config->allowbb > 1 ) {
// http, https, ftp, mailto
$passes = Array();
$passes[] = "url";
$passes[] = "topurl";
foreach ( $passes as $pass ) {
$string = str_replace( "[" . $pass . "=index.php", "#*#LINK" . $pass . "INDEX=#*#", $string );
$string = str_replace( "[" . $pass . "=http://", "#*#LINK" . $pass . "HTTP=#*#", $string );
$string = str_replace( "[" . $pass . "=ftp://", "#*#LINK" . $pass . "FTP=#*#", $string );
$string = str_replace( "[" . $pass . "=file://", "#*#LINK" . $pass . "FILE=#*#", $string );
$string = str_replace( "[" . $pass . "=https://", "#*#LINK" . $pass . "HTTPS=#*#", $string );
$string = str_replace( "[" . $pass . "=mailto:", "#*#LINK" . $pass . "MAILTO=#*#", $string );
$string = str_replace( "[" . $pass . "]index.php", "#*#LINK" . $pass . "INDEX]#*#", $string );
$string = str_replace( "[" . $pass . "]http://", "#*#LINK" . $pass . "HTTP]#*#", $string );
$string = str_replace( "[" . $pass . "]ftp://", "#*#LINK" . $pass . "FTP]#*#", $string );
$string = str_replace( "[" . $pass . "]file://", "#*#LINK" . $pass . "FILE]#*#", $string );
$string = str_replace( "[" . $pass . "]https://", "#*#LINK" . $pass . "HTTPS]#*#", $string );
$string = str_replace( "[" . $pass . "]mailto:", "#*#LINK" . $pass . "MAILTO]#*#", $string );
$string = str_replace( "[" . $pass . "]", "[" . $pass . "]http://", $string );
$string = str_replace( "[" . $pass . "=", "[" . $pass . "=http://", $string );
$string = str_replace( "#*#LINK" . $pass . "HTTP=#*#", "[" . $pass . "=http://", $string );
$string = str_replace( "#*#LINK" . $pass . "FTP=#*#", "[" . $pass . "=ftp://", $string );
$string = str_replace( "#*#LINK" . $pass . "FILE=#*#", "[" . $pass . "=file://", $string );
$string = str_replace( "#*#LINK" . $pass . "HTTPS=#*#", "[" . $pass . "=https://", $string );
$string = str_replace( "#*#LINK" . $pass . "MAILTO=#*#", "[" . $pass . "=mailto:", $string );
$string = str_replace( "#*#LINK" . $pass . "INDEX=#*#", "[" . $pass . "=index.php", $string );
$string = str_replace( "#*#LINK" . $pass . "HTTP]#*#", "[" . $pass . "]http://", $string );
$string = str_replace( "#*#LINK" . $pass . "FTP]#*#", "[" . $pass . "]ftp://", $string );
$string = str_replace( "#*#LINK" . $pass . "FILE]#*#", "[" . $pass . "]file://", $string );
$string = str_replace( "#*#LINK" . $pass . "HTTPS]#*#", "[" . $pass . "]https://", $string );
$string = str_replace( "#*#LINK" . $pass . "MAILTO]#*#", "[" . $pass . "]mailto:", $string );
$string = str_replace( "#*#LINK" . $pass . "INDEX]#*#", "[" . $pass . "]index.php", $string );
}
$string = preg_replace( "/\[img size=([0-9][0-9][0-9])\](http\:\/\/.*?)\[\/img\]/si", "[#*#img size=$1]$2[/#*#img]", $string );
$string = preg_replace( "/\[img size=([0-9][0-9])\](http\:\/\/.*?)\[\/img\]/si", "[#*#img size=$1]$2[/#*#img]", $string );
$string = preg_replace( "/\[img\](http\:\/\/.*?)\[\/img\]/si", "[#*#img]$1[/#*#img]", $string );
$string = preg_replace( "/\[img size=([0-9][0-9][0-9])\](.*?)\[\/img\]/si", "[img size=$1]http://$2[/img]", $string );
$string = preg_replace( "/\[img size=([0-9][0-9])\](.*?)\[\/img\]/si", "[img size=$1]http://$2[/img]", $string );
$string = preg_replace( "/\[img\](.*?)\[\/img\]/si", "[img]$1[/img]", $string );
$string = str_replace( "[#*#img", "[img", $string );
$string = str_replace( "[/#*#img", "[/img", $string );
// ul li replacements
// $string = preg_replace("/(\[ul\])(.*?)(\[\/ul\])/si","<ul>\\2</ul>",$string);
// $string = preg_replace("/(\[ol\])(.*?)(\[\/ol\])/si","<ol type=1>\\2</ol>",$string);
// $string = preg_replace("/(\[li\])(.*?)(\[\/li\])/si","<li>\\2</li>",$string);
// make regular HTML URL links targets _blank, bbCode URL translation
// this is very bad: since when we have two links (a link without comprofiler and a link with comprofiler) this matches both
// but it is even worse when we do not remove javascript links...
$string = preg_replace( '/\[(top)?url\](.*?)javascript(.*?)\[\/\\1url\]/si', '<span style=\'text-decoration: line-through\'>javascript link</span>', $string );
$string = preg_replace( '/\[(top)?url=(.*?)javascript(.*?)\](.*?)\[\/\\1url\]/si', '<span style=\'text-decoration: line-through\'>javascript link</span>', $string );
// if comprofiler in link, make link to top instead of blank
// this is very bad: since when we have two links (a link without comprofiler and a link with comprofiler) this matches both
// $string = preg_replace("/\[url\](.*?)comprofiler(.*?)\[\/url\]/si","<a href=\\1comprofiler\\2 target=\"_top\">\\1comprofiler\\2</a>",$string);
// $string = preg_replace("/\[url=(.*?)comprofiler(.*?)\](.*?)\[\/url\]/si","<a href=\"\\1comprofiler\\2\" target=\"_top\">\\3</a>",$string);
// now the rest of the links to blank
$string = preg_replace( "/\[url\](.*?)\[\/url\]/si", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $string );
$string = preg_replace( "/\[url=(.*?)\](.*?)\[\/url\]/si", "<a href=\"\\1\" target=\"_blank\">\\2</a>", $string );
$string = preg_replace( "/\[topurl\](.*?)\[\/topurl\]/si", "<a href=\"\\1\">\\1</a>", $string );
$string = preg_replace( "/\[topurl=(.*?)\](.*?)\[\/topurl\]/si", "<a href=\"\\1\">\\2</a>", $string );
// img replacement
$string = preg_replace( "/\[img size=([0-9][0-9][0-9])\](.*?)\[\/img\]/si", "<img src=\"$2\" border=\"0\" width=\"$1\" />", $string );
$string = preg_replace( "/\[img size=([0-9][0-9])\](.*?)\[\/img\]/si", "<img src=\"$2\" border=\"0\" width=\"$1\" />", $string );
$string = preg_replace( "/\[img\](.*?)\[\/img\]/si", "<img src=\"$1\" border=\"0\" />", $string );
$string = preg_replace( "/<img(.*?)javascript(.*?)>/si", '<span style=\'text-decoration: line-through\'>javascript link</span>', $string );
}
return $string;
}
$text = '[img]https://media1.giphy.com/media/1026x5Ybk81eRW/giphy.gif[/img]
test HTTPS
test HTTP
[img]http://media1.giphy.com/media/1026x5Ybk81eRW/giphy.gif[/img]
';
$config = new stdClass();
$config->allowbb = 2;
echo "String in: <br>".nl2br($text).'<br><hr>BBCode Out: <br>';
print_r(nl2br(uddeIMbbcode_replace($text, $config)));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment