", bbencode_code($message, $is_html_disabled)); // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff. $message = bbencode_quote($message); // [list] and [list=x] for (un)ordered lists. $message = bbencode_list($message); // [b] and [/b] for bolding text. $message = preg_replace("/\[b\](.*?)\[\/b\]/si", "\\1", $message); // [i] and [/i] for italicizing text. $message = preg_replace("/\[i\](.*?)\[\/i\]/si", "\\1", $message); // [u] and [/u] for underlining text. $message = preg_replace("/\[u\](.*?)\[\/u\]/si", "\\1", $message); // [img]image_url_here[/img] code.. // echo ""; $ie7 = ""; $ff3 = "
"; //$test = ""; //$rplac = $ie7; $rplac = ""; if($user["displayimages"] || $_GET["forceimages"]) $message = preg_replace("/\[img\](.*?)\[\/img\]/si", $rplac, $message); else $message = preg_replace("/\[img\](.*?)\[\/img\]/si", "[image disabled] ", $message); // Patterns and replacements for URL and email tags.. $patterns = array(); $replacements = array(); // [url]xxxx://www.phpbb.com[/url] code.. $patterns[0] = "#\[url\]([a-z]+?://){1}(.*?)\[/url\]#si"; $replacements[0] = '\1\2'; // [url]www.phpbb.com[/url] code.. (no xxxx:// prefix). $patterns[1] = "#\[url\](.*?)\[/url\]#si"; $replacements[1] = '\1'; // [url=xxxx://www.phpbb.com]phpBB[/url] code.. $patterns[2] = "#\[url=([a-z]+?://){1}(.*?)\](.*?)\[/url\]#si"; $replacements[2] = '\3'; // [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix). $patterns[3] = "#\[url=(.*?)\](.*?)\[/url\]#si"; $replacements[3] = '\2'; // [email]user@domain.tld[/email] code.. $patterns[4] = "#\[email\](.*?)\[/email\]#si"; $replacements[4] = '\1'; $message = preg_replace($patterns, $replacements, $message); // Remove our padding from the string.. $message = substr($message, 1); return $message; } // bbencode() function bbdecode($message) { // Undo [code] // $code_start_html = "
Code:
";
		// $code_end_html = "

"; $code_start_html = "
Code:
";
		$code_end_html = "
"; $message = str_replace($code_start_html, "[code]", $message); $message = str_replace($code_end_html, "[/code]", $message); // Undo [quote] // $quote_start_html = "
Quote:
"; // $quote_end_html = "

"; $quote_start_html = "
Quote:
"; $quote_end_html = "
"; $message = str_replace($quote_start_html, "[quote]", $message); $message = str_replace($quote_end_html, "[/quote]", $message); // Undo [b] and [i] $message = preg_replace("#(.*?)#s", "[b]\\1[/b]", $message); $message = preg_replace("#(.*?)#s", "[i]\\1[/i]", $message); // Undo [url] (long form) $message = preg_replace("#(.*?)#s", "[url=\\1\\2]\\3[/url]", $message); // Undo [url] (short form) $message = preg_replace("#(.*?)#s", "[url]\\3[/url]", $message); // Undo [email] $message = preg_replace("#(.*?)#s", "[email]\\1[/email]", $message); // Undo [img] $message = preg_replace("##s", "[img]\\1[/img]", $message); // Undo lists (unordered/ordered) //
  • tags: $message = str_replace("
  • ", "[*]", $message); // [list] tags: $message = str_replace("", "[/list]", $message); $message = str_replace("", "[/list]", $message); return($message); } /** * James Atkinson - Feb 5, 2001 * This function does exactly what the PHP4 function array_push() does * however, to keep phpBB compatable with PHP 3 we had to come up with out own * method of doing it. */ function bbcode_array_push(&$stack, $value) { $stack[] = $value; return(sizeof($stack)); } /** * James Atkinson - Feb 5, 2001 * This function does exactly what the PHP4 function array_pop() does * however, to keep phpBB compatable with PHP 3 we had to come up with out own * method of doing it. */ function bbcode_array_pop(&$stack) { $arrSize = count($stack); $x = 1; while(list($key, $val) = each($stack)) { if($x < count($stack)) { $tmpArr[] = $val; } else { $return_val = $val; } $x++; } $stack = $tmpArr; return($return_val); } /** * Nathan Codding - Jan. 12, 2001. * Performs [quote][/quote] bbencoding on the given string, and returns the results. * Any unmatched "[quote]" or "[/quote]" token will just be left alone. * This works fine with both having more than one quote in a message, and with nested quotes. * Since that is not a regular language, this is actually a PDA and uses a stack. Great fun. * * Note: This function assumes the first character of $message is a space, which is added by * bbencode(). */ function bbencode_quote($message) { // First things first: If there aren't any "[quote]" strings in the message, we don't // need to process it at all. if (!strpos(strtolower($message), "[quote]")) { return $message; } $stack = Array(); $curr_pos = 1; while ($curr_pos && ($curr_pos < strlen($message))) { $curr_pos = strpos($message, "[", $curr_pos); // If not found, $curr_pos will be 0, and the loop will end. if ($curr_pos) { // We found a [. It starts at $curr_pos. // check if it's a starting or ending quote tag. $possible_start = substr($message, $curr_pos, 7); $possible_end = substr($message, $curr_pos, 8); if (strcasecmp("[quote]", $possible_start) == 0) { // We have a starting quote tag. // Push its position on to the stack, and then keep going to the right. bbcode_array_push($stack, $curr_pos); ++$curr_pos; } else if (strcasecmp("[/quote]", $possible_end) == 0) { // We have an ending quote tag. // Check if we've already found a matching starting tag. if (sizeof($stack) > 0) { // There exists a starting tag. // We need to do 2 replacements now. $start_index = bbcode_array_pop($stack); // everything before the [quote] tag. $before_start_tag = substr($message, 0, $start_index); // everything after the [quote] tag, but before the [/quote] tag. $between_tags = substr($message, $start_index + 7, $curr_pos - $start_index - 7); // everything after the [/quote] tag. $after_end_tag = substr($message, $curr_pos + 8); $message = $before_start_tag . "
    Quote:
    "; $message .= $between_tags . "
    "; $message .= $after_end_tag; // Now.. we've screwed up the indices by changing the length of the string. // So, if there's anything in the stack, we want to resume searching just after it. // otherwise, we go back to the start. if (sizeof($stack) > 0) { $curr_pos = bbcode_array_pop($stack); bbcode_array_push($stack, $curr_pos); ++$curr_pos; } else { $curr_pos = 1; } } else { // No matching start tag found. Increment pos, keep going. ++$curr_pos; } } else { // No starting tag or ending tag.. Increment pos, keep looping., ++$curr_pos; } } } // while return $message; } // bbencode_quote() /** * Nathan Codding - Jan. 12, 2001. * Performs [code][/code] bbencoding on the given string, and returns the results. * Any unmatched "[code]" or "[/code]" token will just be left alone. * This works fine with both having more than one code block in a message, and with nested code blocks. * Since that is not a regular language, this is actually a PDA and uses a stack. Great fun. * * Note: This function assumes the first character of $message is a space, which is added by * bbencode(). */ function bbencode_code($message, $is_html_disabled) { // First things first: If there aren't any "[code]" strings in the message, we don't // need to process it at all. if (!strpos(strtolower($message), "[code]")) { return $message; } //return preg_replace("/\[code\](.*?)\[\/code\]/si", "
    Code:
    \1
    ", $message); // $message = preg_replace("/$str_to_match/si", "
    Code:
    $after_replace
    ", $message); // Second things second: we have to watch out for stuff like [1code] or [/code1] in the // input.. So escape them to [#1code] or [/code#1] for now: $message = preg_replace("/\[([0-9]+?)code\]/si", "[#\\1code]", $message); $message = preg_replace("/\[\/code([0-9]+?)\]/si", "[/code#\\1]", $message); $stack = Array(); $curr_pos = 1; $max_nesting_depth = 0; while ($curr_pos && ($curr_pos < strlen($message))) { $curr_pos = strpos($message, "[", $curr_pos); // If not found, $curr_pos will be 0, and the loop will end. if ($curr_pos) { // We found a [. It starts at $curr_pos. // check if it's a starting or ending code tag. $possible_start = substr($message, $curr_pos, 6); $possible_end = substr($message, $curr_pos, 7); if (strcasecmp("[code]", $possible_start) == 0) { // We have a starting code tag. // Push its position on to the stack, and then keep going to the right. bbcode_array_push($stack, $curr_pos); ++$curr_pos; } else if (strcasecmp("[/code]", $possible_end) == 0) { // We have an ending code tag. // Check if we've already found a matching starting tag. if (sizeof($stack) > 0) { // There exists a starting tag. $curr_nesting_depth = sizeof($stack); $max_nesting_depth = ($curr_nesting_depth > $max_nesting_depth) ? $curr_nesting_depth : $max_nesting_depth; // We need to do 2 replacements now. $start_index = bbcode_array_pop($stack); // everything before the [code] tag. $before_start_tag = substr($message, 0, $start_index); // everything after the [code] tag, but before the [/code] tag. $between_tags = substr($message, $start_index + 6, $curr_pos - $start_index - 6); // everything after the [/code] tag. $after_end_tag = substr($message, $curr_pos + 7); $message = $before_start_tag . "[" . $curr_nesting_depth . "code]"; $message .= $between_tags . "[/code" . $curr_nesting_depth . "]"; $message .= $after_end_tag; // Now.. we've screwed up the indices by changing the length of the string. // So, if there's anything in the stack, we want to resume searching just after it. // otherwise, we go back to the start. if (sizeof($stack) > 0) { $curr_pos = bbcode_array_pop($stack); bbcode_array_push($stack, $curr_pos); ++$curr_pos; } else { $curr_pos = 1; } } else { // No matching start tag found. Increment pos, keep going. ++$curr_pos; } } else { // No starting tag or ending tag.. Increment pos, keep looping., ++$curr_pos; } } } // while //echo ""; if ($max_nesting_depth > 0) { for ($i = 1; $i <= $max_nesting_depth; ++$i) { $start_tag = escape_slashes(preg_quote("[" . $i . "code]")); $end_tag = escape_slashes(preg_quote("[/code" . $i . "]")); $match_count = preg_match_all("/$start_tag(.*?)$end_tag/si", $message, $matches); for ($j = 0; $j < $match_count; $j++) { $before_replace = escape_slashes(preg_quote($matches[1][$j])); //debuglog($before_replace); $after_replace = preg_quote($matches[1][$j]); //debuglog($after_replace); if (($i < 2) && !$is_html_disabled) { // don't escape special chars when we're nested, 'cause it was already done // at the lower level.. // also, don't escape them if HTML is disabled in this post. it'll already be done // by the posting routines. $after_replace = htmlspecialchars($after_replace); } $str_to_match = $start_tag . $before_replace . $end_tag; $message = preg_replace("/".$str_to_match."/si", "
    Code:
    ".$after_replace."
    ", $message); } } } // Undo our escaping from "second things second" above.. $message = preg_replace("/\[#([0-9]+?)code\]/si", "[\\1code]", $message); $message = preg_replace("/\[\/code#([0-9]+?)\]/si", "[/code\\1]", $message); return $message; } // bbencode_code() /** * Nathan Codding - Jan. 12, 2001. * Performs [list][/list] and [list=?][/list] bbencoding on the given string, and returns the results. * Any unmatched "[list]" or "[/list]" token will just be left alone. * This works fine with both having more than one list in a message, and with nested lists. * Since that is not a regular language, this is actually a PDA and uses a stack. Great fun. * * Note: This function assumes the first character of $message is a space, which is added by * bbencode(). */ function bbencode_list($message) { $start_length = Array(); $start_length[ordered] = 8; $start_length[unordered] = 6; // First things first: If there aren't any "[list" strings in the message, we don't // need to process it at all. if (!strpos(strtolower($message), "[list")) { return $message; } $stack = Array(); $curr_pos = 1; while ($curr_pos && ($curr_pos < strlen($message))) { $curr_pos = strpos($message, "[", $curr_pos); // If not found, $curr_pos will be 0, and the loop will end. if ($curr_pos) { // We found a [. It starts at $curr_pos. // check if it's a starting or ending list tag. $possible_ordered_start = substr($message, $curr_pos, $start_length[ordered]); $possible_unordered_start = substr($message, $curr_pos, $start_length[unordered]); $possible_end = substr($message, $curr_pos, 7); if (strcasecmp("[list]", $possible_unordered_start) == 0) { // We have a starting unordered list tag. // Push its position on to the stack, and then keep going to the right. bbcode_array_push($stack, array($curr_pos, "")); ++$curr_pos; } else if (preg_match("/\[list=([a1])\]/si", $possible_ordered_start, $matches)) { // We have a starting ordered list tag. // Push its position on to the stack, and the starting char onto the start // char stack, the keep going to the right. bbcode_array_push($stack, array($curr_pos, $matches[1])); ++$curr_pos; } else if (strcasecmp("[/list]", $possible_end) == 0) { // We have an ending list tag. // Check if we've already found a matching starting tag. if (sizeof($stack) > 0) { // There exists a starting tag. // We need to do 2 replacements now. $start = bbcode_array_pop($stack); $start_index = $start[0]; $start_char = $start[1]; $is_ordered = ($start_char != ""); $start_tag_length = ($is_ordered) ? $start_length[ordered] : $start_length[unordered]; // everything before the [list] tag. $before_start_tag = substr($message, 0, $start_index); // everything after the [list] tag, but before the [/list] tag. $between_tags = substr($message, $start_index + $start_tag_length, $curr_pos - $start_index - $start_tag_length); // Need to replace [*] with
  • inside the list. $between_tags = str_replace("[*]", "
  • ", $between_tags); // everything after the [/list] tag. $after_end_tag = substr($message, $curr_pos + 7); if ($is_ordered) { $message = $before_start_tag . "
      "; $message .= $between_tags . "
    "; } else { $message = $before_start_tag . ""; } $message .= $after_end_tag; // Now.. we've screwed up the indices by changing the length of the string. // So, if there's anything in the stack, we want to resume searching just after it. // otherwise, we go back to the start. if (sizeof($stack) > 0) { $a = bbcode_array_pop($stack); $curr_pos = $a[0]; bbcode_array_push($stack, $a); ++$curr_pos; } else { $curr_pos = 1; } } else { // No matching start tag found. Increment pos, keep going. ++$curr_pos; } } else { // No starting tag or ending tag.. Increment pos, keep looping., ++$curr_pos; } } } // while return $message; } // bbencode_list() /** * Nathan Codding - Oct. 30, 2000 * * Escapes the "/" character with "\/". This is useful when you need * to stick a runtime string into a PREG regexp that is being delimited * with slashes. */ function escape_slashes($input) { $output = str_replace('/', '\/', $input); //$output = str_replace('$', '\$', $input); return $output; } /** * Rewritten by Nathan Codding - Feb 6, 2001. * - Goes through the given string, and replaces xxxx://yyyy with an HTML tag linking * to that URL * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML tag linking * to http://www.xxxx.yyyy[/zzzz] * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking * to that email address * - Only matches these 2 patterns either after a space, or at the beginning of a line * * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe * have it require something like xxxx@yyyy.zzzz or such. We'll see. */ function make_clickable($text) { // pad it with a space so we can match things at the start of the 1st line. $ret = " " . $text; // matches an "xxxx://yyyy" URL at the start of a line, or after a space. // xxxx can only be alpha characters. // yyyy is anything up to the first space, newline, or comma. $ret = preg_replace("#([\n ])([a-z]+?)://([^, \n\r]+)#i", "\\1\\2://\\3", $ret); // matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing // Must contain at least 2 dots. xxxx contains either alphanum, or "-" // yyyy contains either alphanum, "-", or "." // zzzz is optional.. will contain everything up to the first space, newline, or comma. // This is slightly restrictive - it's not going to match stuff like "forums.foo.com" // This is to keep it from getting annoying and matching stuff that's not meant to be a link. $ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^, \n\r]*)?)#i", "\\1www.\\2.\\3\\4", $ret); // matches an email@domain type address at the start of a line, or after a space. // Note: before the @ sign, the only valid characters are the alphanums and "-", "_", or ".". // After the @ sign, we accept anything up to the first space, linebreak, or comma. $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^, \n\r]+)#i", "\\1\\2@\\3", $ret); // Remove our padding.. $ret = substr($ret, 1); return($ret); } /** * Nathan Codding - Feb 6, 2001 * Reverses the effects of make_clickable(), for use in editpost. * - Does not distinguish between "www.xxxx.yyyy" and "http://aaaa.bbbb" type URLs. * */ function undo_make_clickable($text) { $text = preg_replace("#.*?#i", "\\1", $text); $text = preg_replace("#.*?#i", "\\1", $text); return $text; } /** * Nathan Codding - August 24, 2000. * Takes a string, and does the reverse of the PHP standard function * htmlspecialchars(). */ function undo_htmlspecialchars($input) { $input = preg_replace("/>/i", ">", $input); $input = preg_replace("/</i", "<", $input); $input = preg_replace("/"/i", "\"", $input); $input = preg_replace("/&/i", "&", $input); return $input; } /** * Translates any sequence of whitespace (\t, \r, \n, or space) in the given * string into a single space character. * Returns the result. */ function normalize_whitespace($str) { $output = ""; $tok = preg_split("/[ \t\r\n]+/", $str); $tok_count = sizeof($tok); for ($i = 0; $i < ($tok_count - 1); $i++) { $output .= $tok[$i] . " "; } $output .= $tok[$tok_count - 1]; return $output; } /** * Less agressive version of stripslashes. Only replaces \\ \' and \" * The PHP stripslashes() also removed single backslashes from the string. * Expects a string or array as an argument. * Returns the result. */ function own_stripslashes($string) { $find = array( '/\\\\\'/', // \\\' '/\\\\/', // \\ '/\\\'/', // \' '/\\\"/'); // \" $replace = array( '\'', // \ '\\', // \ '\'', // ' '"'); // " return preg_replace($find, $replace, $string); } function htmlcleanonerow($inhtml){ $inhtml= str_replace( "<", "&" . "lt;", $inhtml); $inhtml= str_replace( ">", "&" . "gt;", $inhtml); $inhtml= str_replace( "\"", "&" . "quot;", $inhtml); $inhtml= str_replace( "\n", " ", $inhtml); return $inhtml; } function parseUrl( $str, $splitPosition=56 ) /* * /!\ it assumes that $str went through the htmlentities( ) function. */ { $textWithATags = preg_replace( "(([a-zA-Z]+://)([\\S\"']+))", "link me beautiful", $str ); if( $splitPosition ) { $str = ""; $i = 0; $tagOpen = 0; while( strlen( $textWithATags ) ) { $currentChar = substr( $textWithATags, 0, 1 ); $str .= $currentChar; $textWithATags = substr( $textWithATags, 1 ); if( $currentChar=="<" ) $tagOpen++; if( $tagOpen==0 ) $i = $currentChar==" "?0:$i+1; if( $currentChar==">" ) $tagOpen--; if( $i==$splitPosition ) { $i = 0; $str .= "
    "; } } return $str; } return $textWithATags; } ?>