Don't like ads? PRO users don't see any ads ;-)
Guest

bbcode

By: a guest on Apr 30th, 2012  |  syntax: PHP  |  size: 7.80 KB  |  hits: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. +------------------------------------------------
  4. |   TBDev.net BitTorrent Tracker PHP
  5. |   =============================================
  6. |   by CoLdFuSiOn
  7. |   (c) 2003 - 2009 TBDev.Net
  8. |   http://www.tbdev.net
  9. |   =============================================
  10. |   svn: http://sourceforge.net/projects/tbdevnet/
  11. |   Licence Info: GPL
  12. +------------------------------------------------
  13. |   $Date$
  14. |   $Revision$
  15. |   $Author$
  16. |   $URL$
  17. +------------------------------------------------
  18. */
  19. require_once "emoticons.php";
  20.  
  21. //Finds last occurrence of needle in haystack
  22. //in PHP5 use strripos() instead of this
  23. function _strlastpos ($haystack, $needle, $offset = 0)
  24. {
  25.         $addLen = strlen ($needle);
  26.         $endPos = $offset - $addLen;
  27.         while (true)
  28.         {
  29.                 if (($newPos = strpos ($haystack, $needle, $endPos + $addLen)) === false) break;
  30.                 $endPos = $newPos;
  31.         }
  32.         return ($endPos >= 0) ? $endPos : false;
  33. }
  34.  
  35.  
  36. function format_urls($s)
  37. {
  38.         return preg_replace(
  39.         "/(\A|[^=\]'\"a-zA-Z0-9])((http|ftp|https|ftps|irc):\/\/[^()<>\s]+)/i",
  40.             "\\1<a href=\"\\2\">\\2</a>", $s);
  41. }
  42.  
  43. /*
  44.  
  45. // Removed this fn, I've decided we should drop the redir script...
  46. // it's pretty useless since ppl can still link to pics...
  47. // -Rb
  48.  
  49. function format_local_urls($s)
  50. {
  51.         return preg_replace(
  52.     "/(<a href=redir\.php\?url=)((http|ftp|https|ftps|irc):\/\/(www\.)?torrentbits\.(net|org|com)(:8[0-3])?([^<>\s]*))>([^<]+)<\/a>/i",
  53.     "<a href=\\2>\\8</a>", $s);
  54. }
  55. */
  56.  
  57. function format_quotes($s)
  58. {
  59.   $old_s = '';
  60.   while ($old_s != $s)
  61.   {
  62.         $old_s = $s;
  63.  
  64.           //find first occurrence of [/quote]
  65.           $close = strpos($s, "[/quote]");
  66.           if ($close === false)
  67.                 return $s;
  68.  
  69.           //find last [quote] before first [/quote]
  70.           //note that there is no check for correct syntax
  71.           $open = _strlastpos(substr($s,0,$close), "[quote");
  72.           if ($open === false)
  73.             return $s;
  74.  
  75.           $quote = substr($s,$open,$close - $open + 8);
  76.  
  77.           //[quote]Text[/quote]
  78.           $quote = preg_replace(
  79.             "/\[quote\]\s*((\s|.)+?)\s*\[\/quote\]\s*/i",
  80.             "<p class='sub'><b>Quote:</b></p><table class='main' border='1' cellspacing='0' cellpadding='10'><tr><td style='border: 2px #427cff dotted'>\\1</td></tr></table><br />", $quote);
  81.  
  82.           //[quote=Author]Text[/quote]
  83.           $quote = preg_replace(
  84.             "/\[quote=(.+?)\]\s*((\s|.)+?)\s*\[\/quote\]\s*/i",
  85.             "<p class='sub'><b>\\1 wrote:</b></p><table class='main' border='1' cellspacing='0' cellpadding='10'><tr><td style='border: 2px #427cff dotted'>\\2</td></tr></table><br />", $quote);
  86.  
  87.           $s = substr($s,0,$open) . $quote . substr($s,$close + 8);
  88.   }
  89.  
  90.         return $s;
  91. }
  92.  
  93. function format_comment($text, $strip_html = true)
  94. {
  95.         global $smilies, $TBDEV;
  96.  
  97.         $s = $text;
  98.   unset($text);
  99.   // This fixes the extraneous ;) smilies problem. When there was an html escaped
  100.   // char before a closing bracket - like >), "), ... - this would be encoded
  101.   // to &xxx;), hence all the extra smilies. I created a new :wink: label, removed
  102.   // the ;) one, and replace all genuine ;) by :wink: before escaping the body.
  103.   // (What took us so long? :blush:)- wyz
  104.  
  105.         $s = str_replace(";)", ":wink:", $s);
  106.  
  107.         if ($strip_html)
  108.                 $s = htmlentities($s, ENT_QUOTES, 'UTF-8');
  109.  
  110.   if( preg_match( "#function\s*\((.*?)\|\|#is", $s ) )
  111.   {
  112.     $s = str_replace( ":"     , "&#58;", $s );
  113.                 $s = str_replace( "["     , "&#91;", $s );
  114.                 $s = str_replace( "]"     , "&#93;", $s );
  115.                 $s = str_replace( ")"     , "&#41;", $s );
  116.                 $s = str_replace( "("     , "&#40;", $s );
  117.                 $s = str_replace( "{"    , "&#123;", $s );
  118.                 $s = str_replace( "}"    , "&#125;", $s );
  119.                 $s = str_replace( "$"    , "&#36;", $s );  
  120.   }
  121.  
  122.         // [*]
  123.         $s = preg_replace("/\[\*\]/", "<li>", $s);
  124.        
  125.         // [b]Bold[/b]
  126.         $s = preg_replace("/\[b\]((\s|.)+?)\[\/b\]/", "<b>\\1</b>", $s);
  127.        
  128.         // [center]Centered text[/center]
  129.     $s = preg_replace("/\[center\]((\s|.)+?)\[\/center\]/i", "<center>\\1</center>", $s);
  130.  
  131.         // [i]Italic[/i]
  132.         $s = preg_replace("/\[i\]((\s|.)+?)\[\/i\]/", "<i>\\1</i>", $s);
  133.  
  134.         // [u]Underline[/u]
  135.         $s = preg_replace("/\[u\]((\s|.)+?)\[\/u\]/", "<u>\\1</u>", $s);
  136.  
  137.         // [u]Underline[/u]
  138.         $s = preg_replace("/\[u\]((\s|.)+?)\[\/u\]/i", "<u>\\1</u>", $s);
  139.        
  140.         // [media=]Meditatag
  141.     $s = preg_replace( "#\[media=(youtube|liveleak|GameTrailers|imdb)\](.+?)\[/media\]#ies", "_MediaTag('\\2','\\1')" , $s );
  142.  
  143.         // [img]http://www/image.gif[/img]
  144.         $s = preg_replace("/\[img\](http:\/\/[^\s'\"<>]+(\.(jpg|gif|png)))\[\/img\]/i", "<img border=\"0\" src=\"\\1\" alt='' />", $s);
  145.        
  146.         // [img=http://www/image.gif]
  147.         $s = preg_replace("/\[img=(http:\/\/[^\s'\"<>]+(\.(gif|jpg|png)))\]/i", "<img border=\"0\" src=\"\\1\" alt='' />", $s);
  148.  
  149.         // [color=blue]Text[/color]
  150.         $s = preg_replace(
  151.                 "/\[color=([a-zA-Z]+)\]((\s|.)+?)\[\/color\]/i",
  152.                 "<font color='\\1'>\\2</font>", $s);
  153.  
  154.         // [color=#ffcc99]Text[/color]
  155.         $s = preg_replace(
  156.                 "/\[color=(#[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9])\]((\s|.)+?)\[\/color\]/i",
  157.                 "<font color='\\1'>\\2</font>", $s);
  158.  
  159.         // [url=http://www.example.com]Text[/url]
  160.         $s = preg_replace(
  161.                 "/\[url=([^()<>\s]+?)\]((\s|.)+?)\[\/url\]/i",
  162.                 "<a href=\"\\1\">\\2</a>", $s);
  163.  
  164.         // [url]http://www.example.com[/url]
  165.         $s = preg_replace(
  166.                 "/\[url\]([^()<>\s]+?)\[\/url\]/i",
  167.                 "<a href=\"\\1\">\\1</a>", $s);
  168.                                
  169.         // [size=4]Text[/size]
  170.         $s = preg_replace(
  171.                 "/\[size=([1-7])\]((\s|.)+?)\[\/size\]/i",
  172.                 "<font size='\\1'>\\2</font>", $s);
  173.  
  174.         // [font=Arial]Text[/font]
  175.         $s = preg_replace(
  176.                 "/\[font=([a-zA-Z ,]+)\]((\s|.)+?)\[\/font\]/i",
  177.                 "<font face=\"\\1\">\\2</font>", $s);
  178.                
  179.         // Video tag [video=url]
  180.         // YouTube Vids
  181.         $s = preg_replace("/\[video=[^\s'\"<>]*youtube.com.*v=([^\s'\"<>]+)\]/ims",
  182.                 "<object><param name=\"movie\" value=\"http://www.youtube.com/v/
  183.                \\1\"></param><embed width=\"500\" height=\"400\" src=\"http://www.youtube.com/v/\\1\" type=\"application/x-shockwave-flash\"</embed></object>", $s);
  184.  
  185.         // Google Vids
  186.         $s = preg_replace("/\[video=[^\s'\"<>]*video.google.com.*docid=(-?[0-9]+).*\]/ims",
  187.                 "<embed style=\"width:500px; height:410px;\" id=\"VideoPlayback\" align=\"middle\"
  188.                type=\"application/x-shockwave-flash\" src=\"http://video.google.com/googleplayer.swf?docId=\\1\"
  189.                allowScriptAccess=\"sameDomain\" quality=\"best\" bgcolor=\"#ff\" scale=\"noScale\" wmode=\"window\"
  190.                salign=\"TL\"  FlashVars=\"playerMode=embedded\"> </embed>", $s);
  191.  
  192. //  //[quote]Text[/quote]
  193. //  $s = preg_replace(
  194. //    "/\[quote\]\s*((\s|.)+?)\s*\[\/quote\]\s*/i",
  195. //    "<p class=sub><b>Quote:</b></p><table class=main border=1 cellspacing=0 cellpadding=10><tr><td style='border: 1px black dotted'>\\1</td></tr></table><br />", $s);
  196.  
  197. //  //[quote=Author]Text[/quote]
  198. //  $s = preg_replace(
  199. //    "/\[quote=(.+?)\]\s*((\s|.)+?)\s*\[\/quote\]\s*/i",
  200. //    "<p class=sub><b>\\1 wrote:</b></p><table class=main border=1 cellspacing=0 cellpadding=10><tr><td style='border: 1px black dotted'>\\2</td></tr></table><br />", $s);
  201.  
  202.         // Quotes
  203.         $s = format_quotes($s);
  204.  
  205.         // URLs
  206.         $s = format_urls($s);
  207. //      $s = format_local_urls($s);
  208.  
  209.         // Linebreaks
  210.         $s = nl2br($s);
  211.  
  212.         // [pre]Preformatted[/pre]
  213.         $s = preg_replace("/\[pre\]((\s|.)+?)\[\/pre\]/i", "<tt><span style=\"white-space: nowrap;\">\\1</span></tt>", $s);
  214.  
  215.         // [nfo]NFO-preformatted[/nfo]
  216.         $s = preg_replace("/\[nfo\]((\s|.)+?)\[\/nfo\]/i", "<tt><span style=\"white-space: nowrap;\"><font face='MS Linedraw' size='2' style='font-size: 10pt; line-height: " .
  217.                 "10pt'>\\1</font></span></tt>", $s);
  218.  
  219.         // Maintain spacing
  220.         $s = str_replace("  ", " &nbsp;", $s);
  221.  
  222.         foreach($smilies as $code => $url) {
  223.                 $s = str_replace($code, "<img border='0' src=\"{$TBDEV['pic_base_url']}smilies/{$url}\" alt=\"" . htmlspecialchars($code) . "\" />", $s);
  224. }
  225.         return $s;
  226. }
  227.  
  228. ?>