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

    • Alex
    • April 23rd, 2011 10:08pm

    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?

  1. 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/’

    • mahmood
    • June 23rd, 2011 4:14pm

    The user registraion module not working.

    • mahmood
    • June 24th, 2011 1:43pm

    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.

      • how do you this in reverse?
        log into php bb, and get the Codeigniter session?

    • mahmood
    • June 24th, 2011 7:41pm

    i have done it .thanks for this great post and your response.

    • Shadow Caster
    • June 28th, 2011 2:12am

    Thank you for putting this up. You saved me a great deal of time and effort!

  2. 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

    • kiran aghor
    • April 23rd, 2012 6:03pm

    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 :)

    • zorg
    • June 8th, 2012 9:30pm

    hi and thank for this library but i have question. How do you integre the class auth for phpbb3 with class auth form ion auth ?

      • zorg
      • June 9th, 2012 4:21pm

      hi i deed all rename class auth in ion-auth, rename redirect, and i still get :
      Fatal error: Call to a member function user_login() on a non-object

      can you help?

    • Rob Wetzel
    • August 3rd, 2012 4:53am

    This is great! Thank you so much for doing this!

    • Rob Wetzel
    • August 3rd, 2012 5:02am

    One quick question: how would you check to see if the person is logged in? ie. $user->data['user_id'] Thanks!

  3. Hi, i have tried to follow your steps , however i keep coming across the error ” ( ! ) Fatal error: Call to a member function get() on a non-object in C:\wamp\www\sep\forum\includes\auth.php on line 40 ”

    I dont really understand why so because those seem to be internal BB files which i have not touched. Kindly help out

    • Chaitali
    • July 2nd, 2014 4:35pm

    Hi,

    I want to integrate phpbb to a codeigniter website. Now for users in website I have one separate table. Now can you help me that how can I loggedin in phpbb automatically when login in codeigniter site.

    Regards
    Chaitali

  4. The best part is that the integration of WiFi calling is system wide, with Apple mentioning EE at its iPhone 6 launch as one of the first providers to offer the service.

      • sud
      • July 1st, 2015 12:36pm

      best iPhone 6 cases for protection :
      On the other hand, S Voice missed once out of 10 of its tests on the Samsung Galaxy S6. This could be from any specific issue and doing only 10 tests isn’t enough to give a proper accuracy percentage but it is something to note.

      This is not the right forum

        • sud
        • July 1st, 2015 12:38pm

        yes true

        • sudeep
        • July 1st, 2015 12:39pm

        nestin checked

        • sudeep
        • July 1st, 2015 12:40pm

        reply to the paren

  5. On the other hand, S Voice missed once out of 10 of its tests on the Samsung Galaxy S6. This could be from any specific issue and doing only 10 tests isn’t enough to give a proper accuracy percentage but it is something to note.

    • Nerothos
    • October 2nd, 2015 7:42pm

    Hi,
    First, a big thank for this bridge that I’ve been using for a few years now.
    I have a question, do you plan to update it with phpBB 3.1 changes? I have an error with the line 228 on phpbb session.php file and want to know if you are working on a new bridge^^’

    Nerothos

  1. No trackbacks yet.