@@ -9,7 +9,7 @@  | 
                                                    ||
| 9 | 9 | |
| 10 | 10 | /**  | 
                                                        
| 11 | 11 | * @var string $ipAddress  | 
                                                        
| 12 | - **/  | 
                                                        |
| 12 | + **/  | 
                                                        |
| 13 | 13 | private $ipAddress=null;  | 
                                                        
| 14 | 14 | |
| 15 | 15 | public function __construct($ipAddress=null)  | 
                                                        
@@ -10,9 +10,9 @@ discard block  | 
                                                    ||
| 10 | 10 | /**  | 
                                                        
| 11 | 11 | * @var string $ipAddress  | 
                                                        
| 12 | 12 | **/  | 
                                                        
| 13 | - private $ipAddress=null;  | 
                                                        |
| 13 | + private $ipAddress = null;  | 
                                                        |
| 14 | 14 | |
| 15 | - public function __construct($ipAddress=null)  | 
                                                        |
| 15 | + public function __construct($ipAddress = null)  | 
                                                        |
| 16 | 16 |      { | 
                                                        
| 17 | 17 | $ipAddress || $ipAddress = $this->getIPAddress();  | 
                                                        
| 18 | 18 | $this->validateAddress($ipAddress);  | 
                                                        
@@ -26,8 +26,8 @@ discard block  | 
                                                    ||
| 26 | 26 | public function getIPAddress()  | 
                                                        
| 27 | 27 |      { | 
                                                        
| 28 | 28 | $ipAddress = null;  | 
                                                        
| 29 | - $serverIPKeys =['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 | + $serverIPKeys = ['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 ($serverIPKeys as $IPKey):  | 
                                                        
| 32 | 32 |              if (array_key_exists($IPKey, $_SERVER)) { | 
                                                        
| 33 | 33 |                  if (!strlen($_SERVER[$IPKey])) { continue; } | 
                                                        
@@ -39,7 +39,7 @@ discard block  | 
                                                    ||
| 39 | 39 |          { | 
                                                        
| 40 | 40 | $ipAddress = substr($ipAddress, 0, ($commaPos - 1));  | 
                                                        
| 41 | 41 | }  | 
                                                        
| 42 | - return $ipAddress?:'0.0.0.0';  | 
                                                        |
| 42 | + return $ipAddress ?: '0.0.0.0';  | 
                                                        |
| 43 | 43 | }  | 
                                                        
| 44 | 44 | |
| 45 | 45 | /**  | 
                                                        
@@ -79,9 +79,9 @@ discard block  | 
                                                    ||
| 79 | 79 |      { | 
                                                        
| 80 | 80 | $ipAddress = $this->expandAddress($ipAddress);  | 
                                                        
| 81 | 81 | if (strpos($ipAddress, ':') !== false):  | 
                                                        
| 82 | - $bin = inet_pton($ipAddress) ;  | 
                                                        |
| 83 | -            $ints = unpack('J2', $bin) ; | 
                                                        |
| 84 | - return $ints[1] ;  | 
                                                        |
| 82 | + $bin = inet_pton($ipAddress);  | 
                                                        |
| 83 | +            $ints = unpack('J2', $bin); | 
                                                        |
| 84 | + return $ints[1];  | 
                                                        |
| 85 | 85 | endif;  | 
                                                        
| 86 | 86 | return ip2long($ipAddress);  | 
                                                        
| 87 | 87 | }  | 
                                                        
@@ -90,7 +90,7 @@ discard block  | 
                                                    ||
| 90 | 90 | * @param $ipAddress  | 
                                                        
| 91 | 91 | * @return mixed|string  | 
                                                        
| 92 | 92 | */  | 
                                                        
| 93 | - public function ip2long (string $ipAddress)  | 
                                                        |
| 93 | + public function ip2long(string $ipAddress)  | 
                                                        |
| 94 | 94 |      { | 
                                                        
| 95 | 95 | $ipAddress || $ipAddress = $this->getIPAddress();  | 
                                                        
| 96 | 96 | $decimal = null;  | 
                                                        
@@ -105,7 +105,7 @@ discard block  | 
                                                    ||
| 105 | 105 | $network = inet_pton($ipAddress);  | 
                                                        
| 106 | 106 |                      $parts   = unpack('C*', $network); | 
                                                        
| 107 | 107 | foreach ($parts as &$byte):  | 
                                                        
| 108 | - $decimal.= str_pad(decbin($byte), 8, '0', STR_PAD_LEFT);  | 
                                                        |
| 108 | + $decimal .= str_pad(decbin($byte), 8, '0', STR_PAD_LEFT);  | 
                                                        |
| 109 | 109 | endforeach;  | 
                                                        
| 110 | 110 | $decimal = ltrim($decimal, '0');  | 
                                                        
| 111 | 111 | break;  | 
                                                        
@@ -130,7 +130,7 @@ discard block  | 
                                                    ||
| 130 | 130 |      { | 
                                                        
| 131 | 131 | $ipAddress = null;  | 
                                                        
| 132 | 132 |          if (preg_match('/[.:]/', $decimal)) | 
                                                        
| 133 | -        {return strtoupper($decimal);} | 
                                                        |
| 133 | +        {return strtoupper($decimal); } | 
                                                        |
| 134 | 134 | switch ($decimal):  | 
                                                        
| 135 | 135 | case (strlen($decimal) <= 32):  | 
                                                        
| 136 | 136 | $ipAddress .= long2ip($decimal);  | 
                                                        
@@ -141,14 +141,14 @@ discard block  | 
                                                    ||
| 141 | 141 |                  { $decimal = '0'.$decimal; } | 
                                                        
| 142 | 142 | for ($bits = 0; $bits <= 7; $bits++)  | 
                                                        
| 143 | 143 |                  { | 
                                                        
| 144 | - $binPart = substr($decimal,($bits*16),16);  | 
                                                        |
| 144 | + $binPart = substr($decimal, ($bits*16), 16);  | 
                                                        |
| 145 | 145 | $ipAddress .= dechex(bindec($binPart)).':';  | 
                                                        
| 146 | 146 | }  | 
                                                        
| 147 | - $ipAddress = inet_ntop(inet_pton(substr($ipAddress,0,-1)));  | 
                                                        |
| 147 | + $ipAddress = inet_ntop(inet_pton(substr($ipAddress, 0, -1)));  | 
                                                        |
| 148 | 148 | break;  | 
                                                        
| 149 | 149 | endswitch;  | 
                                                        
| 150 | 150 | $ipAddress = strtoupper($ipAddress);  | 
                                                        
| 151 | - return $compress? $ipAddress : $this->expandAddress($ipAddress);  | 
                                                        |
| 151 | + return $compress ? $ipAddress : $this->expandAddress($ipAddress);  | 
                                                        |
| 152 | 152 | }  | 
                                                        
| 153 | 153 | |
| 154 | 154 | /**  | 
                                                        
@@ -178,9 +178,9 @@ discard block  | 
                                                    ||
| 178 | 178 |      { | 
                                                        
| 179 | 179 | try  | 
                                                        
| 180 | 180 |          { | 
                                                        
| 181 | -            if(!preg_match('/[.:]/', $ipAddress)) {$ipAddress = $this->long2ip($ipAddress, false);} | 
                                                        |
| 181 | +            if (!preg_match('/[.:]/', $ipAddress)) {$ipAddress = $this->long2ip($ipAddress, false); } | 
                                                        |
| 182 | 182 | $this->validateAddress($ipAddress);  | 
                                                        
| 183 | - $delimiter = (strpos($ipAddress,':')===false)? '.' : ':';  | 
                                                        |
| 183 | + $delimiter = (strpos($ipAddress, ':') === false) ? '.' : ':';  | 
                                                        |
| 184 | 184 | return current(explode($delimiter, $ipAddress));  | 
                                                        
| 185 | 185 |          } catch (\Throwable $th) { | 
                                                        
| 186 | 186 | trigger_error($th->getMessage(), E_USER_ERROR);  | 
                                                        
@@ -200,7 +200,7 @@ discard block  | 
                                                    ||
| 200 | 200 | try  | 
                                                        
| 201 | 201 |          { | 
                                                        
| 202 | 202 | $ipAddress = $this->expandAddress($ipAddress);  | 
                                                        
| 203 | -            if (strpos($ipAddress, ':') !== false) {return 6;} | 
                                                        |
| 203 | +            if (strpos($ipAddress, ':') !== false) {return 6; } | 
                                                        |
| 204 | 204 | return 4;  | 
                                                        
| 205 | 205 |          } catch (Throwable $th) { | 
                                                        
| 206 | 206 | trigger_error($th->getMessage(), E_USER_ERROR);  | 
                                                        
@@ -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 | }  | 
                                                        
@@ -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  | 
                                                        
@@ -3,7 +3,7 @@ discard block  | 
                                                    ||
| 3 | 3 | namespace geolocation\bin;  | 
                                                        
| 4 | 4 | |
| 5 | 5 | use \PDO;  | 
                                                        
| 6 | -use \SplFileInfo ;  | 
                                                        |
| 6 | +use \SplFileInfo;  | 
                                                        |
| 7 | 7 | use \Throwable;  | 
                                                        
| 8 | 8 | |
| 9 | 9 | class GeoipDatabase  | 
                                                        
@@ -86,7 +86,7 @@ discard block  | 
                                                    ||
| 86 | 86 | */  | 
                                                        
| 87 | 87 | private function genDsn(string $database = null)  | 
                                                        
| 88 | 88 |      { | 
                                                        
| 89 | - $database || $database='Geoip.db.sqlite';  | 
                                                        |
| 89 | + $database || $database = 'Geoip.db.sqlite';  | 
                                                        |
| 90 | 90 |          try { | 
                                                        
| 91 | 91 | $destination = rtrim(dirname(__DIR__), self::DS);  | 
                                                        
| 92 | 92 | if (!is_writeable($destination))  | 
                                                        
@@ -94,15 +94,15 @@ discard block  | 
                                                    ||
| 94 | 94 |                  throw new Throwable(sprintf('The required destination path is not writable: `%s`', $destination)); | 
                                                        
| 95 | 95 | }  | 
                                                        
| 96 | 96 | $info = new SplFileInfo($database);  | 
                                                        
| 97 | - $dbName= $info->getFilename();  | 
                                                        |
| 98 | - $dbSuffix='.sqlite';  | 
                                                        |
| 99 | -            if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix ; } | 
                                                        |
| 97 | + $dbName = $info->getFilename();  | 
                                                        |
| 98 | + $dbSuffix = '.sqlite';  | 
                                                        |
| 99 | +            if (substr_compare(strtolower($dbName), $dbSuffix, -strlen($dbSuffix)) !== 0) { $dbName .= $dbSuffix; } | 
                                                        |
| 100 | 100 |          } catch (Throwable $th) { | 
                                                        
| 101 | 101 | trigger_error($th->getMessage(), E_USER_ERROR);  | 
                                                        
| 102 | 102 | }  | 
                                                        
| 103 | 103 | $destination .= self::DS.'data';  | 
                                                        
| 104 | 104 |          if (!is_dir($destination)) { mkdir($destination, '0755', true); } | 
                                                        
| 105 | - return 'sqlite:'.realpath($destination).self::DS.$dbName ;  | 
                                                        |
| 105 | + return 'sqlite:'.realpath($destination).self::DS.$dbName;  | 
                                                        |
| 106 | 106 | }  | 
                                                        
| 107 | 107 | |
| 108 | 108 | /**  | 
                                                        
@@ -163,16 +163,16 @@ discard block  | 
                                                    ||
| 163 | 163 | $sCommand .= 'FROM `ipv%dRange` ';  | 
                                                        
| 164 | 164 | $sCommand .= 'WHERE `start` <= :start ';  | 
                                                        
| 165 | 165 | $sCommand .= 'ORDER BY start DESC LIMIT 1';  | 
                                                        
| 166 | - $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion)) ;  | 
                                                        |
| 167 | - $statement->execute([':start' => $start ]) ;  | 
                                                        |
| 168 | - $row = $statement->fetch(PDO::FETCH_OBJ) ;  | 
                                                        |
| 166 | + $statement = $this->oPDOInstance->prepare(sprintf($sCommand, $ipVersion));  | 
                                                        |
| 167 | + $statement->execute([':start' => $start]);  | 
                                                        |
| 168 | + $row = $statement->fetch(PDO::FETCH_OBJ);  | 
                                                        |
| 169 | 169 | if (is_bool($row) && $row === false)  | 
                                                        
| 170 | 170 |              { | 
                                                        
| 171 | 171 | $row = new \stdClass();  | 
                                                        
| 172 | - $row->end = 0 ;  | 
                                                        |
| 172 | + $row->end = 0;  | 
                                                        |
| 173 | 173 | }  | 
                                                        
| 174 | -            if ($row->end < $start || !$row->country) { $row->country = 'ZZ' ; } | 
                                                        |
| 175 | - return $row->country ;  | 
                                                        |
| 174 | +            if ($row->end < $start || !$row->country) { $row->country = 'ZZ'; } | 
                                                        |
| 175 | + return $row->country;  | 
                                                        |
| 176 | 176 |          } catch (\PDOException $th) { | 
                                                        
| 177 | 177 | trigger_error($th->getMessage(), E_USER_ERROR);  | 
                                                        
| 178 | 178 | }  | 
                                                        
@@ -184,7 +184,7 @@ discard block  | 
                                                    ||
| 184 | 184 | * @param array $tablesList  | 
                                                        
| 185 | 185 | * @return $this  | 
                                                        
| 186 | 186 | */  | 
                                                        
| 187 | - public function flush(array $tablesList=[])  | 
                                                        |
| 187 | + public function flush(array $tablesList = [])  | 
                                                        |
| 188 | 188 |      { | 
                                                        
| 189 | 189 | $tablesList || $tablesList = $this->showTables();  | 
                                                        
| 190 | 190 | is_array($tablesList) || $tablesList = [$tablesList];  | 
                                                        
@@ -199,7 +199,7 @@ discard block  | 
                                                    ||
| 199 | 199 | endif;  | 
                                                        
| 200 | 200 | return $this;  | 
                                                        
| 201 | 201 |          } catch (\PDOException $th) { | 
                                                        
| 202 | -            trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); | 
                                                        |
| 202 | +            trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); | 
                                                        |
| 203 | 203 | }  | 
                                                        
| 204 | 204 | }  | 
                                                        
| 205 | 205 | |
@@ -216,17 +216,17 @@ discard block  | 
                                                    ||
| 216 | 216 |      { | 
                                                        
| 217 | 217 | try  | 
                                                        
| 218 | 218 |          { | 
                                                        
| 219 | - $sQuery ='INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)';  | 
                                                        |
| 219 | + $sQuery = 'INSERT INTO `ipv%dRange` (`start`, `end`, `country`) values (:start, :end, :country)';  | 
                                                        |
| 220 | 220 | $command = sprintf($sQuery, $ipVersion);  | 
                                                        
| 221 | 221 | $statement = $this->oPDOInstance->prepare($command);  | 
                                                        
| 222 | 222 | $statement->execute([  | 
                                                        
| 223 | 223 | ':start' => $start,  | 
                                                        
| 224 | 224 | ':end' => $end,  | 
                                                        
| 225 | 225 | ':country' => $country  | 
                                                        
| 226 | - ]) ;  | 
                                                        |
| 226 | + ]);  | 
                                                        |
| 227 | 227 | return $this->oPDOInstance->lastInsertId();  | 
                                                        
| 228 | 228 |          } catch (\PDOException $th) { | 
                                                        
| 229 | -            trigger_error('Statement failed: ' . $th->getMessage(), E_USER_ERROR); | 
                                                        |
| 229 | +            trigger_error('Statement failed: '.$th->getMessage(), E_USER_ERROR); | 
                                                        |
| 230 | 230 | }  | 
                                                        
| 231 | 231 | }  | 
                                                        
| 232 | 232 | /**  | 
                                                        
@@ -236,7 +236,7 @@ discard block  | 
                                                    ||
| 236 | 236 | */  | 
                                                        
| 237 | 237 | public function beginTransaction()  | 
                                                        
| 238 | 238 |      { | 
                                                        
| 239 | -        if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction();} | 
                                                        |
| 239 | +        if (!$this->transactionCounter++) {return $this->oPDOInstance->beginTransaction(); } | 
                                                        |
| 240 | 240 | return $this->transactionCounter >= 0;  | 
                                                        
| 241 | 241 | }  | 
                                                        
| 242 | 242 | |
@@ -247,7 +247,7 @@ discard block  | 
                                                    ||
| 247 | 247 | */  | 
                                                        
| 248 | 248 | public function commit()  | 
                                                        
| 249 | 249 |      { | 
                                                        
| 250 | -        if (!--$this->transactionCounter) {return $this->oPDOInstance->commit();} | 
                                                        |
| 250 | +        if (!--$this->transactionCounter) {return $this->oPDOInstance->commit(); } | 
                                                        |
| 251 | 251 | return $this->transactionCounter >= 0;  | 
                                                        
| 252 | 252 | }  | 
                                                        
| 253 | 253 | |
@@ -11,14 +11,14 @@  | 
                                                    ||
| 11 | 11 | * PDO SQLite3 database instance  | 
                                                        
| 12 | 12 | *  | 
                                                        
| 13 | 13 | * @var GeoipDatabase  | 
                                                        
| 14 | - **/  | 
                                                        |
| 14 | + **/  | 
                                                        |
| 15 | 15 | private $oDBInstance=null;  | 
                                                        
| 16 | 16 | |
| 17 | 17 | /**  | 
                                                        
| 18 | 18 | * Network tools class instance  | 
                                                        
| 19 | 19 | *  | 
                                                        
| 20 | 20 | * @var GeoipNetwork  | 
                                                        
| 21 | - **/  | 
                                                        |
| 21 | + **/  | 
                                                        |
| 22 | 22 | private $oNetwork=null;  | 
                                                        
| 23 | 23 | |
| 24 | 24 | /**  | 
                                                        
@@ -12,14 +12,14 @@ discard block  | 
                                                    ||
| 12 | 12 | *  | 
                                                        
| 13 | 13 | * @var GeoipDatabase  | 
                                                        
| 14 | 14 | **/  | 
                                                        
| 15 | - private $oDBInstance=null;  | 
                                                        |
| 15 | + private $oDBInstance = null;  | 
                                                        |
| 16 | 16 | |
| 17 | 17 | /**  | 
                                                        
| 18 | 18 | * Network tools class instance  | 
                                                        
| 19 | 19 | *  | 
                                                        
| 20 | 20 | * @var GeoipNetwork  | 
                                                        
| 21 | 21 | **/  | 
                                                        
| 22 | - private $oNetwork=null;  | 
                                                        |
| 22 | + private $oNetwork = null;  | 
                                                        |
| 23 | 23 | |
| 24 | 24 | /**  | 
                                                        
| 25 | 25 | * Class Constructor  | 
                                                        
@@ -39,7 +39,7 @@ discard block  | 
                                                    ||
| 39 | 39 | * @param string|null $ipAddress  | 
                                                        
| 40 | 40 | * @return string  | 
                                                        
| 41 | 41 | */  | 
                                                        
| 42 | - public function resolve(string $ipAddress= null): string  | 
                                                        |
| 42 | + public function resolve(string $ipAddress = null): string  | 
                                                        |
| 43 | 43 |      { | 
                                                        
| 44 | 44 | $ipAddress || $ipAddress = $this->oNetwork->getIPAddress();  | 
                                                        
| 45 | 45 | $ipVersion = $this->oNetwork->ipVersion($ipAddress);  | 
                                                        
@@ -51,11 +51,11 @@ discard block  | 
                                                    ||
| 51 | 51 | * @param mixed|null $ipAddress  | 
                                                        
| 52 | 52 | * @return bool  | 
                                                        
| 53 | 53 | */  | 
                                                        
| 54 | - public function isReservedAddress($ipAddress=null): bool  | 
                                                        |
| 54 | + public function isReservedAddress($ipAddress = null): bool  | 
                                                        |
| 55 | 55 |      { | 
                                                        
| 56 | 56 | $ipAddress || $ipAddress = $this->oNetwork->getIPAddress();  | 
                                                        
| 57 | 57 | $countryCode = $this->resolve($ipAddress);  | 
                                                        
| 58 | - return !$countryCode || strcasecmp($countryCode, 'ZZ') == 0 ;  | 
                                                        |
| 58 | + return !$countryCode || strcasecmp($countryCode, 'ZZ') == 0;  | 
                                                        |
| 59 | 59 | }  | 
                                                        
| 60 | 60 | |
| 61 | 61 | |