ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to Integrate Vbulletin object into codeigniter?

March 27, 2009 3:24pm

Subscribe [6]
  • #1 / Mar 27, 2009 3:24pm

    CroNiX

    4713 posts

    Ive written some custom software for a company for their needs of using vbulletin outside of vbulletin.  This was pretty easy to do.

    Basically you just:

    require_once(VBULLETIN_PATH . 'global.php');

    and then you have access to the vbulletin object and can use it like: $vbulletin->userinfo();

    I would like to port that app to CI as they now want to add a bunch of additional functionality and CI is the bomb.  How do I use this method in CI to load vbulletin?  I want it to be a global object like the rest of CI so I can have it autoloaded and access it from any controller using $this->vbulletin->xxx.  This is a bit beyond my CI knowledge as Ive only used CI to work with CI 😊

    Thanks!

  • #2 / Mar 29, 2009 2:43pm

    Shadab

    8 posts

    I was trying to achieve the same thing.
    But found out that vBulletin can’t be initialized from inside a function.

    [ Reference: http://www.vbulletin.org/forum/showthread.php?t=205765 ]

    Now trying another approach:
    ยป Initialize vBulletin and pass the $vbulletin object to a (CI) library somehow.

    Will report tomorrow.

  • #3 / Mar 29, 2009 3:02pm

    CroNiX

    4713 posts

    Thank you for the reply.  I also found that reference on vb.org and have since given up the approach of trying to use CI for this project.  No matter what you do, CI is an object and no matter where or how you try to bring vB into CI, CI is an object, so it won’t work….unless Im missing something and that is totally possible.  Ive only used CI for a few months on 2 projects and don’t profess to be an expert.

    This really sucks as I really wanted to use CI for this project to save a few days of coding, but the way vB is written, it won’t let you.

    It has to be done in a non-oop approach.

    If you discover something I missed, please do tell.  Even after 2 days of building a new “framework” to code for vB, I would throw it all away to be able to use CI.

  • #4 / Mar 29, 2009 3:45pm

    Shadab

    8 posts

    >> Initialize vBulletin and pass the $vbulletin object to a (CI) library somehow.

    I tried this approach and it worked. Though it required modifying the main (index.php) file.
    Solution:

    index.php
    Put this right after the opening PHP tag:

    $dir = getcwd();
    
    chdir('path/to/your/vb/installation/');
    require './global.php';
    
    chdir($dir);


    config.php
    Enable the hooks:

    $config['enable_hooks'] = TRUE;


    hooks.php
    Create a ‘post_controller_constructor’ hook (a pre_controller hook won’t work as the $CI object hasn’t been created at that point)

    $hook['post_controller_constructor'] = array(
                                            'class' => 'vBHook',
                                            'function' => 'init',
                                            'filename' => 'vbhook.php',
                                            'filepath' => 'hooks'
                                            );


    vbhook.php in /hooks/ folder

    <?php
    
    class vBHook {
        
        function init() {
            
            $CI =& get_instance();
            $CI->vbulletin = $GLOBALS['vbulletin'];
            unset($GLOBALS['vbulletin']);
        }
    }

    DONE! 😉

    I tested this hook (or hack?) by this controller; and it working fine:

    <?php
    
    class Test extends Controller {
    
        function __construct()
        {
            parent::Controller();
        }
        
        function index()
        {
            $query = $this->vbulletin->db->query_read("SELECT * FROM post WHERE postid = 1");
            $post = $this->vbulletin->db->fetch_array($query);  
            print_r($post);
        }    
    }

     

    Now if somebody could find another (non-invasive?) way of achieving it
    (ie, without needing to edit the index.php file); it would be great!

    Good night. 😊

  • #5 / Mar 29, 2009 3:49pm

    CroNiX

    4713 posts

    Wow, I will go test this out.  I don’t care if it has to have a little mod to index.php for this project, its an easy thing to update if needed and I doubt they really make any changes to index.php when updating CI, or rarely.  I hope it works as you say!  Thank you for your work!

  • #6 / Mar 29, 2009 4:15pm

    CroNiX

    4713 posts

    And it does work great.  Thank you again for this!  [big ass smile]

  • #7 / Mar 29, 2009 10:17pm

    CroNiX

    4713 posts

    I notice that you can’t access the vb object from the constructor of a controller.  You can from any method, but not the constructor.  Do you have any ideas?  I would like to check the usergroup of the currently logged in user from the constructor so I don’t have to do it for each method.  Other than that its working very well so far!

  • #8 / Mar 30, 2009 2:53am

    Shadab

    8 posts

    Yes, the $this->vbulletin object is not available in controller constructors as the $vbulletin is copied into $CI ‘after’ the controller constructor has finished executing. Another approach would be to copy $vbulletin into $CI via a library class instead of the hook.


    index.php
    Same as in previous post


    vb.php in /application/libraries/

    <?php
    
    class vB {
    
        function __construct() {
            
            $CI =& get_instance();
            $CI->vbulletin = $GLOBALS['vbulletin'];
    
            unset($GLOBALS['vbulletin']);
        }
    }

    [No hooks required]


    And then use like this in a controller :

    class Test extends Controller {
    
        function __construct()
        {
            parent::Controller();
            
            $this->load->library('vB');
            
            $query = $this->vbulletin->db->query_read("SELECT * FROM post WHERE postid = 1");
            $post = $this->vbulletin->db->fetch_array($query);
            
            print_r($post);
        }
    
        function index()
        {
            // Some other $this->vbulletin work.
        }
    
    }
  • #9 / Mar 31, 2009 2:32am

    CroNiX

    4713 posts

    This works great.  Thanks again!  This is almost identical to what I came up with earlier.  The only different is I didn’t think to load the vb object from index.php.

  • #10 / Mar 31, 2009 4:40am

    Shadab

    8 posts

    Not a problem. Glad to hear that it works. 😊

  • #11 / Apr 14, 2009 12:19pm

    Choo

    16 posts

    I want to add my experience using this variant… It doesn’t work with data manager. Anybody else try to work with it from CI? I think it happens because vb classes work with $GLOBAL directly, so we can’t unset it. So, for what that library when we can simple write something like $vb=&$GLOBAL[‘vbulletin’] everywhere. And it works with data manager. Any opinions?

  • #12 / Apr 29, 2009 9:35pm

    DemiGod

    2 posts

    You’re right, there are some issues with the Data Manager it seems.  I’ve been working on getting codeIgniter and vBulletin integration for the last few days for a project.  It would be nice to have the Data Manager working correctly so I don’t have to manipulate the database directly.

    I put all my code into the Forums library (still needed to do the loading trick for globals.php in the index.php file).  My test code below contains different ways of accessing data that I found from the codeigniter and vbulletin forums (and software packages).  The idea is to have different options to get around any issues I might come across.  All of these functions work, except the one noted below.

    Anyway, I created a function (setCurrentUserHomePage) that would update the current logged in users homepage to test out the Data Managers.  The function logic outside the codeigniter environment works perfectly without any errors (I set error reporting to E_ALL).  In the codeigniter environment though, vBulletin generates a bunch of error notices about undefined indexes (ipoints, posts, membergroupids, posts, displaygroupid).  The homepage is still updated though.  Code follows below:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
    
    class Forums
    {
        private $CI;
        
        private $vbulletin;
        
        private $db_prefix     = 'vb_';
        private $cookie_prefix = 'bb';
        
        function __construct()
        {
            $this->CI          =& get_instance();
            $this->CI->forums  =  $this;
            $this->vbulletin   =  $GLOBALS['vbulletin'];
        }
        
        function hasSession()
        {
            return $this->vbulletin->userinfo['userid'] > 0;
        }
        
        function getSessionHash()
        {
            // using vbulletin globals
            global $sessionhash;
            return $sessionhash;
        }
        
        function getCurrentUserId()
        {
            // We can use vbuser way of doing things
            // although in this case it would be better to use
            // $this->vbulletin->userinfo['userid']
            return @$_COOKIE[$this->cookie_prefix . 'userid'];
        }
        
        function getUserInfo($user_id)
        {
            return fetch_userinfo($user_id);
        }
        
        function setCurrentUserHomePage($homepage)
        {
            // Get User data manager object
            $dmUser =& datamanager_init('User', $this->vbulletin);
            
            // Load user info
            $dmUser->set_existing($this->vbulletin->userinfo);
            
            // Set new homepage
            $dmUser->set('homepage', $homepage);
            
            // Try to save chagnes
            $dmUser->pre_save();
            if (count($dmUser->errors) > 0)
            {
                return false;
            }
            else
            {
                $dmUser->save();
                return true;
            }  
        }
    }
    ?>

     

    This is in my controller functions to test out the class:

    $this->load->library('forums');
            //echo $this->forums->getCurrentUserId();
             
            if ($this->forums->hasSession())
            {
                $this->forums->setCurrentUserHomePage('www.google.com');
                //print_r($this->forums->getUserInfo(1));
            }
            
            //echo $this->forums->getSessionHash();
  • #13 / Apr 30, 2009 5:00pm

    DemiGod

    2 posts

    Ha… seems like vBulletin changes the error reporting level while callings its functions.  The reason codeigniter sees these errors is because it has its own custom error handler in CodeIgniter.php:

    set_error_handler(‘_exception_handler’);

    So this is why the codeigniter picks up these error notices.  Using similar error code in my test php file produced similar results.  So it seems there are no integration issues with codeigniter… but you will need to set you error level to E_ALL & ~E_NOTICE.  If you want to keep the error level to E_ALL, you could do the following:

    function xyz()
    {
        // Use php error handler at start of function
        restore_error_handler();
    
        // call vbulletin data manager functions
    
        // Restore codeIgniter's error handler before returning
        set_error_handler('_exception_handler');
    }
  • #14 / Sep 10, 2009 2:33am

    CroNiX

    4713 posts

    In the codeigniter environment though, vBulletin generates a bunch of error notices about undefined indexes (ipoints, posts, membergroupids, posts, displaygroupid).  The homepage is still updated though.  Code follows below:

    Yes I ran into this as well, but the solution was the same that you did here:

    function getSessionHash()
    {
        // using vbulletin globals
        global $sessionhash;
        return $sessionhash;
    }

    Those variables you were having troubles with are in the global namespace within the vb application and NOT a part of the object, so you need to declare them global to get access to them from within CI.  I was thinking about compiling a list of these variables and then adding them to my CI vb library, but as of now I have just been adding them as I encounter the errors.  Been working so far…

  • #15 / Apr 18, 2010 7:08pm

    CroNiX

    4713 posts

    Just a note, I couldn’t get the vb datamanagers working from within CI, or at least the one I was trying which was the user datamanager.  I kept getting “Call to a member function query_first_slave() on a non-object” errors and finally tracked it down to the fact that we were unsetting $GLOBALS[‘vbulletin’].  The datamanagers internally call upon the global $vbulletin, so we can’t unset it if we want to use datamangers, unless Im missing something.  After commenting out this line I was able to use the user datamanager and change a persons usergroup with no errors.  This hasn’t yet shown to prove harmful to anything else that I’ve done, although it probably consumes more memory because you aren’t destroying the global vbulletin object.

    Has anybody done anything interesting with CI/VB integration?  Any common issues?  It would be nice if we could collaborate so everything is in one thread.

    Thanks.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases