File: /home/xedaptot/ai.naniguide.com/app/Model/IpLocation.php
<?php
/**
* IpLocation class.
*
* Model class for IP Locations
*
* LICENSE: This product includes software developed at
* the Acelle Co., Ltd. (http://acellemail.com/).
*
* @category MVC Model
*
* @author N. Pham <[email protected]>
* @author L. Pham <[email protected]>
* @copyright Acelle Co., Ltd
* @license Acelle Co., Ltd
*
* @version 1.0
*
* @link http://acellemail.com
*/
namespace App\Model;
use App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log as LaravelLog;
use App\Notifications\System\IpLocationLookupFailed;
use App\Services\Notifications\Notifier;
class IpLocation extends Model
{
protected $connection = 'mysql';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'country_code', 'country_name', 'region_code',
'region_name', 'city', 'zipcode',
'latitude', 'longitude', 'metro_code', 'areacode',
];
/**
* Find-or-create the IpLocation row for `$ip`. Always returns a non-null
* IpLocation so callers can chain `$loc->ip_address` safely; if the
* GeoIP lookup fails (private range, geocoder down, unknown IP) the row
* is still saved with the IP populated and the geo columns left NULL —
* a future re-geocode pass can fill them in. We never throw away the IP
* itself just because enrichment failed.
*
* @return self
*/
public static function add($ip): self
{
//SELECT * FROM `ip2location_db11` WHERE INET_ATON('116.109.245.204') <= ip_to LIMIT 1
$location = self::firstOrNew(['ip_address' => $ip]);
$location->ip_address = $ip;
$geoip = App::make('App\Library\Contracts\GeoIpInterface');
try {
$geoip->resolveIp($ip);
$location->country_code = $geoip->getCountryCode();
$location->country_name = $geoip->getCountryName();
$location->region_name = $geoip->getRegionName();
$location->city = $geoip->getCity();
$location->zipcode = $geoip->getZipcode();
$location->latitude = $geoip->getLatitude();
$location->longitude = $geoip->getLongitude();
} catch (\Throwable $e) {
// Geocoder failed (private IP, API outage, unknown range, …).
// Log + notify admins; persist the row anyway with NULL geo so
// open_logs.ip_address still has a value to JOIN on later.
LaravelLog::warning('Cannot get IP location info for '.$ip.'. Error: '.$e->getMessage());
app(Notifier::class)->shareWithAdmins(new IpLocationLookupFailed($ip, $e));
}
$location->save();
return $location;
}
/**
* Location name.
*
* return Location
*/
public function name()
{
$str = [];
if (!empty($this->city)) {
$str[] = $this->city;
}
if (!empty($this->region_name)) {
$str[] = $this->region_name;
}
if (!empty($this->country_name)) {
$str[] = $this->country_name;
}
$name = implode(', ', $str);
return empty($name) ? trans('messages.unknown') : $name;
}
public function getFlagPath()
{
$path = "/images/flags/" . $this->country_code . ".png";
if (!file_exists(public_path($path))) {
$path = "/images/flags/unknown.png";
}
return $path;
}
public function getCountryName()
{
return (empty($this->country_name) ? trans("messages.unknown") : $this->country_name);
}
}