PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/cms/modules/forum/bbparser.php

https://github.com/jithinkr/pragyan
PHP | 188 lines | 159 code | 13 blank | 16 comment | 25 complexity | ecfa93688a5695947ea22c605e706fa5 MD5 | raw file
  1. <?php
  2. if(!defined('__PRAGYAN_CMS'))
  3. {
  4. header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
  5. echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
  6. echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
  7. exit(1);
  8. }
  9. /**
  10. * @package pragyan
  11. * @copyright (c) 2008 Pragyan Team
  12. * @license http://www.gnu.org/licenses/ GNU Public License
  13. * For more details, see README
  14. */
  15. // htmlentities is too agressive so we use this function
  16. function phpentities($text) {
  17. $search = array("&", "\"", "'", "\\", "<", ">");
  18. $replace = array("&amp;", "&quot;", "&#39;", "&#92;", "&lt;", "&gt;");
  19. $text = str_replace($search, $replace, $text);
  20. return $text;
  21. }
  22. // Parse smiley bbcode into HTML images
  23. global $urlRequestRoot,$sourceFolder,$moduleFolder,$cmsFolder;
  24. $imgpath=$urlRequestRoot."/".$cmsFolder."/".$moduleFolder."/forum/images/smileys/";
  25. define("IMAGES","$imgpath");
  26. function parsesmileys($message) {
  27. global $IMAGES;
  28. $smiley = array(
  29. "#\:\)#si" => "<img title='Smile' src='".IMAGES."smile.gif' alt=':)'>",
  30. "#\;\)#si" => "<img title='Wink' src='".IMAGES."wink.gif' alt=';)'>",
  31. "#\:\(#si" => "<img title='Sad' src='".IMAGES."sad.gif' alt=':('>",
  32. "#\:\|#si" => "<img title='Frown' src='".IMAGES."frown.gif' alt=':|'>",
  33. "#\:o#si" => "<img title='Shock' src='".IMAGES."shock.gif' alt=':o'>",
  34. "#\:p#si" => "<img title='lol'src='".IMAGES."pfft.gif' alt=':P'>",
  35. "#b\)#si" => "<img title='Cool'src='".IMAGES."cool.gif' alt='B)'>",
  36. "#\:d#si" => "<img title='Grin' src='".IMAGES."grin.gif' alt=':D'>",
  37. "#\:@#si" => "<img title='Angry' src='".IMAGES."angry.gif' alt=':@'>",
  38. );
  39. foreach($smiley as $key=>$smiley_img) $message = preg_replace($key, $smiley_img, $message);
  40. return $message;
  41. }
  42. function parsenewline($text) {
  43. $text = preg_replace('#\<br/\>#si', '[br/]', $text);
  44. $text = preg_replace('#\<br /\>#si', '[br /]', $text);
  45. return $text;
  46. }
  47. // Show smiley icons in comments, forum and other post pages
  48. function displaysmileys($textarea) {
  49. $smiles = "";
  50. $smileys = array (
  51. ":)" => "smile.gif",
  52. ";)" => "wink.gif",
  53. ":|" => "frown.gif",
  54. ":(" => "sad.gif",
  55. ":o" => "shock.gif",
  56. ":p" => "pfft.gif",
  57. "B)" => "cool.gif",
  58. ":D" => "grin.gif",
  59. ":@" => "angry.gif",
  60. );
  61. foreach($smileys as $key=>$smiley) $smiles .= "<img src='".IMAGES."$smiley' alt='smiley' onClick=\"insertText('$textarea', '$key');\">\n";
  62. return $smiles;
  63. }
  64. // Parse bbcode into HTML code
  65. function parseubb($text) {
  66. $text = preg_replace('#\[b\](.*?)\[/b\]#si', '<b>\1</b>', $text);
  67. $text = preg_replace('#\[i\](.*?)\[/i\]#si', '<i>\1</i>', $text);
  68. $text = preg_replace('#\[u\](.*?)\[/u\]#si', '<u>\1</u>', $text);
  69. $text = preg_replace('#\[center\](.*?)\[/center\]#si', '<center>\1</center>', $text);
  70. $text = preg_replace('#\[br/\]#si', '<br/>', $text);
  71. $text = preg_replace('#\[br /\]#si', '<br />', $text);
  72. $text = preg_replace('#\[url\]([\r\n]*)(http://|ftp://|https://|ftps://)([^\s\'\";\+]*?)([\r\n]*)\[/url\]#si', '<a href=\'\2\3\' target=\'_blank\'>\2\3</a>', $text);
  73. $text = preg_replace('#\[url\]([\r\n]*)([^\s\'\";\+]*?)([\r\n]*)\[/url\]#si', '<a href=\'http://\2\' target=\'_blank\'>\2</a>', $text);
  74. $text = preg_replace('#\[url=([\r\n]*)(http://|ftp://|https://|ftps://)([^\s\'\";\+]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'\2\3\' target=\'_blank\'>\4</a>', $text);
  75. $text = preg_replace('#\[url=([\r\n]*)([^\s\'\";\+]*?)\](.*?)([\r\n]*)\[/url\]#si', '<a href=\'http://\2\' target=\'_blank\'>\3</a>', $text);
  76. $text = preg_replace('#\[mail\]([\r\n]*)([^\s\'\";:\+]*?)([\r\n]*)\[/mail\]#si', '<a href=\'mailto:\2\'>\2</a>', $text);
  77. $text = preg_replace('#\[mail=([\r\n]*)([^\s\'\";:\+]*?)\](.*?)([\r\n]*)\[/mail\]#si', '<a href=\'mailto:\2\'>\3</a>', $text);
  78. $text = preg_replace('#\[small\](.*?)\[/small\]#si', '<span class=\'small\'>\1</span>', $text);
  79. $text = preg_replace('#\[color=(black|blue|brown|cyan|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|violet|white|yellow)\](.*?)\[/color\]#si', '<span style=\'color:\1\'>\2</span>', $text);
  80. $text = preg_replace('#\[flash width=([0-9]*?) height=([0-9]*?)\]([^\s\'\";:\+]*?)(\.swf)\[/flash\]#si', '<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://active.macromedia.com/flash6/cabs/swflash.cab#version=6,0,0,0\' id=\'\3\4\' width=\'\1\' height=\'\2\'><param name=movie value=\'\3\4\'><param name=\'quality\' value=\'high\'><param name=\'bgcolor\' value=\'#ffffff\'><embed src=\'\3\4\' quality=\'high\' bgcolor=\'#ffffff\' width=\'\1\' height=\'\2\' type=\'application/x-shockwave-flash\' pluginspage=\'http://www.macromedia.com/go/getflashplayer\'></embed></object>', $text);
  81. $text = preg_replace("#\[img\]((http|ftp|https|ftps)://)(.*?)(\.(jpg|jpeg|gif|png|JPG|JPEG|GIF|PNG))\[/img\]#sie","'<img src=\'\\1'.str_replace(array('.php','?','&','='),'','\\3').'\\4\' style=\'border:0px\'>'",$text);
  82. $qcount = substr_count($text, "[quote]"); $ccount = substr_count($text, "[code]");
  83. for ($i=0;$i < $qcount;$i++) $text = preg_replace('#\[quote\](.*?)\[/quote\]#si', '<div class=\'quote\'>\1</div>', $text);
  84. for ($i=0;$i < $ccount;$i++) $text = preg_replace('#\[code\](.*?)\[/code\]#si', '<div class=\'quote\' style=\'width:400px;white-space:nowrap;overflow:auto\'><code style=\'white-space:nowrap\'>\1<br><br><br></code></div>', $text);
  85. $text = descript($text,false);
  86. return $text;
  87. }
  88. // This function sanitises news & article submissions
  89. function descript($text,$striptags=true) {
  90. // Convert problematic ascii characters to their true values
  91. $search = array("40","41","58","65","66","67","68","69","70",
  92. "71","72","73","74","75","76","77","78","79","80","81",
  93. "82","83","84","85","86","87","88","89","90","97","98",
  94. "99","100","101","102","103","104","105","106","107",
  95. "108","109","110","111","112","113","114","115","116",
  96. "117","118","119","120","121","122"
  97. );
  98. $replace = array("(",")",":","a","b","c","d","e","f","g","h",
  99. "i","j","k","l","m","n","o","p","q","r","s","t","u",
  100. "v","w","x","y","z","a","b","c","d","e","f","g","h",
  101. "i","j","k","l","m","n","o","p","q","r","s","t","u",
  102. "v","w","x","y","z"
  103. );
  104. $entities = count($search);
  105. for ($i=0;$i < $entities;$i++) $text = preg_replace("#(&\#)(0*".$search[$i]."+);*#si", $replace[$i], $text);
  106. // the following is based on code from bitflux (http://blog.bitflux.ch/wiki/)
  107. // Kill hexadecimal characters completely
  108. $text = preg_replace('#(&\#x)([0-9A-F]+);*#si', "", $text);
  109. // remove any attribute starting with "on" or xmlns
  110. $text = preg_replace('#(<[^>]+[\\"\'\s])(onmouseover|onmousedown|onmouseup|onmouseout|onmousemove|onclick|ondblclick|onload|xmlns)[^>]*>#iU', ">", $text);
  111. // remove javascript: and vbscript: protocol
  112. $text = preg_replace('#([a-z]*)=([\`\'\"]*)script:#iU', '$1=$2nojscript...', $text);
  113. $text = preg_replace('#([a-z]*)=([\`\'\"]*)javascript:#iU', '$1=$2nojavascript...', $text);
  114. $text = preg_replace('#([a-z]*)=([\'\"]*)vbscript:#iU', '$1=$2novbscript...', $text);
  115. //<span style="width: expression(alert('Ping!'));"></span> (only affects ie...)
  116. $text = preg_replace('#(<[^>]+)style=([\`\'\"]*).*expression\([^>]*>#iU', "$1>", $text);
  117. $text = preg_replace('#(<[^>]+)style=([\`\'\"]*).*behaviour\([^>]*>#iU', "$1>", $text);
  118. if ($striptags) {
  119. do {
  120. $thistext = $text;
  121. $text = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $text);
  122. } while ($thistext != $text);
  123. }
  124. return $text;
  125. }
  126. // Replace offensive words with the defined replacement word
  127. function censorwords($text) {
  128. global $settings;
  129. if ($settings['bad_words_enabled'] == "1" && $settings['bad_words'] != "" ) {
  130. $word_list = explode("\r\n", $settings['bad_words']);
  131. for ($i=0;$i < count($word_list);$i++) {
  132. if ($word_list[$i] != "") $text = preg_replace("/".$word_list[$i]."/si", $settings['bad_word_replace'], $text);
  133. }
  134. }
  135. return $text;
  136. }
  137. // Universal page pagination function by CrappoMan
  138. function makepagenav($start,$count,$total,$range=0,$link=""){
  139. global $locale;
  140. if ($link == "") $link = FUSION_SELF."?";
  141. $res="";
  142. $pg_cnt=ceil($total / $count);
  143. if ($pg_cnt > 1) {
  144. $idx_back = $start - $count;
  145. $idx_next = $start + $count;
  146. $cur_page=ceil(($start + 1) / $count);
  147. $res.="<table cellspacing='1' cellpadding='1' border='0' class='tbl-border'>\n<tr>\n";
  148. $res.="<td class='tbl2'><span class='small'>".$locale['052']."$cur_page".$locale['053']."$pg_cnt</span></td>\n";
  149. if ($idx_back >= 0) {
  150. if ($cur_page > ($range + 1)) $res.="<td class='tbl2'><a class='small' href='$link"."rowstart=0'>&lt;&lt;</a></td>\n";
  151. $res.="<td class='tbl2'><a class='small' href='$link"."rowstart=$idx_back'>&lt;</a></td>\n";
  152. }
  153. $idx_fst=max($cur_page - $range, 1);
  154. $idx_lst=min($cur_page + $range, $pg_cnt);
  155. if ($range==0) {
  156. $idx_fst = 1;
  157. $idx_lst=$pg_cnt;
  158. }
  159. for($i=$idx_fst;$i<=$idx_lst;$i++) {
  160. $offset_page=($i - 1) * $count;
  161. if ($i==$cur_page) {
  162. $res.="<td class='tbl1'><span class='small'><b>$i</b></span></td>\n";
  163. } else {
  164. $res.="<td class='tbl1'><a class='small' href='$link"."rowstart=$offset_page'>$i</a></td>\n";
  165. }
  166. }
  167. if ($idx_next < $total) {
  168. $res.="<td class='tbl2'><a class='small' href='$link"."rowstart=$idx_next'>&gt;</a></td>\n";
  169. if ($cur_page < ($pg_cnt - $range)) $res.="<td class='tbl2'><a class='small' href='$link"."rowstart=".($pg_cnt-1)*$count."'>&gt;&gt;</a></td>\n";
  170. }
  171. $res.="</tr>\n</table>\n";
  172. }
  173. return $res;
  174. }