@@ -57,9 +57,11 @@ |
||
| 57 | 57 | try |
| 58 | 58 | { |
| 59 | 59 | $this->validateAddress($ipAddress); |
| 60 | - if (strpos($ipAddress, ':') !== false) // IPv6 address |
|
| 60 | + if (strpos($ipAddress, ':') !== false) { |
|
| 61 | + // IPv6 address |
|
| 61 | 62 | { |
| 62 | 63 | $hex = unpack('H*hex', inet_pton($ipAddress)); |
| 64 | + } |
|
| 63 | 65 | $ipAddress = substr(preg_replace('/([A-f0-9]{4})/', "$1:", $hex['hex']), 0, -1); |
| 64 | 66 | $ipAddress = strtoupper($ipAddress); |
| 65 | 67 | } |
@@ -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. |
@@ -7,13 +7,13 @@ discard block |
||
| 7 | 7 | /** |
| 8 | 8 | * @var string $ipAddress |
| 9 | 9 | **/ |
| 10 | - private $ipAddress=null; |
|
| 10 | + private $ipAddress = null; |
|
| 11 | 11 | /** |
| 12 | 12 | * Class constructor. |
| 13 | 13 | * |
| 14 | 14 | * @param string $ipAddress |
| 15 | 15 | */ |
| 16 | - public function __construct(string $ipAddress=null) |
|
| 16 | + public function __construct(string $ipAddress = null) |
|
| 17 | 17 | { |
| 18 | 18 | $ipAddress || $ipAddress = $this->getIPAddress(); |
| 19 | 19 | $this->validateAddress($ipAddress); |
@@ -26,8 +26,8 @@ discard block |
||
| 26 | 26 | public function getIPAddress() |
| 27 | 27 | { |
| 28 | 28 | $ipAddress = ''; |
| 29 | - $ipKeys =['HTTP_X_COMING_FROM', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 30 | - 'HTTP_X_FORWARDED', 'HTTP_VIA', 'HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR','REMOTE_ADDR']; |
|
| 29 | + $ipKeys = ['HTTP_X_COMING_FROM', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 30 | + 'HTTP_X_FORWARDED', 'HTTP_VIA', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR']; |
|
| 31 | 31 | foreach ($ipKeys as $key): |
| 32 | 32 | if (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])): |
| 33 | 33 | $ipAddress = $_SERVER[$key]; |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | break; |
| 38 | 38 | endif; |
| 39 | 39 | endforeach; |
| 40 | - return $ipAddress?:'0.0.0.0'; |
|
| 40 | + return $ipAddress ?: '0.0.0.0'; |
|
| 41 | 41 | } |
| 42 | 42 | /** |
| 43 | 43 | * If IPV6, Returns the IP in it's fullest format. |
@@ -75,9 +75,9 @@ discard block |
||
| 75 | 75 | { |
| 76 | 76 | $ipAddress = $this->expandAddress($ipAddress); |
| 77 | 77 | if (strpos($ipAddress, ':') !== false): |
| 78 | - $bin = inet_pton($ipAddress) ; |
|
| 79 | - $ints = unpack('J2', $bin) ; |
|
| 80 | - return $ints[1] ; |
|
| 78 | + $bin = inet_pton($ipAddress); |
|
| 79 | + $ints = unpack('J2', $bin); |
|
| 80 | + return $ints[1]; |
|
| 81 | 81 | endif; |
| 82 | 82 | return ip2long($ipAddress); |
| 83 | 83 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | try |
| 112 | 112 | { |
| 113 | 113 | $ipAddress = $this->expandAddress($ipAddress); |
| 114 | - if (strpos($ipAddress, ':') !== false) {return 6;} |
|
| 114 | + if (strpos($ipAddress, ':') !== false) {return 6; } |
|
| 115 | 115 | return 4; |
| 116 | 116 | } catch (\Throwable $th) { |
| 117 | 117 | trigger_error($th->getMessage(), E_USER_ERROR); |
@@ -17,9 +17,9 @@ |
||
| 17 | 17 | private $oPDOInstance; |
| 18 | 18 | /** |
| 19 | 19 | * PDO transaction Counter |
| 20 | - * |
|
| 21 | - * @var integer |
|
| 22 | - */ |
|
| 20 | + * |
|
| 21 | + * @var integer |
|
| 22 | + */ |
|
| 23 | 23 | private $transactionCounter = 0; |
| 24 | 24 | /** |
| 25 | 25 | * Class Constructor |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | private function genDsn(string $database = null) |
| 80 | 80 | { |
| 81 | - $database || $database='Geoip.db.sqlite'; |
|
| 81 | + $database || $database = 'Geoip.db.sqlite'; |
|
| 82 | 82 | try { |
| 83 | 83 | $destination = rtrim(dirname(__DIR__), self::DS); |
| 84 | 84 | if (!is_writeable($destination)) |
@@ -86,15 +86,15 @@ discard block |
||
| 86 | 86 | throw new \Throwable('The required destination path is not writable: '.$destination); |
| 87 | 87 | } |
| 88 | 88 | $info = new \SplFileInfo($database); |
| 89 | - $dbName= $info->getFilename(); |
|
| 90 | - $dbSuffix='.sqlite'; |
|
| 91 | - if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix ; } |
|
| 89 | + $dbName = $info->getFilename(); |
|
| 90 | + $dbSuffix = '.sqlite'; |
|
| 91 | + if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix; } |
|
| 92 | 92 | } catch (\Throwable $th) { |
| 93 | 93 | trigger_error($th->getMessage(), E_USER_ERROR); |
| 94 | 94 | } |
| 95 | 95 | $destination .= self::DS.'data'; |
| 96 | 96 | if (!is_dir($destination)) { mkdir($destination, '0755', true); } |
| 97 | - return 'sqlite:'.realpath($destination).self::DS.$dbName ; |
|
| 97 | + return 'sqlite:'.realpath($destination).self::DS.$dbName; |
|
| 98 | 98 | } |
| 99 | 99 | /** |
| 100 | 100 | * Get the table list in the database |
@@ -152,16 +152,16 @@ discard block |
||
| 152 | 152 | $sCommand .= 'FROM `ipv%dRange` '; |
| 153 | 153 | $sCommand .= 'WHERE `start` <= :start '; |
| 154 | 154 | $sCommand .= 'ORDER BY start DESC LIMIT 1'; |
| 155 | - $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion)) ; |
|
| 156 | - $statement->execute([':start' => $start ]) ; |
|
| 157 | - $row = $statement->fetch(\PDO::FETCH_OBJ) ; |
|
| 155 | + $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion)); |
|
| 156 | + $statement->execute([':start' => $start]); |
|
| 157 | + $row = $statement->fetch(\PDO::FETCH_OBJ); |
|
| 158 | 158 | if (is_bool($row) && $row === false) |
| 159 | 159 | { |
| 160 | 160 | $row = new \stdClass(); |
| 161 | - $row->end = 0 ; |
|
| 161 | + $row->end = 0; |
|
| 162 | 162 | } |
| 163 | - if ($row->end < $start || !$row->country) { $row->country = 'ZZ' ; } |
|
| 164 | - return $row->country ; |
|
| 163 | + if ($row->end < $start || !$row->country) { $row->country = 'ZZ'; } |
|
| 164 | + return $row->country; |
|
| 165 | 165 | } catch (\PDOException $th) { |
| 166 | 166 | trigger_error($th->getMessage(), E_USER_ERROR); |
| 167 | 167 | } |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @param array $tablesList |
| 173 | 173 | * @return void |
| 174 | 174 | */ |
| 175 | - public function flush(array $tablesList=[]) |
|
| 175 | + public function flush(array $tablesList = []) |
|
| 176 | 176 | { |
| 177 | 177 | !empty($tablesList) || $tablesList = $this->showTables(); |
| 178 | 178 | is_array($tablesList) || $tablesList = [$tablesList]; |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | $this->oPDOInstance->query('VACUUM'); |
| 187 | 187 | endif; |
| 188 | 188 | } catch (\PDOException $th) { |
| 189 | - trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); |
|
| 189 | + trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); |
|
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | 192 | /** |
@@ -202,16 +202,16 @@ discard block |
||
| 202 | 202 | { |
| 203 | 203 | try |
| 204 | 204 | { |
| 205 | - $sQuery ='INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)'; |
|
| 205 | + $sQuery = 'INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)'; |
|
| 206 | 206 | $command = sprintf($sQuery, $ipVersion); |
| 207 | 207 | $statement = $this->oPDOInstance->prepare($command); |
| 208 | 208 | $statement->execute([ |
| 209 | 209 | ':start' => $start, |
| 210 | 210 | ':end' => $end, |
| 211 | 211 | ':country' => $country |
| 212 | - ]) ; |
|
| 212 | + ]); |
|
| 213 | 213 | } catch (\PDOException $th) { |
| 214 | - trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); |
|
| 214 | + trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); |
|
| 215 | 215 | } |
| 216 | 216 | } |
| 217 | 217 | /** |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | public function beginTransaction() |
| 223 | 223 | { |
| 224 | - if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction();} |
|
| 224 | + if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction(); } |
|
| 225 | 225 | return $this->transactionCounter >= 0; |
| 226 | 226 | } |
| 227 | 227 | /** |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | public function commit() |
| 233 | 233 | { |
| 234 | - if (!--$this->transactionCounter) {return $this->oPDOInstance->commit();} |
|
| 234 | + if (!--$this->transactionCounter) {return $this->oPDOInstance->commit(); } |
|
| 235 | 235 | return $this->transactionCounter >= 0; |
| 236 | 236 | } |
| 237 | 237 | /** |
@@ -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 | } |