wtricks Posted January 4, 2021 Share Posted January 4, 2021 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 0 Quote Link to comment Share on other sites More sharing options...
twhiting9275 Posted January 4, 2021 Author Share Posted January 4, 2021 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 0 Quote Link to comment Share on other sites More sharing options...
Hardik Joshi Posted January 5, 2021 Share Posted January 5, 2021 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, 2 Quote Link to comment Share on other sites More sharing options...
ManagedCloud-Hosting Posted January 5, 2021 Share Posted January 5, 2021 (edited) 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 January 5, 2021 by ManagedCloud-Hosting 0 Quote Link to comment Share on other sites More sharing options...
Hardik Joshi Posted January 5, 2021 Share Posted January 5, 2021 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); } 1 Quote Link to comment Share on other sites More sharing options...
Hardik Joshi Posted January 5, 2021 Share Posted January 5, 2021 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. 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) 1 Quote Link to comment Share on other sites More sharing options...
brian! Posted January 6, 2021 Share Posted January 6, 2021 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. 1 Quote Link to comment Share on other sites More sharing options...
twhiting9275 Posted January 6, 2021 Author Share Posted January 6, 2021 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. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted January 7, 2021 Share Posted January 7, 2021 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. 0 Quote Link to comment Share on other sites More sharing options...
Adam Zrik Posted September 1, 2021 Share Posted September 1, 2021 Dear brian, I am using cloudflare for my domain, and unfortunately the IP showing for client alert it's Cloudflare IP. Do you have any idea how i can solve this issue ? Thanks 0 Quote Link to comment Share on other sites More sharing options...
Balram Posted September 7, 2021 Share Posted September 7, 2021 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 0 Quote Link to comment Share on other sites More sharing options...
Balram Posted September 9, 2021 Share Posted September 9, 2021 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 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted September 18, 2021 Share Posted September 18, 2021 (edited) 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 September 18, 2021 by pRieStaKos 0 Quote Link to comment Share on other sites More sharing options...
Balram Posted September 19, 2021 Share Posted September 19, 2021 11 hours ago, pRieStaKos said: What is this ? $values["id"] = $primaryid; For general customtype, API wants the $clientid i need to change Quote $values["id"] = $primaryid; to this? Quote $values["id"] = $clientid ; 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted September 19, 2021 Share Posted September 19, 2021 3 minutes ago, Balram said: i need to change to this? Yes. $values[‘id’] depends on the $customtype. 0 Quote Link to comment Share on other sites More sharing options...
Balram Posted September 19, 2021 Share Posted September 19, 2021 10 minutes ago, pRieStaKos said: Yes. $values[‘id’] depends on the $customtype. i think it is something else i tested @brian! code mentioned above and got same issue. https://prnt.sc/1steqok 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 3, 2021 Share Posted October 3, 2021 On 18/09/2021 at 23:49, pRieStaKos said: What is this ? originally in v7, primaryid was used to determine if the user was a client or contact... in v8, different techniques would need to be used to determine who the user logging in is. 0 Quote Link to comment Share on other sites More sharing options...
Sai Meeraa Posted February 5, 2022 Share Posted February 5, 2022 im getting this issue Local API User Email Sending Failed - Invalid user id provided (Subject: Account Login from ****** 0 Quote Link to comment Share on other sites More sharing options...
Sufiyan Shaikh Posted February 11, 2022 Share Posted February 11, 2022 Not working in WHMCS 8.3.2 0 Quote Link to comment Share on other sites More sharing options...
twhiting9275 Posted April 21, 2022 Author Share Posted April 21, 2022 On 2/11/2022 at 8:25 AM, Sufiyan Shaikh said: Not working in WHMCS 8.3.2 Just tested in 8.4.3 and it absolutely is working. Make sure you're using the code from this post , and that you're not logged in as admin (or have admnotify set to TRUE at the top) 0 Quote Link to comment Share on other sites More sharing options...
Mikhael Posted May 24, 2022 Share Posted May 24, 2022 worked like magic for v8.4.1 also 1 Quote Link to comment Share on other sites More sharing options...
mangelot Posted December 2, 2022 Share Posted December 2, 2022 @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(); 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.