@@ -43,7 +43,7 @@ protected function add_missing_fields(array $record)
4343 if (!isset ($ record ['enable_bbcode ' ], $ record ['enable_smilies ' ], $ record ['enable_magic_url ' ]))
4444 {
4545 $ record += array (
46- 'enable_bbcode ' => ! empty ($ record[ ' bbcode_uid ' ] ),
46+ 'enable_bbcode ' => $ this -> guess_bbcodes ($ record ),
4747 'enable_smilies ' => $ this ->guess_smilies ($ record ),
4848 'enable_magic_url ' => $ this ->guess_magic_url ($ record ),
4949 );
@@ -58,6 +58,13 @@ protected function add_missing_fields(array $record)
5858 $ record [$ field_name ] = $ this ->guess_bbcode ($ record , $ bbcode );
5959 }
6060
61+ // Magic URLs are tied to the URL BBCode, that's why if magic URLs are enabled we make sure
62+ // that the URL BBCode is also enabled
63+ if ($ record ['enable_magic_url ' ])
64+ {
65+ $ record ['enable_url_bbcode ' ] = true ;
66+ }
67+
6168 return $ record ;
6269 }
6370
@@ -74,7 +81,7 @@ protected function guess_bbcode(array $record, $bbcode)
7481 {
7582 // Look for the closing tag, e.g. [/url]
7683 $ match = '[/ ' . $ bbcode . ': ' . $ record ['bbcode_uid ' ];
77- if (stripos ($ record ['text ' ], $ match ) !== false )
84+ if (strpos ($ record ['text ' ], $ match ) !== false )
7885 {
7986 return true ;
8087 }
@@ -84,8 +91,8 @@ protected function guess_bbcode(array $record, $bbcode)
8491 {
8592 // Look for the closing tag inside of a e element, in an element of the same name, e.g.
8693 // <e>[/url]</e></URL>
87- $ match = '<e>[/ ' . $ bbcode . ']</e></ ' . $ bbcode . '> ' ;
88- if (stripos ($ record ['text ' ], $ match ) !== false )
94+ $ match = '<e>[/ ' . $ bbcode . ']</e></ ' . strtoupper ( $ bbcode) . '> ' ;
95+ if (strpos ($ record ['text ' ], $ match ) !== false )
8996 {
9097 return true ;
9198 }
@@ -94,6 +101,33 @@ protected function guess_bbcode(array $record, $bbcode)
94101 return false ;
95102 }
96103
104+ /**
105+ * Guess whether any BBCode is in use in given record
106+ *
107+ * @param array $record
108+ * @return bool
109+ */
110+ protected function guess_bbcodes (array $ record )
111+ {
112+ if (!empty ($ record ['bbcode_uid ' ]))
113+ {
114+ // Test whether the bbcode_uid is in use
115+ $ match = ': ' . $ record ['bbcode_uid ' ];
116+ if (strpos ($ record ['text ' ], $ match ) !== false )
117+ {
118+ return true ;
119+ }
120+ }
121+
122+ if (substr ($ record ['text ' ], 0 , 2 ) == '<r ' )
123+ {
124+ // Look for a closing tag inside of an e element
125+ return (bool ) preg_match ('(<e> \\[/ \\w+ \\]</e>) ' , $ match );
126+ }
127+
128+ return false ;
129+ }
130+
97131 /**
98132 * Guess whether magic URLs are in use in given record
99133 *
@@ -103,7 +137,7 @@ protected function guess_bbcode(array $record, $bbcode)
103137 protected function guess_magic_url (array $ record )
104138 {
105139 // Look for <!-- m --> or for a URL tag that's not immediately followed by <s>
106- return (strpos ($ record ['text ' ], '<!-- m --> ' ) !== false || preg_match ('(<URL [^>]++>(?!<s>)) ' , strpos ( $ row ['text ' ]) ));
140+ return (strpos ($ record ['text ' ], '<!-- m --> ' ) !== false || preg_match ('(<URL [^>]++>(?!<s>)) ' , $ record ['text ' ]));
107141 }
108142
109143 /**
@@ -114,7 +148,7 @@ protected function guess_magic_url(array $record)
114148 */
115149 protected function guess_smilies (array $ record )
116150 {
117- return (strpos ($ row ['text ' ], '<!-- s ' ) !== false || strpos ($ row ['text ' ], '<E> ' ) !== false );
151+ return (strpos ($ record ['text ' ], '<!-- s ' ) !== false || strpos ($ record ['text ' ], '<E> ' ) !== false );
118152 }
119153
120154 /**
@@ -143,8 +177,10 @@ protected function reparse_record(array $record)
143177 OPTION_FLAG_BBCODE | OPTION_FLAG_SMILIES | OPTION_FLAG_LINKS
144178 )
145179 );
180+ // generate_text_for_edit() and decode_message() actually return the text as HTML. It has to
181+ // be decoded to plain text before it can be reparsed
182+ $ parsed_text = html_entity_decode ($ unparsed ['text ' ], ENT_QUOTES , 'UTF-8 ' );
146183 $ bitfield = $ flags = null ;
147- $ parsed_text = $ unparsed ['text ' ];
148184 generate_text_for_storage (
149185 $ parsed_text ,
150186 $ unparsed ['bbcode_uid ' ],
0 commit comments