Having issues pulling avatars from somewhere other than the default Gravatar? Using a CMS such as Joomla! or WordPress? Then this tutorial is for you!
NOTE: Version at the time of this tutorial was 9.6
Introduction
The avatar integration process as explained by the Codologic team is a bit tricky for early coders such as myself. I had the basic concept which I pulled from HERE, but I couldnt find where in my driver file I was supposed to edit. So I started off editing the base.php driver file which caused me some headache. I recommend going to the above link to get familiar with the process before continuing, but I am going to try to make this idiot proof, so lets get started.
What You Need To Know
There are a few things that you need to know before we begin. The files that I refer to here as "driver" files are specific to your CMS. The driver file that we will create is not going to work for you if you do not have Kunena installed in Joomla!. So you will need to use some critical thinking while you do this. Everything that we use a relative path for is just that, relative, so you will need to know your own file hierarchy before continuing. last but not least; for gods sake don't just copy and paste the code here because it will likely break your chat.
Gather The Info
You will need a few pieces of information to create the driver file that will import your avatars for use in Freichat. The first thing you need is where in the world are you pulling your avatars. If you are using Joomla! as a CMS and a forum such as Kunena then you can easily grab them by going to /media/kunena/avatars/users. If you are using another CMS or different forum etc. then you may need to look around a bit to find the path of your users avatars. In that path you will notice that the names of the given avatars are user specific. In this case it uses their ID in the file name. write down the path and set this aside.
We now need to find out how it is stored in the forum database. Is it a relative path or a full link? To do this, go to your database and search for your forum's users table. For Kunena it will be something like jos_kunena_users. if you look at a record in that table you will see a column named something similar to "avatar". Take not of what is in that field for a user. In my case each users value for avatar was users/avatar(ID).png. So we now know that we will have to call the avatar from the jos_kunena_users table from the avatar column with a relative path of /media/kunena/avatars/. Let me digress a bit. Kunena has smaller avatars for each user in a different folder path like: media/kunena/avatars/resized/size36/. If your forum has an option to use resized images I suggest you use them because it cuts down on page load time. The image will be fitted to the size used by Freichat anyways.
We need to check a few global variables at this point. Head over to file/path/to/freichat/hardcode.php and look at the last line.
$avatar_field_name = 'avatar';
If your forum's user table uses "avatar" as its column name for the avatar path, then you are ok. If not, then you need to change this value to the column name of your forum's avatar column name. Kunena is already named avatar so no change needed for us here. You can go back to your database now and look in the frei_config table to determin if show_name column is set to either guest or user. This will determine which we will need to add in our driver file we are going to create.
Creating The Driver File
Now we need to start putting together the driver file. Open up your favorite editor (I use Notepad++) and create a file named whatever your forum software name is .php, so for me it would be Kunena.php Then we need to start writing a bit of code. start off with the code below.
<?php
require 'Joomla.php';
class Kunena extends Joomla {
public function __construct($db) {
//parent::__construct($db);
$this->db = $db;
}
}
Notice the "require 'Joomla.php';" part. You will need to replace this with the name of your current driver file. You can find this in hardcode.php. You also need to change line 5 to match the class associated with your driver. In my case it would be:
class Kunena extends Joomla {
But in your case you may need to change Joomla with the name of your driver file without the .php. I dont know what all the class-names are but you can always ask the Codologic creators. Then you will need to copy and past these three functions from base.php located in file/path/to/freichat/server/drivers
//------------------------------------------------------------------------------
public function avatar_url($res) {
$murl = str_replace("server/freichat.php", "", $this->url);
$avatar_url = $murl . 'client/jquery/user.jpeg';
return $avatar_url;
}
//------------------------------------------------------------------------------
public function get_guests() {
$query = "SELECT DISTINCT status_mesg,username,session_id,status,guest,in_room
FROM frei_session
WHERE time>" . $this->online_time2 . "
AND session_id<>" . $_SESSION[$this->uid . 'usr_ses_id'] . "
AND status<>2
AND status<>0";
//echo $query;
$list = $this->db->query($query)->fetchAll();
return $list;
}
//------------------------------------------------------------------------------
public function get_users() {
$query = "SELECT DISTINCT status_mesg,username,session_id,status,guest,in_room
FROM frei_session
WHERE time>" . $this->online_time2 . "
AND session_id<>" . $_SESSION[$this->uid . 'usr_ses_id'] . "
AND guest=0
AND status<>2
AND status<>0";
$list = $this->db->query($query)->fetchAll();
return $list;
}
//------------------------------------------------------------------------------
between the last two curly braces in the driver file you are creating. Now you should have:
<?php
require 'Joomla.php';
class Kunena extends Joomla {
public function __construct($db) {
//parent::__construct($db);
$this->db = $db;
}
//------------------------------------------------------------------------------
public function avatar_url($res) {
$murl = str_replace("server/freichat.php", "", $this->url);
$avatar_url = $murl . 'client/jquery/user.jpeg';
return $avatar_url;
}
//------------------------------------------------------------------------------
public function get_guests() {
$query = "SELECT DISTINCT status_mesg,username,session_id,status,guest,in_room
FROM frei_session
WHERE time>" . $this->online_time2 . "
AND session_id<>" . $_SESSION[$this->uid . 'usr_ses_id'] . "
AND status<>2
AND status<>0";
//echo $query;
$list = $this->db->query($query)->fetchAll();
return $list;
}
//------------------------------------------------------------------------------
public function get_users() {
$query = "SELECT DISTINCT status_mesg,username,session_id,status,guest,in_room
FROM frei_session
WHERE time>" . $this->online_time2 . "
AND session_id<>" . $_SESSION[$this->uid . 'usr_ses_id'] . "
AND guest=0
AND status<>2
AND status<>0";
$list = $this->db->query($query)->fetchAll();
return $list;
}
//------------------------------------------------------------------------------
}
So the three functions that we are going to work with, to reiterate, are avatar_url(), get_guests(), and get_users(). For avatar_url() we want to make the following changes. Make sure you use your relative path depending on your setup and not just copy mine.
$murl = str_replace($this->to_freichat_path, "", $this->url);
$avatar_url = $murl . 'media/kunena/avatars/resized/size36/' . $res[$this->avatar_field_name];
return $avatar_url;
That should replace the function, not be appended to it. Now for get_users() you need to change the query between the quotes to suite your needs which in my case looks like this now:
$query = "SELECT DISTINCT f.status_mesg,f.username,f.session_id,f.status,f.guest,f.in_room,k.avatar
FROM frei_session AS f
LEFT JOIN " . DBprefix . "kunena_users AS k ON f.session_id=k.userid
WHERE time>" . $this->online_time2 . "
AND f.session_id!=" . $_SESSION[$this->uid . 'usr_ses_id'] . "
AND f.status!=2
AND f.status!=0";
The difference here is we are adding ,k.avatar to the SELECT DISTINCT and adding a LEFT JOIN after the FROM statement. k.avatar is the field you will be pulling from the table, so if your column name was not avatar and you already changed the name in the hardcode.php file then set k.avatar to k.whatever your column name is. The way this LEFT JOIN works is kunena_users needs to be the table where your avatar path for your users lives, and f.session_id needs equal k.userid. In a bit more depth, we are trying to correlate the user from the frei_session table with the user in the jos_kunena_users table. and to do this we use the user ID of that user because it will match. After you do that and understand what you did, simply add the ,k.avatar and the LEFT JOIN statement to the get_users() function. The get_guests() and get_users() functions will look almost identical except for one AND statement will be added to get_users(). This is what makes this function unique because the AND statement makes the query return any record where f.guest is equal to 0.
Finish It Up
I know that was a lot to take in and you are waiting to be done with this but I assure you we are near the end. All you have to do now is save your new driver file and upload it to the drivers directory: file/path/to/freichat/server/drivers. and change your driver to the name of your file in hardcode.php, obviously leaving off the file extension. At that point just go back and refresh your site and check out your new avatars being pulled from wherever you chose to pull them from.
Example Driver File
Below is an example driver file which was named Kunena.php and in the hardcode.php the driver was changed to Kunena. I have added comments to let you know what exactly needs to be changed and why.
<?php
require 'Joomla.php'; // Needs to be the name of the CURRENT driver file you are using. You can find this is "path/to/freichat/hardcode.php"
class Kunena extends Joomla { // "Joomla" Needs to be whatever class you are extending from. Likely this will ALSO be the name of the old driver file as it is in this case.
public function __construct($db) {
//parent::__construct($db);
$this->db = $db;
}
//------------------------------------------------------------------------------
public function avatar_url($res) {
// Builds the avatar URL.
$murl = str_replace($this->to_freichat_path, "", $this->url);
$avatar_url = $murl . 'media/kunena/avatars/resized/size36/' . $res[$this->avatar_field_name]; // Needs to be the path to your avatar folder, relative or absolute. Take into account the path that is stored in your database as well.
return $avatar_url;
}
//------------------------------------------------------------------------------
public function get_guests() {
// Below we are adding ,k.avatar because the value "k" will be assigned as the table name and avatar is the field name in which the path to each users avatar is.
// Queries the db and assigns it to $query. // look at LEFT JOIN; you need to make sure that kunena_users is your table name where the avatars for your users live and also be sure the f.session can correlate to the k.userid to grab the correct avatar.
$query = "SELECT DISTINCT f.status_mesg,f.username,f.session_id,f.status,f.guest,f.in_room,k.avatar
FROM frei_session AS f
LEFT JOIN " . DBprefix . "kunena_users AS k ON f.session_id=k.userid
WHERE time>" . $this->online_time2 . "
AND f.session_id!=" . $_SESSION[$this->uid . 'usr_ses_id'] . "
AND f.status!=2
AND f.status!=0";
//echo $query;
// Creates an array of everything the query asked for.
$list = $this->db->query($query)->fetchAll();
//Returns the array.
return $list;
}
//------------------------------------------------------------------------------
public function get_users() {
// Queries the db and assigns it to $query. // Do the exact same thing as the get_guests() function. Don't copy and past because the AND statements are a bit different.
$query = "SELECT DISTINCT f.status_mesg,f.username,f.session_id,f.status,f.guest,f.in_room,k.avatar
FROM frei_session AS f
LEFT JOIN " . DBprefix . "kunena_users AS k ON f.session_id=k.userid
WHERE time>" . $this->online_time2 . "
AND f.session_id!=" . $_SESSION[$this->uid . 'usr_ses_id'] . "
AND f.guest=0
AND f.status!=2
AND f.status!=0";
// Creates an array of everything the query asked for.
$list = $this->db->query($query)->fetchAll();
//Returns the array.
return $list;
}
//------------------------------------------------------------------------------
}
Happy Hunting...