3

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?

zetetic
  • 171
  • 1
  • 13

2 Answers2

1

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

zetetic
  • 171
  • 1
  • 13
0

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;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
CometChat
  • 57
  • 3
  • While I could do that, adding the extra includes now gives me access to Session directly, and even better Sentinel, which is the user authentication package currently being used. So instead of the large function, it just becomes one line: ```return Sentinel::getUser()->id;``` – zetetic Aug 23 '16 at 02:58