@@ -10,13 +10,13 @@ |
||
10 | 10 | * PDO SQLite3 database instance |
11 | 11 | * |
12 | 12 | * @var GeoipDatabase |
13 | - **/ |
|
13 | + **/ |
|
14 | 14 | private $oDBInstance=null; |
15 | 15 | /** |
16 | 16 | * Network tools class instance |
17 | 17 | * |
18 | 18 | * @var GeoipNetwork |
19 | - **/ |
|
19 | + **/ |
|
20 | 20 | private $oNetwork=null; |
21 | 21 | /** |
22 | 22 | * Class Constructor |
@@ -11,13 +11,13 @@ discard block |
||
11 | 11 | * |
12 | 12 | * @var GeoipDatabase |
13 | 13 | **/ |
14 | - private $oDBInstance=null; |
|
14 | + private $oDBInstance = null; |
|
15 | 15 | /** |
16 | 16 | * Network tools class instance |
17 | 17 | * |
18 | 18 | * @var GeoipNetwork |
19 | 19 | **/ |
20 | - private $oNetwork=null; |
|
20 | + private $oNetwork = null; |
|
21 | 21 | /** |
22 | 22 | * Class Constructor |
23 | 23 | * |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * @param string|null $ipAddress |
36 | 36 | * @return string |
37 | 37 | */ |
38 | - public function resolve(string $ipAddress= null): string |
|
38 | + public function resolve(string $ipAddress = null): string |
|
39 | 39 | { |
40 | 40 | $ipAddress || $ipAddress = $this->oNetwork->getIPAddress(); |
41 | 41 | $ipVersion = $this->oNetwork->ipVersion($ipAddress); |
@@ -46,10 +46,10 @@ discard block |
||
46 | 46 | * @param mixed|null $ipAddress |
47 | 47 | * @return bool |
48 | 48 | */ |
49 | - public function isReservedAddress($ipAddress=null): bool |
|
49 | + public function isReservedAddress($ipAddress = null): bool |
|
50 | 50 | { |
51 | 51 | $ipAddress || $ipAddress = $this->oNetwork->getIPAddress(); |
52 | 52 | $countryCode = $this->resolve($ipAddress); |
53 | - return !$countryCode || strcasecmp($countryCode, 'ZZ') == 0 ; |
|
53 | + return !$countryCode || strcasecmp($countryCode, 'ZZ') == 0; |
|
54 | 54 | } |
55 | 55 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | */ |
81 | 81 | private function genDsn(string $database = null) |
82 | 82 | { |
83 | - $database || $database='Geoip.db.sqlite'; |
|
83 | + $database || $database = 'Geoip.db.sqlite'; |
|
84 | 84 | try { |
85 | 85 | $destination = rtrim(dirname(__DIR__), self::DS); |
86 | 86 | if (!is_writeable($destination)) |
@@ -88,15 +88,15 @@ discard block |
||
88 | 88 | throw new \Throwable(sprintf('The required destination path is not writable: `%s`', $destination)); |
89 | 89 | } |
90 | 90 | $info = new \SplFileInfo($database); |
91 | - $dbName= $info->getFilename(); |
|
92 | - $dbSuffix='.sqlite'; |
|
93 | - if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix ; } |
|
91 | + $dbName = $info->getFilename(); |
|
92 | + $dbSuffix = '.sqlite'; |
|
93 | + if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix; } |
|
94 | 94 | } catch (\Throwable $th) { |
95 | 95 | trigger_error($th->getMessage(), E_USER_ERROR); |
96 | 96 | } |
97 | 97 | $destination .= self::DS.'data'; |
98 | 98 | if (!is_dir($destination)) { mkdir($destination, '0755', true); } |
99 | - return 'sqlite:'.realpath($destination).self::DS.$dbName ; |
|
99 | + return 'sqlite:'.realpath($destination).self::DS.$dbName; |
|
100 | 100 | } |
101 | 101 | /** |
102 | 102 | * Get the table list in the database |
@@ -154,16 +154,16 @@ discard block |
||
154 | 154 | $sCommand .= 'FROM `ipv%dRange` '; |
155 | 155 | $sCommand .= 'WHERE `start` <= :start '; |
156 | 156 | $sCommand .= 'ORDER BY start DESC LIMIT 1'; |
157 | - $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion)) ; |
|
158 | - $statement->execute([':start' => $start ]) ; |
|
159 | - $row = $statement->fetch(\PDO::FETCH_OBJ) ; |
|
157 | + $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion)); |
|
158 | + $statement->execute([':start' => $start]); |
|
159 | + $row = $statement->fetch(\PDO::FETCH_OBJ); |
|
160 | 160 | if (is_bool($row) && $row === false) |
161 | 161 | { |
162 | 162 | $row = new \stdClass(); |
163 | - $row->end = 0 ; |
|
163 | + $row->end = 0; |
|
164 | 164 | } |
165 | - if ($row->end < $start || !$row->country) { $row->country = 'ZZ' ; } |
|
166 | - return $row->country ; |
|
165 | + if ($row->end < $start || !$row->country) { $row->country = 'ZZ'; } |
|
166 | + return $row->country; |
|
167 | 167 | } catch (\PDOException $th) { |
168 | 168 | trigger_error($th->getMessage(), E_USER_ERROR); |
169 | 169 | } |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * @param array $tablesList |
175 | 175 | * @return void |
176 | 176 | */ |
177 | - public function flush(array $tablesList=[]) |
|
177 | + public function flush(array $tablesList = []) |
|
178 | 178 | { |
179 | 179 | !empty($tablesList) || $tablesList = $this->showTables(); |
180 | 180 | is_array($tablesList) || $tablesList = [$tablesList]; |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | $this->oPDOInstance->query('VACUUM'); |
189 | 189 | endif; |
190 | 190 | } catch (\PDOException $th) { |
191 | - trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); |
|
191 | + trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); |
|
192 | 192 | } |
193 | 193 | } |
194 | 194 | /** |
@@ -204,17 +204,17 @@ discard block |
||
204 | 204 | { |
205 | 205 | try |
206 | 206 | { |
207 | - $sQuery ='INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)'; |
|
207 | + $sQuery = 'INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)'; |
|
208 | 208 | $command = sprintf($sQuery, $ipVersion); |
209 | 209 | $statement = $this->oPDOInstance->prepare($command); |
210 | 210 | $statement->execute([ |
211 | 211 | ':start' => $start, |
212 | 212 | ':end' => $end, |
213 | 213 | ':country' => $country |
214 | - ]) ; |
|
214 | + ]); |
|
215 | 215 | return $this->oPDOInstance->lastInsertId(); |
216 | 216 | } catch (\PDOException $th) { |
217 | - trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); |
|
217 | + trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); |
|
218 | 218 | } |
219 | 219 | } |
220 | 220 | /** |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | */ |
225 | 225 | public function beginTransaction() |
226 | 226 | { |
227 | - if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction();} |
|
227 | + if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction(); } |
|
228 | 228 | return $this->transactionCounter >= 0; |
229 | 229 | } |
230 | 230 | /** |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | */ |
235 | 235 | public function commit() |
236 | 236 | { |
237 | - if (!--$this->transactionCounter) {return $this->oPDOInstance->commit();} |
|
237 | + if (!--$this->transactionCounter) {return $this->oPDOInstance->commit(); } |
|
238 | 238 | return $this->transactionCounter >= 0; |
239 | 239 | } |
240 | 240 | /** |
@@ -5,7 +5,7 @@ |
||
5 | 5 | { |
6 | 6 | /** |
7 | 7 | * @var string $ipAddress |
8 | - **/ |
|
8 | + **/ |
|
9 | 9 | private $ipAddress=null; |
10 | 10 | /** |
11 | 11 | * Class constructor. |
@@ -6,13 +6,13 @@ discard block |
||
6 | 6 | /** |
7 | 7 | * @var string $ipAddress |
8 | 8 | **/ |
9 | - private $ipAddress=null; |
|
9 | + private $ipAddress = null; |
|
10 | 10 | /** |
11 | 11 | * Class constructor. |
12 | 12 | * |
13 | 13 | * @param string $ipAddress |
14 | 14 | */ |
15 | - public function __construct(string $ipAddress=null) |
|
15 | + public function __construct(string $ipAddress = null) |
|
16 | 16 | { |
17 | 17 | $ipAddress || $ipAddress = $this->getIPAddress(); |
18 | 18 | $this->validateAddress($ipAddress); |
@@ -25,8 +25,8 @@ discard block |
||
25 | 25 | public function getIPAddress() |
26 | 26 | { |
27 | 27 | $ipAddress = null; |
28 | - $serverIPKeys =['HTTP_X_COMING_FROM', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_X_CLUSTER_CLIENT_IP', |
|
29 | - 'HTTP_X_FORWARDED', 'HTTP_VIA', 'HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR','REMOTE_ADDR']; |
|
28 | + $serverIPKeys = ['HTTP_X_COMING_FROM', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_X_CLUSTER_CLIENT_IP', |
|
29 | + 'HTTP_X_FORWARDED', 'HTTP_VIA', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR']; |
|
30 | 30 | foreach ($serverIPKeys as $IPKey): |
31 | 31 | if (array_key_exists($IPKey, $_SERVER)) { |
32 | 32 | if (!strlen($_SERVER[$IPKey])) { continue; } |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | { |
39 | 39 | $ipAddress = substr($ipAddress, 0, ($commaPos - 1)); |
40 | 40 | } |
41 | - return $ipAddress?:'0.0.0.0'; |
|
41 | + return $ipAddress ?: '0.0.0.0'; |
|
42 | 42 | } |
43 | 43 | /** |
44 | 44 | * If IPV6, Returns the IP in it's fullest format. |
@@ -76,9 +76,9 @@ discard block |
||
76 | 76 | { |
77 | 77 | $ipAddress = $this->expandAddress($ipAddress); |
78 | 78 | if (strpos($ipAddress, ':') !== false): |
79 | - $bin = inet_pton($ipAddress) ; |
|
80 | - $ints = unpack('J2', $bin) ; |
|
81 | - return $ints[1] ; |
|
79 | + $bin = inet_pton($ipAddress); |
|
80 | + $ints = unpack('J2', $bin); |
|
81 | + return $ints[1]; |
|
82 | 82 | endif; |
83 | 83 | return ip2long($ipAddress); |
84 | 84 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * @param $ipAddress |
88 | 88 | * @return mixed|string |
89 | 89 | */ |
90 | - public function ip2long (string $ipAddress) |
|
90 | + public function ip2long(string $ipAddress) |
|
91 | 91 | { |
92 | 92 | $ipAddress || $ipAddress = $this->getIPAddress(); |
93 | 93 | $decimal = null; |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $network = inet_pton($ipAddress); |
103 | 103 | $parts = unpack('C*', $network); |
104 | 104 | foreach ($parts as &$byte): |
105 | - $decimal.= str_pad(decbin($byte), 8, '0', STR_PAD_LEFT); |
|
105 | + $decimal .= str_pad(decbin($byte), 8, '0', STR_PAD_LEFT); |
|
106 | 106 | endforeach; |
107 | 107 | $decimal = ltrim($decimal, '0'); |
108 | 108 | break; |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | { |
127 | 127 | $ipAddress = null; |
128 | 128 | if (preg_match('/[.:]/', $decimal)) |
129 | - {return strtoupper($decimal);} |
|
129 | + {return strtoupper($decimal); } |
|
130 | 130 | switch ($decimal): |
131 | 131 | case (strlen($decimal) <= 32): |
132 | 132 | $ipAddress .= long2ip($decimal); |
@@ -137,14 +137,14 @@ discard block |
||
137 | 137 | { $decimal = '0'.$decimal; } |
138 | 138 | for ($bits = 0; $bits <= 7; $bits++) |
139 | 139 | { |
140 | - $binPart = substr($decimal,($bits*16),16); |
|
140 | + $binPart = substr($decimal, ($bits*16), 16); |
|
141 | 141 | $ipAddress .= dechex(bindec($binPart)).':'; |
142 | 142 | } |
143 | - $ipAddress = inet_ntop(inet_pton(substr($ipAddress,0,-1))); |
|
143 | + $ipAddress = inet_ntop(inet_pton(substr($ipAddress, 0, -1))); |
|
144 | 144 | break; |
145 | 145 | endswitch; |
146 | 146 | $ipAddress = strtoupper($ipAddress); |
147 | - return $compress? $ipAddress : $this->expandAddress($ipAddress); |
|
147 | + return $compress ? $ipAddress : $this->expandAddress($ipAddress); |
|
148 | 148 | } |
149 | 149 | /** |
150 | 150 | * Check IP address validity |
@@ -172,9 +172,9 @@ discard block |
||
172 | 172 | { |
173 | 173 | try |
174 | 174 | { |
175 | - if(!preg_match('/[.:]/', $ipAddress)) {$ipAddress = $this->long2ip($ipAddress, false);} |
|
175 | + if (!preg_match('/[.:]/', $ipAddress)) {$ipAddress = $this->long2ip($ipAddress, false); } |
|
176 | 176 | $this->validateAddress($ipAddress); |
177 | - $delimiter = (strpos($ipAddress,':')===false)? '.' : ':'; |
|
177 | + $delimiter = (strpos($ipAddress, ':') === false) ? '.' : ':'; |
|
178 | 178 | return current(explode($delimiter, $ipAddress)); |
179 | 179 | } catch (\Throwable $th) { |
180 | 180 | trigger_error($th->getMessage(), E_USER_ERROR); |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | try |
194 | 194 | { |
195 | 195 | $ipAddress = $this->expandAddress($ipAddress); |
196 | - if (strpos($ipAddress, ':') !== false) {return 6;} |
|
196 | + if (strpos($ipAddress, ':') !== false) {return 6; } |
|
197 | 197 | return 4; |
198 | 198 | } catch (\Throwable $th) { |
199 | 199 | trigger_error($th->getMessage(), E_USER_ERROR); |