Page 1 of 5

Extensions and phpBB 3.2: you should probably read this topic

Posted: Sun Apr 12, 2015 9:16 pm
by JoshyPHP
In phpBB 3.2, all of the BBCodes, smilies and other text formatting has been replaced by this library: s9e\TextFormatter. Posts and other rich text fields are stored in a different format, many things have changed and some extensions will need to be updated to continue to function properly with phpBB 3.2. Before describing what's changed, here's what has not changed:
  • If your extension does not modify posts, you probably won't have to update it for it to work on 3.2.
  • The functions decode_message(), generate_text_for_display(), generate_text_for_edit(), generate_text_for_storage() and strip_bbcode() work the same way.
  • Most custom BBCodes are compatible.
  • If your extension only modifies text through the core.modify_text_for_display_after event, there's a good chance it will work without any change.
Posts in phpBB 3.1 are stored as HTML, with BBCodes and some other features being replaced at rendering time. In phpBB 3.2, new posts are stored as XML and transformed into HTML at rendering time. This has multiple implications:
  1. It is unwise to modify posts in the database with string functions such as str_replace() or preg_replace().
  2. You cannot inject HTML into a post and have it be displayed as-is.
If you are currently developing an extension that either reads or modifies rich text and you want to make sure that it will work in phpBB 3.2, post a link to your extension here with a description of how it interacts with rich text. I will personally look into it and outline how it needs to be updated, if at all. Some documentation and examples are already available in the library's doc and more will be added based on feedback.

I will update this topic as time permits. What subject I will cover depends heavily on feedback from extension authors.

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Fri May 01, 2015 7:01 pm
by hanelyp
viewtopic.php?f=456&t=2306161
uses events:
core.modify_text_for_display_before to catch text before normal bbcode processing. Matches $event['text'] against
'#\[dice\sseed=(\d+)\ssecure=(\w+):?\w*\](.+?)\[/dice\]#i'
for replacement.

core.posting_modify_message_text to inspect for a specific tag at posting time, again before bbcode processing. Matches $event['message_parser']->message against
'#\[dice\sseed=(\d+)\ssecure=(\w+):?\w*\](.+?)\[/dice\]#i'

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Sat May 02, 2015 1:17 pm
by brunoais
Is that a question...?

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Mon May 04, 2015 4:52 am
by JoshyPHP
hanelyp wrote:viewtopic.php?f=456&t=2306161
uses events:
I looked into it but it's hard to make sense of it. The biggest problem is that you inject your PHP inside the BBCode definition rather than modify the text via an event.

If you want to modify what the user posts, you should probably do it in core.posting_modify_message_text.
For the template, don't inject PHP in second_pass_replace. How about outputting a simple <div> with your metadata as data- attributes then use events like core.modify_text_for_display_after and core.modify_format_display_text_after to fully render the div?

You could have a custom BBCodes such as this:

Code: Select all

[dice seed={NUMBER} secure={IDENTIFIER}]{TEXT}[/dice]

Code: Select all

<div data-seed="{NUMBER}" secure="{IDENTIFIER}">{TEXT}</div>
Then you would use the events mentionned above to add your dice roll to the HTML with preg_replace_callback() using the values in the div's attributes.

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Wed May 06, 2015 8:34 pm
by Neuropass
The usual confusion here with members. I see no ranks on JoshyPHP (no offense) meaning to me is a regular Joe posting something that might be true and might be not. These sorta of notifications should come from a designed phpBB team member or a what about a phpBB info Bot? Shouldn't be so hard. Yet this happens all the time. Stick to One source, make it official , follow a method. Thank-you.

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Wed May 06, 2015 9:19 pm
by bonelifer
Neuropass, a team member stickied this, regular members don't have the ability to make posts stickies. OFFICIAL. Second, JoshyPHP, is the code contributor for s9e\TextFormatter.

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Fri Sep 11, 2015 7:14 pm
by JoshyPHP
marcovo asked me to take a look at his Hide BBcode extension.

The extension adds a BBCode whose content is only displayed to some users. The BBCode's template displays the hidden text between HTML comments that serve as markers, and the extension listens for the core.modify_text_for_display_after and core.modify_format_display_text_after events to remove the text to hide it from unauthorized users. Those events are triggered after the BBCodes have been rendered as HTML so unless I missed something that extension should work in 3.2 without any modifications.

In phpBB 3.2 however, there is a better--albeit not backward compatible--way to display content differently depending on the user: conditionals and template parameters. For instance, the [HIDE] BBCode could be defined as follow:

Code: Select all

[HIDE]{TEXT}[/HIDE]

Code: Select all

<xsl:if test="$SHOW_HIDDEN_POSTS">{TEXT}</xsl:if>
That template will only display its content if $SHOW_HIDDEN_POSTS is true-ish, e.g. anything but an empty string or 0. Template parameters can be set during the core.text_formatter_s9e_renderer_setup or core.text_formatter_s9e_render_before events using the following:

Code: Select all

$renderer->get_renderer()->setParameter('SHOW_HIDDEN_POST', true);

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Fri Jul 22, 2016 11:37 am
by «THÖMÅS®©»
What does this mean for phpBB users running 3.0.x and 3.1.x?

Does this mean that an entire website that may contain thousands of posts may have to be started from scratch or is there a plan for an auto updater for this?

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Fri Jul 22, 2016 11:44 am
by david63
«THÖMÅS®©» wrote:What does this mean for phpBB users running 3.0.x and 3.1.x?

Does this mean that an entire website that may contain thousands of posts may have to be started from scratch or is there a plan for an auto updater for this?
If you read the OP it is talking about extensions, hence the reason it is in Extension Writers Discussion. So if you are developing any extensions you need to take note of what is being said - otherwise there is no problem

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Sun Sep 25, 2016 3:05 pm
by kasimi
The mChat extension has an option in the ACP to disallow BBCodes to be posted. For example the admin specifies b|u|code and when a user then sends the message

Code: Select all

[b]123[/b]
the BBCode should be removed and the message 123 should be stored in the database. The code that currently does this for 3.1 is admittedly not the cleanest but it gets the job done:

Code: Select all

$message = '[b]123[/b]';

$bbcode_replace = array(
    '#\[(' . str_replace('*', '\*', $this->config['mchat_bbcode_disallowed']) . ')[^\[\]]+\]#Usi',
    '#\[/(' . str_replace('*', '\*', $this->config['mchat_bbcode_disallowed']) . ')[^\[\]]+\]#Usi',
);

$message = preg_replace($bbcode_replace, '', $message); // '123'   
What's the best approach for 3.2?

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Sun Sep 25, 2016 3:15 pm
by JoshyPHP
That would be through the textformatter.parser service using the disable_bbcode() method.

Look for parser_interface in https://area51.phpbb.com/docs/code/3.2.x/

Code: Select all

$parser = $phpbb_container->get('textformatter.parser');
$parser->disable_bbcode('b');
$parser->disable_bbcode('u');
$parser->disable_bbcode('code'); 

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Sun Sep 25, 2016 3:35 pm
by kasimi
Works like charm, thank you.

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Mon Sep 26, 2016 9:00 pm
by kasimi
I have to correct myself, it works most of the time. It works with all the BBCodes that I've tested except for images:

Code: Select all

$this->parser->disable_bbcode('img');

$message = '[img]https://images.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png[/img]';

generate_text_for_storage($message, ...)
Posting this $message still shows the image. Any ideas?

Oh nevermind. phpBB's message_parser:parse() method overwrites the isDisabled flag of the [img] BBCode with the $allow_img_bbcode parameter passed to it.

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Mon Sep 26, 2016 9:10 pm
by JoshyPHP
You could also just use the parser service directly to parse the text.

Re: Extensions and phpBB 3.2: you should probably read this topic

Posted: Mon Sep 26, 2016 9:40 pm
by kasimi
generate_text_for_storage() uses phpBB's message_parser class that - in addition to parsing the message - checks for maximum message length, removes quotes that are nested too deep and more. I'd have to do all that myself if I use the parse() method, correct?

Apart from that, does that mean that $uid, $bitfield and $options that are passed to generate_text_for_storage() are obsolete in 3.2.x and the return value of $this->parser->parse($message) is the only thing required for displaying the message later?