Issues (3627)

app/bundles/CoreBundle/Entity/IpAddress.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
16
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
17
18
/**
19
 * Class IpAddress.
20
 */
21
class IpAddress
22
{
23
    /**
24
     * Set by factory of configured IPs to not track.
25
     *
26
     * @var array
27
     */
28
    private $doNotTrack = [];
29
30
    /**
31
     * @var int
32
     */
33
    private $id;
34
35
    /**
36
     * @var string
37
     */
38
    private $ipAddress;
39
40
    /**
41
     * @var array
42
     */
43
    private $ipDetails;
44
45
    public static function loadMetadata(ORM\ClassMetadata $metadata)
46
    {
47
        $builder = new ClassMetadataBuilder($metadata);
48
49
        $builder->setTable('ip_addresses')
50
            ->setCustomRepositoryClass('Mautic\CoreBundle\Entity\IpAddressRepository')
51
            ->addIndex(['ip_address'], 'ip_search');
52
53
        $builder->addId();
54
55
        $builder->createField('ipAddress', 'string')
56
            ->columnName('ip_address')
57
            ->length(45)
58
            ->build();
59
60
        $builder->createField('ipDetails', 'array')
61
            ->columnName('ip_details')
62
            ->nullable()
63
            ->build();
64
    }
65
66
    /**
67
     * Prepares the metadata for API usage.
68
     *
69
     * @param $metadata
70
     */
71
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
72
    {
73
        $metadata->setGroupPrefix('ipAddress')
74
            ->addListProperties(
75
                [
76
                    ['ipAddress', 'ip'],
77
                ]
78
            )
79
            ->addProperties(
80
                [
81
                    'id',
82
                    'ipAddress',
83
                    'ipDetails',
84
                ]
85
            )
86
            ->addGroup('ipAddress', true)
87
            ->build();
88
    }
89
90
    /**
91
     * IpAddress constructor.
92
     *
93
     * @param null $ipAddress
94
     */
95
    public function __construct($ipAddress = null)
96
    {
97
        $this->ipAddress = $ipAddress;
98
    }
99
100
    /**
101
     * Get id.
102
     *
103
     * @return int
104
     */
105
    public function getId()
106
    {
107
        return $this->id;
108
    }
109
110
    /**
111
     * Set ipAddress.
112
     *
113
     * @param $ipAddress
114
     *
115
     * @return $this
116
     */
117
    public function setIpAddress($ipAddress)
118
    {
119
        $this->ipAddress = $ipAddress;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Get ipAddress.
126
     *
127
     * @return string
128
     */
129
    public function getIpAddress()
130
    {
131
        return $this->ipAddress;
132
    }
133
134
    /**
135
     * Set ipDetails.
136
     *
137
     * @param string $ipDetails
138
     *
139
     * @return IpAddress
140
     */
141
    public function setIpDetails($ipDetails)
142
    {
143
        $this->ipDetails = $ipDetails;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Get ipDetails.
150
     *
151
     * @return string
152
     */
153
    public function getIpDetails()
154
    {
155
        return $this->ipDetails;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ipDetails returns the type array which is incompatible with the documented return type string.
Loading history...
156
    }
157
158
    /**
159
     * Set list of IPs to not track.
160
     */
161
    public function setDoNotTrackList(array $ips)
162
    {
163
        $this->doNotTrack = $ips;
164
    }
165
166
    /**
167
     * Get list of IPs to not track.
168
     *
169
     * @return array
170
     */
171
    public function getDoNotTrackList()
172
    {
173
        return $this->doNotTrack;
174
    }
175
176
    /**
177
     * Determine if this IP is trackable.
178
     */
179
    public function isTrackable()
180
    {
181
        if (!empty($this->doNotTrack)) {
182
            foreach ($this->doNotTrack as $ip) {
183
                if (false !== strpos($ip, '/')) {
184
                    // has a netmask range
185
                    // https://gist.github.com/tott/7684443
186
                    list($range, $netmask) = explode('/', $ip, 2);
187
                    $range_decimal         = ip2long($range);
188
                    $ip_decimal            = ip2long($this->ipAddress);
189
                    $wildcard_decimal      = pow(2, (32 - $netmask)) - 1;
190
                    $netmask_decimal       = ~$wildcard_decimal;
191
192
                    if ((($ip_decimal & $netmask_decimal) == ($range_decimal & $netmask_decimal))) {
193
                        return false;
194
                    }
195
196
                    continue;
197
                }
198
199
                if ($ip === $this->ipAddress) {
200
                    return false;
201
                }
202
203
                if (preg_match('/'.str_replace('.', '\\.', $ip).'/', $this->ipAddress)) {
204
                    return false;
205
                }
206
            }
207
        }
208
209
        return true;
210
    }
211
}
212