Integrating phpbb in a website based on CodeIgniter
I have noticed that at the community forums of CodeIgniter there are a lot of questions on integrating phpbb in a code ignited web site. However there is one library posted in the forums – here, but it didn’t fulfill my needs. Actually the only thing that was usefull for me is the login part, e.g. the constructor. It is pretty much the same so here I should thank Tomaž Muraus for that lead. My additions to it are the rest usefull functions. So I gave it a try and created a very simple library for CodeIgniter that manages remote login, remote user add, remote user edit (password change) and remote user delete. I will post it here and will cut it a little because the current state has some additional programming related to the project I did it for.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter phpBB3 Bridge
* @author Georgi Budinov, credits to Tomaž Muraus at http://www.tomaz-muraus.info
* @link georgi.budinov.com
*/
class Phpbb_bridge
{
public $CI;
protected $_user;
/**
* Constructor.
*/
public function __construct()
{
if (!isset($this->CI))
{
$this->CI =& get_instance();
}
// Set the variables scope
global $phpbb_root_path, $phpEx, $user, $auth, $cache, $db, $config, $template, $table_prefix;
$rootPath = $this->CI->config->item('root_path');
define('IN_PHPBB', TRUE);
define('FORUM_ROOT_PATH', $rootPath.'forum/');
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : FORUM_ROOT_PATH;
$phpEx = substr(strrchr(__FILE__, '.'), 1);
// Include needed files
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'config.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
// Initialize phpBB user session
$user->session_begin();
$auth->acl($user->data);
$user->setup();
// Save user data into $_user variable
$this->_user = $user;
}
/**
* @param $email
* @param $username
* @param $password
* @return unknown_type</pre>
*/
public function user_add($email, $username, $password)
{
$user_row = array(
'username' => $username,
'user_password' => phpbb_hash($password),
'user_email' => $email,
'group_id' => 2, // by default, the REGISTERED user group is id 2
'user_timezone' => (float) date('T'),
'user_lang' => 'bg',
'user_type' => USER_NORMAL,
'user_ip' => $_SERVER['REMOTE_ADDR'],
'user_regdate' => time(),
);
return user_add($user_row, false);
}
/**
* @param $username
* @param $password
* @return bool
*/
public function user_edit($username, $password)
{
return user_edit($username, $password);
}
/*
* Logins the user in forum
*/
public function user_login($username, $password)
{
$auth = new auth();
return $auth->login($username, $password);
}
public function user_logout()
{
$this->_user->session_kill();
$this->_user->session_begin();
}
/**
* @param $user_id
* @return unknown_type
*/
public function user_delete($user_id)
{
return user_delete('remove', $user_id, false);
}
}
One more thing to add here: the user_edit function is not available in phpbb3 so I defined one in includes/functions_user.php:
/**
* Change password
*
* @param string $username
* @param string $newPassword
* @return boolean
*/
function user_edit($username, $newPassword)
{
global $db, $user, $auth, $config, $phpbb_root_path, $phpEx;
if (empty($username) || empty($newPassword))
{
return false;
}
$sql = 'UPDATE ' . USERS_TABLE . ' SET user_password=\'' . $db->sql_escape(md5($newPassword)) . '\' WHERE username = \''.$db->sql_escape($username).'\'';
$db->sql_query($sql);
return true;
}
And last but not least you will have to decide wich way to go … there is duplication of the function redirect. I replaced it in the CodeIgniter framework as it was easier for me.
Enjoy!
UPDATE 1:
For versions of PHPBB greater or equal than 3.0.8. you must change the cache class name from cache to cache_phpbb. The files that need changes are:
./common.php , ./style.php , ./download/file.php and ./includes/cache.php
UPDATE 2:
In order to run the library correctly you must choose a different connection driver for the database from the one chosen for CI. For example mysql for CI and mysqli for PHPBB (set up in ./config.php)
This post is also available in: Bulgarian











English
Bulgarian 

Hi,
thank you very much for this post. i need this solution in my project and it works perfect.
what is $rootPath? can i get this value with an CI-Function?
Hi
I use this variable to store the path where the forum folder is located on server. So assuming your www folder is /var/www and your forum installation is in /var/www/forum … this variable should be equal to ‘/var/www/’
The user registraion module not working.
Can you supply more info in order to help you ?
i have done it by changing session file.can you help me in that how i automatically logged in phpbb using codeigniter login funtion.
in short what variables have to store in session so that when the user loggged in codeginter site he is also logged in phpbb forum.
Well you can call user_login($username, $password) from the library which will instantiate the auth object of the phpbb and will take care of that. A requirement is to pass not only the username but the password. You should do this at the event of logging in a user where she supplies the username and password.
i have done it .thanks for this great post and your response.
Glad to hear that from you
Thank you for putting this up. You saved me a great deal of time and effort!
Hi Georgi Budinov,
Thank you for a this library and the time taken to respond to this.
QUESTION: Does this library presume that you’re using the phpBB database as the primary database?
A LITTLE DETAIL: I installed this phpbb on an existing CI app, a db for the CI app and a db for phpbb and when I use the user_add method in the library I get an error stating that the table doesn’t exist in my CI app db.
Hi keron,
actually I am using 2 databases, too. Also I recommend that having in mind all possible vulnerabilities of phpbb …
Haven’t had this problem you are describing but I assume that probably the phpbb source code is not using its own db connection but your ci db connection.
There are two things you can do/check:
1. Version of phpbb I have used is 3.0.7-PL1
2. Try autoloading the library, if you are not doing that already
Hi,
Thanks for the awesome lib. It helped me to integrate CI app with phpbb. I have a query. I want to add link to phpbb’s admin link in my CI admin panel. But each time it must generate and append new SID.
I tried – $this->_admin_link = append_sid(“{$phpbb_root_path}adm/index.$phpEx”, false, true, $user->session_id); in library’s constructor. But it does not work. The sid has to be generated for each new request I guess. Please advise.
Well, I don’t have the time to look into this now … so you will have to digg that by yourself in the phpbb code. It shouldn’t be as difficult though …
If you have found already hte answer .. share the knowledge