File size: 2,210 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

namespace Mautic\CoreBundle\IpLookup;

use IP2Location\Database;

class IP2LocationBinLookup extends AbstractLocalDataLookup
{
    public function getAttribution(): string
    {
        return 'IP2Location Local Bin File DB9BIN only';
    }

    /**
     * @return string
     */
    public function getLocalDataStoreFilepath()
    {
        return $this->getDataDir();
    }

    /**
     * @return string
     */
    public function getRemoteDateStoreDownloadUrl()
    {
        $usernamePass = explode(':', $this->auth);
        $data         = [];

        if (isset($usernamePass[0]) && isset($usernamePass[1])) {
            $data['login']       = $usernamePass[0];
            $data['password']    = $usernamePass[1];
            $data['productcode'] = 'DB9BIN';
            $queryString         = http_build_query($data);
            // the system gets the file name from end of remove file path url so use hardedcoded name
            $queryString .= '&filename=/ip2locaion.zip';

            return 'https://www.ip2location.com/download?'.$queryString;
        } else {
            $this->logger->warning('Both username and password are required');
        }
    }

    /**
     * Extract the IP from the local database.
     */
    protected function lookup()
    {
        try {
            $reader = new Database($this->getLocalDataStoreFilepath().'/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE.BIN', Database::FILE_IO);
            $record = $reader->lookup($this->ip, Database::ALL);

            if (isset($record['countryName'])) {
                $this->country   = $record['countryName'];
                $this->region    = $record['regionName'];
                $this->city      = $record['cityName'];
                $this->latitude  = $record['latitude'];
                $this->longitude = $record['longitude'];
                $this->zipcode   = $record['zipCode'];
                $this->isp       = $record['isp'];
                $this->timezone  = $record['timeZone'];
            }
        } catch (\Exception $exception) {
            if ($this->logger) {
                $this->logger->warning('IP LOOKUP: '.$exception->getMessage());
            }
        }
    }
}