XF 2.1 How to get IPs of a specific user

CStrategies

Member
I am attempting to call the getIpsByUser method located in XF/Repository/Ip. If it makes any difference, I'm calling it from a method in an item in XF/ApprovalQueue. I have tried calling it with both a user_id as well as a user entity object.

Code:
$content = $unapprovedItem->Content;
$userId = $content->user_id;
$user = \XF::em()->find('XF:User', $userId);
$ipRepo = $app->repository('XF:Ip');
$ips = $ipRepo->getIpsByUser($user);

I want a list of IP addresses the user has used so I can match them with banned or discouraged accounts. But whether I feed this method a user_id or a user entity object, it always returns an empty array - even though I can visit the user in the ACP and see that there are IPs recorded in the user's IP logs.

For what it's worth, I also tried the getIp method from the user entity, and that also always returned empty.
 
So I realized that in some tables, IPs are recorded in binary, and in other tables they are recorded as strings. Once I made this distinction, I was able to confirm that I was actually fetching the IP but it was not displaying in my debug log because it was still in binary.

That being said, my original plan was to compare IPs to the xf_ip_match table. Unfortunately, what I wanted was to compare the IPs to any IPs of previously banned or discouraged users. But what I was actually doing was comparing to any banned or discouraged IPs. I want this to work the way it does when a new user registers and the spam filter displays something like "IP matches banned user" in the approval queue. I need to figure out where that information is kept and how the spam filter accesses it so I can use it in my code.
 
I figured it out. If anyone else wants to go down this rabbit hole, Users with banned ips are stored in the xf_ip table as is_banned. Users who have been discouraged are stored in the xf_ip table as is_discouraged. IPs that have been banned or discouraged are stored in the xf_ip_match table as match_type with a value of banned or discouraged respectively. You can set up some logic to capture all of the user's previously used IPs with the getIpsByUser function of the IP repository and then run checks on them based on the database queries I just mentioned. You end up with a filter that tells you if any IP the user has every used matches any previously banned or discouraged users or any currently banned or discouraged IPs. FWIW, not sure how resource intensive this is going to be, fair warning.
 
Back
Top Bottom