WHMCS Guru Support Forums

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Hooks Client Login Notification Hook

WHMCS Hooks

whmcsguru

Chief Guru
Staff member
This one's been around for a while, just getting it updated to v8 today.
This hook will fire off an email notification to your clients when they login to your website
A few caveats to this
  • This will not notify your clients if using the admin login functionality
  • This will not notify 'accounts' (those created by clients directly), YET! I may add this down the road
  • This will not notify the client (the creator of accounts) if a sub account logged in. Why? Well, what happens if you have two clients with the same account owner?
Anyways, the code:
PHP:
<?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'];
if (!empty($isadmin))
{
    //go no farther, get out of here.. No login notifications necessary
    return;

}

function hook_client_login_notify($vars)
{
    global $myver;
    $myver = get_whmcs_version();
    if ($myver < 8)
    {
        $userid = $vars['userid'];
        send_login_notify($userid);
    }
    if ($myver >= 8)
    {
        $user = $vars['user'];
        $userid = $user->id;
        //a few paths we can take from here, handle it
        //are we a client, or contact?
     $real_owner_id = Capsule::table('tblusers_clients')
     ->where('auth_user_id', '=', $userid)
     ->where('owner', '=', 1)
     ->value('client_id');
     if ($real_owner_id > 0)
     {
         send_login_notify($real_owner_id);
         //nothing more to see here
         return;
     }
    }



}


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

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

    return ($retver);

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

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

whmcsguru

Chief Guru
Staff member
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).

PHP:
<?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);

}
 

Users who are viewing this thread

Top