WHMCS User and Client login notification hook (v6-v8)

whmcsguru

Active member
This one's been out for a few years, but with v8, and changes to the user and client system, I thought I'd give it an overhaul if you will.

To use the hook, simply just drop it in includes/hooks/ as a php file (say clientarea,php, clientlogin.php, something like that)

This hook will do just what the title says... Notify clients on login (as they're the account owner). Additionally, if a subaccount of said client logs in, and said subaccount just has one account (ie: they're not a direct client, or are not registered with another client as well), the client will get notified of that login as well

This hook does not notify the subaccount that they have logged in. This is a limitation with the WHMCS API function used to send mail

This hook does not notify anyone, for any reason, if an admin session is detected. This is by design.

I tested this about 5 ways from Sunday, but if you see something wrong, please do let me know and I'll see what I can do to get it fixed
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)
{
    $mailsent=FALSE;
   
    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 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="")
{
   
    $ip = $_SERVER['REMOTE_ADDR'] ;
    $hostname = gethostbyaddr($ip);
    $clientinfo = Capsule::table('tblusers')->select('first_name', 'last_name')->WHERE('id', $myclient)->get();
    //greet them
    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);
   
}
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');
}
 

Forum statistics

Threads
80,896
Messages
248,402
Members
20,678
Latest member
hostys
Top