Jump to content

Client Account Login Notification Hook


Recommended Posts

58 minutes ago, twhiting9275 said:

Please create your own thread for your modifications, rather than diluting this one. As it is, yours is not documented, and will end up causing confusion, because of this

I realize that you think you're trying to improve this, but this isn't helping at all. As posted, your modification does not work, while the hook I created can indeed simply be cut and pasted into a file and works as posted on 8.0 and 8.1.

lol, it works and is improved, all one has todo is to create a template and a custom field after checking my code, not that hard.

but maybe you are right, why wasting my time trying to help, ignore my broken version lol

Link to comment
Share on other sites

4 minutes ago, wtricks said:

not that hard.

'not that hard is subjective, and you didn't provide any information in your post at all. Again, it's not documented. I had to read through it a couple times, and I've been dealing with WHMCS for a number of years

If the user has to do more than copy and paste something into a file, in today's world, you're going to run into issues, and of course, those are passed on to others, and make finding and resolving problems even harder.

When it comes to things like this, follow the KISS principle (look it up), and you'll have fewer problems. More working parts, more components, more chances of breakage and confusion

Link to comment
Share on other sites

6 hours ago, twhiting9275 said:

<?php

/*
Client area login notifications for WHMCS (works with versions 6-8)
Created by whmcsguru
Contributions by brian!
*/

use WHMCS\Database\Capsule;
$myver = get_whmcs_version();
$isadmin = $_SESSION['adminid'];

$admnotify = FALSE;
//change this to true you want to send notifications when admin logged in..
//NOT advisable. this will let your clients know when you're logging into their account

if (!empty($isadmin))
{
	if (!$admnotify)
	{
		return;
	}
}
function hook_client_login_notify($vars)
{
	$mailsent=FALSE;


	global $myver;
	$myver = get_whmcs_version();
	if ($myver < 8)
	{
		$userid = $vars['userid'];

		send_login_notify($userid);
		return;
	}
	if ($myver >= 8)
	{
		$user = $vars['user'];
		$userid = $user->id;
		//a dirty hack to try to work around a couple of things, maybe

		$acctowner = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->where('owner', '=', 1)
		->count();

		$numrows = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->count();

		//we own our account. We must always notify us directly
		if ($acctowner > 0)
		{
			send_login_notify($userid);
			return;
		}

		//we don't own our account, so, notify the owner, if we only exist once.
		if ($numrows < 2)
		{
			foreach (Capsule::table('tblusers_clients')->WHERE('auth_user_id', '=', $userid)->get() as $userstuff){
				$userid = $userstuff->auth_user_id;
				$clientid = $userstuff->client_id;
				$owner = $owner;
				if ($acctowner < 1)
				{
					send_login_notify($clientid, $userid);
					return;
				}

			}
		}

		return;
	}



}


function send_login_notify($myclient, $theuserid="")
{
	global $myver;

	$ip = $_SERVER['REMOTE_ADDR'] ;
	$hostname = gethostbyaddr($ip);

	if ($myver < 8)
	{

		$clientinfo = Capsule::table('tblclients')->select('firstname', 'lastname')->WHERE('id', $myclient)->get();
		foreach ($clientinfo as $clrow)
		{
			$firstname = $clrow->firstname;
			$lastname = $clrow->lastname;
		}
	}
	if ($myver >= 8)
	{

		$clientinfo = Capsule::table('tblusers')->select('first_name', 'last_name')->WHERE('id', $myclient)->get();
		foreach ($clientinfo as $clrow)
		{
			$firstname = $clrow->first_name;
			$lastname = $clrow->last_name;
		}
	}


	$command = "sendemail";
	$values["customtype"] = "general";
	if (empty($theuserid))
	{
		$values["customsubject"] = "Account Login from $hostname";
		$values["custommessage"] = "<p>Hello $firstname $lastname,<p>Your account was recently successfully accessed by a remote user. If this was not you, please do contact us immediately<p>IP Address: $ip<br/>Hostname: $hostname<br />";
	}

	elseif ($theuserid > 0)
	{
		$moreinfo = Capsule::table('tblusers')->select('first_name', 'last_name', 'email')->WHERE('id', $theuserid)->get();
		//greet them
		foreach ($moreinfo as $userrow)
		{
			$ufirst = $userrow->first_name;
			$ulast = $userrow->last_name;
			$uemail = $userrow->email;
		}

		$values["customsubject"] = "Subaccount Login from $hostname";
		$values["custommessage"] = "<p>Hello
		$firstname $lastname,<p>
		A subaccount of yours just logged in. Please see the details of the login below
		<p>
		Name: $ufirst $ulast
		Email: $uemail
		IP Address: $ip
		Hostname: $hostname<br />";
	}
	$values["id"] = $myclient;

	$results = localAPI($command, $values);
	
}
if ($myver < 8)
{
	add_hook('ClientLogin', 1, 'hook_client_login_notify');
}
if ($myver >= 8)

{
	add_hook('UserLogin', 1, 'hook_client_login_notify');
}

function get_whmcs_version()
{
        $theversion = Capsule::table('tblconfiguration')->where('setting', '=', 'Version')->value('value');
        $retver = substr($theversion, 0,1);

        return ($retver);

}

Updated version!
1 - there was an issue with 7.x installs pulling the greeting name (thusly causing the notification to bail) . I had the wrong table / values used in the last update. This version corrects that, and I've tested on 7.x all the way up to 8.1.  All good
2 - I added an admnotify bool . This is a quick way to test that this is working, or to send notifications if you're logged in as an admin user (really not recommended, as it'll let users know when you're logging into their account, and give them your ip address).

As far as templating and the like:
The reason I chose not to do this (could have , years ago) was to reduce the confusion and possible areas of issues. This way, all you have to do is drop the code into a file and you're done.

Yup, Thanks @twhiting9275 Now it's Working Perfect, 

Link to comment
Share on other sites

19 hours ago, wtricks said:

lol, it works and is improved, all one has todo is to create a template and a custom field after checking my code, not that hard.

but maybe you are right, why wasting my time trying to help, ignore my broken version lol

@wtricks  Did you share any link / URL or the code ?  I didn't find any btw

 

Thanks  @Hardik Joshi for the information, will try using the code...

 

Edited by ManagedCloud-Hosting
Link to comment
Share on other sites

1 hour ago, ManagedCloud-Hosting said:

@wtricks  Did you share any link / URL or the code ?  I didn't find any btw

 

Thanks  @Hardik Joshi for the information, will try using the code...

 

Hii @ManagedCloud-Hosting

create a file in WHMCS Installation Directory/includes/hook/clientalert.php

Past this code and Enjoy 🙂

<?php

/*
Client area login notifications for WHMCS (works with versions 6-8)
Created by whmcsguru
Contributions by brian!
*/

use WHMCS\Database\Capsule;
$myver = get_whmcs_version();
$isadmin = $_SESSION['adminid'];

$admnotify = FALSE;
//change this to true you want to send notifications when admin logged in..
//NOT advisable. this will let your clients know when you're logging into their account

if (!empty($isadmin))
{
	if (!$admnotify)
	{
		return;
	}
}
function hook_client_login_notify($vars)
{
	$mailsent=FALSE;


	global $myver;
	$myver = get_whmcs_version();
	if ($myver < 8)
	{
		$userid = $vars['userid'];

		send_login_notify($userid);
		return;
	}
	if ($myver >= 8)
	{
		$user = $vars['user'];
		$userid = $user->id;
		//a dirty hack to try to work around a couple of things, maybe

		$acctowner = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->where('owner', '=', 1)
		->count();

		$numrows = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->count();

		//we own our account. We must always notify us directly
		if ($acctowner > 0)
		{
			send_login_notify($userid);
			return;
		}

		//we don't own our account, so, notify the owner, if we only exist once.
		if ($numrows < 2)
		{
			foreach (Capsule::table('tblusers_clients')->WHERE('auth_user_id', '=', $userid)->get() as $userstuff){
				$userid = $userstuff->auth_user_id;
				$clientid = $userstuff->client_id;
				$owner = $owner;
				if ($acctowner < 1)
				{
					send_login_notify($clientid, $userid);
					return;
				}

			}
		}

		return;
	}



}


function send_login_notify($myclient, $theuserid="")
{
	global $myver;

	$ip = $_SERVER['REMOTE_ADDR'] ;
	$hostname = gethostbyaddr($ip);

	if ($myver < 8)
	{

		$clientinfo = Capsule::table('tblclients')->select('firstname', 'lastname')->WHERE('id', $myclient)->get();
		foreach ($clientinfo as $clrow)
		{
			$firstname = $clrow->firstname;
			$lastname = $clrow->lastname;
		}
	}
	if ($myver >= 8)
	{

		$clientinfo = Capsule::table('tblusers')->select('first_name', 'last_name')->WHERE('id', $myclient)->get();
		foreach ($clientinfo as $clrow)
		{
			$firstname = $clrow->first_name;
			$lastname = $clrow->last_name;
		}
	}


	$command = "sendemail";
	$values["customtype"] = "general";
	if (empty($theuserid))
	{
		$values["customsubject"] = "Account Login from $hostname";
		$values["custommessage"] = "<p>Hello $firstname $lastname,<p>Your account was recently successfully accessed by a remote user. If this was not you, please do contact us immediately<p>IP Address: $ip<br/>Hostname: $hostname<br />";
	}

	elseif ($theuserid > 0)
	{
		$moreinfo = Capsule::table('tblusers')->select('first_name', 'last_name', 'email')->WHERE('id', $theuserid)->get();
		//greet them
		foreach ($moreinfo as $userrow)
		{
			$ufirst = $userrow->first_name;
			$ulast = $userrow->last_name;
			$uemail = $userrow->email;
		}

		$values["customsubject"] = "Subaccount Login from $hostname";
		$values["custommessage"] = "<p>Hello
		$firstname $lastname,<p>
		A subaccount of yours just logged in. Please see the details of the login below
		<p>
		Name: $ufirst $ulast
		Email: $uemail
		IP Address: $ip
		Hostname: $hostname<br />";
	}
	$values["id"] = $myclient;

	$results = localAPI($command, $values);
	
}
if ($myver < 8)
{
	add_hook('ClientLogin', 1, 'hook_client_login_notify');
}
if ($myver >= 8)

{
	add_hook('UserLogin', 1, 'hook_client_login_notify');
}

function get_whmcs_version()
{
        $theversion = Capsule::table('tblconfiguration')->where('setting', '=', 'Version')->value('value');
        $retver = substr($theversion, 0,1);

        return ($retver);

}
Link to comment
Share on other sites

22 hours ago, twhiting9275 said:

This does indeed work in both 8.0 and 8.1 . Just updated (was waiting on theme devs to release an 8.1 theme)

If you're not seeing this working on 8.0 or 8.1, then something is up with your install, but the hook (as of my last update) works like a charm

The most common issue is going to be you being logged in to the admin area. You'll need to be logged out entirely, or use a different browser to test this.

Not sure on 7.0 yet, haven't been playing around in that beast for a bit. Will do some digging on that this afternoon, as Brian did mention it seems to not.

 

login-notify.jpg

Hey @twhiting9275

I want to customise it little more, can we add location name in Hostname ? like below image you can see IP Location : Rajkot, Gujarat (IN)

1394391501_Screenshot2021-01-05at11_27_15PM.thumb.png.ad08f3af91a61270b70e69f135cfc614.png

 

Link to comment
Share on other sites

22 hours ago, Hardik Joshi said:

create a file in WHMCS Installation Directory/includes/hook/clientalert.php

it would be /includes/hooks rather than /includes/hook/ 🙂

22 hours ago, Hardik Joshi said:

I want to customise it little more, can we add location name in Hostname ?

that would require a Geo-IP solution - which depending on how many lookups you're going to do daily, you might need to pay for... e.g with IPLocate, you can get 1000 lookups per day free... so if you think you're use more than that, then you might need to find an alternative or pay.

i've tweaked Tom's hook - there are no changes other than to get the City location from the IP, and to add it to the output.

<?php

/*
Client area login notifications for WHMCS (works with versions 6-8)
Created by whmcsguru
Contributions by brian!
*/

use WHMCS\Database\Capsule;
$myver = get_whmcs_version();
$isadmin = $_SESSION['adminid'];

$admnotify = FALSE;
//change this to true you want to send notifications when admin logged in..
//NOT advisable. this will let your clients know when you're logging into their account

if (!empty($isadmin))
{
	if (!$admnotify)
	{
		return;
	}
}
function hook_client_login_notify($vars)
{
	$mailsent=FALSE;


	global $myver;
	$myver = get_whmcs_version();
	if ($myver < 8)
	{
		$userid = $vars['userid'];

		send_login_notify($userid);
		return;
	}
	if ($myver >= 8)
	{
		$user = $vars['user'];
		$userid = $user->id;
		//a dirty hack to try to work around a couple of things, maybe

		$acctowner = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->where('owner', '=', 1)
		->count();

		$numrows = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->count();

		//we own our account. We must always notify us directly
		if ($acctowner > 0)
		{
			send_login_notify($userid);
			return;
		}

		//we don't own our account, so, notify the owner, if we only exist once.
		if ($numrows < 2)
		{
			foreach (Capsule::table('tblusers_clients')->WHERE('auth_user_id', '=', $userid)->get() as $userstuff){
				$userid = $userstuff->auth_user_id;
				$clientid = $userstuff->client_id;
				$owner = $owner;
				if ($acctowner < 1)
				{
					send_login_notify($clientid, $userid);
					return;
				}

			}
		}

		return;
	}



}


function send_login_notify($myclient, $theuserid="")
{
	global $myver;

	$ip = $_SERVER['REMOTE_ADDR'];

	$res = json_decode(file_get_contents('https://www.iplocate.io/api/lookup/'.$ip));
	$city = $res->city;
	$hostname = gethostbyaddr($ip);

	if ($myver < 8)
	{

		$clientinfo = Capsule::table('tblclients')->select('firstname', 'lastname')->WHERE('id', $myclient)->get();
		foreach ($clientinfo as $clrow)
		{
			$firstname = $clrow->firstname;
			$lastname = $clrow->lastname;
		}
	}
	if ($myver >= 8)
	{

		$clientinfo = Capsule::table('tblusers')->select('first_name', 'last_name')->WHERE('id', $myclient)->get();
		foreach ($clientinfo as $clrow)
		{
			$firstname = $clrow->first_name;
			$lastname = $clrow->last_name;
		}
	}


	$command = "sendemail";
	$values["customtype"] = "general";
	if (empty($theuserid))
	{
		$values["customsubject"] = "Account Login from $hostname";
		$values["custommessage"] = "<p>Hello $firstname $lastname,<p>Your account was recently successfully accessed by a remote user. If this was not you, please do contact us immediately<p>IP Address: $ip<br/>City: $city<br/>Hostname: $hostname<br />";
	}

	elseif ($theuserid > 0)
	{
		$moreinfo = Capsule::table('tblusers')->select('first_name', 'last_name', 'email')->WHERE('id', $theuserid)->get();
		//greet them
		foreach ($moreinfo as $userrow)
		{
			$ufirst = $userrow->first_name;
			$ulast = $userrow->last_name;
			$uemail = $userrow->email;
		}

		$values["customsubject"] = "Subaccount Login from $hostname";
		$values["custommessage"] = "<p>Hello
		$firstname $lastname,<p>
		A subaccount of yours just logged in. Please see the details of the login below
		<p>
		Name: $ufirst $ulast
		Email: $uemail
		IP Address: $ip
		City: $city
		Hostname: $hostname<br />";
	}
	$values["id"] = $myclient;

	$results = localAPI($command, $values);
	
}
if ($myver < 8)
{
	add_hook('ClientLogin', 1, 'hook_client_login_notify');
}
if ($myver >= 8)

{
	add_hook('UserLogin', 1, 'hook_client_login_notify');
}

function get_whmcs_version()
{
        $theversion = Capsule::table('tblconfiguration')->where('setting', '=', 'Version')->value('value');
        $retver = substr($theversion, 0,1);

        return ($retver);

}

there are additional options to get country, country code, continent, long/lat, timezone, postal code, ISP etc

country = $res->country;

and then you just need to add those variables to the mail output as I have with $city.

Link to comment
Share on other sites

On the subject of customizing with things like 'location':

It's important to understand that this is not reliable.  This is why this was originally left out of the hook.

IP location relies on third party services, and the IP owner, actually being up to date and honest. When it comes to location, outside of the US, not terribly accurate. Even inside of the US , it can be inaccurate . Hell, I just checked my own IP against some of the popular sites claiming to be accurate, about a 30% failure rate as far as accuracy goes

This is another one of those points of failure and confusion.

 

Link to comment
Share on other sites

18 hours ago, twhiting9275 said:

It's important to understand that this is not reliable.  This is why this was originally left out of the hook.

IP location relies on third party services, and the IP owner, actually being up to date and honest. When it comes to location, outside of the US, not terribly accurate. Even inside of the US , it can be inaccurate . Hell, I just checked my own IP against some of the popular sites claiming to be accurate, about a 30% failure rate as far as accuracy goes

absolutely agree - was going to mention to be wary of results, but I forget (or thought it obvious) - can't remember which!

when I tried it yesterday, it was accurate for me in the UK... but equally, I could use a VPN and it's going to tell me that i'm logging in from Dallas etc.

Link to comment
Share on other sites

  • 7 months later...
On 4/7/2020 at 8:30 PM, brian! said:

change...


$command = "sendemail";
$values["customtype"] = "general";
$values["customsubject"] = "Account Login from $hostname";
$values["custommessage"] = "<p>Hello $firstname $lastname,<p>Your account was recently successfully accessed by a remote user. If this was not you, please do contact us immediately<p>IP Address: $ip<br/>Hostname: $hostname<br />$signature";
$values["id"] = $userid;

to...


$datenow = date('Y-m-d H:i:s');
$date = fromMySQLDate($datenow,true,true);
$command = "sendemail";
$values["customtype"] = "general";
$values["customsubject"] = "Account Login from $hostname";
$values["custommessage"] = "<p>Hello $firstname $lastname,<p>Your account was successfully accessed by a remote user at $date. If this was not you, please do contact us immediately<p>IP Address: $ip<br/>Hostname: $hostname<br />$signature";
$values["id"] = $primaryid;

hey @brian! this code is not working for me please see below screenshot and let me know if i am making any mistake.

https://prnt.sc/1rgmo76

Link to comment
Share on other sites

On 9/7/2021 at 12:25 PM, Balram said:

hey @brian! this code is not working for me please see below screenshot and let me know if i am making any mistake.

https://prnt.sc/1rgmo76

i just figured out code is working but there is something wrong

example- user ABC logging into client area but user ADBC receiving login notification name on email is ABC but system using any random user email to send email.

see below screenshot both user exist in my admin area as a different client.

https://prnt.sc/1rlikat

Link to comment
Share on other sites

  • 2 weeks later...
On 9/9/2021 at 10:59 AM, Balram said:

i just figured out code is working but there is something wrong

example- user ABC logging into client area but user ADBC receiving login notification name on email is ABC but system using any random user email to send email.

see below screenshot both user exist in my admin area as a different client.

https://prnt.sc/1rlikat

What is this ? 

$values["id"] = $primaryid;

For general customtype, API wants the $clientid 

Edited by pRieStaKos
Link to comment
Share on other sites

  • 2 weeks later...
  • 4 months later...
  • 2 months later...
  • 1 month later...
  • 6 months later...

@Balram is right, @Sai Meeraa this is also fixing your issue I believe.

I also noticed clients receive a email from other logged users,  and the error: Email Sending Failed - Invalid user id provided (Subject: Account Login from ******

The database structure in table tblusers_clients is causing issues auth_user_id needs to be client_id in the script on some query's

A account and also a sub-account is created with the same client_id and with his own  auth_user_id

 

Change the parts below

change:

		$acctowner = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->where('owner', '=', 1)
		->count();

		$numrows = Capsule::table('tblusers_clients')
		->where('auth_user_id', '=', $userid)
		->count();

to

		$acctowner = Capsule::table('tblusers_clients')
		->where('client_id', '=', $userid)
		->where('owner', '=', 1)
		->count();

		$numrows = Capsule::table('tblusers_clients')
		->where('client_id', '=', $userid)
		->count();

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated