Page 1 of 1

Replace br in bbcode

Posted: Fri Jan 31, 2020 10:36 am
by Elya
Hello,
I have a few questions about regex and the database.

I have noticed than in my old phpBB forum, every bbcode field in the database was automatically filled with regular expressions : first_pass_match, first_pass_replace, second_pass_match, second_pass_replace.
In the new version the "replace" fields are empty and the "match" fields contains "/(?!)/" .

The BBcode I'm currently doing is like this:

Code: Select all

[gallery={INTTEXT1} ipl={NUMBER1} thumb={NUMBER2}]{INTTEXT2}[/gallery]
INTTEXT1 contains folders separated with commas;
NUMBER1 contains a number/integer
NUMBER2 contains a number/boolean
INTTEXT2 contains a list of images separated with a comma.

I made a javascript code which transforms all this into proper html and it works.

There is something that bothers me though, if there is a line break in the image list (INTTEXT2), the bbcode doesn't work, it just displays as is as long as I don't find and manually remove the line break. I thought maybe by using the first_pass_match/first_pass_replace fields in the database I could automatically get rid of the line breaks, but since the old regexes from the previous phpBB versions don't seemed to be used anymore in the new version, I'm not sure if it's the right approach or if it would even work? If it would, I need help with the syntax.

I found this in an old bbcode:

first_pass_match:

Code: Select all

!\[test\](.*?)\[/test\]!ies
first_pass_replace:

Code: Select all

'[test:$uid]'.str_replace(array("\r\n", '\"', '\'', '(', ')'), array("\n", '"', ''', '(', ')'), trim('${1}')).'[/test:$uid]'
Can I use something like this? Thanks.

Re: Replace br in bbcode

Posted: Tue Feb 04, 2020 3:17 pm
by Elya
Hi, can someone help me? Thanks :)

Re: Replace br in bbcode

Posted: Tue Feb 04, 2020 3:40 pm
by mrgoldy
The reason the BBCode displays as is, is cause the line break falls outside the {INTTEXT} characters. Therefore it does not match and displays the BBCode as is. Changing it to a regular {TEXT} will solve the problem in the future, but post texts will have to be reparsed.

Do you have an estimate of how many posts contain this BBCode? 10, 100, 1000, etc.. ?

Re: Replace br in bbcode

Posted: Wed Feb 05, 2020 2:52 pm
by Elya
Thank you for your help.
Currently there are very few posts that contains this BBcode because I'm still working on it and still making changes, but I'd like to start using it soon.

What exactly are the risks if I use {TEXT} instead of {INTTEXT} ? This BBcode will not appear in the posting page, it is intended only for admin use, but I get several warnings everytime I edit the BBcode, warning me about risks related to {TEXT} and even {INTTEXT} so it makes me wonder.

I've just tried the BBcode with TEXT instead of INTTEXT. The BBcode doesn't show as is anymore, it just doesn't show at all and I have a JS error "unescaped line break". So it seems like nothing is handling those line breaks, and isn't that normal after all? I mean, normal text {TEXT} is supposed to contain line breaks, doesn't it?

That's why I wanted to know if there was something I could do in the first_pass_match/replace etc in the database so a regex would solve this. Right now there is a default value "/(?!)/" and I don't know what it does.

Re: Replace br in bbcode

Posted: Wed Feb 05, 2020 2:55 pm
by JoshyPHP
Elya wrote:
Wed Feb 05, 2020 2:52 pm
What exactly are the risks if I use {TEXT} instead of {INTTEXT} ?
It entirely depends on the BBCode's template. IIRC, the "not safe" warning in phpBB 3.3 is fairly accurate.

Re: Replace br in bbcode

Posted: Wed Feb 05, 2020 3:00 pm
by Elya
JoshyPHP wrote:
Wed Feb 05, 2020 2:55 pm
It entirely depends on the BBCode's template. IIRC, the "not safe" warning in phpBB 3.3 is fairly accurate.
Here is my code:

BBcode usage:

Code: Select all

[myBBCode={INTTEXT1} ipl={NUMBER1} thumb={NUMBER2}]{TEXT}[/myBBcode]
HTML replacement:

Code: Select all

<script type="text/javascript">
    myFunction("{INTTEXT1}","{TEXT}",{NUMBER2},{NUMBER1});
</script>

Re: Replace br in bbcode

Posted: Wed Feb 05, 2020 3:26 pm
by JoshyPHP
Yeah, this is unsafe. Apart from pure numbers I don't recommend using user input in scripts directly.

You can use a data attribute to store the user input safely, e.g.

Code: Select all

<script data-thing="{TEXT}">
alert(document.currentScript.dataset.thing);
</script>
It doesn't mean your script is safe though. If you build HTML based on user input your script may be susceptible to any kinds of trickery.

Re: Replace br in bbcode

Posted: Wed Feb 05, 2020 3:42 pm
by Elya
I understand and I'll take a deeper look at the data thing.

I'd like to understand the regex part in the database though, can somebody explain to me how it works? What I mean is, if there is the right regex for my BBcode in the database, could the line break removal work whether it is {TEXT} or {INTTEXT} ?

Re: Replace br in bbcode

Posted: Wed Feb 05, 2020 4:10 pm
by JoshyPHP
The regexp was used in 3.1, left unused in 3.2 and removed in 3.3. It has no effect.

Re: Replace br in bbcode

Posted: Wed Feb 05, 2020 4:34 pm
by Elya
OK, so I have no way to apply a regex to my BBcode then?

Re: Replace br in bbcode

Posted: Thu Feb 06, 2020 6:24 pm
by JoshyPHP
There are many ways to do many different things but trying to apply(?) some regexp replacement(?) may not be the right way to do whatever it is you're trying to do.

Re: Replace br in bbcode

Posted: Tue Feb 11, 2020 8:08 pm
by Elya
What I am trying to do is to process a list of images and in case there are line breaks, remove them so the bbcode can transform it into HTML. Is there a way to do this without applying a regex? JS doesn't work.

Re: Replace br in bbcode

Posted: Tue Feb 11, 2020 8:31 pm
by JoshyPHP
You're giving very little information about what you're actually doing so it's impossible to say without spending a lot of time covering every possibility.

If you output things in a <pre> element, line breaks won't be converted to <br> but I don't know whether that's your issue there.

Re: Replace br in bbcode

Posted: Fri Feb 14, 2020 9:02 am
by Elya
I'll try to make it clearer then.

This bbcode calls a JS file that builds an image gallery from a few parameters such as the name of the folder(s) which contains the images on the server, the number of images per line, and the list of the images themselves.

The list of images should look like this : img1.jpg, img2.png, img3.jpg
Each image separated with a comma. But if there is a single line break in this list, the bbcode doesn't work. This list was originally into a {INTTEXT} variable and then I switched to {TEXT} but it didn't solve the problem.

In my JS function I tried to do something like this:

Code: Select all

var x = imageList.replace(/\s/g,'').replace(/(\r\n|\n|\r)/gm,'').replace(/&lt;br&gt;/g,'');
But it doesn't work. How can I make this work?

I can't use pre since the JS code builds HTML to display an image gallery.