Welcome Guest, Not a member yet? Register   Sign In
Using PHPbb3's Authentication inside CodeIgniter
#1

[eluser]Ryan Pharis[/eluser]
I recently had the need to use PHPbb3 and CodeIgniter together and wanted to use PHPbb's authentication to make things easy.
I made a little library to help with that implementation:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* PHPbb CI Auth Class
*
* Authentication library for CodeIgniter that integrates with PHPbb (Use PHPbb's authentication system inside your CI apps)
*
* @author        Ryan Pharis
* @version        0.1
* @link        http://ryanpharis.com
* @credits    Modified code from http://www.mrkirkland.com/adding-a-user-to-phpbb3-from-an-external-script
*/

class Phpbb_ci_auth
{

    function Phpbb_ci_auth()
    {
       define('IN_PHPBB', true);
      //set scope for variables required later
      global $phpbb_root_path;
      global $phpEx;
      global $db;
      global $config;
      global $user;
      global $auth;
      global $cache;
      global $template;

      //your php extension
      $phpEx = substr(strrchr(__FILE__, '.'), 1);
      $phpbb_root_path = './forums/';

      //includes all the libraries etc. required
      require($phpbb_root_path .'common.php');
      $user->session_begin();
      $auth->acl($user->data);

      $this->user_data = $user->data;

        $this->ci =& get_instance();

        log_message('debug', 'PPHbb CI Auth Initialized');
    }

    function check_permissions()
    {
       if (!$this->is_logged_in()) {
          ci_redirect('forums/ucp.php?mode=login','location');
       }
       if (!$this->is_admin()) {
          $this->ci->session->set_flashdata('message', 'NO SOUP FOR YOU!');
          ci_redirect('','location');
       }
    }

    function is_admin()
    {
       if ($this->user_data['group_id'] == 5) {
          return true;
       }
    }

    function is_logged_in()
    {
      if ($this->user_data['group_id'] == 2 || $this->user_data['group_id'] == 3 || $this->user_data['group_id'] == 4 || $this->user_data['group_id'] == 5) {
         return true;
      }
    }

    function get_user_data()
    {
       return $this->user_data;
    }

} //end Class Phpbb_ci_auth

The only problem I ran into was that phpbb uses a function called "redirect" and as you know, CI has a redirect function in the url helper. So, as a compromise, I changed the CI code to ci_redirect() and just have to remember to change that upon upgrading to a new CI version.

I have my phpbb install under a directory within my CI site (on the same level as application) and called it "forums" so if you have a different folder name that you place phpbb in, just change that in the constructor for $phpbb_root_path.

Just thought I'd put that up there if anyone needs or wants to use phpbb inside of CI.

- Ryan Pharis
#2

[eluser]vecima[/eluser]
Thanks for sharing this Library. It gave me the foothold I needed to do what I wanted to do, which was to use the phpBB3 user/session/login system on my main site.

I made only slight modifications to your library, adding a login / logout method. You could just as easily utilize the phpBB3 login / logout methods, except that if you are running locally, (localhost) you need to be sure that phpBB3 is set to use that as it's cookie domain. If you install phpBB3 to your localhost it's probably fine, but if not (I copied my live forum down to my xampplite) then you need to set it.

I added these 2 methods to the library:
Code:
function logout()
{
    global $user;
    
    $user->session_kill();
    $user->session_begin();
}

function login($username, $password, $autologin, $admin)
{
    // use this only for localhost!!
    global $config;
    $config['cookie_domain'] = 'localhost';
    
    global $auth;
    
    // Attempt authorization.  result can be used to send messages or perform
    // loggin success / fail logic.
    $result = $auth->login($username, $password, $autologin, true, $admin);
}

notice in the login function that the lines with $config are only needed in the case of having a forum on localhost, with it's cookie_domain set wrong (you can test this by logging into the forum on localhost, then check your cookies to see if there are any phpbb3 cookies under "localhost").

in my base controller (that all others inherit from) I defined login and logout methods to use CI form validation:

Code:
function login($admin = FALSE)
{    
    //validate the form input
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $this->form_validation->set_rules('username', 'Username', 'required|xss_clean|prep_for_form|encode_php_tags');
    $this->form_validation->set_rules('password', 'Password', 'required|xss_clean|prep_for_form|encode_php_tags');
    $this->form_validation->set_rules('redirect', 'Redirect', 'required|xss_clean|prep_for_form|encode_php_tags');
    
    if ($this->form_validation->run() == FALSE)
    {
        ci_redirect($this->input->post('redirect', TRUE));
    }
    else
    {
        $username = $this->input->post('username', TRUE);
        $password = $this->input->post('password', TRUE);
        $autologin = $this->input->post('autologin', TRUE);
        $this->phpbb_ci_auth->login($username, $password, $autologin, $admin);
        ci_redirect($this->input->post('redirect', TRUE));
    }
}

function logout()
{
    //validate the form input
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $this->form_validation->set_rules('redirect', 'Redirect', 'required|xss_clean|prep_for_form|encode_php_tags');
    
    if ($this->form_validation->run() == FALSE)
    {
        ci_redirect($this->input->post('redirect', TRUE));
    }
    else
    {
        $this->phpbb_ci_auth->logout();
        ci_redirect($this->input->post('redirect', TRUE));
    }
}

and my login view that's shown if you're not logged in has a form:

Code:
&lt;form action="&lt;?=base_url();?&gt;index.php/&lt;?=$controller;?&gt;/login" method="post"&gt;
    <a href="&lt;?=base_url();?&gt;index.php/&lt;?=$controller;?&gt;/login">Login</a>&nbsp; &bull; &nbsp; <a href="&lt;?=base_url();?&gt;PHPBB_FORUM_PATH/ucp.php?mode=register">Register</a>
    <fieldset>
        &lt;?php echo form_error('redirect'); ?&gt;
        &lt;?php echo form_error('username'); ?&gt;
        <label for="username">Username:</label>&nbsp;
        &lt;input type="text" name="username" id="username" size="10" title="Username" /&gt;
        &lt;?php echo form_error('password'); ?&gt;
        <label for="password">Password:</label>&nbsp;
        &lt;input type="password" name="password" id="password" size="10" title="Password" /&gt;
        <label for="autologin">Log me on automatically each visit &lt;input type="checkbox" name="autologin" id="autologin" /&gt;&lt;/label>
        &lt;input type="hidden" name="redirect" value="&lt;?=base_url();?&gt;index.php/&lt;?=$controller;?&gt;" /&gt;
        &lt;input type="submit" name="login" value="Login" /&gt;
    </fieldset>
&lt;/form&gt;

hopefully I was clear in my description. If not feel free to ask questions. Also feel free to call me out on "crap code" and make a better version - I just wanted it to work, but if you can do it better, by all means show us how! Wink
#3

[eluser]FlashUK[/eluser]
Thank you for this useful post!

I have successfully used this method to help integrate phpBB3 into Zend Framework 1.9.2 (sadly not CodeIgniter).
I will be sharing mode code on my blog which is a direct adaption of this. Thanks for sharing your code!

This is probably quickest and easiest method to get phpBB3 up and running in your framework.
#4

[eluser]Del Spooner[/eluser]
Hello!
First, thanks for the class to you both, it's just what I needed.

I'm new to CI and I'm not still sure on how to deal with the views. I have my template with separate header/body/footer, but could you provide an example of how to integrate your view (that is, the form)?
I'm trying right now, and I can integrate the login into a sliding panel that I set for that purpose, but when I try to login nothing happens. Also, in the 'register' link, this is the url that points to 'http://localhost/PHPBB_FORUM_PATH/ucp.php?mode=register', there's a constant there that didn't load so I guess I'm not correctly loading the view :S

Could you show the way to a noob like me?
Thanks for your time =)




Theme © iAndrew 2016 - Forum software by © MyBB