LAR_Maintenance/app/Observers/ActivityObserver.php

38 lines
1.1 KiB
PHP
Raw Normal View History

2024-07-30 16:13:21 +00:00
<?php
namespace App\Observers;
use App\Models\Activity;
class ActivityObserver
{
/**
* Handle the Activity "created" event.
*/
public function creating(Activity $activity): void
{
if (!app()->runningInConsole()) {
$activity->ip = $this->getIp();
$activity->user_id = auth()->id();
} else {
$activity->ip = "localhost";
$activity->user_id = 0;
}
}
public function getIp()
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip); // just to be safe
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $ip;
}
}
}
}
return request()->ip(); // it will return server ip when no client ip found
}
}