HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Countries/CountryService.php
<?php

namespace App\Komma\Countries;

use App\Komma\Countries\Models\Country;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class CountryService
{
    public function getCountryByIp()
    {
        if (app()->environment() == 'local') {
            return Country::where('iso_2', 'SE')->first();
        }

        $countryByServer = \Request::server('HTTP_CF_IPCOUNTRY');

        if (isset($countryByServer) && $countryByServer != 'XX') {
            Log::info('CountryService: Found country by server '.$countryByServer);
            $country = $this->getCountryByIso2(strtoupper($countryByServer));
            if (isset($country)) {
                return $country;
            }
        }

        $serverIp = \Request::server('X_FORWARDED_FOR');

        if (isset($serverIp)) {
            $serverIp = \Request::server('REMOTE_ADDR');
            Log::info('CountryService: X_FORWARDED_FOR is empty -> fallback uses REMOTE_ADDR '.$serverIp);
        } else {
            Log::info('CountryService: Used X_FORWARDED_FOR '.$serverIp);
        }

        $country = $this->extremeIpLookUpApi($serverIp);
        if (isset($country)) {
            return $country;
        }

        $country = $this->ipLocateApi($serverIp);
        if (isset($country)) {
            return $country;
        }

        $country = $this->ipinfoApi($serverIp);
        if (isset($country)) {
            return $country;
        }

        return null;
    }

    public function countriesForCheckout()
    {
        // Get all countries
        $countries = Country::all();

        // Create array
        $checkoutCountries = [];
        foreach ($countries as $country) {
            if (\Lang::has('countries.'.$country->iso_2)) {
                $checkoutCountries[$country->iso_2] = \Lang::get('countries.'.$country->iso_2);
            }
        }
        asort($checkoutCountries);

        return $checkoutCountries;
    }

    public function getCountryForSelect()
    {
        // Get all countries
        $countries = Country::all();

        $countryOptions = collect();

        foreach ($countries as $country) {
            $selectOption = \App::make(SelectOptionInterface::class);
            $selectOption->setValue($country->id)
                ->setContent($country->iso_2.' - '.$country->long_name)
                ->setHtmlContent($country->iso_2.' - '.$country->long_name);

            $countryOptions->push($selectOption);
        }

        return $countryOptions;
    }

    public function getCountryByIso2($iso_2) : Country
    {
        return $country = Country::where('iso_2', $iso_2)
            ->first();
    }

    /**
     * Try to resolve ip to country
     * https://extreme-ip-lookup.com/
     *
     * @param $serverIp
     * @return mixed
     */
    private function extremeIpLookUpApi($serverIp)
    {
        try {
            $client = new Client();
            $response = $client->get('https://extreme-ip-lookup.com/json/'.$serverIp);
            $response = \GuzzleHttp\json_decode($response->getBody()->getContents());

            $country = Country::where('iso_2', $response->countryCode)->first();

            return $country;
        } catch (\Exception $e) {
            if ($e->getCode() == 429) {
                \Log::alert('CustomerLogoComposer: Extreme IP - Too many request this minute');
            } else {
                \Log::alert('CustomerLogoComposer: Extreme IP - Something went wrong');
                \Log::alert($e->getMessage());
                \Log::alert($e->getTraceAsString());
            }
        }
    }

    /**
     * Try to resolve ip to country
     * https://www.iplocate.io/
     *
     * @param $serverIp
     * @return mixed
     */
    private function ipLocateApi($serverIp)
    {
        try {
            $client = new Client();
            $response = $client->get('https://www.iplocate.io/api/lookup/'.$serverIp);
            $response = \GuzzleHttp\json_decode($response->getBody()->getContents());

            $country = Country::where('iso_2', $response->country_code)->first();

            return $country;
        } catch (\Exception $e) {
            if ($e->getCode() == 429) {
                \Log::alert('CustomerLogoComposer: IPLocate - Too many request today');
            } else {
                \Log::alert('CustomerLogoComposer: IPLocate - Something went wrong');
                \Log::alert($e->getMessage());
                \Log::alert($e->getTraceAsString());
            }
        }
    }

    /**
     * Try to resolve ip to country
     * https://ipinfo.io/
     *
     * @param $serverIp
     * @return mixed
     */
    private function ipinfoApi($serverIp)
    {
        try {
            $client = new Client();
            $response = $client->get('https://ipinfo.io/'.$serverIp.'/json');
            $response = \GuzzleHttp\json_decode($response->getBody()->getContents());

            $countryIso2 = strtoupper(trim($response->country));
            $country = Country::where('iso_2', $countryIso2)->first();

            return $country;
        } catch (\Exception $e) {
            if ($e->getCode() == 429) {
                \Log::alert('CustomerLogoComposer: Ipinfo - Too many request today');
            } else {
                \Log::alert('CustomerLogoComposer: Ipinfo -Something went wrong');
                \Log::alert($e->getMessage());
                \Log::alert($e->getTraceAsString());
            }
        }
    }
}