Forum Integration in a CodeIgniter website

Published On: 22 December 2012.By .
  • Digital Engineering

 

Months back I was trying to integrate phpbb in codeigniter based website. I didn’t want that the users register for the forum. Site login/register controllers should handle the forum part as well.

I din’t find any good help from CI community forums. I found one CI library at https://github.com/EllisLab/CodeIgniter/wiki/phpBB3-library, but then it din’t fulfilled all my needs. The Library is too good. But is all about getting info from phpbb session to CI, I had requirement where in backend of my application i need a single login for phpbb admin and CI user session data.

The only thing that was useful for me in that was login part. So I created a very simple library for CodeIgniter that manages remote login, remote user add, remote user edit (password change) and remote user delete.

Step1: Create a library Phpbb_bridge.php in libraries folder. And place the following code:

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

 

/**

* CodeIgniter phpBB Bridge

*/

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

require_once(‘forum/’ . ‘common.’ . $phpEx);

require_once(‘forum/’ . ‘config.’ . $phpEx);

require_once(‘forum/’ . ‘includes/functions_user.’ . $phpEx);

require_once(‘forum/’ . ‘includes/functions_display.’ . $phpEx);

require_once(‘forum/’ . ‘includes/functions_privmsgs.’ . $phpEx);

require_once(‘forum/’ . ‘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,$grp)

{

$user_row = array(

‘username’ => $username,

‘user_password’ => phpbb_hash($password),

‘user_email’ => $email,

‘group_id’ => $grp, // 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();

return;

}

 

/**

* @param $user_id

* @return unknown_type

*/

public function user_delete($user_id)

{

return user_delete(‘remove’, $user_id, false);

}

}

Step 2:

The next issue that came up was the duplication of redirect function in both CI and phpBB. So I had to decide which way to go to rename the function and I decided to rename the function in CI as it was easy for me to handle since forum’s code was heavy and any mistake in core phpBB installation could break the code. Now, since I renamed CI redirect function ( as ciredirect()) , I had to replace redirect() from all the places where I used redirect() in my controllers with the ciredirect() function.This isn’t exactly ‘quick’, but should be workable in a couple of hours if you sat down determined to do it. And, you end up with decent reusable code that shouldn’t be difficult to maintain so you never have to worry about it again.

Step3:

After solving this issue there were few cache class related conflicts that came up.Solution was to rename the cache class in phpBB from cache to cache_phpBB. The files that need changes are:

./common.php , ./style.php , ./download/file.php and ./includes/cache.php

Step4:

One more thing to add: User edit function was not available in phpBB3, so I defined one in includes/functions_user.php

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;

}

Doing all this solved my Single-Sign On problem, now I was able to register/add/login/delete/edit users in forum too from my CI website.

Mitali Patwari

Related content

That’s all for this blog