Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
1.0k views
in Q2A Core by

I've done the following steps:-

 

Introduction

This guide will help you through the installation process and get CometChat running on your site.

Installation is very straight forward, only taking about 30 minutes from uploading the files to viewing the CometChat bar on your site.

Before you begin, you will need an FTP client, if you do not have one, some popular solutions include FileZilla (free) or CuteFTP (trial).

This guide assumes that you have successfully downloaded the latest release of CometChat and have the zip file “unzipped” and ready to go. If not, you can download the package from your client area.

The instructions will term the zip file you downloaded as cometchat.zip.

Uploading

At this point, you should have the zip archive cometchat.zip and find a single folder- “cometchat”.

If you access your site via ‘http://www.domain.com’, then look for the webroot directory.

The “webroot” directory is usually ‘public_html’ or ‘www’, but this varies from server to server so if you’re unsure, contact your hosting provider.

Using your FTP client, copy the cometchat folder to your webroot directory. e.g. http://www.domain.com/cometchat

Configuration

Switch on development mode

Edit config.php and search for the tag:

/* ADVANCED */

Set DEV_MODE to 1, ERROR_LOGGING to 1 and CACHING to 0. After integration, you can set it to default.

define('DEV_MODE','1');
define('ERROR_LOGGING','1');
define('MEMCACHE','0'); 

Add your database information

Edit integration.php and update the DATABASE details.

/* DATABASE */

define('DB_SERVER','localhost');
define('DB_PORT','3306');
define('DB_USERNAME','root');
define('DB_PASSWORD','password');
define('DB_NAME','databasename');
define('TABLE_PREFIX','');
define('DB_USERTABLE','users');
define('DB_USERTABLE_NAME','username');
define('DB_USERTABLE_USERID','userid');
define('DB_AVATARTABLE'," ");
define('DB_AVATARFIELD','users.avatar');

The first 5 lines are used to access the database. You must access your existing site’s database and not create a separate database for CometChat.

The next 6 lines specify details about your database schema.

* If all your tables use a prefix, for example tableprefix_, then specify the TABLE_PREFIX as tableprefix_ or you can leave it empty and mention complete table name while defining DB_USERTABLE
DB_USERTABLE specifies the name of the table in which your user information is stored.
DB_USERTABLE_NAME specifies the name of the field from users table in which the user’s name/display name is stored.
DB_USERTABLE_USERID specifies the name of the field from users table in which the user’s id is stored (usually id or userid or user_id or member_id). This field must be of integer type.
DB_AVATARTABLE specifies join of users table with avatar table. If avatar is stored in the users table then leave this field blank.
DB_AVATARFIELD is the user ID by default. You can change this if your site stores avatar in users or avatars table.

The code will look like:

define('TABLE_PREFIX','');
define('DB_USERTABLE','users');
define('DB_USERTABLE_NAME','username');
define('DB_USERTABLE_USERID','userid');
define('DB_AVATARTABLE'," ");
define('DB_AVATARFIELD','users.avatar');

 

I don't know what to do after this which is mentioned in this article - https://support.cometchat.com/documentation/php/installing-cometchat/custom-coded-site/php-site/​

 

They have easy install option for like 50 CMS but question2answer CMS is not in the list and so I have to install manual only :(

 

Q2A version: Latest
by
How to do the below steps?

Then your getUserID() function will look like:

function getUserID() {
  $userid = 0; // Return 0 if user is not logged in

  if (!empty($_SESSION['userid'])) {
      $userid = $_SESSION['userid'];
  }
  $userid = intval($userid);
  return $userid;
}
If you are using a cookie then configure the same using the following function:

function getUserID() {
  $userid = 0; // Return 0 if user is not logged in

  if (!empty($_COOKIE['userid'])) {
    $userid = $_COOKIE['userid'];
  }
  $userid = intval($userid);
  return $userid;
}
If you are using a more complex method of authentication like storing the session_hash in the database, then your getUserID() function will look something like:

function getUserID() {
    $userid = 0; // Return 0 if user is not logged in
    
if (!empty($_COOKIE['sessionhash'])) {
    $sql = ("select userid from ".TABLE_PREFIX."session
            where sessionhash = '".mysql_real_escape_string($_COOKIE['sessionhash'])."'");
        $query = mysql_query($sql);
        $session = mysql_fetch_array($query);
        $userid = $session['userid'];
    }
   $userid = intval($userid);
   return $userid;
}
Update Who’s Online list fetching mechanism

We need to modify the getFriendsList() function.

The getFriendsList() function returns all the users’ details part of the Who’s Online list. This list need not necessarily be friends of the logged in user. It may simply be all online users instead. You can customize this to return any set of users.

The default configuration assumes that you have a table called friends with the following fields:

toid integer
fromid integer
status integer
All entries are assumed to be two-way i.e. if user A (id:1) and user B (id:2) are friends, then there will be two entries in the table:

--------------------------------
 id    toid    fromid    status
--------------------------------
13       1         2       1
14       2         1       1
--------------------------------
To show only your friends in the Who’s Online list, add the following in your getFriendsList():

function getFriendsList($userid,$time) {
 $sql = ("select users.id userid, users.username username, users.link link, users.avatar avatar, cometchat_status.lastactivity lastactivity,    cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from friends join users on friends.toid = users.id left join   cometchat_status on users.id = cometchat_status.userid where friends.fromid = '".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' order by username asc");
 return $sql;
}
If you wish to show all users, add the following code in your getFriendsList():

function getFriendsList($userid,$time) {
 $sql = ("select DISTINCT users.id userid, users.username username,  users.username link,  users.id  avatar, cometchat_status.lastactivity lastactivity, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from users left join cometchat_status on users.id = cometchat_status.userid order by username asc");
 return $sql;
}
However, we do not recommend this, since it will add load to your site.

If you would like to show only online users, then you can use:

function getFriendsList($userid, $time){
 $sql = ("select userid, username, link, avatar, cometchat_status.lastactivity, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from left join cometchat_status on = cometchat_status.userid where (('" . mysqli_real_escape_string($GLOBALS['dbh'], $time) . "' - cometchat_status.lastactivity < '".((ONLINE_TIMEOUT)*2)."') OR cometchat_status.isdevice = 1) and (cometchat_status.status IS NULL OR cometchat_status.status <> 'invisible' OR cometchat_status.status <> 'offline') order by username asc");
 return $sql;
}
by
I have to do this after the above step:-

Update user information fetching mechanism

Your getUserDetails() will look like:

function getUserDetails($userid){
 $sql = ("select userid, username, link, avatar, cometchat_status.lastactivity, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from left join cometchat_status on userid = cometchat_status.userid where userid = '" . mysqli_real_escape_string($GLOBALS['dbh'], $userid) . "'");
 return $sql;
}
Update status message, avatar and links functionality

The getUserStatus() function returns the current status message as well as the state of the user (available, busy, invisible, offline). If your site already has a status updates feature, then you will have to modify the first field- cometchat_status.message which is returned to pull the status message from your table.

Finally, we modify fetchLink() and getAvatar() function

When the getFriendsList() function is executed, avatar and link contain the user id by default (you can change this if you store the avatar/link location in your table). fetchLink() and getAvatar() functions are used to post-process the data obtained from the getFriendsList() function.

For example:

function fetchLink($link) {
    return BASE_URL.'../users.php?id='.$link;
}
   
function getAvatar($image) {
    if (is_file(dirname(dirname(__FILE__)).'/images/'.$image.'.gif')) {
        return BASE_URL.'../images/'.$image.'.gif';
    } else {
        return BASE_URL.'images/noavatar.png';
    }
}
The hooks_statusupdate() function is called when a user updates his/her status via CometChat. If your site already has a status updates feature, you can update that using this hook.

Adding compatibility with Mobile apps and Desktop Messenger (Optional)

Your chatLogin() will look like:

function chatLogin($userName,$userPass) {
   $userid = 0;   
   $sql = ("SELECT * FROM users WHERE username='".$userName."'");
   $result = mysqli_query($GLOBALS['dbh'],$sql);
   $row = mysqli_fetch_assoc($result);   
   $check = md5(md5($userPass));
   if ($check==$row['password']) {
      $userid = $row['user_id'];
      if (isset($_REQUEST['callbackfn']) && $_REQUEST['callbackfn'] == 'mobileapp') {
        $sql = ("insert into cometchat_status (userid,isdevice) values ('".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."','1') on duplicate     key update isdevice = '1'");
mysqli_query($GLOBALS['dbh'], $sql);
      }
   }
  if (function_exists('mcrypt_encrypt') && defined('ENCRYPT_USERID') && ENCRYPT_USERID == '1') {
    $key = KEY_A.KEY_B.KEY_C;
       $userid = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $userid, MCRYPT_MODE_CBC, md5(md5($key))));
    }
  return $userid;
}
The chatLogin() returns user id of logged in user if the username and password are authenticated successfully.

In your getUserID(), add the following code after $userid = 0;

if (!empty($_SESSION['basedata']) && $_SESSION['basedata'] != 'null') {
  $_REQUEST['basedata'] = $_SESSION['basedata'];
}
if (!empty($_REQUEST['basedata'])) {
 if (function_exists('mcrypt_encrypt')) {
   $key = KEY_A.KEY_B.KEY_C;
   $uid = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($_REQUEST['basedata']), MCRYPT_MODE_CBC, md5(md5($key))), "");
     if (intval($uid) > 0) {
      $userid = $uid;
     }
 } else {
  $userid = $_REQUEST['basedata'];
 }
}
So, your getUserID() will look like:

function getUserID() {
 $userid = 0;
 if (!empty($_SESSION['basedata']) && $_SESSION['basedata'] != 'null') {
   $_REQUEST['basedata'] = $_SESSION['basedata'];
 }
 if (!empty($_REQUEST['basedata'])) {
   if (function_exists('mcrypt_encrypt')) {
    $key = KEY_A.KEY_B.KEY_C;
    $uid = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($_REQUEST['basedata']), MCRYPT_MODE_CBC, md5(md5($key))), "");
      if (intval($uid) > 0) {
       $userid = $uid;
      }
    } else {
    $userid = $_REQUEST['basedata'];
    }
   }
 if (!empty($_SESSION['userid'])){
   $userid = $_SESSION['userid'];
 }
 return $userid;
}
Installation
* Step 1

You should now run the installer file through your web browser by entering the URL into your browser address bar (if you have followed our example, type in http://www.domain.com/cometchat/install.php, naturally substituting ‘domain.com’ for your web address).

If the installation was completed successfully, then two codes will be displayed on your screen. The first code is for docked themes like Glass, Hangout, Facebook etc. and the second is for embedded theme i.e. Synergy. Please copy the appropriate lines of code depending on whether you wish to use docked themes or embedded theme.

* Step 2

To use docked theme:

Edit your template header. Immediately after the opening head tag add the code copied in step 1.

To embed CometChat in your site:

Add the code copied for embedded theme from step 1 in your site’s HTML code to embed the chat.

Now delete install.php file from the cometchat folder.

Adding Cache support

Before you begin with this step, please enable one of the caching options from CometChat Admin Panel.

Then you need to modify the getFriendsList() and getFriendsIds() in integration.php file.

a. Show all online users with caching enabled

To use caching for all online users, no change is required; simply use the getFriendsList function from above (Update Who’s Online list fetching mechanism).

b. Show online friends with caching enabled

When using caching and wanting to show only online friends, you still need to modify the getFriendsList function to show all online users as follows.

function getFriendsList($userid, $time){
  $sql = ("select userid, username, link, avatar, cometchat_status.lastactivity, cometchat_status.status, cometchat_status.message,      cometchat_status.isdevice from left join cometchat_status on = cometchat_status.userid where (('" . mysqli_real_escape_string($GLOBALS['dbh'],     $time) . "' - cometchat_status.lastactivity < '".((ONLINE_TIMEOUT)*2)."') OR cometchat_status.isdevice = 1) and    (cometchat_status.status IS NULL OR cometchat_status.status <> 'invisible' OR cometchat_status.status <> 'offline') order by username    asc");
  return $sql;
}
Then you need to modify getFriendsIds to return friends for the logged in user. The getFriendsIds() function should return SQL query which retrieves userids of friends.

function getFriendsIds($userid) {
  $sql = ("select toid friendid from friends where fromid ='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status = 1 union select   fromid friendid from friends where toid='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status=1");
  return $sql;
}

1 Answer

+1 vote
by
edited by

 

Hi Mani Kan Dan,

For any CometChat related issues, please feel free to create a support ticket at 

https://my.cometchat.com/tickets 

and our team will be happy to assist you further.

If you have already done so, please share the ticket number so I can escalate the issue.

Regards

Robert

by
edited by
I've already submitted the ticket.
...