<?php

namespace App\Helpers;

class Security
{
    public static function real_ip() {
        $potential_ip_headers = [
            'HTTP_CLIENT_IP',
            'HTTP_X_FORWARDED_FOR',
            'HTTP_X_FORWARDED',
            'HTTP_X_CLUSTER_CLIENT_IP',
            'HTTP_FORWARDED_FOR',
            'HTTP_FORWARDED',
            'REMOTE_ADDR'
        ];
    
        foreach ($potential_ip_headers as $key){
            if (array_key_exists($key, $_SERVER) === true) {
                foreach (explode(',', $_SERVER[$key]) as $ip){
                    $ip = trim($ip); // Clean it up - It will probably be keen but security shouldn't predicate on probably.
                    
                    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
                        return $ip;
                    }
                }
            }
        }
        
        // Should we be unable to find anything, default to what Laravel will give us.
        return request()->ip();   
    }
}
?>