@@ -855,28 +855,6 @@ function bbcode_quote($in)
855855 else if (preg_match ('#^quote(?:="(.*?)")?$#is ' , $ buffer , $ m ) && substr ($ out , -1 , 1 ) == '[ ' )
856856 {
857857 $ this ->parsed_items ['quote ' ]++;
858-
859- // the buffer holds a valid opening tag
860- if ($ config ['max_quote_depth ' ] && sizeof ($ close_tags ) >= $ config ['max_quote_depth ' ])
861- {
862- if ($ config ['max_quote_depth ' ] == 1 )
863- {
864- // Depth 1 - no nesting is allowed
865- $ error_ary ['quote_depth ' ] = $ user ->lang ('QUOTE_NO_NESTING ' );
866- }
867- else
868- {
869- // There are too many nested quotes
870- $ error_ary ['quote_depth ' ] = $ user ->lang ('QUOTE_DEPTH_EXCEEDED ' , (int ) $ config ['max_quote_depth ' ]);
871- }
872-
873- $ out .= $ buffer . $ tok ;
874- $ tok = '[] ' ;
875- $ buffer = '' ;
876-
877- continue ;
878- }
879-
880858 array_push ($ close_tags , '/quote: ' . $ this ->bbcode_uid );
881859
882860 if (isset ($ m [1 ]) && $ m [1 ])
@@ -1308,6 +1286,12 @@ function parse($allow_bbcode, $allow_magic_url, $allow_smilies, $allow_img_bbcod
13081286 // Parse this message
13091287 $ this ->message = $ parser ->parse (htmlspecialchars_decode ($ this ->message , ENT_QUOTES ));
13101288
1289+ // Remove quotes that are nested too deep
1290+ if ($ config ['max_quote_depth ' ] > 0 )
1291+ {
1292+ $ this ->remove_nested_quotes ($ config ['max_quote_depth ' ]);
1293+ }
1294+
13111295 // Check for "empty" message. We do not check here for maximum length, because bbcode, smilies, etc. can add to the length.
13121296 // The maximum length check happened before any parsings.
13131297 if ($ mode === 'post ' && utf8_clean_string ($ this ->message ) === '' )
@@ -1904,6 +1888,63 @@ function parse_poll(&$poll)
19041888 $ this ->message = $ tmp_message ;
19051889 }
19061890
1891+ /**
1892+ * Remove nested quotes at given depth in current parsed message
1893+ *
1894+ * @param integer $max_depth Depth limit
1895+ * @return null
1896+ */
1897+ public function remove_nested_quotes ($ max_depth )
1898+ {
1899+ global $ phpbb_container ;
1900+
1901+ if (preg_match ('#^<[rt][ >]# ' , $ this ->message ))
1902+ {
1903+ $ this ->message = $ phpbb_container ->get ('text_formatter.utils ' )->remove_bbcode (
1904+ $ this ->message ,
1905+ 'quote ' ,
1906+ $ max_depth
1907+ );
1908+
1909+ return ;
1910+ }
1911+
1912+ // Capture all [quote] and [/quote] tags
1913+ preg_match_all ('( \\[/?quote(?:="(.*?)")?: ' . $ this ->bbcode_uid . '\\]) ' , $ this ->message , $ matches , PREG_OFFSET_CAPTURE );
1914+
1915+ // Iterate over the quote tags to mark the ranges that must be removed
1916+ $ depth = 0 ;
1917+ $ ranges = array ();
1918+ $ start_pos = 0 ;
1919+ foreach ($ matches [0 ] as $ match )
1920+ {
1921+ if ($ match [0 ][1 ] === '/ ' )
1922+ {
1923+ --$ depth ;
1924+ if ($ depth == $ max_depth )
1925+ {
1926+ $ end_pos = $ match [1 ] + strlen ($ match [0 ]);
1927+ $ length = $ end_pos - $ start_pos ;
1928+ $ ranges [] = array ($ start_pos , $ length );
1929+ }
1930+ }
1931+ else
1932+ {
1933+ ++$ depth ;
1934+ if ($ depth == $ max_depth + 1 )
1935+ {
1936+ $ start_pos = $ match [1 ];
1937+ }
1938+ }
1939+ }
1940+
1941+ foreach (array_reverse ($ ranges ) as $ range )
1942+ {
1943+ list ($ start_pos , $ length ) = $ range ;
1944+ $ this ->message = substr_replace ($ this ->message , '' , $ start_pos , $ length );
1945+ }
1946+ }
1947+
19071948 /**
19081949 * Setter function for passing the plupload object
19091950 *
0 commit comments