Intro
This document will cover how to integrate with phpBB 3's BBCode system, this can useful if you are building a CMS and you are using phpBB 3 as your back end.
This tutorial will not cover setting up phpBB 3 or the basics of PHP, also please note there may well be better ways of doing this but it worked for me
The database
To start I have created a table in the phpBB database called "cms_test" with the following fields.
cms_id
cms_title
cms_body
bbcode_bitfield
bbcode_uid
The 3 Main Functions
generate_text_for_display
Used to convert the BBCode into HTML for display here is an example of its use;
//Get the page id var i.e. cms.php?id=2
if (isset($HTTP_GET_VARS['id'])) {
$id = $HTTP_GET_VARS['id'];
{
//Get the page data from the db
$page_sql = mysql_query("SELECT cms_body, bbcode_uid, bbcode_bitfield FROM cms_test WHERE cms_id = ".$id);
$row_details = mysql_fetch_row($page_sql);
$cms_body = $row_details[0];
$bbcode_uid = $row_details[1];
$bbcode_bitfield = $row_details[2];
//Build the flags on what is allowed
$allow_bbcode = true;
$allow_urls = true;
$allow_smilies = true;
$flags = (($allow_bbcode) ? 1 : 0) + (($allow_smilies) ? 2 : 0) + (($allow_urls) ? 4 : 0);
echo generate_text_for_display($cms_body, $bbcode_uid, $bbcode_bitfield, $flags);
generate_text_for_storage
Used for preparing the page for storage in the database, here is an example of its use;
//Set what to isert into the database
$cms_title = "Test Title";
$cms_body = "[b]Test body in bold[/b]";
//Prepare BB Code
$uid = $bitfield = $flags = ''; // will be modified by generate_text_for_storage
$allow_bbcode = true; // false is default for generate_text_for_storage function
$allow_urls = true; // false is default for generate_text_for_storage function
$allow_smilies = true; // false is default for generate_text_for_storage function
generate_text_for_storage ($cms_body, $uid, $bitfield, $flags, $allow_bbcode, $allow_urls, $allow_smilies);
mysql_query("INSERT INTO cms_test (cms_title, cms_body, bbcode_uid, bbcode_bitfield) VALUES ('".$cms_title."', '".$cms_body."', '".$uid."', '".$bitfield."')") or die(mysql_error());
decode_message
Used on the editing page to display the BBCode for editing, here is an example;
//Get the page id var i.e. cms.php?id=2
if (isset($HTTP_GET_VARS['id'])) {
$id = $HTTP_GET_VARS['id'];
{
//Get the page data from the db
$page_sql = mysql_query("SELECT cms_body, bbcode_uid FROM cms_test WHERE cms_id = ".$id);
$row_details = mysql_fetch_row($page_sql);
$cms_body = $row_details[0];
$bbcode_uid = $row_details[1];
decode_message($cms_body, $bbcode_uid);
echo $cms_body;
Displaying the phpBB3 BBCode Buttons on Your Form
On Your Template File
Somewhere on your template file add the following;
<script type="text/javascript">
// <![CDATA[
var form_name = 'form_add_edit';
var text_name = 'cms_body';
// ]]>
</script>
Define "form_name" as your form name and "text_name" as the text area that users will use to enter the cms_body details.
In a table Where you want the buttons to be displayed enter;
<!-- INCLUDE posting_buttons.html -->
For example;
<script type="text/javascript">
// <![CDATA[
var form_name = 'form_add_edit';
var text_name = 'cms_body';
// ]]>
</script>
<table width="100%" class="row2">
<!-- INCLUDE posting_buttons.html -->
<tr valign="top">
<td valign="top" class="row2" colspan="2" width="90%">
<textarea name="cms_body" cols="60" rows="25">{CMS_BODY}<textarea>
</td>
</tr>
</table>
On Your PHP File
On you php file add the following;
include($phpbb_root_path . 'language/en/posting.' . $phpEx);
//Add the buttons for custom BBCodes
display_custom_bbcodes();
decode_message($cms_body, $bbcode_uid);
// HTML, BBCode, Smilies, Images and Flash status
$bbcode_status = ($config['allow_bbcode']) ? true : false;
$smilies_status = ($bbcode_status && $config['allow_smilies']) ? true : false;
$img_status = ($bbcode_status) ? true : false;
$url_status = ($config['allow_post_links']) ? true : false;
$flash_status = ($bbcode_status && $config['allow_post_flash']) ? true : false;
$quote_status = true;
$template->assign_vars(array(
'CMS_BODY' => $cms_body,
'S_BBCODE_ALLOWED' => $bbcode_status,
'S_BBCODE_IMG' => $img_status,
'S_BBCODE_URL' => $url_status,
'S_BBCODE_FLASH' => $flash_status,
'S_BBCODE_QUOTE' => $quote_status,
'S_HIDE_COLOR' => true,
'S_LINKS_ALLOWED' => $url_status,
'LA_BBCODE_B_HELP' => $lang['BBCODE_B_HELP'],
'LA_BBCODE_I_HELP' => $lang['BBCODE_I_HELP'],
'LA_BBCODE_U_HELP' => $lang['BBCODE_U_HELP'],
'LA_BBCODE_Q_HELP' => $lang['BBCODE_Q_HELP'],
'LA_BBCODE_C_HELP' => $lang['BBCODE_C_HELP'],
'LA_BBCODE_L_HELP' => $lang['BBCODE_L_HELP'],
'LA_BBCODE_O_HELP' => $lang['BBCODE_O_HELP'],
'LA_BBCODE_P_HELP' => $lang['BBCODE_P_HELP'],
'LA_BBCODE_W_HELP' => $lang['BBCODE_W_HELP'],
'LA_BBCODE_A_HELP' => $lang['BBCODE_A_HELP'],
'LA_BBCODE_S_HELP' => $lang['BBCODE_S_HELP'],
'LA_BBCODE_F_HELP' => $lang['BBCODE_F_HELP'],
'LA_BBCODE_E_HELP' => $lang['BBCODE_E_HELP'],
'LA_BBCODE_D_HELP' => $lang['BBCODE_D_HELP'],
'L_FONT_SIZE' => $lang['FONT_SIZE'],
'L_FONT_TINY' => $lang['FONT_TINY'],
'L_FONT_SMALL' => $lang['FONT_SMALL'],
'L_FONT_NORMAL' => $lang['FONT_NORMAL'],
'L_FONT_LARGE' => $lang['FONT_LARGE'],
'L_FONT_HUGE' => $lang['FONT_HUGE'],
'L_STYLES_TIP' => $lang['STYLES_TIP'])
);
Add new comment