Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 106124 | Differences between
and this patch

Collapse All | Expand All

(-)bbcode.php.fgo_orig (-2 / +228 lines)
Lines 91-96 Link Here
91
	$bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $bbcode_tpl['quote_username_open']);
91
	$bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $bbcode_tpl['quote_username_open']);
92
92
93
	$bbcode_tpl['code_open'] = str_replace('{L_CODE}', $lang['Code'], $bbcode_tpl['code_open']);
93
	$bbcode_tpl['code_open'] = str_replace('{L_CODE}', $lang['Code'], $bbcode_tpl['code_open']);
94
	$bbcode_tpl['code_file_open'] = str_replace('{CODE_FILE}', '\\1', $bbcode_tpl['code_file_open']);
94
95
95
	$bbcode_tpl['img'] = str_replace('{URL}', '\\1', $bbcode_tpl['img']);
96
	$bbcode_tpl['img'] = str_replace('{URL}', '\\1', $bbcode_tpl['img']);
96
97
Lines 411-417 Link Here
411
	$text = " " . $text;
412
	$text = " " . $text;
412
413
413
	// [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
414
	// [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
414
	$text = bbencode_first_pass_pda($text, $uid, '[code]', '[/code]', '', true, '');
415
	$text = bbencode_first_pass_code($text, $uid);
415
416
416
	// [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
417
	// [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
417
	$text = bbencode_first_pass_pda($text, $uid, '[quote]', '[/quote]', '', false, '');
418
	$text = bbencode_first_pass_pda($text, $uid, '[quote]', '[/quote]', '', false, '');
Lines 714-719 Link Here
714
} // bbencode_first_pass_pda()
715
} // bbencode_first_pass_pda()
715
716
716
/**
717
/**
718
 * $text - The text to operate on.
719
 * $uid - The UID to add to matching tags.
720
 *
721
 * NOTES:	- this function assumes the first character of $text is a space.
722
 *				- every opening tag and closing tag must be of the [...] format.
723
 */
724
function bbencode_first_pass_code($text, $uid)
725
{
726
	$uid_length = strlen($uid);
727
	$close_tag = '[/code]';
728
	$close_tag_length = 7; // strlen('[/code]');
729
	
730
	// start_tag_index = array()
731
	//   1 = '[code]'
732
	//   2 = '[code="foo"]'
733
734
	$stack = array();
735
736
	// Start at the 2nd char of the string, looking for opening tags.
737
	$curr_pos = 1;
738
	while ($curr_pos && ($curr_pos < strlen($text)))
739
	{
740
		$curr_pos = strpos($text, "[", $curr_pos);
741
742
		// If not found, $curr_pos will be 0, and the loop will end.
743
		if ($curr_pos)
744
		{
745
			// We found a [. It starts at $curr_pos.
746
			// check if it's a starting or ending tag.
747
			$found_start = false;
748
			$which_start_tag = "";
749
			$start_tag_index = -1;
750
751
			// First check for [code]
752
			$possible_start = substr($text, $curr_pos, strpos($text, ']', $curr_pos + 1) - $curr_pos + 1);
753
			if (0 == strcasecmp('[code]', $possible_start))
754
			{
755
				$found_start = true;
756
				$which_start_tag = '[code]';
757
				$start_tag_index = 1;
758
			}
759
760
			// Now check for [code="foo"]
761
			if (! $found_start)
762
			{
763
				$possible_start = substr($text, $curr_pos, strpos($text, ']', $curr_pos + 1) - $curr_pos + 1);
764
765
				//
766
				// We're going to try and catch usernames with "[' characters.
767
				//
768
				if( preg_match('#\[code=\\\&quot;#si', $possible_start, $match) && !preg_match('#\[code=\\\&quot;(.*?)\\\&quot;\]#si', $possible_start) )
769
				{
770
					// OK we are in a code tag that probably contains a ] bracket.
771
					// Grab a bit more of the string to hopefully get all of it..
772
					if ($close_pos = strpos($text, '&quot;]', $curr_pos + 14))
773
					{
774
						if (strpos(substr($text, $curr_pos + 14, $close_pos - ($curr_pos + 14)), '[code') === false)
775
						{
776
							$possible_start = substr($text, $curr_pos, $close_pos - $curr_pos + 7);
777
						}
778
					}
779
				}
780
781
				$match_result = array();
782
				if (preg_match('/\[code=\\\\&quot;(.*?)\\\\&quot;\]/is', $possible_start, $match_result))
783
				{
784
					$found_start = true;
785
					$which_start_tag = $match_result[0];
786
					$start_tag_index = 2;
787
				}
788
			}
789
790
			if ($found_start)
791
			{
792
				// We have an opening tag.
793
				// Push its position, the text we matched, and its index in the open_tag array on to the stack, and then keep going to the right.
794
				$match = array("pos" => $curr_pos, "tag" => $which_start_tag, "index" => $start_tag_index);
795
				array_push($stack, $match);
796
				//
797
				// Rather than just increment $curr_pos
798
				// Set it to the ending of the tag we just found
799
				// Keeps error in nested tag from breaking out
800
				// of table structure..
801
				//
802
				$curr_pos += strlen($possible_start);
803
			}
804
			else
805
			{
806
				// check for a closing tag..
807
				$possible_end = substr($text, $curr_pos, $close_tag_length);
808
				if (0 == strcasecmp($close_tag, $possible_end))
809
				{
810
					// We have an ending tag.
811
					// Check if we've already found a matching starting tag.
812
					if (sizeof($stack) > 0)
813
					{
814
						// There exists a starting tag.
815
						$curr_nesting_depth = sizeof($stack);
816
						// We need to do 2 replacements now.
817
						$match = array_pop($stack);
818
						$start_index = $match['pos'];
819
						$start_tag = $match['tag'];
820
						$start_length = strlen($start_tag);
821
						$start_tag_index = $match['index'];
822
823
						// everything before the opening tag.
824
						$before_start_tag = substr($text, 0, $start_index);
825
826
						// everything after the opening tag, but before the closing tag.
827
						$between_tags = substr($text, $start_index + $start_length, $curr_pos - $start_index - $start_length);
828
						if ($curr_nesting_depth == 1)
829
						{
830
							$code_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#');
831
							$code_entities_replace = array('&lt;', '&gt;', '&quot;', '&#58;', '&#91;', '&#93;', '&#40;', '&#41;', '&#123;', '&#125;');
832
							$between_tags = preg_replace($code_entities_match, $code_entities_replace, $between_tags);
833
						}
834
835
						// everything after the closing tag.
836
						$after_end_tag = substr($text, $curr_pos + $close_tag_length);
837
838
						// Start the replacements
839
						if ($curr_nesting_depth == 1)
840
						{
841
							// [code="foo"] replacement
842
							if ($start_tag_index == 2)
843
							{
844
								// Yes, this is slightly weird, but I can't
845
								// make preg_replace work for the life of me...
846
								$matches = array();
847
								preg_match('/\[code\=\\\\&quot;(.*?)\\\\&quot;\]/is', $start_tag, $matches);
848
								$start_tag = '[code:1:'.$uid.'=\"'.$matches[1].'\"]';
849
							}
850
							else if ($start_tag_index == 1)
851
							{
852
								$start_tag = '[code:1:'.$uid.']';
853
							}
854
							$end_tag = '[/code:1:'.$uid.']';
855
						}
856
						else
857
						{
858
							// [code="foo"] replacement
859
							if ($start_tag_index == 2)
860
							{
861
								$matches = array();
862
								preg_match('/\[code\=\\\\&quot;(.*?)\\\\&quot;\]/is', $start_tag, $matches);
863
								$start_tag = '&#91;code=\"'.$matches[1].'\"&#93;';
864
							}
865
							else
866
							{
867
								$start_tag = '&#91;code&#93;';
868
							}
869
							$end_tag = '&#91;/code&#93;';
870
						}
871
872
						$text = $before_start_tag . $start_tag . $between_tags . $end_tag . $after_end_tag;
873
874
						// Now.. we've screwed up the indices by changing the length of the string.
875
						// So, if there's anything in the stack, we want to resume searching just after it.
876
						// otherwise, we go back to the start.
877
						if (sizeof($stack) > 0)
878
						{
879
							$match = array_pop($stack);
880
							$curr_pos = $match['pos'];
881
//							bbcode_array_push($stack, $match);
882
//							++$curr_pos;
883
						}
884
						else
885
						{
886
							$curr_pos = 1;
887
						}
888
					}
889
					else
890
					{
891
						// No matching start tag found. Increment pos, keep going.
892
						++$curr_pos;
893
					}
894
				}
895
				else
896
				{
897
					// No starting tag or ending tag.. Increment pos, keep looping.,
898
					++$curr_pos;
899
				}
900
			}
901
		}
902
	} // while
903
904
	return $text;
905
906
} // bbencode_first_pass_code()
907
908
/**
717
 * Does second-pass bbencoding of the [code] tags. This includes
909
 * Does second-pass bbencoding of the [code] tags. This includes
718
 * running htmlspecialchars() over the text contained between
910
 * running htmlspecialchars() over the text contained between
719
 * any pair of [code] tags that are at the first level of
911
 * any pair of [code] tags that are at the first level of
Lines 725-733 Link Here
725
{
917
{
726
	global $lang;
918
	global $lang;
727
919
920
	$code_start_file_html = $bbcode_tpl['code_file_open'];
728
	$code_start_html = $bbcode_tpl['code_open'];
921
	$code_start_html = $bbcode_tpl['code_open'];
729
	$code_end_html =  $bbcode_tpl['code_close'];
922
	$code_end_html =  $bbcode_tpl['code_close'];
730
923
924
	// Start off with [code] tags.
925
731
	// First, do all the 1st-level matches. These need an htmlspecialchars() run,
926
	// First, do all the 1st-level matches. These need an htmlspecialchars() run,
732
	// so they have to be handled differently.
927
	// so they have to be handled differently.
733
	$match_count = preg_match_all("#\[code:1:$uid\](.*?)\[/code:1:$uid\]#si", $text, $matches);
928
	$match_count = preg_match_all("#\[code:1:$uid\](.*?)\[/code:1:$uid\]#si", $text, $matches);
Lines 758-766 Link Here
758
	}
953
	}
759
954
760
	// Now, do all the non-first-level matches. These are simple.
955
	// Now, do all the non-first-level matches. These are simple.
956
	// WTF? There aren't any non-first-level matches - the brackets have been
957
	// replaced by html entities already...
761
	$text = str_replace("[code:$uid]", $code_start_html, $text);
958
	$text = str_replace("[code:$uid]", $code_start_html, $text);
762
	$text = str_replace("[/code:$uid]", $code_end_html, $text);
959
	$text = str_replace("[/code:$uid]", $code_end_html, $text);
763
960
961
	// Now do [code="foo"] tags
962
	$match_count = preg_match_all('#\[code:1:'.$uid.'="(.*?)"\](.*?)\[/code:1:'.$uid.'\]#si', $text, $matches);
963
964
	for ($i = 0; $i < $match_count; $i++)
965
	{
966
		$before_replace = $matches[2][$i];
967
		$after_replace = $matches[2][$i];
968
969
		// Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.
970
		$after_replace = str_replace("  ", "&nbsp; ", $after_replace);
971
		// now Replace 2 spaces with " &nbsp;" to catch odd #s of spaces.
972
		$after_replace = str_replace("  ", " &nbsp;", $after_replace);
973
974
		// Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.
975
		$after_replace = str_replace("\t", "&nbsp; &nbsp;", $after_replace);
976
977
		// now Replace space occurring at the beginning of a line
978
		$after_replace = preg_replace("/^ {1}/m", '&nbsp;', $after_replace);
979
980
		$str_to_match = '[code:1:'.$uid.'="'.$matches[1][$i].'"]' . $before_replace . "[/code:1:$uid]";
981
982
		$code_start_file_html = str_replace('\\1', $matches[1][$i], $code_start_file_html);
983
		$replacement = $code_start_file_html;
984
		$replacement .= $after_replace;
985
		$replacement .= $code_end_html;
986
987
		$text = str_replace($str_to_match, $replacement, $text);
988
	}
989
764
	return $text;
990
	return $text;
765
991
766
} // bbencode_second_pass_code()
992
} // bbencode_second_pass_code()
Lines 1005-1008 Link Here
1005
	return ( strlen($a['code']) > strlen($b['code']) ) ? -1 : 1;
1231
	return ( strlen($a['code']) > strlen($b['code']) ) ? -1 : 1;
1006
}
1232
}
1007
1233
1008
?>
1234
?>

Return to bug 106124