Custom PHP Site

Do not know programming? Want us to perform the installation?

Simply purchase our Premium or Platinum Edition and our team will perform the complete integration and installation. That’s right! All the steps below will be performed by our team and all you need to do is sit back and relax.

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',setConfigValue('DEV_MODE','1'));
define('ERROR_LOGGING',setConfigValue('ERROR_LOGGING','1'));
define('MEMCACHE',setConfigValue('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');
$table_prefix = '';
$db_usertable = 'users';	
$db_usertable_userid = 'userid';
$db_usertable_name = 'username';
$db_avatartable = ' ';
$db_avatarfield = ' '.$table_prefix.$db_usertable.'.'.$db_usertable_userid.' ';

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:

$table_prefix = '';
$db_usertable = 'users';	
$db_usertable_userid = 'userid';
$db_usertable_name = 'username';
$db_avatartable = ' ';
$db_avatarfield = ' '.$table_prefix.$db_usertable.'.'.$db_usertable_userid.' ';

Update single-sign-on functionality

Scroll down to find the getUserID() function.

The getUserID() function is used to return the logged in user’s ID. Depending on how you have programmed your site, you will have to find out the user’s ID.

If you have written a simple PHP authentication, then during the authentication, you can specify a session variable like:

 /* In your own login.php */
/* After you authenticate the user */
$_SESSION['userid'] = $users['id']; // Modify to suit requirements

Then your getUserID() function will look like:

function getUserID() {
  $userid = 0; // Return 0 if user is not logged in  $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  $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
       $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.lastseen, cometchat_status.lastseensetting,   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.lastseen, cometchat_status.lastseensetting, 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 users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.lastactivity,cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from users left join cometchat_status on users.id = 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;
}

Update user information fetching mechanism

Your getUserDetails() will look like:

function getUserDetails($userid){
 $sql = ("select users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message, cometchat_status.isdevice from users 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 users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message,  cometchat_status.isdevice from users left join cometchat_status on users.id = 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;
}

The getFriendsList function helps to store the details of all the online users in a cache and getFriendsIds helps to store only ids of all the friends in a cache. CometChat then uses both the caches to get the details of all the friends.

That’s all! Now log-in to your site and you will be able to see the CometChat bar. For customizing the text, icons, plugins, theme and modules, please refer to other documents.

Not finding what you need?

The CometChat team is here to help!