CometChat integration with laravel 5.1

734 Views Asked by At

My question relates to integrating CometChat with laravel 5.1.

I'm trying to give access to Laravel within the integration.php file of Cometchat. I want to give access to the Session class so I can access the session info from the database (by default Cometchat uses file sessions). For the moment I have switched Laravel to use file sessions.

So how can I get access to sessions from Laravel so I can access it within the integration.php file?

2

There are 2 best solutions below

0
On

Ok I think I have worked it out. The following code gives me access to the existing Laravel app and I am able to access Session and even Sentinel.

I also added an include that points to vendor/autoload.php, which has now given me access to the QueryBuilder and other systems.

At the top of integration.php I have:

// integration.php includes the laravel files to give access, it just
// didn't use it fully

$app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture());

$id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
$app['session']->driver()->setId($id);
$app['session']->driver()->start();

This returns the current running Laravel, and I am then able to do something like $app['session']->get('dataname')

Though with the added vendor/autoload.php I can now also access DB::table or Sentinel::getUser(), etc

1
On

Please add the following line after include_once( dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'bootstrap'.DIRECTORY_SEPARATOR.'app.php'); line.

include_once( dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers'.DIRECTORY_SEPARATOR.'Auth'.DIRECTORY_SEPARATOR.'app.php');

Replace the getUserID() function with following in integration.php file, or you can download the CometChat package for Laravel 5 from http://my.cometchat.com

function getUserID() {
    $userid = 0;
    if (!empty($_SESSION['basedata']) && $_SESSION['basedata'] != 'null') {
        $_REQUEST['basedata'] = $_SESSION['basedata'];
    }

    if (!empty($_REQUEST['basedata'])) {
        if (function_exists('mcrypt_encrypt') && defined('ENCRYPT_USERID') && ENCRYPT_USERID == '1') {
            $key = "";
            if( defined('KEY_A') && defined('KEY_B') && defined('KEY_C') ){
                $key = KEY_A.KEY_B.KEY_C;
            }
            $uid = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode(rawurldecode($_REQUEST['basedata'])), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
            if (intval($uid) > 0) {
                $userid = $uid;
            }
        } else {
            $userid = $_REQUEST['basedata'];
        }
    }
    if (!empty($_COOKIE['laravel_session'])) {    
        $app = app();
        $kernel = $app->make('Illuminate\Contracts\Http\Kernel');
        $response = $kernel->handle(
            $request = Illuminate\Http\Request::capture()
            );
        $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
        $app['session']->driver()->setId($id);
        $app['session']->driver()->start();
        if($app['auth']->user()!= NULL){
            $userid = $app['auth']->user()->id;
        }
    }
    $userid = intval($userid);
    return $userid;
}