Jump to content

Client Account Login Notification Hook


Recommended Posts

I don't see why notifying clients that they logged into a billing area would be a bad thing. In fact, it should probably be a default option.

 

Save this as a php file in whmcs/includes/hooks. Change the 'adminuser' to be your username (or another full admin)

 


<?php
/* 
Client area login notification for WHMCS
Developed by http://www.whmcsguru.com
*/

use Illuminate\Database\Capsule\Manager as Capsule;
function hook_client_login_notify($vars)
{
$userid = $vars['userid'];

send_login_notify($userid);
}

function send_login_notify($userid)
{
$ip = $_SERVER['REMOTE_ADDR'] ;
$hostname = gethostbyaddr($ip);
$userinfo = Capsule::table('tblclients')->select('firstname', 'lastname')->WHERE('id', $userid)->get();
//greet them
foreach ($userinfo as $userrow)
{
	$firstname = $userrow->firstname;
	$lastname = $userrow->lastname;
}
$command = "sendemail";
$adminuser = "YOUR ADMIN USERNAME HERE";
$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 />";
$values["id"] = $userid;

$results = localAPI($command, $values, $adminuser);
}

add_hook('ClientLogin', 1, 'hook_client_login_notify');

 

This uses Hooks:ClientLogin and API:SendEmail

Link to comment
Share on other sites

  • 3 months later...
  • 2 years later...

I know this topic is a bit old, but I was looking to add an option to this hook that if an admin does a login as a client, it would not send the email notification. This is handy if you need to troubleshoot something on the client side of whmcs. 

Any feedback would be appreciated.

Edited by Kevin K
Link to comment
Share on other sites

  • 8 months later...
30 minutes ago, HOSKIA INDIA said:

the hook is working 😍 but i need to add $signature also pls any one help me

<?php
/* 
Client area login notification for WHMCS
modified by brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;
function hook_client_login_notify($vars)
{
$userid = $vars['userid'];

send_login_notify($userid);
}

function send_login_notify($userid)
{
$ip = $_SERVER['REMOTE_ADDR'] ;
$hostname = gethostbyaddr($ip);
$userinfo = Capsule::table('tblclients')->select('firstname', 'lastname')->WHERE('id', $userid)->get();
$signature = Capsule::table('tblconfiguration')->where('setting', 'Signature')->value('value');
//greet them
foreach ($userinfo as $userrow)
{
	$firstname = $userrow->firstname;
	$lastname = $userrow->lastname;
}
$command = "sendemail";
$adminuser = "YOUR ADMIN USERNAME HERE";
$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;

$results = localAPI($command, $values, $adminuser);
}

add_hook('ClientLogin', 1, 'hook_client_login_notify');

 

Link to comment
Share on other sites

 
 
 
 
 
3
35 minutes ago, brian! said:

<?php
/* 
Client area login notification for WHMCS
modified by brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;
function hook_client_login_notify($vars)
{
$userid = $vars['userid'];

send_login_notify($userid);
}

function send_login_notify($userid)
{
$ip = $_SERVER['REMOTE_ADDR'] ;
$hostname = gethostbyaddr($ip);
$userinfo = Capsule::table('tblclients')->select('firstname', 'lastname')->WHERE('id', $userid)->get();
$signature = Capsule::table('tblconfiguration')->where('setting', 'Signature')->value('value');
//greet them
foreach ($userinfo as $userrow)
{
	$firstname = $userrow->firstname;
	$lastname = $userrow->lastname;
}
$command = "sendemail";
$adminuser = "YOUR ADMIN USERNAME HERE";
$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;

$results = localAPI($command, $values, $adminuser);
}

add_hook('ClientLogin', 1, 'hook_client_login_notify');

i need to give space between signature and hostname like this https://prnt.sc/ow1021 i did like this https://prnt.sc/ow107p is it correct ?

 

Link to comment
Share on other sites

Just now, HOSKIA INDIA said:

i need to give space between signature and hostname like this https://prnt.sc/ow1021 i did like this https://prnt.sc/ow107p is it correct ?

it should just be a case of adding another break return before the signature...

$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 /><br />$signature";
Link to comment
Share on other sites

15 hours ago, mino said:

how to block message notification if you connected in client area with admin permission

I think the following would work - change...

function hook_client_login_notify($vars)
{
$userid = $vars['userid'];
send_login_notify($userid);
}

to...

function hook_client_login_notify($vars)
{
if ($_SESSION['adminid']) {return;}
$userid = $vars['userid'];
send_login_notify($userid);
}

if you are logged in to the admin area, and MANUALLY login into the client area in a new tabb/window (e.g you type in their email address and password), then the email notification will still be sent (I can't see any obvious way to prevent that)...

however, if you use the "Login As Client" link in the admin client summary page, then the email shouldn't be sent to the client.

Link to comment
Share on other sites

5 hours ago, brian! said:

I think the following would work - change...


function hook_client_login_notify($vars)
{
$userid = $vars['userid'];
send_login_notify($userid);
}

to...


function hook_client_login_notify($vars)
{
if ($_SESSION['adminid']) {return;}
$userid = $vars['userid'];
send_login_notify($userid);
}

if you are logged in to the admin area, and MANUALLY login into the client area in a new tabb/window (e.g you type in their email address and password), then the email notification will still be sent (I can't see any obvious way to prevent that)...

however, if you use the "Login As Client" link in the admin client summary page, then the email shouldn't be sent to the client. 

Thank You @brian! and its possible add Web browser in this hook

Link to comment
Share on other sites

15 hours ago, mino said:

its possible add Web browser in this hook

three ways I can quickly think of - there will be lots more!

  1. the old fashioned way of using $_SERVER['HTTP_USER_AGENT'] - though that can be faked or even omitted, so not 100% reliable.
  2. there's a PHP internal function, get_browser, that can identify the browser - but I think it's disabled by default in PHP, so it would have to be enabled on your server before you could use it in a hook.
  3. there are numerous GitHub PHP functions that can be used to detect browsers etc.

but to keep things simple, let's just use HTTP_USER_AGENT..

{
	if ($_SESSION['adminid'] || strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {return;}
	$userid = $vars['userid'];
	send_login_notify($userid);
}

so the above says, if the user is an admin, OR anyone using the Firefox browser, then don't send the email.

if you wanted to change that to prevent the emails being sent when Admins using Chrome are logged in, then it would be...

if ($_SESSION['adminid'] && strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) {return;}
Link to comment
Share on other sites

4 hours ago, brian! said:

three ways I can quickly think of - there will be lots more!

  1. the old fashioned way of using $_SERVER['HTTP_USER_AGENT'] - though that can be faked or even omitted, so not 100% reliable.
  2. there's a PHP internal function, get_browser, that can identify the browser - but I think it's disabled by default in PHP, so it would have to be enabled on your server before you could use it in a hook.
  3. there are numerous GitHub PHP functions that can be used to detect browsers etc.

but to keep things simple, let's just use HTTP_USER_AGENT..


{
	if ($_SESSION['adminid'] || strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {return;}
	$userid = $vars['userid'];
	send_login_notify($userid);
}

so the above says, if the user is an admin, OR anyone using the Firefox browser, then don't send the email.

if you wanted to change that to prevent the emails being sent when Admins using Chrome are logged in, then it would be...


if ($_SESSION['adminid'] && strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) {return;}

Ok after its show Like in a picture or no @brian!

22.jpg

Link to comment
Share on other sites

  • 7 months later...
On 8/22/2019 at 3:28 PM, brian! said:

<?php
/* 
Client area login notification for WHMCS
modified by brian!
*/

use Illuminate\Database\Capsule\Manager as Capsule;
function hook_client_login_notify($vars)
{
$userid = $vars['userid'];

send_login_notify($userid);
}

function send_login_notify($userid)
{
$ip = $_SERVER['REMOTE_ADDR'] ;
$hostname = gethostbyaddr($ip);
$userinfo = Capsule::table('tblclients')->select('firstname', 'lastname')->WHERE('id', $userid)->get();
$signature = Capsule::table('tblconfiguration')->where('setting', 'Signature')->value('value');
//greet them
foreach ($userinfo as $userrow)
{
	$firstname = $userrow->firstname;
	$lastname = $userrow->lastname;
}
$command = "sendemail";
$adminuser = "YOUR ADMIN USERNAME HERE";
$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;

$results = localAPI($command, $values, $adminuser);
}

add_hook('ClientLogin', 1, 'hook_client_login_notify');

 

bro hook is not working please check it again

Link to comment
Share on other sites

21 minutes ago, hardikjoshi said:

hook is not working please check it again

if you're using any recent version of WHMCS, just remove the references to $adminuser...

$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;

... or ensure that the admin is authenticated to use SendEmail.

Link to comment
Share on other sites

16 hours ago, brian! said:

if you're using any recent version of WHMCS, just remove the references to $adminuser...


$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;

... or ensure that the admin is authenticated to use SendEmail.

thanks you bro it's working fine 

Link to comment
Share on other sites

7 hours ago, twhiting9275 said:

changing 


$results = localAPI($command, $values, $adminuser);

 

to


$results = localAPI($command, $values);

will do the trick too.  I mean, that's the easiest hack to do it. No longer need that adminuser for localAPI

yes brother it's also working well

Link to comment
Share on other sites

16 hours ago, brian! said:

if you're using any recent version of WHMCS, just remove the references to $adminuser...


$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;

... or ensure that the admin is authenticated to use SendEmail.

hey bro is it possible to saw internet provider name in hostname
 

https://prnt.sc/rqkolg

Link to comment
Share on other sites

9 hours ago, hardikjoshi said:

is it possible to saw internet provider name in hostname

the hook should already be doing that (and does when I test it locally) - though if gethostbyaddr fails, then it returns the IP address (which seems to be what you're seeing).

Link to comment
Share on other sites

18 hours ago, hardikjoshi said:

hey bro is it possible to saw internet provider name in hostname
 

https://prnt.sc/rqkolg

If you're not seeing a hostname in the email, then there's not likely to be one in there.

Put the ip that is given in the email here . This will likely return the IP back, instead of the hostname.

That's something the provider needs to setup

Link to comment
Share on other sites

On 02/04/2020 at 06:20, HostyFly said:

how to setup this hook can you help me please

you would create an empty .php file in /includes/hooks; give it a memorable filename e,g clientlogin.php; paste the hook code into the file, save it and it should start working immediately.

Link to comment
Share on other sites

12 hours ago, Vittor said:

Hello, does anyone know how to put date and time in the email?

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;
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